TripController.java
2,077 bytes
| 1 | package com.tasteprint.journey; |
|---|---|
| 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.PutMapping; |
| 15 | import org.springframework.web.bind.annotation.RequestBody; |
| 16 | import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | import org.springframework.web.bind.annotation.ResponseStatus; |
| 18 | import org.springframework.web.bind.annotation.RestController; |
| 19 | |
| 20 | import com.tasteprint.account.AuthenticatedUser; |
| 21 | |
| 22 | @RestController |
| 23 | @RequestMapping("/api/v1/trips") |
| 24 | class TripController { |
| 25 | |
| 26 | private final TripService trips; |
| 27 | |
| 28 | TripController(TripService trips) { |
| 29 | this.trips = trips; |
| 30 | } |
| 31 | |
| 32 | @GetMapping |
| 33 | List<TripView> list(@AuthenticationPrincipal AuthenticatedUser user) { |
| 34 | return trips.list(user.id()); |
| 35 | } |
| 36 | |
| 37 | @GetMapping("/{tripId}") |
| 38 | TripView get(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable UUID tripId) { |
| 39 | return trips.get(user.id(), tripId); |
| 40 | } |
| 41 | |
| 42 | @PostMapping |
| 43 | @ResponseStatus(HttpStatus.CREATED) |
| 44 | TripView create(@AuthenticationPrincipal AuthenticatedUser user, |
| 45 | @Valid @RequestBody SaveTripRequest request) { |
| 46 | return trips.create(user.id(), request); |
| 47 | } |
| 48 | |
| 49 | @PutMapping("/{tripId}") |
| 50 | TripView update(@AuthenticationPrincipal AuthenticatedUser user, |
| 51 | @PathVariable UUID tripId, |
| 52 | @Valid @RequestBody SaveTripRequest request) { |
| 53 | return trips.update(user.id(), tripId, request); |
| 54 | } |
| 55 | |
| 56 | @DeleteMapping("/{tripId}") |
| 57 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 58 | void delete(@AuthenticationPrincipal AuthenticatedUser user, @PathVariable UUID tripId) { |
| 59 | trips.delete(user.id(), tripId); |
| 60 | } |
| 61 | } |
| 62 | |