ChallengeController.java
2,352 bytes
| 1 | package com.tasteprint.social; |
|---|---|
| 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.UUID; |
| 5 | |
| 6 | import jakarta.validation.Valid; |
| 7 | |
| 8 | import org.springframework.http.HttpStatus; |
| 9 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 10 | import org.springframework.web.bind.annotation.DeleteMapping; |
| 11 | import org.springframework.web.bind.annotation.GetMapping; |
| 12 | import org.springframework.web.bind.annotation.PathVariable; |
| 13 | import org.springframework.web.bind.annotation.PostMapping; |
| 14 | import org.springframework.web.bind.annotation.RequestBody; |
| 15 | import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | import org.springframework.web.bind.annotation.ResponseStatus; |
| 17 | import org.springframework.web.bind.annotation.RestController; |
| 18 | |
| 19 | import com.tasteprint.account.AuthenticatedUser; |
| 20 | |
| 21 | @RestController |
| 22 | @RequestMapping("/api/v1/challenges") |
| 23 | class ChallengeController { |
| 24 | |
| 25 | private final ChallengeService challenges; |
| 26 | |
| 27 | ChallengeController(ChallengeService challenges) { |
| 28 | this.challenges = challenges; |
| 29 | } |
| 30 | |
| 31 | @GetMapping |
| 32 | List<ChallengeView> list(@AuthenticationPrincipal AuthenticatedUser user) { |
| 33 | return challenges.list(user.id()); |
| 34 | } |
| 35 | |
| 36 | @GetMapping("/{challengeId}") |
| 37 | ChallengeView get(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable UUID challengeId) { |
| 38 | return challenges.get(user.id(), challengeId); |
| 39 | } |
| 40 | |
| 41 | @PostMapping |
| 42 | @ResponseStatus(HttpStatus.CREATED) |
| 43 | ChallengeView create(@AuthenticationPrincipal AuthenticatedUser user, |
| 44 | @Valid @RequestBody CreateChallengeRequest request) { |
| 45 | return challenges.create(user.id(), request); |
| 46 | } |
| 47 | |
| 48 | @PostMapping("/join") |
| 49 | ChallengeView join(@AuthenticationPrincipal AuthenticatedUser user, |
| 50 | @Valid @RequestBody JoinChallengeRequest request) { |
| 51 | return challenges.join(user.id(), request.joinCode()); |
| 52 | } |
| 53 | |
| 54 | @DeleteMapping("/{challengeId}/members/me") |
| 55 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 56 | void leave(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable UUID challengeId) { |
| 57 | challenges.leave(user.id(), challengeId); |
| 58 | } |
| 59 | |
| 60 | @DeleteMapping("/{challengeId}") |
| 61 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 62 | void delete(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable UUID challengeId) { |
| 63 | challenges.delete(user.id(), challengeId); |
| 64 | } |
| 65 | } |
| 66 | |