Skip to content

Commit

Permalink
Merge pull request #58 from codestates-seb/feat_BE_postResponse
Browse files Browse the repository at this point in the history
Merging from Feat be post response to Dev_Back
  • Loading branch information
Sangyoo authored Jan 19, 2023
2 parents 4edd032 + 629d1d7 commit b827494
Show file tree
Hide file tree
Showing 40 changed files with 365 additions and 392 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import com.main36.picha.domain.attraction.entity.Attraction;
import com.main36.picha.domain.attraction.mapper.AttractionMapper;
import com.main36.picha.domain.attraction.service.AttractionService;
import com.main36.picha.domain.attraction_file.entity.AttractionImage;
import com.main36.picha.domain.attraction_file.service.AttractionImageService;
import com.main36.picha.domain.attraction_likes.repository.AttractionLikesRepository;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.member.service.MemberService;
import com.main36.picha.global.auth.jwt.JwtTokenizer;
import com.main36.picha.global.config.S3Service;
import com.main36.picha.global.authorization.jwt.JwtTokenizer;
import com.main36.picha.global.response.DataResponseDto;
import com.main36.picha.global.response.MultiResponseDto;
import io.jsonwebtoken.Claims;
Expand All @@ -20,23 +17,13 @@
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/attractions")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.main36.picha.domain.attraction.dto;

import com.main36.picha.domain.post.dto.PostResponseDto;
import lombok.Builder;
import lombok.Data;

import java.util.List;

// 명소 상세 페이지
@Data
public class AttractionDetailResponseDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
import com.main36.picha.domain.attraction.dto.AttractionPostDto;
import com.main36.picha.domain.attraction.dto.AttractionResponseDto;
import com.main36.picha.domain.attraction.entity.Attraction;
import com.main36.picha.domain.post.dto.PostResponseDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring")
public interface AttractionMapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.main36.picha.domain.comment.controller;

import com.main36.picha.domain.comment.dto.CommentDto;
import com.main36.picha.domain.comment.dto.CommentPostDto;
import com.main36.picha.domain.comment.dto.CommentResponseDto;
import com.main36.picha.domain.comment.entity.Comment;
import com.main36.picha.domain.comment.mapper.CommentMapper;
import com.main36.picha.domain.comment.service.CommentService;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.member.service.MemberService;
import com.main36.picha.domain.post.service.PostService;
import com.main36.picha.global.auth.jwt.JwtTokenizer;
import com.main36.picha.global.authorization.jwt.JwtTokenizer;
import com.main36.picha.global.exception.BusinessLogicException;
import com.main36.picha.global.exception.ExceptionCode;
import com.main36.picha.global.response.DataResponseDto;
import com.main36.picha.global.response.MultiResponseDto;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -22,7 +21,7 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.util.List;

