Commit 76e73323 authored by Vitoria Ebeling Hahn's avatar Vitoria Ebeling Hahn
Browse files

Merge branch 'feature/endpoint-editar-produtos' into 'dev'

feat: update and find by id endpoints for products

See merge request !23
Showing with 71 additions and 63 deletions
+71 -63
......@@ -15,46 +15,46 @@ export class ProductsService {
return await this.productRepository.find();
}
// async findOne(id_alimento: number): Promise<ProductEntity> {
// const product = await this.productRepository.findOne({
// where: { id_alimento },
// });
// if (!product) {
// throw new NotFoundException(`Product with ID ${id_alimento} not found`);
// }
// return product;
// }
async findOne(id: number): Promise<ProductEntity> {
const product = await this.productRepository.findOne({
where: { id },
});
if (!product) {
throw new NotFoundException(`Product with ID ${id} not found`);
}
return product;
}
async create(product: CreateProductDto): Promise<ProductEntity> {
const newProduct = this.productRepository.create(product);
return await this.productRepository.save(newProduct);
}
// async update(
// id: number,
// updateDto: CreateProductDto,
// ): Promise<ProductEntity> {
// const entity = await this.productRepository.preload({
// id: id,
// ...updateDto,
// });
// if (!entity) {
// throw new NotFoundException(
// `Product with ID ${id} not found`,
// );
// }
// entity.updatedAt = new Date(Date.now());
// return await this.productRepository.save(entity);
// }
async update(
id: number,
updateDto: CreateProductDto,
): Promise<ProductEntity> {
const entity = await this.productRepository.findOne({ where: { id } });
if (!entity) {
throw new NotFoundException(`Product with ID ${id} not found`);
}
await this.productRepository.update(id, updateDto);
const updatedEntity = await this.productRepository.findOne({
where: { id },
});
return updatedEntity;
}
async delete(id: number): Promise<ProductEntity> {
const product = await this.productRepository.findOne({ where: { id } });
if (!product) {
throw new NotFoundException(
`Product with ID ${id} not found`,
);
throw new NotFoundException(`Product with ID ${id} not found`);
}
product.deletedAt = new Date(Date.now());
return await this.productRepository.save(product);
}
}
\ No newline at end of file
}
......@@ -10,7 +10,8 @@ import {
export class CreateProductDto {
@ApiProperty({
example: '1',
description: 'ID da empresa (temporario; será determinado pela empresa autenticada)',
description:
'ID da empresa (temporario; será determinado pela empresa autenticada)',
required: true,
})
@IsNotEmpty({ message: 'Empresa nao pode ser vazia' })
......
import { Column, Entity, JoinColumn, OneToMany } from 'typeorm';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { BaseEntity } from '../base/entities/base.entity';
import { ContractorCompaniesEntity } from '../contractorCompanies/contractorCompanies.entity';
@Entity('alimentos')
export class ProductEntity extends BaseEntity {
@OneToMany(() => ProductEntity, (product) => product.company_id)
@ManyToOne(() => ContractorCompaniesEntity)
@JoinColumn()
company_id: number;
......
......@@ -8,6 +8,7 @@ import {
Param,
Post,
UseGuards,
Put,
} from '@nestjs/common';
import {
ApiBearerAuth,
......@@ -44,26 +45,29 @@ export class ProductsController {
}
}
//@ApiOperation({ summary: 'Atualiza uma produto' })
//@ApiParam({
// name: 'id',
// type: Number,
// required: true,
// description: 'ID do Produto',
//})
//@Put(':id')
//async update(@Param('id') id: number, @Body() product: CreateProductDto) {
// try {
// const updatedProduct = await this.productService.update(+id, product);
// return {
// message: 'Produto atualizado com sucesso!',
// product: updatedProduct,
// statusCode: HttpStatus.OK,
// };
// } catch (error) {
// throw new HttpException({ message: error.message }, error.status);
// }
//}
@ApiOperation({ summary: 'Atualiza uma produto' })
@ApiResponse({ status: 200, description: 'Produto editado com sucesso' })
@ApiBearerAuth()
@UseGuards(AuthGuard)
@ApiParam({
name: 'id',
type: Number,
required: true,
description: 'ID do Produto',
})
@Put(':id')
async update(@Param('id') id: number, @Body() product: CreateProductDto) {
try {
const updatedProduct = await this.productService.update(+id, product);
return {
message: 'Produto atualizado com sucesso!',
product: updatedProduct,
statusCode: HttpStatus.OK,
};
} catch (error) {
throw new HttpException({ message: error.message }, error.status);
}
}
@ApiOperation({ summary: 'Procura todos os produtos' })
@ApiBearerAuth()
......@@ -73,17 +77,19 @@ export class ProductsController {
return this.productService.findAll();
}
//@ApiOperation({ summary: 'Procura um produto por id' })
//@ApiParam({
// name: 'id',
// type: Number,
// required: true,
// description: 'ID do Produto',
//})
//@Get(':id')
//async findOne(@Param('id') id: number) {
// return this.productService.findOne(id);
//}
@ApiOperation({ summary: 'Procura um produto por id' })
@ApiParam({
name: 'id',
type: Number,
required: true,
description: 'ID do Produto',
})
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Get(':id')
async findOne(@Param('id') id: number) {
return this.productService.findOne(id);
}
@ApiOperation({ summary: 'Marca um produto como deletado' })
@ApiResponse({
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment