import type { AxiosInstance } from 'axios'; import type { User, Rating, RatingHalListSchemaType, RatingHalSchemaType, } from '../../types'; import { RatingHalListSchema, RatingHalSchema } from '../../types'; import type { AsyncResult } from '../../utilities'; import { errorResult, successResult, UnsupportedError } from '../../utilities'; import { BaseUsersResourceRepository } from './BaseUsersResourceRepository'; import type { ResourceQuery } from 'modules/BaseResourceRepository'; export class RatingsResourceRepository extends BaseUsersResourceRepository< RatingHalSchemaType, RatingHalListSchemaType > { constructor(axios: AxiosInstance) { super(axios, '/ratings', { get: RatingHalSchema, listForUser: RatingHalListSchema, }); } public async add( userId: User['id'], productionId: Rating['productionId'], rating: Rating['rating'], query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.post( `/users/${userId}${this._baseUrl}`, { productionId, rating }, this.getRequestConfig(query) ); const responseData = this._schemas.get.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } public async update( userId: User['id'], ratingId: Rating['id'], rating: Rating['rating'], query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.put( `/users/${userId}${this._baseUrl}/${ratingId}`, { rating }, this.getRequestConfig(query) ); const responseData = this._schemas.get.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } public async remove( userId: User['id'], ratingId: Rating['id'], query?: ResourceQuery ): AsyncResult { try { const response = await this._axios.delete( `/users/${userId}${this._baseUrl}/${ratingId}`, this.getRequestConfig(query) ); return successResult(response.data); } catch (error) { return errorResult(error as Error); } } public async getByProductionId( userId: string, productionId: string, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); try { const response = await this._axios.get( `/users/${userId}${this._baseUrl}/by-production/${productionId}`, this.getRequestConfig(query) ); if (response.status === 204) return successResult(null); const responseData = this._schemas.get.parse(response.data); return successResult(responseData); } catch (error) { return errorResult(error as Error); } } }