-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationController.java
More file actions
258 lines (247 loc) · 10.2 KB
/
Copy pathNotificationController.java
File metadata and controls
258 lines (247 loc) · 10.2 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package net.onelitefeather.vulpes.backend.controller;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.validation.Validated;
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 jakarta.inject.Inject;
import net.onelitefeather.vulpes.api.model.NotificationEntity;
import net.onelitefeather.vulpes.backend.domain.notification.NotificationModelDTO;
import net.onelitefeather.vulpes.backend.domain.notification.NotificationModelResponseDTO;
import net.onelitefeather.vulpes.backend.service.NotificationService;
import net.onelitefeather.vulpes.backend.validation.ValidationGroup;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
/**
* Controller for managing notifications.
* Provides endpoints to add, retrieve, update, and delete notifications.
*
* @author theEvilReaper
* @version 1.0.0
* @since 1.0.0
*/
@Controller("/notification")
public class NotificationController {
private final NotificationService notificationService;
@Inject
public NotificationController(NotificationService notificationService) {
this.notificationService = notificationService;
}
/**
* Adds a new notification.
*
* @param model the notification model to be added
* @return HttpResponse containing the added notification
*/
@Operation(
summary = "Add a new notification",
operationId = "addNotification",
description = "Adds a new notification to the database.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "The notification was successfully added to the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class)
)
)
@ApiResponse(
responseCode = "500",
description = "The notification could not be added to the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelErrorDTO.class)
)
)
@Post
@Produces(MediaType.APPLICATION_JSON)
@Validated(groups = ValidationGroup.Create.class)
public HttpResponse<NotificationModelResponseDTO> add(
@Body NotificationModelDTO model
) {
NotificationModelResponseDTO.NotificationModelDTO result = notificationService.create(model);
return HttpResponse.ok(result);
}
/**
* Retrieves a notification by its ID.
*
* @param id the ID of the notification to retrieve
* @return HttpResponse containing the notification if found, or not found response
*/
@Operation(
summary = "Get a notification by ID",
operationId = "getNotificationById",
description = "Retrieves a notification from the database by its ID.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "The notification was successfully retrieved from the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "The notification was not found in the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelErrorDTO.class)
)
)
@Get("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public HttpResponse<NotificationModelResponseDTO> getById(@PathVariable UUID id) {
Optional<NotificationEntity> model = notificationService.findById(id);
if (model.isPresent()) {
return HttpResponse.ok(NotificationModelResponseDTO.NotificationModelDTO.createDTO(model.get()));
}
return HttpResponse.notFound(new NotificationModelResponseDTO.NotificationModelErrorDTO("Notification not found"));
}
/**
* Removes a notification by its ID.
*
* @param id the ID of the notification to remove
* @return HttpResponse containing the removed notification if found, or not found response
*/
@Operation(
summary = "Remove a notification by ID",
operationId = "removeNotificationById",
description = "Removes a notification from the database by its ID.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "The notification was successfully removed from the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "The notification was not found in the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelErrorDTO.class)
)
)
@Delete("/delete/{id}")
@Produces(MediaType.APPLICATION_JSON)
public HttpResponse<NotificationModelResponseDTO> remove(@PathVariable UUID id) {
NotificationModelResponseDTO result = notificationService.delete(id);
if (result instanceof NotificationModelResponseDTO.NotificationModelErrorDTO) {
return HttpResponse.notFound(result);
}
return HttpResponse.ok(result);
}
/**
* Retrieves all notifications.
*
* @return HttpResponse containing a list of all notifications
*/
@Operation(
summary = "Get all notifications",
operationId = "getAllNotifications",
description = "Retrieves all notifications from the database.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "The notifications were successfully retrieved from the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
array = @ArraySchema(
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class),
arraySchema = @Schema(implementation = Page.class)
)
)
)
@ApiResponse(
responseCode = "404",
description = "No notifications were found in the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelErrorDTO.class)
)
)
@Get(uris = {"/"})
@Produces(MediaType.APPLICATION_JSON)
public HttpResponse<Page<NotificationModelResponseDTO.NotificationModelDTO>> getAll(Pageable pageable) {
Page<NotificationModelResponseDTO.NotificationModelDTO> list = notificationService.getAll(pageable);
return HttpResponse.ok(list);
}
/**
* Deletes all notifications.
*
* @return HttpResponse containing an empty list
*/
@Operation(
summary = "Delete all notifications",
operationId = "deleteAllNotifications",
description = "Deletes all notifications from the database.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "All notifications were successfully deleted from the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class)
)
)
@Delete("/delete/")
@Produces(MediaType.APPLICATION_JSON)
public HttpResponse<List<NotificationModelResponseDTO>> deleteAll() {
List<NotificationModelResponseDTO> result = notificationService.deleteAll();
return HttpResponse.ok(result);
}
/**
* Updates an existing notification.
*
* @param model the notification model to update
* @return HttpResponse containing the updated notification
*/
@Operation(
summary = "Update a notification",
operationId = "updateNotification",
description = "Updates an existing notification in the database.",
tags = {"Notification"}
)
@ApiResponse(
responseCode = "200",
description = "The notification was successfully updated in the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "The notification was not found in the database.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = NotificationModelResponseDTO.NotificationModelErrorDTO.class)
)
)
@Post("/update")
@Produces(MediaType.APPLICATION_JSON)
@Validated(groups = ValidationGroup.Update.class)
public HttpResponse<NotificationModelResponseDTO> update(@Body NotificationModelDTO model) {
NotificationModelResponseDTO result = notificationService.update(model);
if (result instanceof NotificationModelResponseDTO.NotificationModelErrorDTO) {
return HttpResponse.notFound(result);
}
return HttpResponse.ok(result);
}
}