Added shelf functionality
This commit is contained in:
+5
-3
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.AssignShelvesRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.service.ShelfService;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -38,9 +40,9 @@ public class ShelfController {
|
||||
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));
|
||||
@PostMapping("/assign-shelves")
|
||||
public ResponseEntity<BookDTO> addBookToShelf(@RequestBody @Valid AssignShelvesRequest request) {
|
||||
return ResponseEntity.ok(shelfService.addBookToShelf(request.getBookId(), request.getShelfIds()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{shelfId}")
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class AssignShelvesRequest {
|
||||
private Long bookId;
|
||||
private List<Long> shelfIds;
|
||||
}
|
||||
@@ -22,11 +22,6 @@ public class Shelf {
|
||||
@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")
|
||||
)
|
||||
@ManyToMany(mappedBy = "shelves", fetch = FetchType.LAZY)
|
||||
private Set<Book> books = new HashSet<>();
|
||||
}
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
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.BookTransformer;
|
||||
import com.adityachandel.booklore.transformer.ShelfTransformer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class ShelfService {
|
||||
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public ShelfDTO createShelf(ShelfCreateRequest request) {
|
||||
boolean exists = shelfRepository.existsByName(request.getName());
|
||||
@@ -36,14 +40,26 @@ public class ShelfService {
|
||||
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));
|
||||
@Transactional
|
||||
public BookDTO addBookToShelf(Long bookId, List<Long> shelfIds) {
|
||||
Book book = bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
shelf.getBooks().add(book);
|
||||
shelfRepository.save(shelf);
|
||||
return ShelfTransformer.convertToShelfDTO(shelf);
|
||||
|
||||
List<Shelf> currentShelves = book.getShelves();
|
||||
List<Shelf> shelvesToAdd = shelfRepository.findAllById(shelfIds);
|
||||
|
||||
currentShelves.stream()
|
||||
.filter(shelf -> !shelfIds.contains(shelf.getId()))
|
||||
.forEach(shelf -> shelf.getBooks().remove(book));
|
||||
|
||||
book.getShelves().clear();
|
||||
book.getShelves().addAll(shelvesToAdd);
|
||||
|
||||
shelvesToAdd.forEach(shelf -> shelf.getBooks().add(book));
|
||||
|
||||
bookRepository.save(book);
|
||||
shelfRepository.saveAll(shelvesToAdd);
|
||||
return BookTransformer.convertToBookDTO(book);
|
||||
}
|
||||
|
||||
public List<ShelfDTO> getShelves() {
|
||||
|
||||
@@ -28,6 +28,8 @@ import {LazyLoadImageModule} from 'ng-lazyload-image';
|
||||
import {ConfirmDialogModule} from 'primeng/confirmdialog';
|
||||
import { ShelfAssignerComponent } from './shelf-assigner/shelf-assigner.component';
|
||||
import {CheckboxModule} from 'primeng/checkbox';
|
||||
import {DividerModule} from 'primeng/divider';
|
||||
import {DialogModule} from 'primeng/dialog';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
@@ -57,6 +59,8 @@ import {CheckboxModule} from 'primeng/checkbox';
|
||||
LazyLoadImageModule,
|
||||
ConfirmDialogModule,
|
||||
CheckboxModule,
|
||||
DividerModule,
|
||||
DialogModule,
|
||||
],
|
||||
providers: [
|
||||
DialogService,
|
||||
|
||||
@@ -26,10 +26,8 @@ export class BookCardComponent implements OnInit {
|
||||
constructor(
|
||||
private libraryBookService: LibraryAndBookService, private router: Router,
|
||||
private dialogService: DialogService) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
coverImageSrc(book: Book): string {
|
||||
return this.libraryBookService.getBookCoverUrl(book.id);
|
||||
}
|
||||
@@ -61,10 +59,10 @@ export class BookCardComponent implements OnInit {
|
||||
|
||||
private openShelfDialog(book: Book) {
|
||||
this.dialogService.open(ShelfAssignerComponent, {
|
||||
header: 'Edit Shelves for Book: ' + book.metadata.title,
|
||||
header: 'Update Shelves: ' + book.metadata.title,
|
||||
modal: true,
|
||||
width: '50%',
|
||||
height: '75%',
|
||||
width: '30%',
|
||||
height: '70%',
|
||||
contentStyle: {overflow: 'auto'},
|
||||
baseZIndex: 10,
|
||||
data: {
|
||||
|
||||
@@ -18,7 +18,7 @@ export class LibraryCreatorComponent {
|
||||
ref: DynamicDialogRef | undefined;
|
||||
|
||||
constructor(
|
||||
public dialogService: DialogService,
|
||||
private dialogService: DialogService,
|
||||
private dynamicDialogRef: DynamicDialogRef,
|
||||
private libraryAndBookService: LibraryAndBookService,
|
||||
private router: Router,
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface Category {
|
||||
}
|
||||
|
||||
export interface Shelf {
|
||||
id: number;
|
||||
id?: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,17 +41,54 @@ export class LibraryAndBookService {
|
||||
this.http.get<Shelf[]>(this.shelfUrl).pipe(
|
||||
map(response => response),
|
||||
catchError(error => {
|
||||
console.error('Error loading libraries:', error);
|
||||
console.error('Error loading shelves:', error);
|
||||
return of([]);
|
||||
})
|
||||
).subscribe(
|
||||
(shelves) => {
|
||||
this.#shelves.set(shelves);
|
||||
console.log("Library Initialized")
|
||||
console.log("Shelves Initialized")
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
createShelf(name: string): Observable<Shelf> {
|
||||
const newShelf: Shelf = {name};
|
||||
return this.http.post<Shelf>(this.shelfUrl, newShelf).pipe(
|
||||
tap((createdShelf) => {
|
||||
this.#shelves.set([...this.#shelves(), createdShelf]);
|
||||
console.log("New Shelf Created", createdShelf);
|
||||
}),
|
||||
catchError((error) => {
|
||||
console.error('Error creating shelf:', error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
assignShelvesToBook(book: Book, shelfIds: number[]): Observable<Book> {
|
||||
const requestPayload = {
|
||||
bookId: book.id,
|
||||
shelfIds: shelfIds
|
||||
};
|
||||
return this.http.post<Book>(`${this.shelfUrl}/assign-shelves`, requestPayload).pipe(
|
||||
tap((updatedBook) => {
|
||||
const bookLibraryId = book.libraryId;
|
||||
const updatedMap = new Map(this.libraryBooksMap());
|
||||
const libraryBooks = updatedMap.get(bookLibraryId) || [];
|
||||
const updatedLibraryBooks = libraryBooks.map(b =>
|
||||
b.id === book.id ? { ...b, shelves: updatedBook.shelves } : b
|
||||
);
|
||||
updatedMap.set(bookLibraryId, updatedLibraryBooks);
|
||||
this.libraryBooksMap.set(updatedMap);
|
||||
}),
|
||||
catchError((error) => {
|
||||
console.error('Error assigning shelves to book:', error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*---------- Library Methods go below ----------*/
|
||||
|
||||
@@ -266,4 +303,6 @@ export class LibraryAndBookService {
|
||||
const requestBody = {googleBookId: googleBookId};
|
||||
return this.http.put<void>(`${this.bookUrl}/${bookId}/set-metadata`, requestBody);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,33 @@
|
||||
<div *ngFor="let shelf of shelves()" class="flex items-center">
|
||||
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves" />
|
||||
<label [for]="shelf.id" class="ml-2"> {{shelf.name }} </label>
|
||||
<div class="dialog-container">
|
||||
<div class="checkbox-list">
|
||||
<div *ngFor="let shelf of shelves()" class="flex items-center">
|
||||
<p-checkbox [inputId]="shelf.name" name="group" [value]="shelf" [(ngModel)]="selectedShelves" />
|
||||
<label [for]="shelf.id" class="ml-2">{{ shelf.name }}</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Divider and Bottom Section -->
|
||||
<div class="bottom-section">
|
||||
<p-divider></p-divider>
|
||||
<div class="bottom-container">
|
||||
<p-button outlined="true" label="Create Shelf" (onClick)="createShelfDialog()"></p-button>
|
||||
<div>
|
||||
<p-button class="spacing-right" severity="secondary" label="Cancel" (onClick)="closeDialog()"></p-button>
|
||||
<p-button label="Save" (onClick)="updateBooksShelves()"></p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p-dialog [(visible)]="displayShelfDialog" header="Create Shelf" [modal]="true" [closable]="true" (onHide)="closeShelfDialog()" [style]="{ width: '400px', height: 'auto' }">
|
||||
<div class="p-fluid">
|
||||
<div class="field">
|
||||
<label for="shelfName">Shelf Name</label>
|
||||
<input id="shelfName" type="text" pInputText [(ngModel)]="shelfName"/>
|
||||
</div>
|
||||
</div>
|
||||
<ng-template pTemplate="footer">
|
||||
<p-button label="Cancel" severity="secondary" (onClick)="closeShelfDialog()" class="p-button-text"></p-button>
|
||||
<p-button label="Save" (onClick)="saveNewShelf()"></p-button>
|
||||
</ng-template>
|
||||
</p-dialog>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
.dialog-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.bottom-section {
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.bottom-container {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
p-divider {
|
||||
margin: 1rem 0;
|
||||
border-color: var(--text-color-secondary);
|
||||
}
|
||||
|
||||
.spacing-right {
|
||||
margin-right: 1rem;
|
||||
}
|
||||
|
||||
p-dialog .p-dialog-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
gap: 1rem; /* Adds space between elements inside the dialog */
|
||||
}
|
||||
|
||||
p-dialog .p-dialog-footer {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.dialog-container .checkbox-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem; /* Adds gap between each checkbox row */
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.checkbox-list .flex {
|
||||
margin-bottom: 0.5rem; /* Optional: adds a smaller gap between each checkbox item */
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import {Component, computed, OnInit} from '@angular/core';
|
||||
import {LibraryAndBookService} from '../book/service/library-and-book.service';
|
||||
import {DynamicDialogConfig} from 'primeng/dynamicdialog';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {Book} from '../book/model/book.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shelf-assigner',
|
||||
@@ -18,8 +19,12 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
|
||||
book: Book;
|
||||
selectedShelves: any[] = [];
|
||||
displayShelfDialog: boolean = false;
|
||||
shelfName: string = '';
|
||||
|
||||
constructor(private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig) {
|
||||
constructor(
|
||||
private libraryBookService: LibraryAndBookService, private dynamicDialogConfig: DynamicDialogConfig,
|
||||
private dynamicDialogRef: DynamicDialogRef, private messageService: MessageService) {
|
||||
this.book = this.dynamicDialogConfig.data.book;
|
||||
}
|
||||
|
||||
@@ -30,4 +35,42 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
saveNewShelf() {
|
||||
this.libraryBookService.createShelf(this.shelfName).subscribe(
|
||||
() => {
|
||||
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Shelf created: ' + this.shelfName});
|
||||
this.displayShelfDialog = false;
|
||||
},
|
||||
(error) => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to create shelf'});
|
||||
console.error('Error creating shelf:', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
updateBooksShelves() {
|
||||
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
|
||||
this.libraryBookService.assignShelvesToBook(this.book, shelfIds).subscribe(
|
||||
() => {
|
||||
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
|
||||
this.dynamicDialogRef.close();
|
||||
},
|
||||
(error) => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
|
||||
this.dynamicDialogRef.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
createShelfDialog(): void {
|
||||
this.displayShelfDialog = true;
|
||||
}
|
||||
|
||||
closeShelfDialog(): void {
|
||||
this.displayShelfDialog = false;
|
||||
}
|
||||
|
||||
closeDialog() {
|
||||
this.dynamicDialogRef.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user