Spaces:
Sleeping
Sleeping
Merge pull request #13 from PBL6-team-CATS/feat/backend/branch-and-menu
Browse files
backend/src/modules/branch/branch.controller.ts
CHANGED
@@ -8,34 +8,46 @@ import {
|
|
8 |
Delete,
|
9 |
} from '@nestjs/common';
|
10 |
import { BranchService } from './branch.service.js';
|
11 |
-
import {
|
|
|
12 |
|
13 |
@Controller('branch')
|
14 |
export class BranchController {
|
15 |
constructor(private readonly branchService: BranchService) {}
|
16 |
|
17 |
@Post()
|
18 |
-
create(@Body() createBranchDto:
|
19 |
return this.branchService.create(createBranchDto);
|
20 |
}
|
21 |
|
22 |
@Get()
|
23 |
-
findAll() {
|
24 |
return this.branchService.findAll();
|
25 |
}
|
26 |
|
27 |
@Get(':id')
|
28 |
-
findOne(@Param('id') id: string) {
|
29 |
-
return this.branchService.findOne(
|
30 |
}
|
31 |
|
32 |
@Patch(':id')
|
33 |
-
update(
|
34 |
-
|
|
|
|
|
|
|
35 |
}
|
36 |
|
37 |
@Delete(':id')
|
38 |
-
remove(@Param('id') id: string) {
|
39 |
-
return this.branchService.remove(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
}
|
|
|
8 |
Delete,
|
9 |
} from '@nestjs/common';
|
10 |
import { BranchService } from './branch.service.js';
|
11 |
+
import { CreateBranchDto } from './dto/create-branch.dto.js';
|
12 |
+
import { UpdateBranchDto } from './dto/update-branch.dto.js';
|
13 |
|
14 |
@Controller('branch')
|
15 |
export class BranchController {
|
16 |
constructor(private readonly branchService: BranchService) {}
|
17 |
|
18 |
@Post()
|
19 |
+
create(@Body() createBranchDto: CreateBranchDto) {
|
20 |
return this.branchService.create(createBranchDto);
|
21 |
}
|
22 |
|
23 |
@Get()
|
24 |
+
async findAll() {
|
25 |
return this.branchService.findAll();
|
26 |
}
|
27 |
|
28 |
@Get(':id')
|
29 |
+
async findOne(@Param('id') id: string) {
|
30 |
+
return this.branchService.findOne(id);
|
31 |
}
|
32 |
|
33 |
@Patch(':id')
|
34 |
+
async update(
|
35 |
+
@Param('id') id: string,
|
36 |
+
@Body() updateBranchDto: UpdateBranchDto,
|
37 |
+
) {
|
38 |
+
return this.branchService.update(id, updateBranchDto);
|
39 |
}
|
40 |
|
41 |
@Delete(':id')
|
42 |
+
async remove(@Param('id') id: string) {
|
43 |
+
return this.branchService.remove(id);
|
44 |
+
}
|
45 |
+
|
46 |
+
@Post(':id/menu-items')
|
47 |
+
async addMenuItemToBranch(@Param('id') id: string) {}
|
48 |
+
|
49 |
+
@Get(':id/menu-items')
|
50 |
+
async getMenuItemWithBranchId(@Param('id') id: string) {
|
51 |
+
|
52 |
}
|
53 |
}
|
backend/src/modules/branch/branch.service.ts
CHANGED
@@ -1,25 +1,44 @@
|
|
1 |
-
import { Injectable } from '@nestjs/common';
|
2 |
-
import {
|
|
|
|
|
|
|
|
|
3 |
|
|
|
4 |
@Injectable()
|
5 |
export class BranchService {
|
6 |
-
create(createBranchDto:
|
7 |
-
return
|
8 |
}
|
9 |
|
10 |
-
findAll() {
|
11 |
-
return
|
12 |
}
|
13 |
|
14 |
-
findOne(id:
|
15 |
-
return
|
16 |
}
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
}
|
|
|
1 |
+
import { Injectable, NotFoundException } from '@nestjs/common';
|
2 |
+
import { CreateBranchDto } from './dto/create-branch.dto.js';
|
3 |
+
import { BranchEntity } from '../../entities/branch.entity.js';
|
4 |
+
import { Public } from '../authentication/authentication.decorator.js';
|
5 |
+
import { UpdateBranchDto } from './dto/update-branch.dto.js';
|
6 |
+
import { plainToClass } from 'class-transformer';
|
7 |
|
8 |
+
@Public()
|
9 |
@Injectable()
|
10 |
export class BranchService {
|
11 |
+
async create(createBranchDto: CreateBranchDto) {
|
12 |
+
return await BranchEntity.create({ ...createBranchDto }).save();
|
13 |
}
|
14 |
|
15 |
+
async findAll() {
|
16 |
+
return await BranchEntity.find();
|
17 |
}
|
18 |
|
19 |
+
async findOne(id: string) {
|
20 |
+
return await BranchEntity.findOneBy({ id: id });
|
21 |
}
|
22 |
|
23 |
+
async getBranchOrError(id: string) {
|
24 |
+
const branch = await BranchEntity.findOneBy({ id });
|
25 |
+
if (!branch) {
|
26 |
+
throw new NotFoundException('Menu item not found');
|
27 |
+
}
|
28 |
+
return branch;
|
29 |
}
|
30 |
|
31 |
+
async update(id: string, updateBranchDto: UpdateBranchDto) {
|
32 |
+
let branch = await this.getBranchOrError(id);
|
33 |
+
branch = plainToClass(BranchEntity, {
|
34 |
+
...branch,
|
35 |
+
...updateBranchDto,
|
36 |
+
});
|
37 |
+
return await branch.save();
|
38 |
+
}
|
39 |
+
|
40 |
+
async remove(id: string) {
|
41 |
+
let menuItem = await this.getBranchOrError(id);
|
42 |
+
return await menuItem.remove();
|
43 |
}
|
44 |
}
|
backend/src/modules/branch/dto/create-branch.dto.ts
CHANGED
@@ -1,7 +1,12 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
3 |
name: string;
|
|
|
|
|
4 |
location: string;
|
|
|
|
|
5 |
phone_number: string;
|
6 |
-
owner_id: string;
|
7 |
}
|
|
|
1 |
+
import { IsString } from 'class-validator';
|
2 |
+
|
3 |
+
export class CreateBranchDto {
|
4 |
+
@IsString()
|
5 |
name: string;
|
6 |
+
|
7 |
+
@IsString()
|
8 |
location: string;
|
9 |
+
|
10 |
+
@IsString()
|
11 |
phone_number: string;
|
|
|
12 |
}
|
backend/src/modules/branch/dto/update-branch.dto.ts
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { IsOptional, IsString } from 'class-validator';
|
2 |
+
|
3 |
+
export class UpdateBranchDto {
|
4 |
+
@IsString()
|
5 |
+
@IsOptional()
|
6 |
+
name?: string;
|
7 |
+
|
8 |
+
@IsString()
|
9 |
+
@IsOptional()
|
10 |
+
location?: string;
|
11 |
+
|
12 |
+
@IsString()
|
13 |
+
@IsOptional()
|
14 |
+
phone_number?: string;
|
15 |
+
|
16 |
+
@IsString()
|
17 |
+
@IsOptional()
|
18 |
+
owner_id?: string;
|
19 |
+
}
|