import { AbstractService, EndpointCreator, HttpMethod, Modules, NextRef, PreviousRef } from "../../../core"; import { HowToInput, HowToInterface } from "./HowToInterface"; export class HowToService extends AbstractService { static async findOne(params: { id: string }): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.GET, endpoint: new EndpointCreator({ endpoint: Modules.HowTo, id: params.id }).generate(), }); } static async findMany( params: { search?: string; fetchAll?: boolean; next?: NextRef; prev?: PreviousRef; } = {}, ): Promise { const endpoint = new EndpointCreator({ endpoint: Modules.HowTo }); if (params.fetchAll) endpoint.addAdditionalParam("fetchAll", "true"); if (params.search) endpoint.addAdditionalParam("search", params.search); if (Modules.HowTo.inclusions?.lists?.fields) endpoint.limitToFields(Modules.HowTo.inclusions.lists.fields); if (Modules.HowTo.inclusions?.lists?.types) endpoint.limitToType(Modules.HowTo.inclusions.lists.types); return this.callApi({ type: Modules.HowTo, method: HttpMethod.GET, endpoint: endpoint.generate(), next: params.next, }); } static async create(params: HowToInput): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.POST, endpoint: new EndpointCreator({ endpoint: Modules.HowTo }).generate(), input: params, }); } static async update(params: HowToInput): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.PUT, endpoint: new EndpointCreator({ endpoint: Modules.HowTo, id: params.id }).generate(), input: params, }); } static async delete(params: { howToId: string }): Promise { await this.callApi({ type: Modules.HowTo, method: HttpMethod.DELETE, endpoint: new EndpointCreator({ endpoint: Modules.HowTo, id: params.howToId }).generate(), }); } static async findPublished(params: { howToType?: string } = {}): Promise { const endpoint = new EndpointCreator({ endpoint: `public/${Modules.HowTo.name}` }); if (params.howToType) endpoint.addAdditionalParam("type", params.howToType); return this.callApi({ type: Modules.HowTo, method: HttpMethod.GET, endpoint: endpoint.generate(), }); } static async findPublishedArticle(params: { howToType: string; slug: string }): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.GET, endpoint: new EndpointCreator({ endpoint: `public/${Modules.HowTo.name}`, id: params.howToType, childEndpoint: params.slug, }).generate(), }); } static async findRelated(params: { howToType: string; slug: string }): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.GET, endpoint: new EndpointCreator({ endpoint: `public/${Modules.HowTo.name}`, id: params.howToType, childEndpoint: params.slug, childId: "related", }).generate(), }); } static async addRelated(params: { howToId: string; relatedId: string }): Promise { return this.callApi({ type: Modules.HowTo, method: HttpMethod.POST, endpoint: new EndpointCreator({ endpoint: Modules.HowTo, id: params.howToId, childEndpoint: "related", childId: params.relatedId, }).generate(), }); } static async removeRelated(params: { howToId: string; relatedId: string }): Promise { await this.callApi({ type: Modules.HowTo, method: HttpMethod.DELETE, endpoint: new EndpointCreator({ endpoint: Modules.HowTo, id: params.howToId, childEndpoint: "related", childId: params.relatedId, }).generate(), }); } }