diff --git a/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java b/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java index 77054f7..e1b15c4 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java @@ -14,4 +14,11 @@ public ResponseEntity errorHandler(BindException e) { new ErrorResponse(e.getAllErrors().get(0).getDefaultMessage(), 400) ); } + + @ExceptionHandler(value = {IllegalArgumentException.class}) + public ResponseEntity errorHandler(IllegalArgumentException e) { + return ResponseEntity.badRequest().body( + new ErrorResponse(e.getMessage(), 400) + ); + } } diff --git a/src/main/java/com/example/marumaru_sparta_verspring/configuration/AmazonS3Config.java b/src/main/java/com/example/marumaru_sparta_verspring/config/AmazonS3Config.java similarity index 94% rename from src/main/java/com/example/marumaru_sparta_verspring/configuration/AmazonS3Config.java rename to src/main/java/com/example/marumaru_sparta_verspring/config/AmazonS3Config.java index f41d316..1ffbbb5 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/configuration/AmazonS3Config.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/config/AmazonS3Config.java @@ -1,4 +1,4 @@ -package com.example.marumaru_sparta_verspring.configuration; +package com.example.marumaru_sparta_verspring.config; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; diff --git a/src/main/java/com/example/marumaru_sparta_verspring/controller/FrontController.java b/src/main/java/com/example/marumaru_sparta_verspring/controller/FrontController.java index b3aa0cc..23b1e37 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/controller/FrontController.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/controller/FrontController.java @@ -2,7 +2,7 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; + @Controller public class FrontController { diff --git a/src/main/java/com/example/marumaru_sparta_verspring/controller/MeetController.java b/src/main/java/com/example/marumaru_sparta_verspring/controller/MeetController.java index bcb02d6..37626b9 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/controller/MeetController.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/controller/MeetController.java @@ -8,13 +8,11 @@ import com.example.marumaru_sparta_verspring.security.UserDetailsImpl; import com.example.marumaru_sparta_verspring.service.MeetService; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.io.IOException; -import java.sql.SQLException; import java.util.List; @RequiredArgsConstructor @@ -41,13 +39,15 @@ public Meet showMeet(@PathVariable Long id) throws IOException { } @PutMapping("/meet/{id}") - public Meet update(@PathVariable Long id, @RequestBody MeetUpdateRequestDto meetUpdateRequestDto) { - return meetService.update(id, meetUpdateRequestDto); + public Meet update(@PathVariable Long id, @RequestBody MeetUpdateRequestDto meetUpdateRequestDto, @AuthenticationPrincipal UserDetailsImpl userDetails) { + Long userId = userDetails.getUser().getId(); + return meetService.update(id, meetUpdateRequestDto, userId); } @DeleteMapping("/meet/{id}") - public Long deleteMeet(@PathVariable Long id) throws IOException { - meetService.delete(id); + public Long deleteMeet(@PathVariable Long id, @AuthenticationPrincipal UserDetailsImpl userDetails) throws IOException { + Long userId = userDetails.getUser().getId(); + meetService.delete(id, userId); return id; } @@ -58,13 +58,15 @@ public MeetComment setMeetComment(@RequestBody MeetCommentRequestDto meetComment } @DeleteMapping("/meet/comment/{id}") - public void deleteComment(@PathVariable Long id) throws IOException { - meetService.deleteComment(id); + public void deleteComment(@PathVariable Long id, @AuthenticationPrincipal UserDetailsImpl userDetails) throws IOException { + Long userId = userDetails.getUser().getId(); + meetService.deleteComment(id, userId); } @PutMapping("/meet/comment") - public void updateComment(@RequestBody MeetCommentRequestDto meetCommentRequestDto) throws IOException { - meetService.updateComment(meetCommentRequestDto); + public void updateComment(@RequestBody MeetCommentRequestDto meetCommentRequestDto, @AuthenticationPrincipal UserDetailsImpl userDetails) throws IOException { + Long userId = userDetails.getUser().getId(); + meetService.updateComment(meetCommentRequestDto, userId); } } diff --git a/src/main/java/com/example/marumaru_sparta_verspring/service/MeetService.java b/src/main/java/com/example/marumaru_sparta_verspring/service/MeetService.java index edb4440..a68fc89 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/service/MeetService.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/service/MeetService.java @@ -43,7 +43,6 @@ public Meet saveMeet(MeetRequestDto meetRequestDto, Long userId) throws IOExcept meet.setContent(meetRequestDto.getContent()); meet.setAddress(meetRequestDto.getAddress()); meet.setDate(meetRequestDto.getDate()); - meetRepository.save(meet); return meet; } @@ -77,35 +76,56 @@ public MeetComment saveMeetComment(MeetCommentRequestDto meetCommentRequestDto, } @Transactional - public void delete(Long id) { + public void delete(Long id, Long userId) { Meet meet = meetRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다.")); - meetRepository.delete(meet); + if (meet.getUserId() != userId) { + throw new IllegalArgumentException("작성자만 삭제 가능"); + } else { + meetRepository.delete(meet); + } } @Transactional - public Meet update(Long id, MeetUpdateRequestDto meetUpdateRequestDto) { + public Meet update(Long id, MeetUpdateRequestDto meetUpdateRequestDto, Long userId) { Meet meet = meetRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다.")); - meet.setTitle(meetUpdateRequestDto.getTitle()); - meet.setContent(meetUpdateRequestDto.getContent()); - meetRepository.save(meet); - return meet; + + if (meet.getUserId() != userId) { + throw new IllegalArgumentException("작성자만 수정 가능"); + } else { + meet.setTitle(meetUpdateRequestDto.getTitle()); + meet.setContent(meetUpdateRequestDto.getContent()); + meetRepository.save(meet); + return meet; + } } @Transactional - public void deleteComment(Long id) { - meetCommentRepository.deleteById(id); + public void deleteComment(Long id, Long userId) { + MeetComment meetComment = meetCommentRepository.findById(id) + .orElseThrow(() -> + new IllegalArgumentException("해당 게시글이 없습니다.")); + if (meetComment.getUser().getId() != userId) { + throw new IllegalArgumentException("작성자만 삭제 가능"); + } else { + meetCommentRepository.deleteById(id); + } } @Transactional - public void updateComment(MeetCommentRequestDto meetCommentRequestDto) { + public void updateComment(MeetCommentRequestDto meetCommentRequestDto, Long userId) { MeetComment meetComment = meetCommentRepository.findById(meetCommentRequestDto.getIdx()).orElseThrow( () -> new NullPointerException("해당 아이디가 존재하지 않습니다.") ); - meetComment.setComment(meetCommentRequestDto.getComment()); - meetCommentRepository.save(meetComment); + + if (meetComment.getUser().getId() != userId) { + throw new IllegalArgumentException("작성자만 수정 가능"); + } else { + meetComment.setComment(meetCommentRequestDto.getComment()); + meetCommentRepository.save(meetComment); + } } } diff --git a/src/main/resources/static/js/meets/meet_detail.js b/src/main/resources/static/js/meets/meet_detail.js index 7c0aaa4..2c92293 100644 --- a/src/main/resources/static/js/meets/meet_detail.js +++ b/src/main/resources/static/js/meets/meet_detail.js @@ -95,8 +95,8 @@ function deleteMeet() { alert("삭제 성공!"); window.location.href = '/meets'; }, - error: (error) => { - console.log(error) + error: (request) => { + alert(request.responseJSON.message); } }) } @@ -152,7 +152,10 @@ function showComments(comments) { function saveComment() { const content = $('#comment_content'); const id = $('#idx').val(); - console.log(id) + if (!content.val().trim()) { + alert("내용은 필수입니다."); + return; + } const inputData = { idx: id, comment: content.val() @@ -200,8 +203,8 @@ function saveComment() { $('#comment_list').append(comment); // Todo : 내림차순 추가 }, - error: (error) => { - console.log(error); + error: (request) => { + alert(request.responseJSON.message); } }) @@ -227,7 +230,7 @@ function deleteComment(id) { window.location.reload(); }, error: function (request, status, error) { - console.log(error); + alert(request.responseJSON.message); } }) } @@ -250,7 +253,7 @@ function updateComment(id) { window.location.reload(); }, error: function (request, status, error) { - console.log(error); + alert(request.responseJSON.message); } }) } diff --git a/src/main/resources/static/js/meets/meet_detail_upload.js b/src/main/resources/static/js/meets/meet_detail_upload.js index 3698937..452f8a0 100644 --- a/src/main/resources/static/js/meets/meet_detail_upload.js +++ b/src/main/resources/static/js/meets/meet_detail_upload.js @@ -2,8 +2,20 @@ $(document).ready(function () { const curUrl = window.location.href.split('/'); const idx = curUrl[curUrl.length - 1]; showUpload(idx); + + + if (localStorage.getItem('token')) { + $.ajaxPrefilter(function (options, originalOptions, jqXHR) { + jqXHR.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('token')); + }); + } else { + alert('로그인을 해주세요') + location.replace('/user/login') + } }); + + $(function () { $("#datepicker").datepicker({ dateFormat: "yy-mm-dd", @@ -81,7 +93,7 @@ function saveUpload(idx) { window.location.href = `/meet/` + idx; }, error: function (request, status, error) { - alert(error); + alert(request.responseJSON.message); } }); } diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index bfcb79f..91cd7d8 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -52,6 +52,7 @@

404 Error!

페이지를 찾을 수 없습니다.

+

WEB에서만 동작하는 미니게임 입니다. SPACE를 눌러보세요!

diff --git a/src/main/resources/templates/meets/meet_detail.html b/src/main/resources/templates/meets/meet_detail.html index 6404d9c..de76f19 100644 --- a/src/main/resources/templates/meets/meet_detail.html +++ b/src/main/resources/templates/meets/meet_detail.html @@ -117,9 +117,6 @@
ID
  - diff --git a/src/main/resources/templates/meets/meet_list.html b/src/main/resources/templates/meets/meet_list.html index 1d7c967..68c7585 100644 --- a/src/main/resources/templates/meets/meet_list.html +++ b/src/main/resources/templates/meets/meet_list.html @@ -71,7 +71,7 @@ -