107 lines
2.9 KiB
TypeScript
107 lines
2.9 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Observable } from 'rxjs';
|
|
import { Collection } from '../../shared/models/collection.model';
|
|
import { CollectionEntry } from '../../shared/models/collection-entry.model';
|
|
import { Entry } from '../../shared/models/entry.model';
|
|
import { environment } from '../../../environments/environment.prod';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CollectionService {
|
|
private backend_apiUrl = environment.API_URL;
|
|
private apiAction = 'collections';
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
// get all collections
|
|
getCollections(): Observable<Collection[]> {
|
|
return this.http.get<Collection[]>(
|
|
`${this.backend_apiUrl}/${this.apiAction}`
|
|
);
|
|
}
|
|
|
|
// get a collection by id
|
|
getCollection(collectionId: number): Observable<Collection> {
|
|
return this.http.get<Collection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}`
|
|
);
|
|
}
|
|
|
|
// Create a new collection
|
|
createCollection(collection: Collection): Observable<Collection> {
|
|
return this.http.post<Collection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}`,
|
|
collection
|
|
);
|
|
}
|
|
|
|
// * PUT - update a collection
|
|
updateCollection(
|
|
collectionId: number,
|
|
formData: FormData
|
|
): Observable<Collection> {
|
|
return this.http.put<Collection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}`,
|
|
formData
|
|
);
|
|
}
|
|
|
|
// * PUT - REFRESH COLLECTION
|
|
refreshCollection(collectionId: number): Observable<Collection> {
|
|
return this.http.put<Collection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}/refresh`,
|
|
{}
|
|
);
|
|
}
|
|
|
|
// * PUT - GET COLLECTION PARTS
|
|
getCollectionParts(collectionId: number): Observable<Collection> {
|
|
return this.http.put<Collection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}/parts`,
|
|
{}
|
|
);
|
|
}
|
|
|
|
// Delete a collection by ID
|
|
deleteCollection(collectionId: number): Observable<any> {
|
|
return this.http.delete(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}`
|
|
);
|
|
}
|
|
|
|
// Add an entry to a collection
|
|
addEntryToCollection(
|
|
collectionId: number,
|
|
entryId: number
|
|
): Observable<CollectionEntry> {
|
|
return this.http.post<CollectionEntry>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}/entries/${entryId}`,
|
|
{}
|
|
);
|
|
}
|
|
|
|
// Update an entry's order in a collection
|
|
updateEntryOrder(
|
|
collectionId: number,
|
|
entryId: number,
|
|
newOrder: number
|
|
): Observable<any> {
|
|
return this.http.patch(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}/entries/${entryId}`,
|
|
{ newOrder: newOrder }
|
|
);
|
|
}
|
|
|
|
// Remove an entry from a collection
|
|
removeEntryFromCollection(
|
|
collectionId: number,
|
|
entryId: number
|
|
): Observable<Entry> {
|
|
return this.http.delete<Entry>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}/entries/${entryId}`
|
|
);
|
|
}
|
|
}
|