Implement shelf sorting
This commit is contained in:
@@ -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<ShelfDTO> updateShelf(@PathVariable long shelfId, @RequestBody Sort sort) {
|
||||
return ResponseEntity.ok(shelfService.updateSort(shelfId, sort));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Book> 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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ export class ShelfService {
|
||||
}
|
||||
|
||||
updateSort(shelfId: number, sort: SortOption): Observable<Shelf> {
|
||||
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe(
|
||||
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, sort).pipe(
|
||||
map(updatedShelf => {
|
||||
const updatedShelves = this.shelves.value.map(shelf =>
|
||||
shelf.id === shelfId ? updatedShelf : shelf
|
||||
|
||||
Reference in New Issue
Block a user