Skip to content

Commit

Permalink
Merge pull request #133 from sendkite/sendkite
Browse files Browse the repository at this point in the history
이벤트 글 삭제, 수정, 댓글 권한부여
  • Loading branch information
sendkite authored Dec 19, 2021
2 parents 7561cbb + 93401b9 commit ca79008
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public ResponseEntity<ErrorResponse> errorHandler(BindException e) {
new ErrorResponse(e.getAllErrors().get(0).getDefaultMessage(), 400)
);
}

@ExceptionHandler(value = {IllegalArgumentException.class})
public ResponseEntity<ErrorResponse> errorHandler(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(
new ErrorResponse(e.getMessage(), 400)
);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
17 changes: 10 additions & 7 deletions src/main/resources/static/js/meets/meet_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ function deleteMeet() {
alert("삭제 성공!");
window.location.href = '/meets';
},
error: (error) => {
console.log(error)
error: (request) => {
alert(request.responseJSON.message);
}
})
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -200,8 +203,8 @@ function saveComment() {
$('#comment_list').append(comment);
// Todo : 내림차순 추가
},
error: (error) => {
console.log(error);
error: (request) => {
alert(request.responseJSON.message);
}

})
Expand All @@ -227,7 +230,7 @@ function deleteComment(id) {
window.location.reload();
},
error: function (request, status, error) {
console.log(error);
alert(request.responseJSON.message);
}
})
}
Expand All @@ -250,7 +253,7 @@ function updateComment(id) {
window.location.reload();
},
error: function (request, status, error) {
console.log(error);
alert(request.responseJSON.message);
}
})
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/static/js/meets/meet_detail_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -81,7 +93,7 @@ function saveUpload(idx) {
window.location.href = `/meet/` + idx;
},
error: function (request, status, error) {
alert(error);
alert(request.responseJSON.message);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<div class="col-lg-12">
<h1>404 Error!</h1>
<h2>페이지를 찾을 수 없습니다.</h2>
<h2>WEB에서만 동작하는 미니게임 입니다. SPACE를 눌러보세요!</h2>

<button onclick="location.href='/'" class="btn btn-primary">돌아가기</button>
</div>
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/templates/meets/meet_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ <h6 class="card-subtitle mb-2 text-muted">ID</h6>
<i class="fa fa-trash" aria-hidden="true"> 삭제</i>
</button>
&nbsp;
<button type="button" class="btn btn-danger">
<i class="far fa-thumbs-up"> 좋아요0</i>
</button>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/meets/meet_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</ul>
</li>
</ul>
<button id="login-button" onclick="location.href='/login'" type="button" class="btn btn-success">로그인
<button id="login-button" onclick="location.href='/user/login'" type="button" class="btn btn-success">로그인
</button>
<div onclick="get_card()"><img id="user-profile" class="rounded-circle"
src="/img/profile_placeholder.png" width="50px"
Expand Down

0 comments on commit ca79008

Please sign in to comment.