mirror of
https://github.com/booklore-app/booklore.git
synced 2025-12-23 22:28:11 -05:00
Implement shelf API
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.service.ShelfService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping(("/v1/shelf"))
|
||||
public class ShelfController {
|
||||
|
||||
private final ShelfService shelfService;
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<ShelfDTO>> findAll() {
|
||||
return ResponseEntity.ok(shelfService.getShelves());
|
||||
}
|
||||
|
||||
@GetMapping("/{shelfId}")
|
||||
public ResponseEntity<ShelfDTO> findById(@PathVariable Long shelfId) {
|
||||
return ResponseEntity.ok(shelfService.getShelf(shelfId));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<ShelfDTO> createShelf(@Valid @RequestBody ShelfCreateRequest request) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(shelfService.createShelf(request));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<ShelfDTO> updateShelf(@PathVariable Long id, @Valid @RequestBody ShelfCreateRequest request) {
|
||||
return ResponseEntity.ok(shelfService.updateShelf(id, request));
|
||||
}
|
||||
|
||||
@PostMapping("/{shelfId}/book/{bookId}")
|
||||
public ResponseEntity<ShelfDTO> addBookToShelf(@PathVariable Long shelfId, @PathVariable Long bookId) {
|
||||
return ResponseEntity.ok(shelfService.addBookToShelf(shelfId, bookId));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{shelfId}")
|
||||
public ResponseEntity<Void> deleteShelf(@PathVariable Long shelfId) {
|
||||
shelfService.deleteShelf(shelfId);
|
||||
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,7 +17,9 @@ public enum ApiError {
|
||||
DIRECTORY_CREATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create the directory: %s"),
|
||||
INVALID_LIBRARY_PATH(HttpStatus.BAD_REQUEST, "Invalid library path"),
|
||||
FILE_ALREADY_EXISTS(HttpStatus.CONFLICT, "File already exists"),
|
||||
INVALID_QUERY_PARAMETERS(HttpStatus.BAD_REQUEST, "Query parameters are required for the search.");
|
||||
INVALID_QUERY_PARAMETERS(HttpStatus.BAD_REQUEST, "Query parameters are required for the search."),
|
||||
SHELF_ALREADY_EXISTS(HttpStatus.CONFLICT, "Shelf already exists: %s"),
|
||||
SHELF_NOT_FOUND(HttpStatus.NOT_FOUND, "Shelf not found with ID: %d");
|
||||
|
||||
private final HttpStatus status;
|
||||
private final String message;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.adityachandel.booklore.exception;
|
||||
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -42,6 +43,12 @@ public class GlobalExceptionHandler {
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||
public ResponseEntity<ErrorResponse> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
|
||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(), "Data integrity violation: " + ex.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred.");
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@@ -15,4 +16,5 @@ public class BookDTO {
|
||||
private Instant lastReadTime;
|
||||
private Instant addedOn;
|
||||
private BookMetadataDTO metadata;
|
||||
private List<ShelfDTO> shelves;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class ShelfDTO {
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Null;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ShelfCreateRequest {
|
||||
|
||||
@Null(message = "Id should be null for creation.")
|
||||
private Long id;
|
||||
|
||||
@NotBlank(message = "Shelf name must not be empty.")
|
||||
private String name;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@@ -39,4 +40,12 @@ public class Book {
|
||||
|
||||
@Column(name = "added_on")
|
||||
private Instant addedOn;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "book_shelf_mapping",
|
||||
joinColumns = @JoinColumn(name = "book_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "shelf_id")
|
||||
)
|
||||
private List<Shelf> shelves;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package com.adityachandel.booklore.model.entity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@@ -22,6 +24,6 @@ public class Category {
|
||||
private String name;
|
||||
|
||||
@ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY)
|
||||
private List<BookMetadata> bookMetadataList;
|
||||
private Set<BookMetadata> bookMetadataList = new HashSet<>();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.adityachandel.booklore.model.entity;
|
||||
|
||||
public class PathInfo {
|
||||
private String name;
|
||||
private String fullPath;
|
||||
|
||||
public PathInfo(String name, String fullPath) {
|
||||
this.name = name;
|
||||
this.fullPath = fullPath;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getFullPath() {
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
public void setFullPath(String fullPath) {
|
||||
this.fullPath = fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.adityachandel.booklore.model.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "shelf")
|
||||
public class Shelf {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name", nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(
|
||||
name = "book_shelf_mapping",
|
||||
joinColumns = @JoinColumn(name = "book_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "shelf_id")
|
||||
)
|
||||
private Set<Book> books = new HashSet<>();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.adityachandel.booklore.repository;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.Shelf;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ShelfRepository extends JpaRepository<Shelf, Long> {
|
||||
List<Shelf> findShelfByName(String name);
|
||||
|
||||
boolean existsByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
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.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.ShelfRepository;
|
||||
import com.adityachandel.booklore.transformer.ShelfTransformer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class ShelfService {
|
||||
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public ShelfDTO createShelf(ShelfCreateRequest request) {
|
||||
boolean exists = shelfRepository.existsByName(request.getName());
|
||||
if (exists) {
|
||||
throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName());
|
||||
}
|
||||
Shelf shelf = Shelf.builder().name(request.getName()).build();
|
||||
return ShelfTransformer.convertToShelfDTO(shelfRepository.save(shelf));
|
||||
}
|
||||
|
||||
public ShelfDTO updateShelf(Long id, ShelfCreateRequest request) {
|
||||
Shelf shelf = shelfRepository.findById(id)
|
||||
.orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(id));
|
||||
shelf.setName(request.getName());
|
||||
return ShelfTransformer.convertToShelfDTO(shelfRepository.save(shelf));
|
||||
}
|
||||
|
||||
public ShelfDTO addBookToShelf(Long shelfId, Long bookId) {
|
||||
Shelf shelf = shelfRepository.findById(shelfId)
|
||||
.orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
|
||||
Book book = bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
shelf.getBooks().add(book);
|
||||
shelfRepository.save(shelf);
|
||||
return ShelfTransformer.convertToShelfDTO(shelf);
|
||||
}
|
||||
|
||||
public List<ShelfDTO> getShelves() {
|
||||
return shelfRepository.findAll().stream().map(ShelfTransformer::convertToShelfDTO).toList();
|
||||
}
|
||||
|
||||
public ShelfDTO getShelf(Long shelfId) {
|
||||
Shelf shelf = shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
|
||||
return ShelfTransformer.convertToShelfDTO(shelf);
|
||||
}
|
||||
|
||||
public void deleteShelf(Long shelfId) {
|
||||
shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
|
||||
shelfRepository.deleteById(shelfId);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.adityachandel.booklore.transformer;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.entity.Book;
|
||||
|
||||
public class BookTransformer {
|
||||
@@ -13,6 +14,7 @@ public class BookTransformer {
|
||||
bookDTO.setLastReadTime(book.getLastReadTime());
|
||||
bookDTO.setAddedOn(book.getAddedOn());
|
||||
bookDTO.setMetadata(BookMetadataTransformer.convertToBookDTO(book.getMetadata()));
|
||||
bookDTO.setShelves(book.getShelves() == null ? null : book.getShelves().stream().map(ShelfTransformer::convertToShelfDTO).toList());
|
||||
return bookDTO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.adityachandel.booklore.transformer;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.entity.Shelf;
|
||||
|
||||
public class ShelfTransformer {
|
||||
|
||||
public static ShelfDTO convertToShelfDTO(Shelf shelf) {
|
||||
return ShelfDTO.builder()
|
||||
.id(shelf.getId())
|
||||
.name(shelf.getName())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS library
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) UNIQUE NOT NULL,
|
||||
paths TEXT,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) UNIQUE NOT NULL,
|
||||
paths TEXT,
|
||||
initial_processed BOOLEAN DEFAULT false
|
||||
);
|
||||
|
||||
@@ -78,4 +78,20 @@ CREATE TABLE IF NOT EXISTS book_metadata_author_mapping
|
||||
CONSTRAINT fk_book_metadata_author_mapping_author FOREIGN KEY (author_id) REFERENCES author (id) ON DELETE CASCADE,
|
||||
INDEX idx_book_metadata_id (book_id),
|
||||
INDEX idx_author_id (author_id)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS shelf
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS book_shelf_mapping
|
||||
(
|
||||
book_id BIGINT NOT NULL,
|
||||
shelf_id BIGINT NOT NULL,
|
||||
PRIMARY KEY (book_id, shelf_id),
|
||||
CONSTRAINT fk_book_shelf_mapping_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -91,9 +91,9 @@ export class LibraryAndBookService {
|
||||
/*---------- Book Methods go below ----------*/
|
||||
|
||||
readBook(book: Book): void {
|
||||
this.addToLastReadBooks(book);
|
||||
const url = `/pdf-viewer/book/${book.id}`;
|
||||
window.open(url, '_blank');
|
||||
this.addToLastReadBooks(book);
|
||||
this.updateLastReadTime(book.id).subscribe({
|
||||
complete: () => {
|
||||
},
|
||||
@@ -102,8 +102,9 @@ export class LibraryAndBookService {
|
||||
}
|
||||
|
||||
addToLastReadBooks(book: Book): void {
|
||||
const updatedBooks = [book, ...this.#lastReadBooks()];
|
||||
this.#lastReadBooks.set(updatedBooks.slice(0, 25));
|
||||
this.#lastReadBooks.set([book, ...this.#lastReadBooks()
|
||||
.filter(b => b.id !== book.id)]
|
||||
.slice(0, 25));
|
||||
}
|
||||
|
||||
addToLatestAddedBooks(book: Book): void {
|
||||
|
||||
Reference in New Issue
Block a user