Add item creation and "get all" endpoints.
This commit is contained in:
@@ -4,6 +4,7 @@ import com.zeenea.experiments.virtualthreads.reactorapp.domain.item.model.Item;
|
||||
import com.zeenea.experiments.virtualthreads.reactorapp.domain.item.port.ItemPort;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class ItemService {
|
||||
@@ -16,4 +17,9 @@ public class ItemService {
|
||||
public Flux<Item> getAllItems() {
|
||||
return itemPort.getAll();
|
||||
}
|
||||
|
||||
public Mono<Void> create(String name, Boolean shared) {
|
||||
Item newItem = new Item(name, shared);
|
||||
return itemPort.save(newItem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,14 @@ public record Item(
|
||||
String name,
|
||||
ZonedDateTime sharedDate
|
||||
) {
|
||||
public Item(String name, Boolean shared) {
|
||||
this(
|
||||
UUID.randomUUID(),
|
||||
name,
|
||||
shared ? ZonedDateTime.now() : null
|
||||
);
|
||||
}
|
||||
|
||||
public boolean isShared() {
|
||||
return sharedDate != null;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.zeenea.experiments.virtualthreads.reactorapp.domain.item.port;
|
||||
|
||||
import com.zeenea.experiments.virtualthreads.reactorapp.domain.item.model.Item;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public interface ItemPort {
|
||||
Flux<Item> getAll();
|
||||
Mono<Void> save(Item newItem);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,4 @@ import com.zeenea.experiments.virtualthreads.reactorapp.domain.item.model.Item;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Marketplace(
|
||||
List<Catalog> catalogs,
|
||||
List<Item> sharedItems
|
||||
) {}
|
||||
public record Marketplace(List<Catalog> catalogs, List<Item> sharedItems) {}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.zeenea.experiments.virtualthreads.reactorapp.exposition.item;
|
||||
|
||||
import com.zeenea.experiments.virtualthreads.reactorapp.domain.item.ItemService;
|
||||
import com.zeenea.experiments.virtualthreads.reactorapp.exposition.item.model.CreateItemRequest;
|
||||
import com.zeenea.experiments.virtualthreads.reactorapp.exposition.marketplace.model.ItemDto;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.springframework.http.HttpStatus.CREATED;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/items")
|
||||
public class ItemController {
|
||||
private final ItemService itemService;
|
||||
|
||||
public ItemController(ItemService itemService) {
|
||||
this.itemService = itemService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(CREATED)
|
||||
public Mono<Void> createAnItem(@RequestBody CreateItemRequest request) {
|
||||
return itemService.create(request.name(), request.isShared());
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Flux<ItemDto> getAll() {
|
||||
return itemService.getAllItems()
|
||||
.map(ItemDto::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.zeenea.experiments.virtualthreads.reactorapp.exposition.item.model;
|
||||
|
||||
public record CreateItemRequest(String name, Boolean isShared) {}
|
||||
@@ -24,4 +24,12 @@ public class ItemJpaAdapter implements ItemPort {
|
||||
.flatMapMany(Flux::fromIterable)
|
||||
.map(ItemJpaEntity::toDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> save(Item newItem) {
|
||||
ItemJpaEntity newItemJpaEntity = new ItemJpaEntity(newItem);
|
||||
return Mono.fromCallable(() -> itemJpaRepository.save(newItemJpaEntity))
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.then();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class ItemJpaEntity {
|
||||
private UUID id;
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
@Column
|
||||
private ZonedDateTime sharedDate;
|
||||
|
||||
public ItemJpaEntity(Item item) {
|
||||
|
||||
@@ -10,5 +10,5 @@ CREATE TABLE IF NOT EXISTS catalog (
|
||||
CREATE TABLE IF NOT EXISTS item (
|
||||
id UUID NOT NULL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
shared_date TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
shared_date TIMESTAMP WITH TIME ZONE NULL
|
||||
);
|
||||
Reference in New Issue
Block a user