1. 회원정보 수정하기
package shop.mtcoding.blog.user; import lombok.Data; public class UserResponse { // 회원정보 수정하기 @Data public static class UpdateDTO { private int id; private String password; private String email; public UpdateDTO(User user) { this.id = user.getId(); this.password = user.getPassword(); this.email = user.getEmail(); } } }
package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; @RequiredArgsConstructor @RestController public class UserController { private final UserService userService; private final HttpSession session; @PutMapping("/api/users/{id}") public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody UserRequest.UpdateDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); User newSessionUser = userService.update(sessionUser.getId(), reqDTO); session.setAttribute("sessionUser", newSessionUser); // 이 친구만 DTO 생성위치 예외 UserResponse.UpdateDTO respDTO = new UserResponse.UpdateDTO(sessionUser); return ResponseEntity.ok(new ApiUtil(respDTO)); } }
package shop.mtcoding.blog.user; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception400; import shop.mtcoding.blog._core.errors.exception.Exception401; import shop.mtcoding.blog._core.errors.exception.Exception404; import java.util.Optional; @RequiredArgsConstructor @Service // IoC 등록 public class UserService { private final BoardJPARepository boardJPARepository; @Transactional public User update(int id, UserRequest.UpdateDTO reqDTO){ User user = userJPARepository.findById(id) .orElseThrow(() -> new Exception404("회원정보를 찾을 수 없습니다")); user.setPassword(reqDTO.getPassword()); user.setEmail(reqDTO.getEmail()); return user; }
2. 댓글 쓰기
package shop.mtcoding.blog.reply; import lombok.Data; public class ReplyResponse { @Data public static class SaveDTO { private Integer id; private Integer boardId; private String comment; public SaveDTO(Reply reply) { this.id = reply.getId(); this.boardId = reply.getBoard().getId(); this.comment = reply.getComment(); } } }
package shop.mtcoding.blog.reply; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import shop.mtcoding.blog.user.UserResponse; @RequiredArgsConstructor @RestController public class ReplyController { private final ReplyService replyService; private final HttpSession session; @PostMapping("/api/replies") public ResponseEntity<?> save(@RequestBody ReplyRequest.SaveDTO reqDTO){ User sessionUser = (User) session.getAttribute("sessionUser"); ReplyResponse.SaveDTO respDTO = replyService.save(reqDTO, sessionUser); return ResponseEntity.ok(new ApiUtil(respDTO)); } }
package shop.mtcoding.blog.reply; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception401; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardJPARepository; import shop.mtcoding.blog.user.User; import shop.mtcoding.blog.user.UserResponse; @RequiredArgsConstructor @Service public class ReplyService { private final BoardJPARepository boardJPARepository; private final ReplyJPARepository replyJPARepository; @Transactional public ReplyResponse.SaveDTO save(ReplyRequest.SaveDTO reqDTO, User sessionUser) { Board board = boardJPARepository.findById(reqDTO.getBoardId()) .orElseThrow(() -> new Exception404("없는 게시글에 댓글을 작성할 수 없어요")); Reply reply = reqDTO.toEntity(sessionUser, board); replyJPARepository.save(reply); return new ReplyResponse.SaveDTO(reply); } }
3. 댓글 삭제하기
package shop.mtcoding.blog.reply; import lombok.Data; public class ReplyResponse { @Data public static class DeleteDTO { private Integer id; private Integer boardId; private String comment; public DeleteDTO(Reply reply) { this.id = reply.getId(); this.boardId = reply.getBoard().getId(); this.comment = reply.getComment(); } } }
package shop.mtcoding.blog.reply; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import shop.mtcoding.blog.user.UserResponse; @RequiredArgsConstructor @RestController public class ReplyController { private final ReplyService replyService; private final HttpSession session; @DeleteMapping("/api/replies/{id}") public ResponseEntity<?> delete(@PathVariable Integer id){ User sessionUser = (User) session.getAttribute("sessionUser"); replyService.delete(id, sessionUser.getId()); return ResponseEntity.ok(new ApiUtil(null)); } }
package shop.mtcoding.blog.reply; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception401; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardJPARepository; import shop.mtcoding.blog.user.User; import shop.mtcoding.blog.user.UserResponse; @RequiredArgsConstructor @Service public class ReplyService { private final BoardJPARepository boardJPARepository; private final ReplyJPARepository replyJPARepository; @Transactional public ReplyResponse.DeleteDTO delete(int replyId, int sessionUserId) { Reply reply = replyJPARepository.findById(replyId) .orElseThrow(() -> new Exception404("없는 댓글을 삭제할 수 없어요")); if(reply.getUser().getId() != sessionUserId){ throw new Exception403("댓글을 삭제할 권한이 없어요"); } replyJPARepository.deleteById(replyId); return new ReplyResponse.DeleteDTO(reply); } }
Share article