src/instruments/instruments.controller.ts
instruments
Methods |
| Async create | ||||||
create(createInstrumentDto: CreateInstrumentDto)
|
||||||
Decorators :
@UseGuards(PoliciesGuard)
|
||||||
|
Defined in src/instruments/instruments.controller.ts:52
|
||||||
|
Parameters :
Returns :
Promise<Instrument>
|
| Async findAll | ||||||
findAll(filter?: string)
|
||||||
Decorators :
@UseGuards(PoliciesGuard)
|
||||||
|
Defined in src/instruments/instruments.controller.ts:78
|
||||||
|
Parameters :
Returns :
Promise<Instrument[]>
|
| Async findById | ||||||
findById(pid: string)
|
||||||
Decorators :
@UseGuards(PoliciesGuard)
|
||||||
|
Parameters :
Returns :
Promise<Instrument | null>
|
| Async findOne | ||||||
findOne(filter?: string)
|
||||||
Decorators :
@UseGuards(PoliciesGuard)
|
||||||
|
Parameters :
Returns :
Promise<Instrument | null>
|
| Async remove | ||||||
remove(id: string)
|
||||||
Decorators :
@UseGuards(PoliciesGuard)
|
||||||
|
Parameters :
Returns :
Promise<>
|
| Async update | |||||||||
update(id: string, updateInstrumentDto: UpdateInstrumentDto)
|
|||||||||
Decorators :
@UseGuards(PoliciesGuard)
|
|||||||||
|
Parameters :
Returns :
Promise<Instrument | null>
|
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UseGuards,
Query,
UseInterceptors,
HttpException,
HttpStatus,
} from "@nestjs/common";
import { InstrumentsService } from "./instruments.service";
import { CreateInstrumentDto } from "./dto/create-instrument.dto";
import { UpdateInstrumentDto } from "./dto/update-instrument.dto";
import {
ApiBearerAuth,
ApiOperation,
ApiQuery,
ApiResponse,
ApiTags,
} from "@nestjs/swagger";
import { PoliciesGuard } from "src/casl/guards/policies.guard";
import { CheckPolicies } from "src/casl/decorators/check-policies.decorator";
import { AppAbility } from "src/casl/casl-ability.factory";
import { Action } from "src/casl/action.enum";
import { Instrument, InstrumentDocument } from "./schemas/instrument.schema";
import { FormatPhysicalQuantitiesInterceptor } from "src/common/interceptors/format-physical-quantities.interceptor";
import { IFilters } from "src/common/interfaces/common.interface";
import {
filterDescription,
filterExample,
replaceLikeOperator,
} from "src/common/utils";
@ApiBearerAuth()
@ApiTags("instruments")
@Controller("instruments")
export class InstrumentsController {
constructor(private readonly instrumentsService: InstrumentsService) {}
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentCreate, Instrument),
)
@UseInterceptors(
new FormatPhysicalQuantitiesInterceptor<Instrument>("customMetadata"),
)
@Post()
async create(
@Body() createInstrumentDto: CreateInstrumentDto,
): Promise<Instrument> {
try {
const instrument = await this.instrumentsService.create(
createInstrumentDto,
);
return instrument;
} catch (e) {
throw new HttpException(
"Instrument with the same unique name already exists",
HttpStatus.BAD_REQUEST,
);
}
}
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentRead, Instrument),
)
@Get()
@ApiQuery({
name: "filter",
description: "Database filters to apply when retrieve all instruments",
required: false,
})
async findAll(@Query("filter") filter?: string): Promise<Instrument[]> {
const instrumentFilter: IFilters<InstrumentDocument> = replaceLikeOperator(
JSON.parse(filter ?? "{}"),
);
return this.instrumentsService.findAll(instrumentFilter);
}
// GET /instrument/findOne
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentRead, Instrument),
)
@Get("/findOne")
@ApiOperation({
summary: "It returns the first instrument found.",
description:
"It returns the first instrument of the ones that matches the filter provided. The list returned can be modified by providing a filter.",
})
@ApiQuery({
name: "filter",
description:
"Database filters to apply when retrieving instruments\n" +
filterDescription,
required: false,
type: String,
example: filterExample,
})
@ApiResponse({
status: 200,
type: Instrument,
description: "Return the instrument requested",
})
async findOne(@Query("filter") filter?: string): Promise<Instrument | null> {
const instrumentFilters = replaceLikeOperator(JSON.parse(filter ?? "{}"));
return this.instrumentsService.findOne(instrumentFilters);
}
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentRead, Instrument),
)
@Get(":id")
async findById(@Param("id") pid: string): Promise<Instrument | null> {
return this.instrumentsService.findOne({ where: { _id: pid } });
}
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentUpdate, Instrument),
)
@UseInterceptors(
new FormatPhysicalQuantitiesInterceptor<Instrument>("customMetadata"),
)
@Patch(":id")
async update(
@Param("id") id: string,
@Body() updateInstrumentDto: UpdateInstrumentDto,
): Promise<Instrument | null> {
try {
const instrument = await this.instrumentsService.update(
{ _id: id },
updateInstrumentDto,
);
return instrument;
} catch (e) {
throw new HttpException(
"Instrument with the same unique name already exists",
HttpStatus.BAD_REQUEST,
);
}
}
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.InstrumentDelete, Instrument),
)
@Delete(":id")
async remove(@Param("id") id: string): Promise<unknown> {
return this.instrumentsService.remove({ pid: id });
}
}