diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/CardsController.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/CardsController.kt index 0d0cc6f..14c67ca 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/CardsController.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/CardsController.kt @@ -5,6 +5,7 @@ import com.vmaier.marvel.snap.cards.db.dao.Card import com.vmaier.marvel.snap.cards.dto.CreateCardDTO import com.vmaier.marvel.snap.cards.service.CardsService import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Sort import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.* @@ -25,7 +26,7 @@ class CardsController constructor(private val cardsService: CardsService) { if (pageSize > Constants.MAX_PAGE_SIZE) { pageSize = Constants.MAX_PAGE_SIZE } - val cardPage = cardsService.getAllCardsByKeyword(PageRequest.of(currentPage - 1, pageSize), keyword) + val cardPage = cardsService.getAllCardsByKeyword(PageRequest.of(currentPage - 1, pageSize, Sort.by("name").ascending()), keyword) model.addAttribute("keyword", keyword) model.addAttribute("cardPage", cardPage) return "cards" @@ -36,14 +37,16 @@ class CardsController constructor(private val cardsService: CardsService) { model: Model, @RequestParam(value = "keyword", required = false) keyword: String?, @RequestParam(value = "cost", required = false) cost: Int?, - @RequestParam(value = "power", required = false) power: Int? + @RequestParam(value = "power", required = false) power: Int?, + @RequestParam(value = "isOwned", required = false) isOwned: Boolean?, ): String { - val cards = cardsService.getAllCardsByKeyword(keyword, cost, power) + val cards = cardsService.getAllCardsByKeyword(keyword, cost, power, isOwned).sortedBy { card -> card.name } val costValues = cards.map { card: Card -> card.cost }.toSet().sorted() val powerValues = cards.map { card: Card -> card.power }.toSet().sorted() model.addAttribute("keyword", keyword) model.addAttribute("cost", cost) model.addAttribute("power", power) + model.addAttribute("isOwned", isOwned) model.addAttribute("costValues", costValues) model.addAttribute("powerValues", powerValues) model.addAttribute("cards", cards) @@ -72,6 +75,6 @@ class CardsController constructor(private val cardsService: CardsService) { @DeleteMapping("cards") fun removeCards(model: Model): String { cardsService.removeAllCards() - return "redirect:/cards" + return "redirect:/card-grid" } } \ No newline at end of file diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/api/CardsApiController.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/api/CardsApiController.kt index 6de40fc..1a80f0a 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/api/CardsApiController.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/controller/api/CardsApiController.kt @@ -50,4 +50,16 @@ class CardsApiController constructor(private val cardsService: CardsService) : C cardsService.removeAllCards() return ResponseEntity(HttpStatus.NO_CONTENT) } + + override fun addToCollection(cardId: Int): ResponseEntity { + Validator.checkIfCardIdIsValid(cardId) + cardsService.addToCollection(cardId) + return ResponseEntity(HttpStatus.NO_CONTENT) + } + + override fun removeFromCollection(cardId: Int): ResponseEntity { + Validator.checkIfCardIdIsValid(cardId) + cardsService.removeFromCollection(cardId) + return ResponseEntity(HttpStatus.NO_CONTENT) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/db/dao/Card.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/db/dao/Card.kt index 6ab3b64..b75515e 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/db/dao/Card.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/db/dao/Card.kt @@ -10,6 +10,7 @@ data class Card( val power: Int, val ability: String? = null, val url: String? = null, + val isOwned: Boolean = false, @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Int? = null, diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/db/repo/CardRepository.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/db/repo/CardRepository.kt index 272ff99..d7f0333 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/db/repo/CardRepository.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/db/repo/CardRepository.kt @@ -22,4 +22,8 @@ interface CardRepository : CrudRepository { @Modifying @Query("UPDATE Card c SET c.url = :url WHERE c.id = :id") fun updateImageUrl(@Param("id") id: Int, @Param("url") url: String) + + @Modifying + @Query("UPDATE Card c SET c.isOwned = :isOwned WHERE c.id = :id") + fun updateIsOwned(@Param("id") id: Int, @Param("isOwned") isOwned: Boolean) } \ No newline at end of file diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/openapi/api/CardsApi.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/openapi/api/CardsApi.kt index 799299f..91bf57c 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/openapi/api/CardsApi.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/openapi/api/CardsApi.kt @@ -117,4 +117,22 @@ interface CardsApi { ) @DeleteMapping fun removeCards(): ResponseEntity + + @Operation(summary = "Add card to collection", description = "TODO ...") + @ApiResponses( + value = [ + ApiResponse(responseCode = "204", description = "No Content") + ] + ) + @PostMapping("{cardId}/collection") + fun addToCollection(@PathVariable("cardId") cardId: Int): ResponseEntity + + @Operation(summary = "Remove card from collection", description = "TODO ...") + @ApiResponses( + value = [ + ApiResponse(responseCode = "204", description = "No Content") + ] + ) + @DeleteMapping("{cardId}/collection") + fun removeFromCollection(@PathVariable("cardId") cardId: Int): ResponseEntity } \ No newline at end of file diff --git a/src/main/kotlin/com/vmaier/marvel/snap/cards/service/CardsService.kt b/src/main/kotlin/com/vmaier/marvel/snap/cards/service/CardsService.kt index ced4608..c165b2b 100644 --- a/src/main/kotlin/com/vmaier/marvel/snap/cards/service/CardsService.kt +++ b/src/main/kotlin/com/vmaier/marvel/snap/cards/service/CardsService.kt @@ -43,7 +43,7 @@ class CardsService constructor( return cards } - fun getAllCardsByKeyword(keyword: String?, cost: Int?, power: Int?): Iterable { + fun getAllCardsByKeyword(keyword: String?, cost: Int?, power: Int?, isOwned: Boolean?): Iterable { var cards = if (keyword.isNullOrEmpty()) { cardRepository.findAll() } else { @@ -59,6 +59,11 @@ class CardsService constructor( card.power == power } } + if (isOwned != null) { + cards = cards.filter { card -> + card.isOwned == isOwned + } + } return cards } @@ -81,4 +86,12 @@ class CardsService constructor( fun removeAllCards() { cardRepository.deleteAll() } + + fun addToCollection(cardId: Int) { + cardRepository.updateIsOwned(getOneCard(cardId).id!!, true) + } + + fun removeFromCollection(cardId: Int) { + cardRepository.updateIsOwned(getOneCard(cardId).id!!, false) + } } \ No newline at end of file diff --git a/src/main/resources/db/migration/V1_0_0__Initial.sql b/src/main/resources/db/migration/V1_0_0__Initial.sql index b4a4166..2b65ef8 100644 --- a/src/main/resources/db/migration/V1_0_0__Initial.sql +++ b/src/main/resources/db/migration/V1_0_0__Initial.sql @@ -1,9 +1,10 @@ CREATE TABLE cards ( - id SERIAL PRIMARY KEY, - ability VARCHAR(1024), - cost INTEGER NOT NULL, - name VARCHAR(255), - power INTEGER NOT NULL, - url VARCHAR(255) + id SERIAL PRIMARY KEY, + ability VARCHAR(1024), + cost INTEGER NOT NULL, + name VARCHAR(255), + power INTEGER NOT NULL, + url VARCHAR(255), + is_owned BOOLEAN ); diff --git a/src/main/resources/templates/card-grid.html b/src/main/resources/templates/card-grid.html index 738753a..522e833 100644 --- a/src/main/resources/templates/card-grid.html +++ b/src/main/resources/templates/card-grid.html @@ -5,57 +5,82 @@ All cards +

All cards

-
- +
+ table_rows
- -
-
+ -
+
-
-
+
-
- +
+ + +
+ - Add card -
+ Add card +
-
+
+ th:data-id="${card.id}" th:src="@{${card.url}}" alt="card image"/> + check_circle

diff --git a/src/main/resources/templates/cards.html b/src/main/resources/templates/cards.html index f2260ff..34a684a 100644 --- a/src/main/resources/templates/cards.html +++ b/src/main/resources/templates/cards.html @@ -5,6 +5,8 @@ All cards +