Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './remote-create-input-use-active-ingredient-use-case'
export * from './remote-delete-input-use-active-ingredient-use-case'
export * from './remote-get-input-use-active-ingredient-use-case'
export * from './remote-get-input-use-active-ingredients-use-case'
export * from './remote-update-input-use-active-ingredient-use-case'
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { type HttpClient, HttpStatusCode } from '@/core/data/protocols/http'
import {
BadRequestError,
ForbiddenError,
UnexpectedError,
} from '@/core/domain/errors'

import type { CreateInputUseActiveIngredientUseCase } from '../../../domain/use-cases/input-use-active-ingredients-use-cases'

export class RemoteCreateInputUseActiveIngredientUseCase
implements CreateInputUseActiveIngredientUseCase
{
constructor(
private readonly url: string,
private readonly httpClient: HttpClient
) {}

execute: CreateInputUseActiveIngredientUseCase['execute'] = async ({
inputUseActiveIngredient,
}) => {
const { statusCode } = await this.httpClient.request({
url: this.url,
method: 'post',
body: inputUseActiveIngredient,
})

if (statusCode === HttpStatusCode.created) return

if (statusCode === HttpStatusCode.badRequest) throw new BadRequestError()

if (statusCode === HttpStatusCode.forbidden) {
throw new ForbiddenError(
'Você não tem permissão para criar um princípio ativo.'
)
}

throw new UnexpectedError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { type HttpClient, HttpStatusCode } from '@/core/data/protocols/http'
import {
UnexpectedError,
NotFoundError,
ForbiddenError,
} from '@/core/domain/errors'

import type { DeleteInputUseActiveIngredientUseCase } from '../../../domain/use-cases/input-use-active-ingredients-use-cases'

export class RemoteDeleteInputUseActiveIngredientUseCase
implements DeleteInputUseActiveIngredientUseCase
{
constructor(
private readonly url: string,
private readonly httpClient: HttpClient
) {}

execute: DeleteInputUseActiveIngredientUseCase['execute'] = async ({
id,
}) => {
const { statusCode } = await this.httpClient.request({
url: `${this.url}/${id}`,
method: 'delete',
})

if (statusCode === HttpStatusCode.noContent) return

if (statusCode === HttpStatusCode.notFound) {
throw new NotFoundError('Princípio Ativo')
}

if (statusCode === HttpStatusCode.forbidden) {
throw new ForbiddenError(
'Você não tem permissão para excluir um princípio ativo.'
)
}

throw new UnexpectedError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type HttpClient, HttpStatusCode } from '@/core/data/protocols/http'
import {
BadRequestError,
ForbiddenError,
NotFoundError,
UnexpectedError,
} from '@/core/domain/errors'

import type {
InputUseActiveIngredientDetailsApiResponse,
InputUseActiveIngredientDetailsModel,
} from '../../../domain/models/input-use-active-ingredients-model'
import type { GetInputUseActiveIngredientUseCase } from '../../../domain/use-cases/input-use-active-ingredients-use-cases'

export class RemoteGetInputUseActiveIngredientUseCase
implements GetInputUseActiveIngredientUseCase
{
constructor(
private readonly url: string,
private readonly httpClient: HttpClient<
InputUseActiveIngredientDetailsModel,
InputUseActiveIngredientDetailsApiResponse
>
) {}

execute: GetInputUseActiveIngredientUseCase['execute'] = async ({ id }) => {
const { statusCode, body } = await this.httpClient.request({
url: `${this.url}/${id}`,
method: 'get',
})

if (statusCode === HttpStatusCode.ok && !!body)
return {
name: body.name,
}

if (statusCode === HttpStatusCode.badRequest) throw new BadRequestError()

if (statusCode === HttpStatusCode.notFound)
throw new NotFoundError('Princípio Ativo')

if (statusCode === HttpStatusCode.forbidden) {
throw new ForbiddenError(
'Você não tem permissão para acessar os dados deste princípio ativo.'
)
}

throw new UnexpectedError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { type HttpClient, HttpStatusCode } from '@/core/data/protocols/http'
import {
UnexpectedError,
NotFoundError,
ForbiddenError,
} from '@/core/domain/errors'

import type {
InputUseActiveIngredientApiResponse,
InputUseActiveIngredientModel,
} from '../../../domain/models/input-use-active-ingredients-model'
import type { GetInputUseActiveIngredientsUseCase } from '../../../domain/use-cases/input-use-active-ingredients-use-cases'
import type { ListApiResponse, MapApiProperties } from '@/core/domain/types'

export class RemoteGetInputUseActiveIngredientsUseCase
implements GetInputUseActiveIngredientsUseCase
{
constructor(
private readonly url: string,
private readonly httpClient: HttpClient<
InputUseActiveIngredientModel,
InputUseActiveIngredientApiResponse,
ListApiResponse<InputUseActiveIngredientApiResponse[]>
>
) {}

execute: GetInputUseActiveIngredientsUseCase['execute'] = async ({
filters,
pagination,
sort,
}) => {
const mapApiProperties: MapApiProperties<
InputUseActiveIngredientModel,
InputUseActiveIngredientApiResponse
> = {
id: 'id',
name: 'name',
}

const { statusCode, body } = await this.httpClient.request({
url: `${this.url}/search`,
method: 'post',
filters,
pagination,
sort,
mapApiProperties,
})

if (statusCode === HttpStatusCode.ok && !!body) {
return {
resources: body.content.map((item) => {
return {
id: item.id,
name: item.name,
}
}),
totalPages: Math.ceil(body.numberOfElements / body.pageable.pageSize),
}
}

if (statusCode === HttpStatusCode.notFound) {
throw new NotFoundError('Princípio Ativo')
}

if (statusCode === HttpStatusCode.forbidden) {
throw new ForbiddenError(
'Você não tem permissão para buscar os princípios ativos.'
)
}

throw new UnexpectedError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { type HttpClient, HttpStatusCode } from '@/core/data/protocols/http'
import {
BadRequestError,
ForbiddenError,
UnexpectedError,
} from '@/core/domain/errors'

import type { UpdateInputUseActiveIngredientUseCase } from '../../../domain/use-cases/input-use-active-ingredients-use-cases'

export class RemoteUpdateInputUseActiveIngredientUseCase
implements UpdateInputUseActiveIngredientUseCase
{
constructor(
private readonly url: string,
private readonly httpClient: HttpClient
) {}

execute: UpdateInputUseActiveIngredientUseCase['execute'] = async ({
inputUseActiveIngredient: { id, ...inputUseActiveIngredient },
}) => {
const { statusCode } = await this.httpClient.request({
url: `${this.url}/${id}`,
method: 'patch',
body: inputUseActiveIngredient,
})

if (statusCode === HttpStatusCode.noContent) return

if (statusCode === HttpStatusCode.badRequest) throw new BadRequestError()

if (statusCode === HttpStatusCode.forbidden) {
throw new ForbiddenError(
'Você não tem permissão para editar um princípio ativo.'
)
}

throw new UnexpectedError()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { WithId } from '@/core/domain/types'

export type InputUseActiveIngredientDetailsModel = {
name: string
}

export type InputUseActiveIngredientDetailsApiResponse = {
name: string
}

export type InputUseActiveIngredientModel = WithId<{
name: string
}>

export type InputUseActiveIngredientApiResponse = WithId<{
name: string
}>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { InputUseActiveIngredientDetailsModel } from '../../models/input-use-active-ingredients-model'
import type { RequestInterface } from '@/core/domain/types'

export type CreateInputUseActiveIngredientUseCase = RequestInterface<
{
inputUseActiveIngredient: InputUseActiveIngredientDetailsModel
},
void
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { RequestInterface } from '@/core/domain/types'

export type DeleteInputUseActiveIngredientUseCase = RequestInterface<
{
id: number
},
void
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { InputUseActiveIngredientDetailsModel } from '../../models/input-use-active-ingredients-model'
import type { RequestInterface } from '@/core/domain/types'

export type GetInputUseActiveIngredientUseCase = RequestInterface<
{
id: number
},
InputUseActiveIngredientDetailsModel
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { InputUseActiveIngredientModel } from '../../models/input-use-active-ingredients-model'
import type {
RequestInterface,
ListParams,
ListResponse,
} from '@/core/domain/types'

export type GetInputUseActiveIngredientsUseCase = RequestInterface<
ListParams<InputUseActiveIngredientModel>,
ListResponse<InputUseActiveIngredientModel>
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './create-input-use-active-ingredient-use-case'
export * from './delete-input-use-active-ingredient-use-case'
export * from './get-input-use-active-ingredient-use-case'
export * from './get-input-use-active-ingredients-use-case'
export * from './update-input-use-active-ingredient-use-case'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { InputUseActiveIngredientDetailsModel } from '../../models/input-use-active-ingredients-model'
import type { RequestInterface, WithId } from '@/core/domain/types'

export type UpdateInputUseActiveIngredientUseCase = RequestInterface<
{
inputUseActiveIngredient: WithId<InputUseActiveIngredientDetailsModel>
},
void
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './remote-create-input-use-active-ingredient-use-case-factory'
export * from './remote-delete-input-use-active-ingredient-use-case-factory'
export * from './remote-get-input-use-active-ingredient-use-case-factory'
export * from './remote-get-input-use-active-ingredients-use-case-factory'
export * from './remote-update-input-use-active-ingredient-use-case-factory'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { makeApiHttpClient } from '@/core/main/factories/http'

import { RemoteCreateInputUseActiveIngredientUseCase } from '../../../../data/use-cases/input-use-active-ingredients-use-cases'

import type { CreateInputUseActiveIngredientUseCase } from '../../../../domain/use-cases/input-use-active-ingredients-use-cases'

export function makeRemoteCreateInputUseActiveIngredientUseCase(): CreateInputUseActiveIngredientUseCase {
return new RemoteCreateInputUseActiveIngredientUseCase(
'/input-uses/active-ingredients',
makeApiHttpClient()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { makeApiHttpClient } from '@/core/main/factories/http'

import { RemoteDeleteInputUseActiveIngredientUseCase } from '../../../../data/use-cases/input-use-active-ingredients-use-cases'

import type { DeleteInputUseActiveIngredientUseCase } from '../../../../domain/use-cases/input-use-active-ingredients-use-cases'

export function makeRemoteDeleteInputUseActiveIngredientUseCase(): DeleteInputUseActiveIngredientUseCase {
return new RemoteDeleteInputUseActiveIngredientUseCase(
'/input-uses/active-ingredients',
makeApiHttpClient()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { makeApiHttpClient } from '@/core/main/factories/http'

import { RemoteGetInputUseActiveIngredientUseCase } from '../../../../data/use-cases/input-use-active-ingredients-use-cases'

import type { GetInputUseActiveIngredientUseCase } from '../../../../domain/use-cases/input-use-active-ingredients-use-cases'

export function makeRemoteGetInputUseActiveIngredientUseCase(): GetInputUseActiveIngredientUseCase {
return new RemoteGetInputUseActiveIngredientUseCase(
'/input-uses/active-ingredients',
makeApiHttpClient()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { makeApiHttpClient } from '@/core/main/factories/http'

import { RemoteGetInputUseActiveIngredientsUseCase } from '../../../../data/use-cases/input-use-active-ingredients-use-cases'

import type { GetInputUseActiveIngredientsUseCase } from '../../../../domain/use-cases/input-use-active-ingredients-use-cases'

export function makeRemoteGetInputUseActiveIngredientsUseCase(): GetInputUseActiveIngredientsUseCase {
return new RemoteGetInputUseActiveIngredientsUseCase(
'/input-uses/active-ingredients',
makeApiHttpClient()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { makeApiHttpClient } from '@/core/main/factories/http'

import { RemoteUpdateInputUseActiveIngredientUseCase } from '../../../../data/use-cases/input-use-active-ingredients-use-cases'

import type { UpdateInputUseActiveIngredientUseCase } from '../../../../domain/use-cases/input-use-active-ingredients-use-cases'

export function makeRemoteUpdateInputUseActiveIngredientUseCase(): UpdateInputUseActiveIngredientUseCase {
return new RemoteUpdateInputUseActiveIngredientUseCase(
'/input-uses/active-ingredients',
makeApiHttpClient()
)
}
Loading
Loading