Skip to content
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node

WORKDIR /usr/src/app
WORKDIR /src/app

COPY package*.json ./

Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ConfigModule } from '@nestjs/config';

import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { ProductsModule } from './products/products.module';

@Module({
imports: [
Expand All @@ -15,6 +16,7 @@ import { UsersModule } from './users/users.module';
),
AuthModule,
UsersModule,
ProductsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
25 changes: 25 additions & 0 deletions src/products/controllers/products.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, Get, Post, Request, ValidationPipe, Body } from "@nestjs/common";

import { ProductsService } from "../products.service";
import { ProductsDTO } from "../dto/products.dto";

@Controller('products')
export class ProductsController {
constructor(
private productsService: ProductsService
) {}

@Get('all')
getUser(@Request() req){
return this.productsService.extractAllProductData();
}

@Post('register')
async register(
@Body(ValidationPipe) productsDTO: ProductsDTO
): Promise<any> {
const message = await this.productsService.register(productsDTO);

return { message };
}
}
23 changes: 23 additions & 0 deletions src/products/dto/products.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
IsString,
IsNotEmpty,
IsNumberString
} from 'class-validator';

export class ProductsDTO{
@IsNotEmpty()
@IsString()
idKey: string;

@IsNotEmpty()
@IsString()
name: string;

@IsNotEmpty()
@IsString()
brand: string;

@IsNotEmpty()
@IsNumberString()
fenilalanina: number;
}
8 changes: 8 additions & 0 deletions src/products/interfaces/products.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Document } from 'mongoose';

export interface Products extends Document {
readonly idKey: string;
readonly name: string;
readonly brand: string;
readonly fenilalanina: number;
}
15 changes: 15 additions & 0 deletions src/products/products.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { ProductsController } from './controllers/products.controller';
import { ProductsSchema } from './schemas/products.schema';
import { ProductsService } from './products.service';

@Module({
providers: [ProductsService],
controllers: [ProductsController],
imports: [
MongooseModule.forFeature([{ name: 'Products', schema: ProductsSchema }]),
],
})
export class ProductsModule {}
18 changes: 18 additions & 0 deletions src/products/products.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsService } from './products.service';

describe('ProductsService', () => {
let service: ProductsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ProductsService],
}).compile();

service = module.get<ProductsService>(ProductsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
45 changes: 45 additions & 0 deletions src/products/products.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ConflictException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { ProductsDTO } from './dto/products.dto';
import { Products } from './interfaces/products.interface';

@Injectable()
export class ProductsService {
constructor(
@InjectModel('Products') private productsModel: Model<Products>
) {}

/**
* Creates new product with given user data.
* @param productsDTO - validated product's data
* @returns - product's object in case it's created successfully.
*/
async register(productsDTO: ProductsDTO): Promise<Products> {
const { idKey, name, brand, fenilalanina } = productsDTO; // extracts product's data to be saved

const product = new this.productsModel({ "idKey": idKey, "name": name, "brand": brand, "fenilalanina": fenilalanina });

try {
await product.save(); // saves product to database

return product;
} catch (error) {
if(error.code === 11000){ // duplicate key error
throw new ConflictException('Product already exists');
}
throw error;
}
}

/**
* Extracts product's data from MongoDB Document
* @param product - product's object
* @returns relevant product's data.
*/
async extractAllProductData(){
var product = await this.productsModel.find().exec();
return product;
}
}
15 changes: 15 additions & 0 deletions src/products/schemas/products.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as mongoose from 'mongoose';

/**
* Schema of product's data to be saved in database
* @example { 'idKey': 'Rúculaxx', 'name': 'Rúcula', 'brand': 'xx', 'fenilalanina': '97' }
*/
export const ProductsSchema = new mongoose.Schema({
idKey: {
type: String,
unique: true,
},
name: String,
brand: String,
fenilalanina: Number
});