Expand All @@ -42,75 +41,74 @@ public class CommentController {

private final CommentMapper mapper;

@PostMapping("/upload/{post-id}")
public ResponseEntity postComment(HttpServletRequest request,
@PathVariable("post-id")@Positive long postId,
@RequestBody CommentDto postDto) {
Comment comment = new Comment();
// 멤버 넣기
String userEmail = extractedUsername(request);
System.out.println("userEmail is : "+ userEmail);
comment.setMember(memberService.findMember(userEmail));

// 글 넣기
comment.setPost(postService.findPost(postId));

// 댓글 내용 넣기
comment.setCommentContent(postDto.getCommentContent());
Comment createdComment = commentService.createComment(comment);
return new ResponseEntity<>(new DataResponseDto<>(
mapper.commentToCommentResponseDto(createdComment)), HttpStatus.CREATED);
}
@PostMapping("/upload/{member-id}/{post-id}")
public ResponseEntity<DataResponseDto<?>> postComment(@PathVariable("member-id") @Positive long memberId,
@PathVariable("post-id") @Positive long postId,
@RequestBody @Valid CommentPostDto commentPostDto) {

@PatchMapping("/edit/{comment-id}")
public ResponseEntity patchComment(HttpServletRequest request,
@PathVariable("comment-id")@Positive long commentId,
@RequestBody CommentDto postDto){
Comment.CommentBuilder commentBuilder = Comment.builder();

String userEmail = extractedUsername(request);
Comment findComment = commentService.findComment(commentId);
Comment comment =
commentService.createComment(
commentBuilder
.commentContent(commentPostDto.getCommentContent())
.member(memberService.findVerifiedMemberById(memberId))
.post(postService.findPost(postId))
.build()
);

// 다른 회원의 댓글을 수정할 수 없음
if(!findComment.getMember().getEmail().equals(userEmail)){
throw new BusinessLogicException(ExceptionCode.NOT_AUTHOR);
}
CommentResponseDto commentResponseDto = mapper.commentToCommentResponseDto(comment);

return new ResponseEntity<>(new DataResponseDto<>(commentResponseDto), HttpStatus.CREATED);
}

@PatchMapping("/edit/{member-id}/{comment-id}")
public ResponseEntity<DataResponseDto<?>> patchComment(@PathVariable("member-id") @Positive long memberId,
@PathVariable("comment-id") @Positive long commentId,
@RequestBody @Valid CommentPostDto commentPostDto) {
Comment comment = verifiedById(memberId, commentId);
comment.setCommentContent(commentPostDto.getCommentContent());

CommentResponseDto commentResponseDto = mapper.commentToCommentResponseDto(comment);

Comment comment = new Comment();
comment.setCommentId(commentId);
comment.setCommentContent(postDto.getCommentContent());
Comment updatedComment = commentService.updateComment(comment);
return new ResponseEntity<>(new DataResponseDto<>(mapper.commentToCommentResponseDto(updatedComment)),HttpStatus.OK);
return ResponseEntity.ok(new DataResponseDto<>(commentResponseDto));
}

@GetMapping("/{comment-id}")
public ResponseEntity getComment(@PathVariable("comment-id")@Positive long commentId){
public ResponseEntity<DataResponseDto<?>> getComment(@PathVariable("comment-id") @Positive long commentId) {
CommentResponseDto response =
mapper.commentToCommentResponseDto(commentService.findComment(commentId));
return new ResponseEntity<>(new DataResponseDto<>(response), HttpStatus.OK);
return ResponseEntity.ok(new DataResponseDto<>(response));
}

@GetMapping
public ResponseEntity getComment(@Positive @RequestParam(required = false, defaultValue = "1") int page,
@Positive @RequestParam(required = false, defaultValue = "10") int size){
Page<Comment> commentPage = commentService.findComments(page-1, size);
@GetMapping()
public ResponseEntity<MultiResponseDto<?>> getComment(@Positive @RequestParam(required = false, defaultValue = "1") int page,
@Positive @RequestParam(required = false, defaultValue = "10") int size) {
Page<Comment> commentPage = commentService.findComments(page - 1, size);
List<Comment> comments = commentPage.getContent();
return new ResponseEntity<>(new MultiResponseDto<>(
mapper.commentsToCommentResponseDtos(comments),commentPage), HttpStatus.OK);

List<CommentResponseDto> commentResponseDtos = mapper.commentsToCommentResponseDtos(comments);

return ResponseEntity.ok(new MultiResponseDto<>(commentResponseDtos, commentPage));
}

@DeleteMapping("/delete/{comment-id}")
public ResponseEntity deleteCommnet(@PathVariable("comment-id")@Positive long commentId){
@DeleteMapping("/delete/{member-id}/{comment-id}")
public ResponseEntity<HttpStatus> deleteComment(@PathVariable("member-id") @Positive long memberId,
@PathVariable("comment-id") @Positive long commentId) {
verifiedById(memberId, commentId);
commentService.deleteComment(commentId);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

private String extractedUsername(HttpServletRequest request) {
String authorization = request.getHeader("authorization");
String substring = authorization.substring(7);
log.info("subString = {}", substring);
String secretKey = jwtTokenizer.getSecretKey();
Jws<Claims> claims = jwtTokenizer.getClaims(substring, jwtTokenizer.encodeBase64SecretKey(secretKey));
private Comment verifiedById(long memberId, long commentId) {
Member member = memberService.findVerifiedMemberById(memberId);
Comment comment = commentService.findComment(commentId);

if (!comment.getMember().getMemberId().equals(memberId)) {
throw new BusinessLogicException(ExceptionCode.NOT_AUTHOR);
}

return String.valueOf(claims.getBody().get("username"));
return comment;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.main36.picha.domain.comment.dto;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;

import javax.validation.constraints.NotBlank;


@Data
public class CommentPostDto {

@NotBlank(message = "댓글 내용을 입력해주세요.")
private String commentContent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import lombok.*;

import javax.persistence.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class Comment extends Auditable {

Expand All @@ -27,6 +26,7 @@ public class Comment extends Auditable {
@JoinColumn(name = "member_id")
private Member member;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.main36.picha.domain.comment.mapper;

import com.main36.picha.domain.comment.dto.CommentPostDto;
import com.main36.picha.domain.comment.dto.CommentResponseDto;
import com.main36.picha.domain.comment.entity.Comment;
import org.mapstruct.Mapper;
Expand All @@ -9,6 +10,9 @@

@Mapper(componentModel = "spring")
public interface CommentMapper {

Comment CommentDtoToComment(CommentPostDto commentPostDto);

@Mapping(target = "username", source = "member.username")
@Mapping(target = "memberId", source = "member.memberId")
@Mapping(target = "memberPicture", source = "member.picture")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ public Comment createComment(Comment comment){
return commentRepository.save(comment);
}

public Comment updateComment(Comment comment){
Comment findComment = findVerifiedComment(comment.getCommentId());

// 댓글 내용만 변경 가능
Optional.ofNullable(comment.getCommentContent())
.ifPresent(findComment::setCommentContent);
return commentRepository.save(findComment);
}

public Comment findComment(long commentId){
return findVerifiedComment(commentId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@


import com.main36.picha.global.audit.Auditable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import javax.persistence.*;

@Getter
@Builder
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Entity
public class HashTag extends Auditable {
Expand Down
Loading

0 comments on commit b827494

Please sign in to comment.