Skip to content

Instantly share code, notes, and snippets.

View hkakutalua's full-sized avatar
🎯
Focusing

Henrick hkakutalua

🎯
Focusing
View GitHub Profile
[
{
"id": 1,
"firstName": "Henrick",
"lastName": "Kakutalua",
"birthday": "2020-06-02T22:10:16.016+01:00",
"bio": "Lorem ipsum",
"imageUrl": "https://image/profile_photo.jpg"
},
{
@hkakutalua
hkakutalua / PeopleController.java
Created June 2, 2020 21:44
Fixed People Controller
Person person = personOptional.get();
if (personUpdateDto.getFirstName() != null) {
person.setFirstName(personUpdateDto.getFirstName());
}
if (personUpdateDto.getLastName() != null) {
person.setLastName(personUpdateDto.getLastName());
}
if (personUpdateDto.getBirthday() != null) {
person.setBirthday(personUpdateDto.getBirthday());
}
package com.example.partialupdates.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class JacksonConfiguration {
@hkakutalua
hkakutalua / PersonUpdateDto.java
Created June 3, 2020 22:24
Updated PersonUpdateDto
package com.example.partialupdates.controllers.dtos;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.constraints.NotNull;
import java.time.OffsetDateTime;
@SuppressWarnings("FieldMayBeFinal")
public class PersonUpdateDto {
@NotNull
@hkakutalua
hkakutalua / PeopleController.java
Last active June 3, 2020 22:31
Updated PeopleController
@RestController
@RequestMapping("/api/people")
public class PeopleController {
...
@PatchMapping("{id}")
public ResponseEntity<Void> updatePerson(@PathVariable("id") long id,
@Valid @RequestBody PersonUpdateDto personUpdateDto) {
Optional<Person> personOptional = peopleRepository.getById(id);
if (!personOptional.isPresent()) {
package com.example.partialupdates.utils;
import org.openapitools.jackson.nullable.JsonNullable;
import java.util.function.Consumer;
public final class JsonNullableUtils {
private JsonNullableUtils() {}
public static <T> void changeIfPresent(JsonNullable<T> nullable, Consumer<T> consumer) {
@hkakutalua
hkakutalua / PeopleController.java
Created June 3, 2020 22:49
Updated PeopleController with JsonNullableUtils
@RestController
@RequestMapping("/api/people")
public class PeopleController {
...
@PatchMapping("{id}")
public ResponseEntity<Void> updatePerson(@PathVariable("id") long id,
@Valid @RequestBody PersonUpdateDto personUpdateDto) {
Optional<Person> personOptional = peopleRepository.getById(id);
if (!personOptional.isPresent()) {
@hkakutalua
hkakutalua / CartItemsControllerTests.kt
Created July 19, 2020 04:57
LazyInitializationException
val product = productsRepository.findByIdOrNull(cartItem.productId)
if (product == null) {
return ResponseEntity.notFound().build()
}
val cart = cartsRepository.findByIdOrNull(cartId)
if (cart == null) {
return ResponseEntity.notFound().build()
}
@RestController
@RequestMapping("carts/{cart_id}/items")
class CartItemsController(
private val cartsRepository: CartsRepository,
private val productsRepository: ProductsRepository) {
@PostMapping
fun addItemToCart(
@PathVariable("cart_id") cartId: UUID,
@RequestBody cartItem: CartItemInputModel
@hkakutalua
hkakutalua / CartsControllerTests.kt
Created July 19, 2020 10:16
Test wth PostgresDbCleanerExtension
@SpringBootTest
@ExtendWith(PostgresDbCleanerExtension::class)
internal class CartsControllerTests {
@Autowired
lateinit var cartsRepository: CartsRepository
@Autowired
lateinit var webApplicationContext: WebApplicationContext
...