diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt index 965b001..117f08f 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt @@ -1,13 +1,17 @@ package net.halfbinary.scavengerhuntapi.controller import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.tags.Tag import jakarta.validation.Valid import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.ItemId +import net.halfbinary.scavengerhuntapi.model.converter.toDomain +import net.halfbinary.scavengerhuntapi.model.converter.toResponse import net.halfbinary.scavengerhuntapi.model.request.ItemRequest import net.halfbinary.scavengerhuntapi.model.response.ItemResponse import net.halfbinary.scavengerhuntapi.service.HuntService import org.springframework.http.ResponseEntity +import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping @@ -29,10 +33,12 @@ class ItemController(private val huntService: HuntService) { TODO("Get detailed information about the specified Item for the specified Hunt") } + @PreAuthorize("hasRole('ADMIN')") + @Tag(name = "Admin") @PostMapping @Operation(summary = "Adds new Item to specified Hunt") - fun addItemToHunt(@PathVariable huntId: HuntId, @Valid @RequestBody body: ItemRequest) { - TODO("Add a new item to the specified Hunt") + fun addItemToHunt(@PathVariable huntId: HuntId, @Valid @RequestBody body: ItemRequest): ResponseEntity { + return ResponseEntity.ok(huntService.addItemToHunt(huntId, body.toDomain()).toResponse()) } } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/HuntItemConverter.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/HuntItemConverter.kt new file mode 100644 index 0000000..72e8a80 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/HuntItemConverter.kt @@ -0,0 +1,8 @@ +package net.halfbinary.scavengerhuntapi.model.converter + +import net.halfbinary.scavengerhuntapi.model.domain.HuntItem +import net.halfbinary.scavengerhuntapi.model.record.HuntItemRecord + +fun HuntItem.toRecord() = HuntItemRecord(id = id, huntId = huntId, itemId = itemId) + +fun HuntItemRecord.toDomain() = HuntItem(id = id, huntId = huntId, itemId = itemId) diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/ItemConverter.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/ItemConverter.kt new file mode 100644 index 0000000..50eca38 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/converter/ItemConverter.kt @@ -0,0 +1,14 @@ +package net.halfbinary.scavengerhuntapi.model.converter + +import net.halfbinary.scavengerhuntapi.model.domain.Item +import net.halfbinary.scavengerhuntapi.model.record.ItemRecord +import net.halfbinary.scavengerhuntapi.model.request.ItemRequest +import net.halfbinary.scavengerhuntapi.model.response.ItemResponse + +fun ItemRequest.toDomain() = Item(name = name, points = points) + +fun Item.toRecord() = ItemRecord(id = id, name = name, points = points) + +fun ItemRecord.toDomain() = Item(id = id, name = name, points = points) + +fun Item.toResponse() = ItemResponse(id = id, name = name, points = points) \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/HuntItem.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/HuntItem.kt new file mode 100644 index 0000000..a3a5c9b --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/HuntItem.kt @@ -0,0 +1,11 @@ +package net.halfbinary.scavengerhuntapi.model.domain + +import net.halfbinary.scavengerhuntapi.model.HuntId +import net.halfbinary.scavengerhuntapi.model.ItemId +import java.util.* + +data class HuntItem( + val id: UUID = UUID.randomUUID(), + val huntId: HuntId, + val itemId: ItemId +) diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/Item.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/Item.kt new file mode 100644 index 0000000..c2c56bb --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/model/domain/Item.kt @@ -0,0 +1,10 @@ +package net.halfbinary.scavengerhuntapi.model.domain + +import net.halfbinary.scavengerhuntapi.model.ItemId +import java.util.* + +data class Item( + val id: ItemId = UUID.randomUUID(), + val name: String, + val points: Int +) \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HuntItemRepository.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HuntItemRepository.kt new file mode 100644 index 0000000..5e0523e --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HuntItemRepository.kt @@ -0,0 +1,9 @@ +package net.halfbinary.scavengerhuntapi.repository + +import net.halfbinary.scavengerhuntapi.model.record.HuntItemRecord +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository +import java.util.* + +@Repository +interface HuntItemRepository : JpaRepository diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt index a9b572a..a32cdac 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt @@ -6,14 +6,22 @@ import net.halfbinary.scavengerhuntapi.model.HunterId import net.halfbinary.scavengerhuntapi.model.converter.toDomain import net.halfbinary.scavengerhuntapi.model.converter.toRecord import net.halfbinary.scavengerhuntapi.model.domain.Hunt +import net.halfbinary.scavengerhuntapi.model.domain.HuntItem +import net.halfbinary.scavengerhuntapi.model.domain.Item import net.halfbinary.scavengerhuntapi.model.request.HuntStatus +import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository import net.halfbinary.scavengerhuntapi.repository.HuntRepository +import net.halfbinary.scavengerhuntapi.repository.ItemRepository import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import java.time.LocalDateTime @Service -class HuntService(private val huntRepository: HuntRepository) { +class HuntService( + private val huntRepository: HuntRepository, + private val itemRepository: ItemRepository, + private val huntItemRepository: HuntItemRepository +) { fun getHunt(huntId: HuntId): Hunt { return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found") } @@ -55,4 +63,11 @@ class HuntService(private val huntRepository: HuntRepository) { fun createHunt(hunt: Hunt): Hunt { return huntRepository.save(hunt.toRecord()).toDomain() } + + fun addItemToHunt(huntId: HuntId, item: Item): Item { + huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found") + val savedItem = itemRepository.save(item.toRecord()).toDomain() + huntItemRepository.save(HuntItem(huntId = huntId, itemId = savedItem.id).toRecord()) + return savedItem + } } \ No newline at end of file