-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentController.java
More file actions
178 lines (169 loc) · 10.8 KB
/
Copy pathAssignmentController.java
File metadata and controls
178 lines (169 loc) · 10.8 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.itasocialacademy.oitassist.taskassignment.controller;
import com.itasocialacademy.oitassist.core.dao.dto.response.PageResponse;
import com.itasocialacademy.oitassist.core.web.ErrorResponse;
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateAndAssignTaskRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateTaskAssignmentRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.request.UpdateTaskAssignmentRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.DetailedTaskAssignmentResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.LinkedToursResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.TaskAssignmentResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.service.interfaces.AssignmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Tag(name = "Task Assignments Management V1", description = "Operations related to task linkage to tours")
public class AssignmentController {
private final AssignmentService assignmentService;
@Operation(
summary = "Assign a task to a tour",
description = "Creates a new task assignment for a specific tour. Requires ADMIN or ORG role.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Task assignment created successfully",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = DetailedTaskAssignmentResponseDTO.class))),
@ApiResponse(responseCode = "400", description = "Invalid input data",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", description = "Access denied (requires ADMIN or ORG role)",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Tour or task not found",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "409", description = "Link between task and tour already exists",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/tours/{tourId}/task-assignments")
@PreAuthorize("hasAnyRole('ADMIN', 'ORG')")
public ResponseEntity<DetailedTaskAssignmentResponseDTO> assignTask(@PathVariable Long tourId,
@Valid @RequestBody CreateTaskAssignmentRequestDTO request) {
return ResponseEntity.status(HttpStatus.CREATED).body(assignmentService.assignTask(tourId, request));
}
@Operation(
summary = "Create a new task and assign it to a tour",
description = "Creates a new task body and immediately assigns it to the specified tour. "
+ "Requires ADMIN or ORG role.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Task created and assigned successfully",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = DetailedTaskAssignmentResponseDTO.class))),
@ApiResponse(responseCode = "400", description = "Invalid input data",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", description = "Access denied (requires ADMIN or ORG role)",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Tour not found",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping("/tours/{tourId}/task-assignments/new")
@PreAuthorize("hasAnyRole('ADMIN','ORG')")
public ResponseEntity<DetailedTaskAssignmentResponseDTO> createAndAssignTask(@PathVariable Long tourId,
@Valid @RequestBody CreateAndAssignTaskRequestDTO request) {
return ResponseEntity.status(HttpStatus.CREATED).body(assignmentService.createAndAssignTask(tourId, request));
}
@Operation(
summary = "Get task assignment by id",
description = "Retrieves a specific task assignment by its id. Requires authentication.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Task assignment retrieved successfully",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = DetailedTaskAssignmentResponseDTO.class))),
@ApiResponse(responseCode = "404", description = "Task assignment not found",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/task-assignments/{assignmentId}")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<DetailedTaskAssignmentResponseDTO> getById(@PathVariable Long assignmentId) {
return ResponseEntity.ok().body(assignmentService.getTaskAssignmentById(assignmentId));
}
@Operation(
summary = "Get task assignments by tour",
description = "Retrieves all task assignments for a specific tour. Requires authentication.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Task assignments retrieved successfully",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = PageResponse.class))),
@ApiResponse(responseCode = "404", description = "Tour not found",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/tours/{tourId}/task-assignments")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<PageResponse<TaskAssignmentResponseDTO>> getByTour(@PathVariable Long tourId,
@ParameterObject @PageableDefault(size = 15, sort = "createdAt") Pageable pageable) {
return ResponseEntity.ok(PageResponse.from(assignmentService.getAssignmentsByTourId(pageable, tourId)));
}
@Operation(
summary = "Get tours linked to a task",
description = "Retrieves a list of tours linked to the specified task body. Requires ADMIN or ORG role.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Linked tours retrieved successfully",
content = @Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = LinkedToursResponseDTO.class)))),
@ApiResponse(responseCode = "403", description = "Access denied (requires ADMIN or ORG role)",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Task body or linked tour not found",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/tasks/{taskId}/linked-tours")
@PreAuthorize("hasAnyRole('ADMIN', 'ORG')")
public ResponseEntity<List<LinkedToursResponseDTO>> getLinkedTours(@PathVariable Long taskId) {
return ResponseEntity.ok().body(assignmentService.getLinkedToursByTaskId(taskId));
}
@Operation(
summary = "Update a task assignment",
description = "Updates an existing task assignment. Only users with ADMIN or ORG role can perform this action.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Task assignment updated successfully",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = DetailedTaskAssignmentResponseDTO.class))),
@ApiResponse(responseCode = "400", description = "Validation failed",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", description = "Access denied (requires ADMIN or ORG role)",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Task assignment not found",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/task-assignments/{assignmentId}")
@PreAuthorize("hasAnyRole('ADMIN', 'ORG')")
public ResponseEntity<DetailedTaskAssignmentResponseDTO> update(@PathVariable Long assignmentId,
@Valid @RequestBody UpdateTaskAssignmentRequestDTO request) {
return ResponseEntity.ok().body(assignmentService.updateTaskAssignment(assignmentId, request));
}
@Operation(
summary = "Delete a task assignment",
description = "Deletes a task assignment by its id. Only users with ADMIN or ORG role can perform this action.")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Task assignment deleted successfully"),
@ApiResponse(responseCode = "403", description = "Access denied (requires ADMIN or ORG role)",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "Task assignment not found",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = ErrorResponse.class)))
})
@DeleteMapping("/task-assignments/{assignmentId}")
@PreAuthorize("hasAnyRole('ADMIN', 'ORG')")
public ResponseEntity<Void> delete(@PathVariable Long assignmentId) {
assignmentService.deleteTaskAssignment(assignmentId);
return ResponseEntity.noContent().build();
}
}