11package be.sgl.backend.controller
22
33import be.sgl.backend.config.security.OnlyAdmin
4+ import be.sgl.backend.config.security.Public
5+ import be.sgl.backend.dto.MemberRoleDTO
6+ import be.sgl.backend.dto.StaffRoleDTO
47import be.sgl.backend.dto.ExternalFunction
58import be.sgl.backend.dto.RoleDTO
6- import be.sgl.backend.dto.UserDTO
7- import be.sgl.backend.dto.UserRoleDTO
8- import be.sgl.backend.entity.user.RoleLevel
99import be.sgl.backend.service.RoleService
10+ import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiErrorResponse
1011import io.swagger.v3.oas.annotations.Operation
1112import io.swagger.v3.oas.annotations.media.Content
1213import io.swagger.v3.oas.annotations.media.Schema
1314import io.swagger.v3.oas.annotations.responses.ApiResponse
1415import io.swagger.v3.oas.annotations.tags.Tag
16+ import jakarta.validation.Valid
1517import org.springframework.beans.factory.annotation.Autowired
18+ import org.springframework.http.HttpStatus
1619import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
1720import org.springframework.http.ResponseEntity
1821import org.springframework.stereotype.Controller
19- import org.springframework.web.bind.annotation.GetMapping
20- import org.springframework.web.bind.annotation.PathVariable
21- import org.springframework.web.bind.annotation.RequestMapping
22+ import org.springframework.web.bind.annotation.*
2223
2324@Controller
2425@RequestMapping(" /roles" )
@@ -41,6 +42,20 @@ class RoleController {
4142 return ResponseEntity .ok(roleService.getAllRoles())
4243 }
4344
45+ @GetMapping(" /admin" )
46+ @Public
47+ @Operation(
48+ summary = " Get the admin role" ,
49+ description = " Returns the single admin role." ,
50+ responses = [
51+ ApiResponse (responseCode = " 200" , description = " Ok" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
52+ ApiResponse (responseCode = " 500" , description = " No admin role configured" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
53+ ]
54+ )
55+ fun getAdminRole (): ResponseEntity <RoleDTO > {
56+ return ResponseEntity .ok(roleService.getAdminRole())
57+ }
58+
4459 @GetMapping(" /functions" )
4560 @OnlyAdmin
4661 @Operation(
@@ -67,16 +82,111 @@ class RoleController {
6782 return ResponseEntity .ok(roleService.getPaidExternalFunctions())
6883 }
6984
70- @GetMapping(" /{level}/users" )
85+ @GetMapping(" /branch/{branchId}" )
86+ @Public
87+ @Operation(
88+ summary = " Get the role linked to the specified branch" ,
89+ description = " Returns the single role that is assigned when a user engages in a membership for the given branch, if one." ,
90+ responses = [
91+ ApiResponse (responseCode = " 200" , description = " Ok" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
92+ ApiResponse (responseCode = " 204" , description = " Not found" ),
93+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
94+ ]
95+ )
96+ fun getRoleToSyncByBranch (@PathVariable branchId : Int ): ResponseEntity <RoleDTO ?> {
97+ val memberRole = roleService.getRoleToSyncByBranch(branchId)
98+ return memberRole?.let { ResponseEntity .ok(it) } ? : ResponseEntity .noContent().build()
99+ }
100+
101+ @GetMapping(" /staff-branch/{branchId}" )
102+ @Public
103+ @Operation(
104+ summary = " Get the role linked to the specified staff branch" ,
105+ description = " Returns the single staff role that is assigned when a user is marked as staff of the given branch, if one." ,
106+ responses = [
107+ ApiResponse (responseCode = " 200" , description = " Ok" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
108+ ApiResponse (responseCode = " 204" , description = " Not found" ),
109+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
110+ ]
111+ )
112+ fun getStaffRoleToSyncByBranch (@PathVariable branchId : Int ): ResponseEntity <RoleDTO ?> {
113+ val staffRole = roleService.getStaffRoleToSyncByBranch(branchId)
114+ return staffRole?.let { ResponseEntity .ok(it) } ? : ResponseEntity .noContent().build()
115+ }
116+
117+ @PostMapping(" /branch/{branchId}" )
118+ @OnlyAdmin
119+ @Operation(
120+ summary = " Create a member role" ,
121+ description = " Creates a member role with the provided request body and returns it." ,
122+ responses = [
123+ ApiResponse (responseCode = " 201" , description = " Role created" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
124+ ApiResponse (responseCode = " 400" , description = " Bad role format" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))]),
125+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
126+
127+ ]
128+ )
129+ fun createMemberRole (@PathVariable branchId : Int , @Valid @RequestBody roleDto : MemberRoleDTO ): ResponseEntity <RoleDTO > {
130+ return ResponseEntity (roleService.createMemberRole(branchId, roleDto), HttpStatus .CREATED )
131+ }
132+
133+ @PostMapping(" /staff-branch/{branchId}" )
134+ @OnlyAdmin
135+ @Operation(
136+ summary = " Create a staff role" ,
137+ description = " Creates a staff role with the provided request body and returns it." ,
138+ responses = [
139+ ApiResponse (responseCode = " 201" , description = " Role created" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
140+ ApiResponse (responseCode = " 400" , description = " Bad role format" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))]),
141+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
142+ ]
143+ )
144+ fun createStaffRole (@PathVariable branchId : Int , @Valid @RequestBody roleDto : StaffRoleDTO ): ResponseEntity <RoleDTO > {
145+ return ResponseEntity (roleService.createStaffRole(branchId, roleDto), HttpStatus .CREATED )
146+ }
147+
148+ @PutMapping(" /branch/{id}" )
149+ @OnlyAdmin
150+ @Operation(
151+ summary = " Update an existing member role" ,
152+ description = " Updates a member role, identified with the given id, with the provided request body and returns it." ,
153+ responses = [
154+ ApiResponse (responseCode = " 200" , description = " Role updated" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
155+ ApiResponse (responseCode = " 400" , description = " Bad role format" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))]),
156+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
157+ ]
158+ )
159+ fun updateMemberRole (@PathVariable id : Int , @Valid @RequestBody roleDTO : MemberRoleDTO ): ResponseEntity <RoleDTO > {
160+ return ResponseEntity .ok(roleService.mergeMemberRoleDTOChanges(id, roleDTO))
161+ }
162+
163+ @PutMapping(" /staff-branch/{id}" )
164+ @OnlyAdmin
165+ @Operation(
166+ summary = " Update an existing staff role" ,
167+ description = " Updates a staff role, identified with the given id, with the provided request body and returns it." ,
168+ responses = [
169+ ApiResponse (responseCode = " 200" , description = " Role updated" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = RoleDTO ::class ))]),
170+ ApiResponse (responseCode = " 400" , description = " Bad role format" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))]),
171+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
172+ ]
173+ )
174+ fun updateStaffRole (@PathVariable id : Int , @Valid @RequestBody roleDTO : StaffRoleDTO ): ResponseEntity <RoleDTO > {
175+ return ResponseEntity .ok(roleService.mergeStaffRoleDTOChanges(id, roleDTO))
176+ }
177+
178+ @DeleteMapping(" /{id}" )
71179 @OnlyAdmin
72180 @Operation(
73- summary = " Get all users with a role of the specified level " ,
74- description = " Returns a list of all users roles, filtered by their current role level ." ,
181+ summary = " Delete an existing role" ,
182+ description = " Deletes a role, identified with the given id. All linked user roles are also deleted ." ,
75183 responses = [
76- ApiResponse (responseCode = " 200" , description = " Ok" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (type = " array" , implementation = UserDTO ::class ))])
184+ ApiResponse (responseCode = " 200" , description = " Role deleted" ),
185+ ApiResponse (responseCode = " 404" , description = " Invalid id" , content = [Content (mediaType = APPLICATION_JSON_VALUE , schema = Schema (implementation = ApiErrorResponse ::class ))])
77186 ]
78187 )
79- fun getAllUsers (@PathVariable level : RoleLevel ): ResponseEntity <List <UserRoleDTO >> {
80- return ResponseEntity .ok(roleService.getUserRoles(level))
188+ fun deleteRole (@PathVariable id : Int ): ResponseEntity <Unit > {
189+ roleService.deleteRole(id)
190+ return ResponseEntity .ok().build()
81191 }
82192}
0 commit comments