forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.controller.ts
More file actions
129 lines (119 loc) · 3.73 KB
/
request.controller.ts
File metadata and controls
129 lines (119 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
Controller,
Get,
Param,
ParseIntPipe,
Post,
Body,
ValidationPipe,
Patch,
Delete,
} from '@nestjs/common';
import { ApiBody } from '@nestjs/swagger';
import { RequestsService } from './request.service';
import { FoodRequest } from './request.entity';
import { Roles } from '../auth/roles.decorator';
import { Role } from '../users/types';
import { RequestSize } from './types';
import { OrderDetailsDto } from '../orders/dtos/order-details.dto';
import { CreateRequestDto } from './dtos/create-request.dto';
import { FoodType } from '../donationItems/types';
import {
MatchingItemsDto,
MatchingManufacturersDto,
} from './dtos/matching.dto';
import { UpdateRequestDto } from './dtos/update-request.dto';
@Controller('requests')
export class RequestsController {
constructor(private requestsService: RequestsService) {}
@Roles(Role.ADMIN)
@Get()
async getAllFoodRequests(): Promise<FoodRequest[]> {
return this.requestsService.getAll();
}
@Roles(Role.PANTRY, Role.ADMIN, Role.VOLUNTEER)
@Get('/:requestId')
async getRequest(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<FoodRequest> {
return this.requestsService.findOne(requestId);
}
@Roles(Role.PANTRY, Role.ADMIN)
@Get('/:pantryId/all')
async getAllPantryRequests(
@Param('pantryId', ParseIntPipe) pantryId: number,
): Promise<FoodRequest[]> {
return this.requestsService.find(pantryId);
}
@Roles(Role.VOLUNTEER, Role.PANTRY, Role.ADMIN)
@Get('/:requestId/order-details')
async getAllOrderDetailsFromRequest(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<OrderDetailsDto[]> {
return this.requestsService.getOrderDetails(requestId);
}
@Roles(Role.VOLUNTEER)
@Get('/:requestId/matching-manufacturers')
async getMatchingManufacturers(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<MatchingManufacturersDto> {
return this.requestsService.getMatchingManufacturers(requestId);
}
@Roles(Role.VOLUNTEER)
@Get('/:requestId/matching-manufacturers/:manufacturerId/available-items')
async getAvailableItemsForManufacturer(
@Param('requestId', ParseIntPipe) requestId: number,
@Param('manufacturerId', ParseIntPipe) manufacturerId: number,
): Promise<MatchingItemsDto> {
return this.requestsService.getAvailableItems(requestId, manufacturerId);
}
@Post('/create')
@ApiBody({
description: 'Details for creating a food request',
schema: {
type: 'object',
properties: {
pantryId: { type: 'integer', example: 1 },
requestedSize: {
type: 'string',
enum: Object.values(RequestSize),
example: RequestSize.LARGE,
},
requestedFoodTypes: {
type: 'array',
items: { type: 'string', enum: Object.values(FoodType) },
example: [FoodType.DAIRY_FREE_ALTERNATIVES, FoodType.DRIED_BEANS],
},
additionalInformation: {
type: 'string',
nullable: true,
example: 'Urgent request',
},
},
},
})
async createRequest(
@Body(new ValidationPipe())
requestData: CreateRequestDto,
): Promise<FoodRequest> {
return this.requestsService.create(
requestData.pantryId,
requestData.requestedSize,
requestData.requestedFoodTypes,
requestData.additionalInformation,
);
}
@Patch('/:requestId')
async updateRequest(
@Param('requestId', ParseIntPipe) requestId: number,
@Body() body: UpdateRequestDto,
): Promise<FoodRequest> {
return this.requestsService.updateRequest(requestId, body);
}
@Delete('/:requestId')
async deleteRequest(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<void> {
return this.requestsService.delete(requestId);
}
}