-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduct-category-service.ts
146 lines (134 loc) · 5.02 KB
/
product-category-service.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { FindManyOptions } from 'typeorm';
import ProductCategory from '../entity/product/product-category';
import {
PaginatedProductCategoryResponse,
ProductCategoryResponse,
} from '../controller/response/product-category-response';
import ProductCategoryRequest from '../controller/request/product-category-request';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import { PaginationParameters } from '../helpers/pagination';
/**
* Define productCategory filtering parameters used to filter query results.
*/
export interface ProductCategoryFilterParameters {
/**
* Filter based on product id.
*/
id?: number;
/**
* Filter based on product owner.
*/
name?: string;
}
/**
* Wrapper for all Product related logic.
*/
export default class ProductCategoryService {
/**
* Creates a productCategoryResponse from a productCategory
* @param {ProductCategory.model} productCategory - productCategory
* @returns {ProductCategoryResponse.model} - a productCategoryResponse
* created with the productCategory
*/
public static asProductCategoryResponse(productCategory: ProductCategory)
: ProductCategoryResponse {
return {
id: productCategory.id,
name: productCategory.name,
createdAt: productCategory.createdAt.toISOString(),
updatedAt: productCategory.updatedAt.toISOString(),
};
}
/**
* Query for getting the productCategories.
*/
public static async getProductCategories(
filters: ProductCategoryFilterParameters = {}, pagination: PaginationParameters = {},
): Promise<PaginatedProductCategoryResponse> {
const { take, skip } = pagination;
const filterMapping: FilterMapping = {
id: 'id',
name: 'name',
};
const options: FindManyOptions = {
where: QueryFilter.createFilterWhereClause(filterMapping, filters),
order: { id: 'ASC' },
};
const results = await Promise.all([
ProductCategory.find({ ...options, take, skip }),
ProductCategory.count(options),
]);
const records = results[0].map(
(productCategory) => (this.asProductCategoryResponse(productCategory)),
);
return {
_pagination: {
take, skip, count: results[1],
},
records,
};
}
/**
* Saves a ProductCategory to the database.
* @param request - The ProductCategoryRequest with values.
*/
public static async postProductCategory(request: ProductCategoryRequest)
: Promise<ProductCategoryResponse> {
const productCategory = Object.assign(new ProductCategory(), request);
return ProductCategory.save(productCategory)
.then(() => this.asProductCategoryResponse(productCategory));
}
/**
* Updates a ProductCategory in the database.
* @param id - The id of the productCategory that needs to be updated.
* @param request - The ProductCategoryRequest with updated values.
*/
public static async patchProductCategory(id: number, request: ProductCategoryRequest)
: Promise<ProductCategoryResponse> {
const productCategoryToUpdate = await ProductCategory.findOne({ where: { id } });
if (!productCategoryToUpdate) return null;
const productCategory = Object.assign(productCategoryToUpdate, request);
await ProductCategory.save(productCategory);
return this.asProductCategoryResponse(productCategory);
}
/**
* Deletes a ProductCategory from the database.
* @param id - The id of the productCategory that needs to be deleted.
*/
public static async deleteProductCategory(id: number): Promise<ProductCategoryResponse> {
const productCategory = await ProductCategory.findOne({ where: { id } });
if (!productCategory) {
return null;
}
return ProductCategory.delete(id).then(() => this.asProductCategoryResponse(productCategory));
}
/**
* Verifies whether the productCategory request translates to a valid productCategory
* @param {ProductCategoryRequest.model} request
* - the productCategory request to verify
* @returns {boolean} - whether productCategory is ok or not
*/
public static async verifyProductCategory(request: ProductCategoryRequest):
Promise<boolean> {
return request.name != null && request.name !== ''
&& request.name.length <= 64
&& !(await ProductCategory.findOne({ where: { name: request.name } }));
}
}