Sorting works with the BE! Yay! Need to improve the the code though

This commit is contained in:
aditya.chandel
2024-12-25 18:17:40 -07:00
parent affdcb95c6
commit 383a3189bb
9 changed files with 117 additions and 102 deletions
@@ -1,10 +1,11 @@
import {Injectable, OnInit} from '@angular/core';
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of, tap} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {catchError, map} from 'rxjs/operators';
import {Library} from '../model/library.model';
import {BookService} from './book.service';
import {BookWithNeighborsDTO} from '../model/book.model';
import {Sort} from '../model/sort.model';
@Injectable({
providedIn: 'root'
@@ -51,6 +52,22 @@ export class LibraryService {
);
}
updateSort(libraryId: number, sort: Sort): Observable<Library> {
return this.http.put<Library>(`${this.url}/${libraryId}/sort`, sort).pipe(
map(updatedLibrary => {
const updatedShelves = this.libraries.value.map(library =>
library.id === libraryId ? updatedLibrary : library
);
this.libraries.next(updatedShelves);
return updatedLibrary;
}),
catchError(error => {
console.error('Error updating library sort:', error);
throw error;
})
);
}
getBookWithNeighbours(libraryId: number, bookId: number): Observable<BookWithNeighborsDTO> {
return this.http.get<BookWithNeighborsDTO>(`${this.url}/${libraryId}/book/${bookId}/withNeighbors`);
}
@@ -3,6 +3,7 @@ import {BehaviorSubject, Observable, of} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
import {Shelf} from '../model/shelf.model';
import {Sort} from '../model/sort.model';
@Injectable({
providedIn: 'root'
@@ -35,4 +36,21 @@ export class ShelfService {
})
);
}
updateSort(shelfId: number, sort: Sort): Observable<Shelf> {
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe(
map(updatedShelf => {
const updatedShelves = this.shelves.value.map(shelf =>
shelf.id === shelfId ? updatedShelf : shelf
);
this.shelves.next(updatedShelves);
return updatedShelf;
}),
catchError(error => {
console.error('Error updating shelf sort:', error);
throw error;
})
);
}
}
@@ -5,21 +5,8 @@ import { SortOption } from '../model/sort-option.model';
providedIn: 'root',
})
export class SortService {
private selectedSorts: { [key: string]: SortOption | null } = {};
generateKey(entityType: string, entityId: number): string {
if (entityType && entityId) {
return `${entityType.toLowerCase()}-${entityId}`;
}
return '';
}
getSortOption(key: string): SortOption | null {
return this.selectedSorts[key] || null;
}
setSortOption(key: string, sortOption: SortOption): void {
this.selectedSorts[key] = sortOption;
}
}