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 com.zeenea.experiments.virtualthreads.reactorapp.domain.item.port.ItemPort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ItemService {
|
public class ItemService {
|
||||||
@@ -16,4 +17,9 @@ public class ItemService {
|
|||||||
public Flux<Item> getAllItems() {
|
public Flux<Item> getAllItems() {
|
||||||
return itemPort.getAll();
|
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,
|
String name,
|
||||||
ZonedDateTime sharedDate
|
ZonedDateTime sharedDate
|
||||||
) {
|
) {
|
||||||
|
public Item(String name, Boolean shared) {
|
||||||
|
this(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
name,
|
||||||
|
shared ? ZonedDateTime.now() : null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isShared() {
|
public boolean isShared() {
|
||||||
return sharedDate != null;
|
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 com.zeenea.experiments.virtualthreads.reactorapp.domain.item.model.Item;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public interface ItemPort {
|
public interface ItemPort {
|
||||||
Flux<Item> getAll();
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
public record Marketplace(
|
public record Marketplace(List<Catalog> catalogs, List<Item> sharedItems) {}
|
||||||
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)
|
.flatMapMany(Flux::fromIterable)
|
||||||
.map(ItemJpaEntity::toDomain);
|
.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;
|
private UUID id;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
@Column(nullable = false)
|
@Column
|
||||||
private ZonedDateTime sharedDate;
|
private ZonedDateTime sharedDate;
|
||||||
|
|
||||||
public ItemJpaEntity(Item item) {
|
public ItemJpaEntity(Item item) {
|
||||||
|
|||||||
@@ -10,5 +10,5 @@ CREATE TABLE IF NOT EXISTS catalog (
|
|||||||
CREATE TABLE IF NOT EXISTS item (
|
CREATE TABLE IF NOT EXISTS item (
|
||||||
id UUID NOT NULL PRIMARY KEY,
|
id UUID NOT NULL PRIMARY KEY,
|
||||||
name VARCHAR NOT NULL,
|
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