Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature brands relation #144

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { OffersModule } from './offers/offers.module';
password: process.env.DB_PASS,
database: process.env.DB_NAME,
autoLoadEntities: true,
// synchronize: true,
//synchronize: true,
}),

AccountStatusModule,
Expand Down Expand Up @@ -76,4 +76,5 @@ import { OffersModule } from './offers/offers.module';
controllers: [AppController],
providers: [AppService],
})

export class AppModule {}
14 changes: 7 additions & 7 deletions src/brands/controllers/brands.controllers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('brandsController', () => {
const mockBrandService = {
create: jest.fn((dto) => {
return {
id: expect.any(Number),
id: Date.now(),
...dto,
};
}),
Expand Down Expand Up @@ -38,19 +38,19 @@ describe('brandsController', () => {
});

it('should create a brand', () => {
expect(controller.create({ name: 'Mario', photoId: 'ph id' })).toEqual({
expect(controller.create({ name: 'Mario', photoId: 123 })).toEqual({
id: expect.any(Number),
name: 'Mario',
photoId: 'ph id',
photoId: 123,
});
});

it('should update a brand', () => {
const dto: createBrandDTO = { name: 'Mario', photoId: 'ph id' };
expect(controller.update(1, dto)).toEqual({
id: expect.any(Number),
const brand = {name: 'Mario', photoId:123 };
expect(controller.update(1, brand)).toEqual({
id: 1,
name: 'Mario',
photoId: 'ph id',
photoId: 123,
});
});
});
3 changes: 3 additions & 0 deletions src/brands/controllers/brands.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BrandsService } from '../services/brands.serveces';
import { createBrandDTO } from '../entities/create-brands-dto.entity';
import {
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiOperation,
ApiResponse,
ApiTags,
Expand Down Expand Up @@ -51,6 +52,7 @@ export class BrandsController {
description: 'Shows the search result for a brand by id',
type: Brand,
})
@ApiNotFoundResponse({status: 404, description: 'No Brand was found that matches that id'})
@Get(':id')
getOne(@Param('id') id: number) {
return this.brandService.findOne(id);
Expand Down Expand Up @@ -90,6 +92,7 @@ export class BrandsController {
@ApiBadRequestResponse({
description: 'The brand could not be deleted',
})
@ApiNotFoundResponse({status: 404, description: 'No Brand was found that matches that id'})
@Delete()
delete(@Param('id') id: number) {
return this.brandService.delete(id);
Expand Down
11 changes: 7 additions & 4 deletions src/brands/entities/brands.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn } from 'typeorm';
import { PhotosEntity } from '../../photos/models/photos.entity';

@Entity({ name: 'brands' })
export class Brand {
Expand All @@ -19,9 +20,11 @@ export class Brand {

@Column()
@ApiProperty({
example: '',
example: 1234,
description: 'The logo of the brand',
type: String,
type: Number,
})
photoId: string;
@OneToOne(() => PhotosEntity)
@JoinColumn({ name: 'photo_id' })
photoId: PhotosEntity;
}
11 changes: 6 additions & 5 deletions src/brands/entities/create-brands-dto.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { IsString, IsInt } from 'class-validator';

export class createBrandDTO {

@IsString()
@ApiProperty({
example: '',
Expand All @@ -10,11 +11,11 @@ export class createBrandDTO {
})
name: string;

@IsString()
@IsInt()
@ApiProperty({
example: '',
example: 123,
description: 'The logo of the brand',
type: String,
type: Number,
})
photoId: string;
photoId: number;
}
12 changes: 0 additions & 12 deletions src/brands/mock/brands.ts

This file was deleted.

10 changes: 3 additions & 7 deletions src/brands/services/brands.serveces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ export class BrandsService {
private brandRepo: Repository<Brand>,
) {}

findOne(id: number) {
findOne(id: number): Promise<Brand> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

falta tirar un 404 cuando no encuentra el brand

return this.brandRepo.findOne(id);
}

findAll() {
return this.brandRepo.find();
}

create(body: any) {
create(body: any): Promise<Brand[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiene que recibir el dto, no un any

const newBrand = this.brandRepo.create(body);
return this.brandRepo.save(newBrand);
}

async update(id: number, body: any) {
async update(id: number, body: any): Promise<Brand> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

falta tirar un 404 cuando no encuenta el id
lo mismo para delete

ademas aca tiene que recibir el dto, no un any

const brand = await this.brandRepo.findOne(id);
this.brandRepo.merge(brand, body);
return this.brandRepo.save(brand);
Expand Down