Skip to content

Commit

Permalink
feat: add dish api
Browse files Browse the repository at this point in the history
  • Loading branch information
dribble-njr committed Dec 31, 2024
1 parent f55e4e2 commit 536a3fd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
24 changes: 17 additions & 7 deletions apps/server/src/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ export class CategoryController {

@Get('trash')
findTrashCategories() {
return this.categoryService.findAll(false);
return this.categoryService.findAll(true);
}

@Get(':id/dishes')
findDishesByCategory(@Param('id') id: string) {
return this.categoryService.findDishesByCategory(+id);
}

@Get(':id/dishes/trash')
findTrashDishesByCategory(@Param('id') id: string) {
return this.categoryService.findDishesByCategory(+id, true);
}

@Get(':id')
Expand All @@ -33,16 +43,16 @@ export class CategoryController {
}

@Delete(':id')
moveToTrash(@Param('id') id: string) {
return this.categoryService.moveToTrash(+id);
remove(@Param('id') id: string) {
return this.categoryService.remove(+id);
}

@Delete('trash/:id')
permanentDelete(@Param('id') id: string) {
return this.categoryService.moveToTrash(+id, true);
@Delete(':id/trash')
permanentRemove(@Param('id') id: string) {
return this.categoryService.remove(+id, true);
}

@Patch('trash/:id/restore')
@Patch(':id/trash/restore')
restoreFromTrash(@Param('id') id: string) {
return this.categoryService.restoreFromTrash(+id);
}
Expand Down
26 changes: 16 additions & 10 deletions apps/server/src/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,51 @@ export class CategoryService {
throw new ConflictException(`Category '${name}' already exists in this kitchen`);
}

return this.prisma.category.create({
return await this.prisma.category.create({
data: createCategoryDto
});
}

findAll(includeTrash: boolean = false) {
return this.prisma.category.findMany({
async findAll(includeTrash: boolean = false) {
return await this.prisma.category.findMany({
where: { isActive: !includeTrash }
});
}

findOne(id: number) {
return this.prisma.category.findUnique({
async findOne(id: number) {
return await this.prisma.category.findUnique({
where: { id }
});
}

async findDishesByCategory(id: number, includeTrash: boolean = false) {
return await this.prisma.dish.findMany({
where: { categoryId: id, isActive: !includeTrash }
});
}

async update(id: number, updateCategoryDto: UpdateCategoryDto) {
return this.prisma.category.update({
return await this.prisma.category.update({
where: { id },
data: updateCategoryDto
});
}

async moveToTrash(id: number, permanent: boolean = false) {
async remove(id: number, permanent: boolean = false) {
if (!permanent) {
return this.prisma.category.update({
return await this.prisma.category.update({
where: { id },
data: { isActive: false }
});
}

return this.prisma.category.delete({
return await this.prisma.category.delete({
where: { id }
});
}

async restoreFromTrash(id: number) {
return this.prisma.category.update({
return await this.prisma.category.update({
where: { id },
data: { isActive: true }
});
Expand Down
15 changes: 15 additions & 0 deletions apps/server/src/dish/dish.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class DishController {
return this.dishService.findAll();
}

@Get('trash')
findTrashDishes() {
return this.dishService.findAll(true);
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.dishService.findOne(+id);
Expand All @@ -31,4 +36,14 @@ export class DishController {
remove(@Param('id') id: string) {
return this.dishService.remove(+id);
}

@Delete(':id/trash')
permanentRemove(@Param('id') id: string) {
return this.dishService.remove(+id, true);
}

@Patch(':id/trash/restore')
restoreFromTrash(@Param('id') id: string) {
return this.dishService.restoreFromTrash(+id);
}
}
50 changes: 38 additions & 12 deletions apps/server/src/dish/dish.service.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
import { Injectable } from '@nestjs/common';
import { CreateDishDto } from './dto/create-dish.dto';
import { UpdateDishDto } from './dto/update-dish.dto';
import { PrismaService } from 'src/shared/prisma.service';

@Injectable()
export class DishService {
create(createDishDto: CreateDishDto) {
console.log(createDishDto);
return 'This action adds a new dish';
constructor(private readonly prisma: PrismaService) {}

async create(createDishDto: CreateDishDto) {
return await this.prisma.dish.create({
data: createDishDto
});
}

async findAll(includeTrash: boolean = false) {
return await this.prisma.dish.findMany({
where: { isActive: !includeTrash }
});
}

findAll() {
return `This action returns all dish`;
async findOne(id: number) {
return await this.prisma.dish.findUnique({
where: { id }
});
}

findOne(id: number) {
return `This action returns a #${id} dish`;
async update(id: number, updateDishDto: UpdateDishDto) {
return await this.prisma.dish.update({
where: { id },
data: updateDishDto
});
}

update(id: number, updateDishDto: UpdateDishDto) {
console.log(updateDishDto);
return `This action updates a #${id} dish`;
async remove(id: number, permanent: boolean = false) {
if (!permanent) {
return await this.prisma.dish.update({
where: { id },
data: { isActive: false }
});
}

return await this.prisma.dish.delete({
where: { id }
});
}

remove(id: number) {
return `This action removes a #${id} dish`;
async restoreFromTrash(id: number) {
return await this.prisma.dish.update({
where: { id },
data: { isActive: true }
});
}
}

0 comments on commit 536a3fd

Please sign in to comment.