import { inject, injectable } from "inversify"; import BaseService from "../../service"; import { IHttpClient, IService, IResourceMapper, IOpenIdUserInfoResponse, IFindParams, IJsonApiResponse } from "../../interfaces"; import Bookmark, { IBookmark, IBookmarkEntryResource, IBookmarkEntryResponse } from "../bookmark"; import Mapper from "./bookmarkMapper"; import { IBookmarkList } from "../bookmarkList"; import * as TYPES from "../../types"; export interface IBookmarkService extends IService { addBookmarkToList(userId: string, bookmark: IBookmark, list: IBookmarkList): Promise; updateBookmark(userId: string, bookmark: IBookmark, list: IBookmarkList): Promise; isResourceBookmarked(userId, targetId): Promise; } @injectable() export default class BookmarkService extends BaseService implements IBookmarkService { resource: string = "bookmark-entry"; mapper: IResourceMapper = new Mapper(); public url({ userId, listId }) { return `${this.host}/users/${userId}/bookmark-lists/${listId}/entries`; } public async addBookmarkToList(userId: string, bookmark: IBookmark, list: IBookmarkList) { const resource = await this.http.fetch( this.url({ userId, listId: list.id }), { method: "POST", body: { data: this.mapper.toResource(bookmark), }, headers: this.headers(), } ); return this.mapper.map(resource); } public async updateOrInsertBookmarksToList(userId: string, bookmarks: IBookmark[], list: IBookmarkList) { const upsetUrl = `${this.url({ userId, listId: list.id })}/upsert`; const response = await this.http.fetch( upsetUrl, { method: "POST", body: { data: bookmarks.map(this.mapper.toResource) }, headers: this.headers(), } ); return this.mapper.map(response); } public async updateBookmark(userId: string, bookmark: IBookmark, list: IBookmarkList) { const resource = await this.http.fetch( `${this.url({ userId, listId: list.id })}/${bookmark.id}`, { method: "PATCH", body: { data: this.mapper.toResource(bookmark), }, headers: this.headers(), }); return this.mapper.map(resource); } public async isResourceBookmarked(userId, targetId): Promise { const url = `${this.host}/users/${userId}/bookmark-entries?filter[target][id][equals]=${targetId}`; const response = await this.http.fetch(url, { headers: this.headers(), }); return this.mapper.map(response); } }