79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Observable } from 'rxjs';
|
|
import { UserCollection } from '../../shared/models/userCollection.model';
|
|
import { UserCollectionEntry } from '../../shared/models/userCollectionEntry.model';
|
|
import { Entry } from '../../shared/models/entry.model';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class UserCollectionService {
|
|
private backend_apiUrl = 'http://localhost:4000/api/v1';
|
|
private apiAction = 'user-collections';
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
// get all lists
|
|
getCollections(): Observable<UserCollection[]> {
|
|
return this.http.get<UserCollection[]>(
|
|
`${this.backend_apiUrl}/${this.apiAction}`
|
|
);
|
|
}
|
|
|
|
// get a list by id
|
|
getCollection(userCollectionId: string) {
|
|
return this.http.get<UserCollection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}`
|
|
);
|
|
}
|
|
|
|
// Create a new list
|
|
createCollection(userCollection: UserCollection): Observable<UserCollection> {
|
|
return this.http.post<UserCollection>(
|
|
`${this.backend_apiUrl}/${this.apiAction}`,
|
|
userCollection
|
|
);
|
|
}
|
|
|
|
// Delete a list by ID
|
|
deleteCollection(userCollectionId: number): Observable<any> {
|
|
return this.http.delete(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}`
|
|
);
|
|
}
|
|
|
|
// Add an entry to a list
|
|
addEntryToCollection(
|
|
userCollectionId: number,
|
|
entryId: number
|
|
): Observable<UserCollectionEntry> {
|
|
return this.http.post<UserCollectionEntry>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`,
|
|
{}
|
|
);
|
|
}
|
|
|
|
// Update an entry's order in a list
|
|
updateEntryOrder(
|
|
userCollectionId: string,
|
|
entryId: number,
|
|
newOrder: number
|
|
): Observable<any> {
|
|
return this.http.patch(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`,
|
|
{ newOrder: newOrder }
|
|
);
|
|
}
|
|
|
|
// Remove an entry from a list
|
|
removeEntryFromCollection(
|
|
userCollectionId: string,
|
|
entryId: number
|
|
): Observable<Entry> {
|
|
return this.http.delete<Entry>(
|
|
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`
|
|
);
|
|
}
|
|
}
|