Skip to content

Instantly share code, notes, and snippets.

View alexandre-jacquot-ptl's full-sized avatar

Alexandre JACQUOT alexandre-jacquot-ptl

View GitHub Profile
@Getter
public enum NotificationTopic {
ITEM_SAVED,
ITEM_DELETED;
}
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / ItemService.java
Last active May 23, 2021 16:39
r2dbc ItemService events
@Service
@RequiredArgsConstructor
public class ItemService {
private final NotificationService notificationService;
...
/**
* Listen to all saved items
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / ItemController.java
Created May 23, 2021 16:38
r2dbc ItemController listen
@RestController
@RequestMapping(value = "/items")
public class ItemController {
private final ItemService itemService;
...
@GetMapping("/events")
public Flux<ServerSentEvent<Event>> listenToEvents() {
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / NotificationService.java
Last active June 28, 2021 11:26
r2dbc notification service
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final ConnectionFactory connectionFactory;
private final Set<NotificationTopic> watchedTopics = new HashSet<>();
private PostgresqlConnection connection;
private ObjectMapper objectMapper;
CREATE FUNCTION notify_item_deleted()
RETURNS TRIGGER
AS
BEGIN
PERFORM pg_notify('ITEM_DELETED', row_to_json(OLD)::text);
RETURN NULL;
END;
CREATE TRIGGER item_deleted_trigger
AFTER DELETE
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / item_saved_trigger.sql
Last active May 23, 2021 16:18
r2dbc item_saved_trigger.sql
CREATE FUNCTION notify_item_saved()
RETURNS TRIGGER
AS
BEGIN
PERFORM pg_notify('ITEM_SAVED', row_to_json(NEW)::text);
RETURN NULL;
END;
CREATE TRIGGER item_saved_trigger
AFTER INSERT OR UPDATE
@Data
@Accessors(chain = true)
public class ItemPatchResource {
private Optional<@NotBlank @Size(max=4000) String> description;
private Optional<@NotNull ItemStatus> status;
private Optional<Long> assigneeId;
private Optional<Set<Long>> tagIds;
}
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / ItemController.java
Created May 23, 2021 13:16
r2dbc ItemController patch
@RestController
@RequestMapping(value = "/items")
public class ItemController {
private final ItemService itemService;
private final ItemMapper itemMapper;
...
@ApiOperation("Patch an existing item following the patch merge RCF (https://tools.ietf.org/html/rfc7396)")
@alexandre-jacquot-ptl
alexandre-jacquot-ptl / ItemService.java
Created May 23, 2021 12:57
r2dbc ItemService update
@Service
@RequiredArgsConstructor
public class ItemService {
private final ItemRepository itemRepository;
private final ItemTagRepository itemTagRepository;
private final TagMapper tagMapper;
/**
* Update an Item
@Data
@Accessors(chain = true)
public class ItemUpdateResource {
@NotBlank
@Size(max=4000)
private String description;
@NotNull
private ItemStatus status;