-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCommentController.java
More file actions
76 lines (64 loc) · 2.88 KB
/
CommentController.java
File metadata and controls
76 lines (64 loc) · 2.88 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
package com.example.devSns.controller;
import com.example.devSns.annotation.LoginUser;
import com.example.devSns.dto.GenericDataDto;
import com.example.devSns.dto.comment.CommentCreateDto;
import com.example.devSns.dto.comment.CommentResponseDto;
import com.example.devSns.service.CommentService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
@RestController
@RequestMapping("/comments")
public class CommentController {
private final CommentService commentService;
public CommentController(CommentService commentService) {
this.commentService = commentService;
}
@PostMapping
public ResponseEntity<GenericDataDto<Long>> create(@RequestBody @Valid CommentCreateDto commentCreateDto, @LoginUser Long memberId) {
Long id = commentService.create(commentCreateDto, memberId);
URI uri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(id)
.toUri();
return ResponseEntity.created(uri).body(new GenericDataDto<>(id));
}
@GetMapping("/{id}")
public ResponseEntity<CommentResponseDto> getOne(@PathVariable @Positive Long id) {
CommentResponseDto comment = commentService.findCommentById(id);
return ResponseEntity.ok().body(comment);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable @Positive Long id, @LoginUser Long memberId) {
commentService.delete(id, memberId);
return ResponseEntity.noContent().build();
}
@PatchMapping("/{id}/contents")
public ResponseEntity<CommentResponseDto> contents(@PathVariable @Positive Long id,
@RequestBody @Valid GenericDataDto<String> contentsDto,
@LoginUser Long memberId) {
CommentResponseDto comment = commentService.updateContent(id, contentsDto, memberId);
return ResponseEntity.ok().body(comment);
}
@GetMapping
public ResponseEntity<Slice<CommentResponseDto>> findByMemberIdAsPaginated(
@PageableDefault(
size = 15,
sort = "id",
direction = Sort.Direction.DESC
)
Pageable pageable,
@RequestParam @Positive Long memberId
) {
Slice<CommentResponseDto> comments = commentService.findByMemberAsSlice(pageable, memberId);
return ResponseEntity.ok().body(comments);
}
}