-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriptionRestController.java
More file actions
109 lines (94 loc) · 5.27 KB
/
SubscriptionRestController.java
File metadata and controls
109 lines (94 loc) · 5.27 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
package org.openpodcastapi.opa.subscription;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.openpodcastapi.opa.service.CustomUserDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@RestController
@RequiredArgsConstructor
@Log4j2
@RequestMapping("/api/v1/subscriptions")
public class SubscriptionRestController {
private final SubscriptionService service;
/// Returns all subscriptions for a given user
///
/// @param user the [CustomUserDetails] of the authenticated user
/// @param pageable the [Pageable] pagination object
/// @param includeUnsubscribed whether to include unsubscribed feeds in the response
/// @return a [ResponseEntity] containing [SubscriptionDTO.SubscriptionPageDTO] objects
@GetMapping
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasRole('USER')")
public ResponseEntity<SubscriptionDTO.SubscriptionPageDTO> getAllSubscriptionsForUser(@AuthenticationPrincipal CustomUserDetails user, Pageable pageable, @RequestParam(defaultValue = "false") boolean includeUnsubscribed) {
log.info("{}", user.getAuthorities());
Page<SubscriptionDTO.UserSubscriptionDTO> dto;
if (includeUnsubscribed) {
dto = service.getAllSubscriptionsForUser(user.id(), pageable);
} else {
dto = service.getAllActiveSubscriptionsForUser(user.id(), pageable);
}
log.debug("{}", dto);
return new ResponseEntity<>(SubscriptionDTO.SubscriptionPageDTO.fromPage(dto), HttpStatus.OK);
}
/// Returns a single subscription entry by UUID
///
/// @param uuid the UUID value to query for
/// @return a [ResponseEntity] containing a [SubscriptionDTO.UserSubscriptionDTO] object
/// @throws EntityNotFoundException if no entry is found
/// @throws IllegalArgumentException if the UUID is improperly formatted
@GetMapping("/{uuid}")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasRole('USER')")
public ResponseEntity<SubscriptionDTO.UserSubscriptionDTO> getSubscriptionByUuid(@PathVariable String uuid, @AuthenticationPrincipal CustomUserDetails user) throws EntityNotFoundException {
// Attempt to validate the UUID value from the provided string
// If the value is invalid, the GlobalExceptionHandler will throw a 400.
final var uuidValue = UUID.fromString(uuid);
// Fetch the subscription, throw an EntityNotFoundException if this fails
final var dto = service.getUserSubscriptionBySubscriptionUuid(uuidValue, user.id());
// Return the mapped subscriptionEntity entry
return new ResponseEntity<>(dto, HttpStatus.OK);
}
/// Updates the subscription status of a subscription for a given user
///
/// @param uuid the UUID of the subscription to update
/// @return a [ResponseEntity] containing a [SubscriptionDTO.UserSubscriptionDTO] object
/// @throws EntityNotFoundException if no entry is found
/// @throws IllegalArgumentException if the UUID is improperly formatted
@PostMapping("/{uuid}/unsubscribe")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasRole('USER')")
public ResponseEntity<SubscriptionDTO.UserSubscriptionDTO> unsubscribeUserFromFeed(@PathVariable String uuid, @AuthenticationPrincipal CustomUserDetails user) {
// Attempt to validate the UUID value from the provided string
// If the value is invalid, the GlobalExceptionHandler will throw a 400.
final var uuidValue = UUID.fromString(uuid);
final var dto = service.unsubscribeUserFromFeed(uuidValue, user.id());
return new ResponseEntity<>(dto, HttpStatus.OK);
}
/// Bulk creates [UserSubscriptionEntity] objects for a user. Creates new [SubscriptionEntity] objects if not already present
///
/// @param request a list of [SubscriptionDTO.SubscriptionCreateDTO] objects
/// @return a [ResponseEntity] containing a [SubscriptionDTO.BulkSubscriptionResponseDTO] object
@PostMapping
@PreAuthorize("hasRole('USER')")
public ResponseEntity<SubscriptionDTO.BulkSubscriptionResponseDTO> createUserSubscriptions(@RequestBody List<SubscriptionDTO.SubscriptionCreateDTO> request, @AuthenticationPrincipal CustomUserDetails user) {
final var response = service.addSubscriptions(request, user.id());
if (response.success().isEmpty() && !response.failure().isEmpty()) {
// If all requests failed, return a 400 error
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
} else if (!response.success().isEmpty() && !response.failure().isEmpty()) {
// If some requests succeeded and some failed, return a 207
return new ResponseEntity<>(response, HttpStatus.MULTI_STATUS);
} else {
// If all requests succeeded, return a 200
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
}