|
| 1 | +package ru.codebattles.backend.web.controllers |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation |
| 4 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement |
| 5 | +import io.swagger.v3.oas.annotations.tags.Tag |
| 6 | +import jakarta.annotation.security.RolesAllowed |
| 7 | +import org.springframework.http.HttpStatus |
| 8 | +import org.springframework.http.ResponseEntity |
| 9 | +import org.springframework.security.core.annotation.AuthenticationPrincipal |
| 10 | +import org.springframework.web.bind.annotation.* |
| 11 | +import ru.codebattles.backend.dto.CreatePostDto |
| 12 | +import ru.codebattles.backend.dto.PostDto |
| 13 | +import ru.codebattles.backend.entity.User |
| 14 | +import ru.codebattles.backend.services.PostService |
| 15 | + |
| 16 | +@Tag(name = "Posts", description = "Endpoints for managing posts") |
| 17 | +@RestController |
| 18 | +@RequestMapping("/api/posts") |
| 19 | +@SecurityRequirement(name = "JWT") |
| 20 | +class PostController( |
| 21 | + private val postService: PostService, |
| 22 | +) { |
| 23 | + |
| 24 | + @Operation( |
| 25 | + summary = "Get all posts", |
| 26 | + description = "Retrieves a list of all posts." |
| 27 | + ) |
| 28 | + @GetMapping |
| 29 | + fun getAll(): List<PostDto> { |
| 30 | + return postService.getAll() |
| 31 | + } |
| 32 | + |
| 33 | + @Operation( |
| 34 | + summary = "Get main page posts", |
| 35 | + description = "Retrieves posts that should be shown on the main page." |
| 36 | + ) |
| 37 | + @GetMapping("/main") |
| 38 | + fun getMainPagePosts(): List<PostDto> { |
| 39 | + return postService.getMainPagePosts() |
| 40 | + } |
| 41 | + |
| 42 | + @Operation( |
| 43 | + summary = "Get post by ID", |
| 44 | + description = "Retrieves a post by its ID." |
| 45 | + ) |
| 46 | + @GetMapping("/{id}") |
| 47 | + fun getById(@PathVariable id: Long): PostDto { |
| 48 | + return postService.getById(id) |
| 49 | + } |
| 50 | + |
| 51 | + @Operation( |
| 52 | + summary = "[ADMIN] Create a new post", |
| 53 | + description = "Creates a new post. Required admin role." |
| 54 | + ) |
| 55 | + @RolesAllowed("ADMIN") |
| 56 | + @PostMapping |
| 57 | + fun create(@RequestBody createPostDto: CreatePostDto, @AuthenticationPrincipal user: User): PostDto { |
| 58 | + return postService.create(createPostDto) |
| 59 | + } |
| 60 | + |
| 61 | + @Operation( |
| 62 | + summary = "[ADMIN] Update a post", |
| 63 | + description = "Updates an existing post by ID. Required admin role." |
| 64 | + ) |
| 65 | + @RolesAllowed("ADMIN") |
| 66 | + @PutMapping("/{id}") |
| 67 | + fun update(@PathVariable id: Long, @RequestBody postDto: PostDto, @AuthenticationPrincipal user: User): PostDto { |
| 68 | + return postService.update(id, postDto) |
| 69 | + } |
| 70 | + |
| 71 | + @Operation( |
| 72 | + summary = "[ADMIN] Delete a post", |
| 73 | + description = "Deletes a post by ID. Required admin role." |
| 74 | + ) |
| 75 | + @RolesAllowed("ADMIN") |
| 76 | + @DeleteMapping("/{id}") |
| 77 | + fun delete(@PathVariable id: Long, @AuthenticationPrincipal user: User): ResponseEntity<Void> { |
| 78 | + postService.delete(id) |
| 79 | + return ResponseEntity.noContent().build() |
| 80 | + } |
| 81 | +} |
0 commit comments