Commit ec78d748 authored by Gabriel Fernando Giaretta's avatar Gabriel Fernando Giaretta
Browse files

adicionadas timestamps e boolean deleted detalhados no banco

parent a2bb2df9
Showing with 43 additions and 13 deletions
+43 -13
......@@ -27,6 +27,9 @@ export class ProductsService {
async create(product: CreateProductDto): Promise<ProductEntity> {
const newProduct = this.productRepository.create(product);
newProduct.createdAt = new Date(Date.now());
newProduct.updatedAt = new Date(Date.now());
newProduct.deleted = false;
return await this.productRepository.save(newProduct);
}
......@@ -43,13 +46,19 @@ export class ProductsService {
`Product with ID ${id} not found`,
);
}
entity.updatedAt = new Date(Date.now());
return await this.productRepository.save(entity);
}
async delete(id: number) {
const result = await this.productRepository.delete(id);
if (result.affected === 0) {
throw new NotFoundException(`Product with ID ${id} not found`);
async delete(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`,
);
}
product.deletedAt = new Date(Date.now());
product.deleted = true;
return await this.productRepository.save(product);
}
}
}
\ No newline at end of file
......@@ -10,8 +10,8 @@ import {
export class CreateProductDto {
@ApiProperty({
example: '12345678901234',
description: 'ID da empresa',
example: '1',
description: 'ID da empresa (temporario; será determinado pela empresa autenticada)',
required: true,
})
@IsNotEmpty({ message: 'Empresa nao pode ser vazia' })
......
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn, Timestamp } from 'typeorm';
@Entity('alimentos')
export class ProductEntity extends BaseEntity {
......@@ -36,4 +36,16 @@ export class ProductEntity extends BaseEntity {
@Column({ length: 13, nullable: true })
cod_barras: string;
@Column('timestamp')
createdAt: Date;
@Column('timestamp')
updatedAt: Date;
@Column('timestamp', {nullable: true})
deletedAt: Date;
@Column({type: 'bool', nullable: true})
deleted: boolean;
}
......@@ -37,6 +37,12 @@ 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 {
......@@ -58,16 +64,19 @@ export class ProductsController {
}
@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: 'Deleta um produto' })
@ApiResponse({
status: 200,
description: 'Produto deletado com sucesso.',
})
@ApiOperation({ summary: 'Marca um produto como deletado' })
@ApiResponse({ status: 200, description: 'Produto marcado como deletado com sucesso' })
@ApiParam({
name: 'id',
type: Number,
......
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