@@ -2,48 +2,56 @@ import { type Request, type Response } from 'express'
22import { arrayFormatter } from 'expresso-core'
33import authorization from '~/app/middleware/authorization'
44import { permissionAccess } from '~/app/middleware/permission'
5+ import roleSchema from '~/app/schema/role.schema'
56import RoleService from '~/app/service/role.service'
67import { env } from '~/config/env'
78import ConstRole from '~/core/constants/ConstRole'
89import { type IReqOptions } from '~/core/interface/ReqOptions'
910import HttpResponse from '~/core/modules/response/HttpResponse'
1011import { asyncHandler } from '~/core/utils/asyncHandler'
11- import route from '~/routes/v1'
12+ import Role from '~/database/entities/Role'
13+ import v1Route from '~/routes/v1'
14+
15+ const route = v1Route
16+ const routePath = `/role`
17+ const newRoleService = new RoleService ( {
18+ entity : 'role' ,
19+ repository : Role ,
20+ } )
1221
1322route . get (
14- '/role' ,
23+ ` ${ routePath } ` ,
1524 authorization ,
1625 asyncHandler ( async function findAll ( req : Request , res : Response ) {
1726 const { lang } = req . getQuery ( )
1827 const defaultLang = lang ?? env . APP_LANG
1928 const options : IReqOptions = { lang : defaultLang }
2029
21- const data = await RoleService . findAll ( req )
30+ const data = await newRoleService . findAll ( req )
2231
2332 const httpResponse = HttpResponse . get ( data , options )
2433 res . status ( 200 ) . json ( httpResponse )
2534 } )
2635)
2736
2837route . get (
29- '/role/ :id' ,
38+ ` ${ routePath } / :id` ,
3039 authorization ,
3140 asyncHandler ( async function findOne ( req : Request , res : Response ) {
3241 const { lang } = req . getQuery ( )
3342 const defaultLang = lang ?? env . APP_LANG
3443 const options : IReqOptions = { lang : defaultLang }
3544
3645 const { id } = req . getParams ( )
37-
38- const data = await RoleService . findById ( id , options )
46+ const data = await newRoleService . findById ( id , options )
3947
4048 const httpResponse = HttpResponse . get ( { data } , options )
4149 res . status ( 200 ) . json ( httpResponse )
4250 } )
4351)
4452
4553route . post (
46- '/role' ,
54+ ` ${ routePath } ` ,
4755 authorization ,
4856 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
4957 asyncHandler ( async function create ( req : Request , res : Response ) {
@@ -52,16 +60,15 @@ route.post(
5260 const options : IReqOptions = { lang : defaultLang }
5361
5462 const formData = req . getBody ( )
55-
56- const data = await RoleService . create ( formData )
63+ const data = await newRoleService . create ( formData )
5764
5865 const httpResponse = HttpResponse . created ( { data } , options )
5966 res . status ( 201 ) . json ( httpResponse )
6067 } )
6168)
6269
6370route . put (
64- '/role/ :id' ,
71+ ` ${ routePath } / :id` ,
6572 authorization ,
6673 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
6774 asyncHandler ( async function update ( req : Request , res : Response ) {
@@ -72,15 +79,16 @@ route.put(
7279 const { id } = req . getParams ( )
7380 const formData = req . getBody ( )
7481
75- const data = await RoleService . update ( id , formData , options )
82+ const newFormData = roleSchema . create . parse ( formData )
83+ const data = await newRoleService . update ( id , newFormData , options )
7684
7785 const httpResponse = HttpResponse . updated ( { data } , options )
7886 res . status ( 200 ) . json ( httpResponse )
7987 } )
8088)
8189
8290route . put (
83- '/role/ restore/:id' ,
91+ ` ${ routePath } / restore/:id` ,
8492 authorization ,
8593 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
8694 asyncHandler ( async function restore ( req : Request , res : Response ) {
@@ -89,16 +97,15 @@ route.put(
8997 const options : IReqOptions = { lang : defaultLang }
9098
9199 const { id } = req . getParams ( )
92-
93- await RoleService . restore ( id , options )
100+ await newRoleService . restore ( id , options )
94101
95102 const httpResponse = HttpResponse . updated ( { } , options )
96103 res . status ( 200 ) . json ( httpResponse )
97104 } )
98105)
99106
100107route . delete (
101- '/role/ soft-delete/:id' ,
108+ ` ${ routePath } / soft-delete/:id` ,
102109 authorization ,
103110 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
104111 asyncHandler ( async function softDelete ( req : Request , res : Response ) {
@@ -107,16 +114,15 @@ route.delete(
107114 const options : IReqOptions = { lang : defaultLang }
108115
109116 const { id } = req . getParams ( )
110-
111- await RoleService . softDelete ( id , options )
117+ await newRoleService . softDelete ( id , options )
112118
113119 const httpResponse = HttpResponse . deleted ( { } , options )
114120 res . status ( 200 ) . json ( httpResponse )
115121 } )
116122)
117123
118124route . delete (
119- '/role/ force-delete/:id' ,
125+ ` ${ routePath } / force-delete/:id` ,
120126 authorization ,
121127 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
122128 asyncHandler ( async function forceDelete ( req : Request , res : Response ) {
@@ -125,16 +131,15 @@ route.delete(
125131 const options : IReqOptions = { lang : defaultLang }
126132
127133 const { id } = req . getParams ( )
128-
129- await RoleService . forceDelete ( id , options )
134+ await newRoleService . forceDelete ( id , options )
130135
131136 const httpResponse = HttpResponse . deleted ( { } , options )
132137 res . status ( 200 ) . json ( httpResponse )
133138 } )
134139)
135140
136141route . post (
137- '/role/ multiple/restore' ,
142+ ` ${ routePath } / multiple/restore` ,
138143 authorization ,
139144 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
140145 asyncHandler ( async function multipleRestore ( req : Request , res : Response ) {
@@ -144,16 +149,15 @@ route.post(
144149
145150 const formData = req . getBody ( )
146151 const arrayIds = arrayFormatter ( formData . ids )
147-
148- await RoleService . multipleRestore ( arrayIds , options )
152+ await newRoleService . multipleRestore ( arrayIds , options )
149153
150154 const httpResponse = HttpResponse . updated ( { } , options )
151155 res . status ( 200 ) . json ( httpResponse )
152156 } )
153157)
154158
155159route . post (
156- '/role/ multiple/soft-delete' ,
160+ ` ${ routePath } / multiple/soft-delete` ,
157161 authorization ,
158162 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
159163 asyncHandler ( async function multipleSoftDelete ( req : Request , res : Response ) {
@@ -163,16 +167,15 @@ route.post(
163167
164168 const formData = req . getBody ( )
165169 const arrayIds = arrayFormatter ( formData . ids )
166-
167- await RoleService . multipleSoftDelete ( arrayIds , options )
170+ await newRoleService . multipleSoftDelete ( arrayIds , options )
168171
169172 const httpResponse = HttpResponse . deleted ( { } , options )
170173 res . status ( 200 ) . json ( httpResponse )
171174 } )
172175)
173176
174177route . post (
175- '/role/ multiple/force-delete' ,
178+ ` ${ routePath } / multiple/force-delete` ,
176179 authorization ,
177180 permissionAccess ( ConstRole . ROLE_ADMIN ) ,
178181 asyncHandler ( async function multipleForceDelete ( req : Request , res : Response ) {
@@ -182,8 +185,7 @@ route.post(
182185
183186 const formData = req . getBody ( )
184187 const arrayIds = arrayFormatter ( formData . ids )
185-
186- await RoleService . multipleForceDelete ( arrayIds , options )
188+ await newRoleService . multipleForceDelete ( arrayIds , options )
187189
188190 const httpResponse = HttpResponse . deleted ( { } , options )
189191 res . status ( 200 ) . json ( httpResponse )
0 commit comments