From b7531670bf124ee31f098faa717f7bc99f5f7a4c Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Wed, 25 Dec 2024 22:07:13 -0700 Subject: [PATCH] Implement shelf sorting --- .../adityachandel/booklore/controller/ShelfController.java | 7 +++++++ .../com/adityachandel/booklore/service/ShelfService.java | 7 +++++++ booklore-ui/src/app/book/service/shelf.service.ts | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/controller/ShelfController.java b/booklore-api/src/main/java/com/adityachandel/booklore/controller/ShelfController.java index aa389ebb4..e17b456a9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/controller/ShelfController.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/controller/ShelfController.java @@ -1,8 +1,10 @@ package com.adityachandel.booklore.controller; import com.adityachandel.booklore.model.dto.BookDTO; +import com.adityachandel.booklore.model.dto.LibraryDTO; import com.adityachandel.booklore.model.dto.ShelfDTO; import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest; +import com.adityachandel.booklore.model.entity.Sort; import com.adityachandel.booklore.service.ShelfService; import jakarta.validation.Valid; import lombok.AllArgsConstructor; @@ -50,4 +52,9 @@ public class ShelfController { return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } + @PutMapping("/{shelfId}/sort") + public ResponseEntity updateShelf(@PathVariable long shelfId, @RequestBody Sort sort) { + return ResponseEntity.ok(shelfService.updateSort(shelfId, sort)); + } + } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java index 428b77e68..5cf4c0803 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/ShelfService.java @@ -6,6 +6,7 @@ import com.adityachandel.booklore.model.dto.ShelfDTO; import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest; import com.adityachandel.booklore.model.entity.Book; import com.adityachandel.booklore.model.entity.Shelf; +import com.adityachandel.booklore.model.entity.Sort; import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.repository.ShelfRepository; import com.adityachandel.booklore.transformer.BookTransformer; @@ -57,4 +58,10 @@ public class ShelfService { List books = bookRepository.findByShelfId(shelfId); return books.stream().map(BookTransformer::convertToBookDTO).toList(); } + + public ShelfDTO updateSort(long shelfId, Sort sort) { + Shelf shelf = shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId)); + shelf.setSort(sort); + return ShelfTransformer.convertToShelfDTO(shelfRepository.save(shelf)); + } } diff --git a/booklore-ui/src/app/book/service/shelf.service.ts b/booklore-ui/src/app/book/service/shelf.service.ts index b801f789e..e9cea9aa2 100644 --- a/booklore-ui/src/app/book/service/shelf.service.ts +++ b/booklore-ui/src/app/book/service/shelf.service.ts @@ -38,7 +38,7 @@ export class ShelfService { } updateSort(shelfId: number, sort: SortOption): Observable { - return this.http.put(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe( + return this.http.put(`${this.url}/${shelfId}/sort`, sort).pipe( map(updatedShelf => { const updatedShelves = this.shelves.value.map(shelf => shelf.id === shelfId ? updatedShelf : shelf