generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Drink resource #13
Merged
Merged
Drink resource #13
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post } from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
import { DrinksService } from './drinks.service'; | ||
import { CreateDrinkDto } from './dto/create-drink.dto'; | ||
import { UpdateDrinkDto } from './dto/update-drink.dto'; | ||
import { Drink } from './entities/drink.entity'; | ||
|
||
@ApiTags('Drinks') | ||
@Controller('drinks') | ||
export class DrinksController { | ||
constructor(private readonly drinksService: DrinksService) {} | ||
|
||
@Post() | ||
create(@Body() createDrinkDto: CreateDrinkDto): Promise<Drink> { | ||
return this.drinksService.create(createDrinkDto); | ||
} | ||
|
||
@Get() | ||
findAll(): Promise<Drink[]> { | ||
return this.drinksService.findAll(); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id', ParseUUIDPipe) id: string): Promise<Drink> { | ||
return this.drinksService.findOne(id); | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id', ParseUUIDPipe) id: string, @Body() updateDrinkDto: UpdateDrinkDto): Promise<Drink> { | ||
return this.drinksService.update(id, updateDrinkDto); | ||
} | ||
|
||
@Delete(':id') | ||
remove(@Param('id', ParseUUIDPipe) id: string): Promise<Drink> { | ||
return this.drinksService.remove(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { DrinksController } from './drinks.controller'; | ||
import { DrinksService } from './drinks.service'; | ||
|
||
@Module({ | ||
controllers: [DrinksController], | ||
providers: [DrinksService], | ||
}) | ||
export class DrinksModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { PrismaService } from 'nestjs-prisma'; | ||
|
||
import { CreateDrinkDto } from './dto/create-drink.dto'; | ||
import { UpdateDrinkDto } from './dto/update-drink.dto'; | ||
import { Drink } from './entities/drink.entity'; | ||
|
||
@Injectable() | ||
export class DrinksService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
create(createDrinkDto: CreateDrinkDto): Promise<Drink> { | ||
return this.prisma.drink.create({ data: createDrinkDto }); | ||
} | ||
|
||
findAll(): Promise<Drink[]> { | ||
return this.prisma.drink.findMany(); | ||
} | ||
|
||
async findOne(id: string): Promise<Drink> { | ||
const drink = await this.prisma.drink.findUnique({ where: { id } }); | ||
if (!drink) { | ||
throw new NotFoundException('Drink not found'); | ||
} | ||
return drink; | ||
} | ||
|
||
async update(id: string, updateDrinkDto: UpdateDrinkDto): Promise<Drink> { | ||
try { | ||
return this.prisma.drink.update({ where: { id }, data: updateDrinkDto }); | ||
} catch { | ||
throw new NotFoundException('Drink not found'); | ||
} | ||
} | ||
|
||
async remove(id: string): Promise<Drink> { | ||
try { | ||
return this.prisma.drink.delete({ where: { id } }); | ||
} catch { | ||
throw new NotFoundException('Drink not found'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { OmitType } from '@nestjs/swagger'; | ||
|
||
import { Drink } from '../entities/drink.entity'; | ||
|
||
export class CreateDrinkDto extends OmitType(Drink, ['id', 'createdAt']) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PartialType } from '@nestjs/swagger'; | ||
|
||
import { CreateDrinkDto } from './create-drink.dto'; | ||
|
||
export class UpdateDrinkDto extends PartialType(CreateDrinkDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DrinkType } from '@prisma/client'; | ||
import { IsBoolean, IsDateString, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID, Min } from 'class-validator'; | ||
import { IsFloat } from 'src/util/is-float.validator'; | ||
|
||
export class Drink { | ||
/** | ||
* @example aaaaaaaa-bbbb-cccc-dddd-eeee-ff0123456789 | ||
*/ | ||
@IsUUID() | ||
id: string; | ||
|
||
/** | ||
* Name of the drink | ||
* @example 'Soproni meggy' | ||
*/ | ||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
/** | ||
* Type of the drink | ||
* @example BEER | ||
*/ | ||
@IsEnum(DrinkType) | ||
type: DrinkType; | ||
|
||
/** | ||
* Alcohol content of the drink | ||
* @example 4.5 | ||
*/ | ||
@IsFloat() | ||
@Min(0) | ||
alcoholContent: number; | ||
|
||
/** | ||
* Whether it was created by a user | ||
*/ | ||
@IsBoolean() | ||
custom: boolean; | ||
|
||
/** | ||
* @example "A beer that doesn't really taste like beer." | ||
*/ | ||
@IsString() | ||
@IsOptional() | ||
description?: string; | ||
|
||
@IsDateString() | ||
createdAt: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator'; | ||
|
||
export function IsFloat(validationOptions?: ValidationOptions) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isFloat', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate(value: any) { | ||
return typeof value === 'number' && !isNaN(value); | ||
}, | ||
defaultMessage(args: ValidationArguments) { | ||
return `${args.property} must be a float number`; | ||
}, | ||
}, | ||
}); | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could add examples for the optional fields too.