Spring47 [Spring Data JPA] 댓글 구현하기 -5 유효성 검사하기(null 체크) @Data public class CommentDto { //NotNull = null값 체크 //@NotEmpty 빈값이거나 null을 체크 //NotBlank = 빈값이거나 null체크 그리고 빈 공백(스페이스) 까지 @NotBlank private String content; @NotNull private Integer imageId; // toEntity가 필요없다 } Dto 에서 int - Integer로 바꿔주었다. @PostMapping("/api/comment") public ResponseEntity commentSave(@Valid @RequestBody CommentDto commentDto, BindingResult bindingResult, @AuthenticationPrincip.. 2023. 1. 15. [Spring Data JPA] 댓글 구현하기 -4 댓글 삭제 ajax 및 동적처리 우리는 댓글 삭제를 구현하고 싶다. story를 켰을때 댓글이 나올것이고, 댓글 삭제버튼은 유저 본인이 아닌이상 X표시가 나와선 안된다. 그렇다면 문제가 하나 있다. 그렇다면 principalId가 필요할텐데 어디서 가져오냐는 거다. 우리가 가진 정보는 image.id 밖에 없다. 우리가 현재 getStoryItem 을 보면 principalId를 받아오지 않기 때문에 삭제 로직을 하기 위해선 principalId를 어디선가 받아와야 한다. 한가지 방법이 있는데 정답은 header 에 있다. header.jsp 의 body문 바로 아래에 이렇게 히든타입을 걸고 value에 담는다. 그 후 //(0) 현재 로그인한 사용자 아이디 let principalId = $("#principalId").val(); .. 2023. 1. 15. [Spring Data JPA] 댓글 구현하기 -3 댓글 뷰 렌더링 $.ajax({ type:"post", url:"/api/comment", data:JSON.stringify(data), contentType:"application/json; charset=utf-8", dataType:"json" }).done(res =>{ // console.log("성공",res) let comment = res.data; let content = ` ${comment.user.username} : ${comment.content} `; commentList.prepend(content); // append= 뒤에다가 넣는거 prepend는 앞에다가넣음 최신댓글이 위로 올라와야하니까 }).fail(error => { console.log(error,"오류") }); commen.. 2023. 1. 15. [Spring Data JPA] 댓글 구현하기 -2 댓글 ajax 구현 및 DB연동 @Data @NoArgsConstructor @AllArgsConstructor @Builder @Entity public class Comment { @GeneratedValue(strategy = GenerationType.IDENTITY) @Id private int id; @Column(length = 100 , nullable = false) private String content; @JsonIgnoreProperties @ManyToOne @JoinColumn(name = "userId") private User user; @JsonIgnoreProperties @ManyToOne @JoinColumn(name = "imageId") private Image image; private Loc.. 2023. 1. 14. 이전 1 2 3 4 5 6 7 ··· 12 다음