Skip to content

Commit

Permalink
Merge pull request #111 from dongkyun0713/dongkyun
Browse files Browse the repository at this point in the history
사용자가 작성한 게시글, 답글 보는 api
  • Loading branch information
dongkyun0713 authored Feb 27, 2024
2 parents b3ed497 + 8876624 commit 50e728b
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -18,6 +19,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -160,4 +162,32 @@ public ResponseEntity<String> userLevelUp(@RequestParam Long userId) {
experienceService.levelUp(userId);
return ResponseEntity.ok("사용자 레벨업이 완료되었습니다");
}

@GetMapping("/posts/{userId}")
@Operation(
summary = "사용자 작성 글 보기",
description = "사용자가 작성한 게시글을 볼 수 있습니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<List<Object>> getUserPosts(@PathVariable Long userId) {
List<Object> userPosts = userService.userPostsView(userId);
return new ResponseEntity<>(userPosts, HttpStatus.OK);
}

@GetMapping("/answers/{userId}")
@Operation(
summary = "사용자 작성 답글 보기",
description = "사용자가 작성한 답들을 볼 수 있습니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<List<Object>> getUserAnswers(@PathVariable Long userId) {
List<Object> userAnswers = userService.userAnswerView(userId);
return new ResponseEntity<>(userAnswers, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.example.titto_backend.auth.dto.response;

import com.example.titto_backend.auth.domain.User;
import com.example.titto_backend.matchingBoard.domain.matchingBoard.MatchingPost;
import com.example.titto_backend.questionBoard.domain.Answer;
import com.example.titto_backend.questionBoard.domain.Question;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -28,12 +24,8 @@ public class UserProfileViewDto {
private Integer countAnswer;
private Integer countAccept;
private Integer level;
private List<MatchingPost> matchingPosts;
private List<Question> questions;
private List<Answer> answers;

public static UserProfileViewDto of(User user, List<MatchingPost> matchingPosts, List<Question> questions,
List<Answer> answers) {
public static UserProfileViewDto of(User user) {
return new UserProfileViewDto(
user.getProfile(),
user.getName(),
Expand All @@ -47,10 +39,7 @@ public static UserProfileViewDto of(User user, List<MatchingPost> matchingPosts,
user.getCurrentExperience(),
user.getCountAnswer(),
user.getCountAccept(),
user.getLevel(),
matchingPosts,
questions,
answers
user.getLevel()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import com.example.titto_backend.auth.repository.UserRepository;
import com.example.titto_backend.common.exception.CustomException;
import com.example.titto_backend.common.exception.ErrorCode;
import com.example.titto_backend.matchingBoard.domain.matchingBoard.MatchingPost;
import com.example.titto_backend.matchingBoard.repository.matchingBoard.MatchingPostRepository;
import com.example.titto_backend.questionBoard.domain.Answer;
import com.example.titto_backend.questionBoard.domain.Question;
import com.example.titto_backend.questionBoard.repository.AnswerRepository;
import com.example.titto_backend.questionBoard.repository.QuestionRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,10 +72,31 @@ public void updateUserProfile(String email, UserProfileUpdateDTO userProfileUpda
public UserProfileViewDto userProfileView(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
List<MatchingPost> matchingPosts = matchingPostRepository.findMatchingPostByUser(user);
List<Question> questions = questionRepository.findQuestionByAuthor(user);
return UserProfileViewDto.of(user);
}

// 유저 작성 글 보기
public List<Object> userPostsView(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Object> userPosts = new ArrayList<>(matchingPostRepository.findMatchingPostByUser(user));

userPosts.addAll(questionRepository.findQuestionByAuthor(user)
.stream()
.toList());

return userPosts;
}

// 유저 작성 답글 보기
public List<Object> userAnswerView(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Answer> answers = answerRepository.findAnswerByAuthor(user);
return UserProfileViewDto.of(user, matchingPosts, questions, answers);

return new ArrayList<>(answers);
}

//닉네임 중복 여부
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public ResponseEntity<MatchingPostUpdateResponseDto> updateMatchingPost(@PathVar
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostDeleteResponseDto> deleteMatchingPostByMatchingPostId(
@PathVariable Long matchingPostId) {
@PathVariable Long matchingPostId,
Principal principal) {
MatchingPostDeleteResponseDto responseDto = matchingPostService.deleteMatchingPostByMatchingPostId(
matchingPostId);
matchingPostId, principal);
return new ResponseEntity<>(responseDto, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.example.titto_backend.matchingBoard.dto.request.matchingPostReviewRequest.MatchingPostReviewCreateRequestDto;
import com.example.titto_backend.matchingBoard.dto.request.matchingPostReviewRequest.MatchingPostReviewDeleteRequestDto;
import com.example.titto_backend.matchingBoard.dto.request.matchingPostReviewRequest.MatchingPostReviewUpdateRequestDto;
import com.example.titto_backend.matchingBoard.dto.response.matchingPostReviewResponse.MatchingPostReviewCreateResponseDto;
import com.example.titto_backend.matchingBoard.dto.response.matchingPostReviewResponse.MatchingPostReviewDeleteResponseDto;
Expand All @@ -11,81 +12,90 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.security.Principal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/matching-board-review")
@RequiredArgsConstructor
@Tag(name = "Matching Post Review Controller", description = "매칭 게시글 리뷰 관련 API")
public class MatchingPostReviewController {

private final MatchingPostReviewService matchingBoardReviewService;
private final MatchingPostReviewService matchingBoardReviewService;

@PostMapping("/create")
@Operation(
summary = "매칭 게시글 리뷰 작성",
description = "매칭 게시글에 리뷰를 작성합니다",
responses = {
@ApiResponse(responseCode = "201", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewCreateResponseDto> createReview(Principal principal,
@RequestBody MatchingPostReviewCreateRequestDto matchingPostReviewCreateRequestDto) {
MatchingPostReviewCreateResponseDto responseDto = matchingBoardReviewService.createReview(principal,
matchingPostReviewCreateRequestDto);
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}
@PostMapping("/create")
@Operation(
summary = "매칭 게시글 리뷰 작성",
description = "매칭 게시글에 리뷰를 작성합니다",
responses = {
@ApiResponse(responseCode = "201", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewCreateResponseDto> createReview(Principal principal,
@RequestBody MatchingPostReviewCreateRequestDto matchingPostReviewCreateRequestDto) {
MatchingPostReviewCreateResponseDto responseDto = matchingBoardReviewService.createReview(principal,
matchingPostReviewCreateRequestDto);
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}

@GetMapping("/get/{postId}")
@Operation(
summary = "매칭 게시글 리뷰 전체 조회",
description = "특정 매칭 게시글에 대한 전체 리뷰를 조회합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<List<MatchingPostReviewResponseDto>> getAllMatchingBoardReviewsByPostId(
@PathVariable Long postId) {
List<MatchingPostReviewResponseDto> responseDtoList = matchingBoardReviewService.getAllMatchingBoardReviewsByPostId(
postId);
return ResponseEntity.ok(responseDtoList);
}
@GetMapping("/get/{postId}")
@Operation(
summary = "매칭 게시글 리뷰 전체 조회",
description = "특정 매칭 게시글에 대한 전체 리뷰를 조회합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<List<MatchingPostReviewResponseDto>> getAllMatchingBoardReviewsByPostId(
@PathVariable Long postId) {
List<MatchingPostReviewResponseDto> responseDtoList = matchingBoardReviewService.getAllMatchingBoardReviewsByPostId(
postId);
return ResponseEntity.ok(responseDtoList);
}

@PutMapping("/update/{reviewId}")
@Operation(
summary = "매칭 게시글 리뷰 수정",
description = "매칭 게시글에 작성된 리뷰를 수정합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewUpdateResponseDto> updateReview(Principal principal,
@RequestBody MatchingPostReviewUpdateRequestDto matchingPostReviewUpdateRequestDto) {
MatchingPostReviewUpdateResponseDto responseDto = matchingBoardReviewService.updateReview(principal,
matchingPostReviewUpdateRequestDto);
return ResponseEntity.ok(responseDto);
}
@PutMapping("/update/{reviewId}")
@Operation(
summary = "매칭 게시글 리뷰 수정",
description = "매칭 게시글에 작성된 리뷰를 수정합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewUpdateResponseDto> updateReview(Principal principal,
@RequestBody MatchingPostReviewUpdateRequestDto matchingPostReviewUpdateRequestDto) {
MatchingPostReviewUpdateResponseDto responseDto = matchingBoardReviewService.updateReview(principal,
matchingPostReviewUpdateRequestDto);
return ResponseEntity.ok(responseDto);
}

@DeleteMapping("/delete/{reviewId}")
@Operation(
summary = "매칭 게시글 리뷰 삭제",
description = "매칭 게시글에 작성된 리뷰를 삭제합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewDeleteResponseDto> deleteReviewByReviewId(@PathVariable Long reviewId) {
MatchingPostReviewDeleteResponseDto responseDto = matchingBoardReviewService.deleteReviewByReviewId(reviewId);
return ResponseEntity.ok(responseDto);
}
@DeleteMapping("/delete/{reviewId}")
@Operation(
summary = "매칭 게시글 리뷰 삭제",
description = "매칭 게시글에 작성된 리뷰를 삭제합니다",
responses = {
@ApiResponse(responseCode = "200", description = "요청 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "관리자 문의")
})
public ResponseEntity<MatchingPostReviewDeleteResponseDto> deleteReviewByReviewId(
@RequestBody MatchingPostReviewDeleteRequestDto matchingPostReviewDeleteRequestDto,
Principal principal) {
MatchingPostReviewDeleteResponseDto responseDto = matchingBoardReviewService.deleteReviewByReviewId(
matchingPostReviewDeleteRequestDto, principal);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class MatchingPost extends BaseEntity {
private MatchingBoard matchingBoard;

// 카테고리(멘토, 멘티, 한솥밥, 스터디)
// @Convert(converter = CategoryToIntegerConverter.class)

@Enumerated(EnumType.STRING)
private Category category;

Expand All @@ -70,8 +68,9 @@ public class MatchingPost extends BaseEntity {
private Integer viewCount;

// 댓글 수
@Setter
@Column(name = "review_count", columnDefinition = "integer default 0")
private Integer review_count;
private Integer reviewCount;

public void updateViewCount() {
this.viewCount++;
Expand All @@ -80,7 +79,7 @@ public void updateViewCount() {
@PrePersist
public void prePersist() {
this.viewCount = this.viewCount == null ? 0 : this.viewCount;
this.review_count = this.review_count == null ? 0 : this.review_count;
this.reviewCount = this.reviewCount == null ? 0 : this.reviewCount;
}

public void update(Category category, String title, String content, Status status) {
Expand All @@ -89,4 +88,16 @@ public void update(Category category, String title, String content, Status statu
this.content = content;
this.status = status;
}

public void increaseReviewCount() {
this.reviewCount++;
}

// 댓글 수를 감소시키는 메소드
public void decreaseReviewCount() {
if (this.reviewCount != null && this.reviewCount > 0) {
this.reviewCount--;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.titto_backend.matchingBoard.dto.request.matchingPostReviewRequest;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MatchingPostReviewDeleteRequestDto {
@NotNull
private Long reviewId;
@NotNull
private Long postId;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.titto_backend.matchingBoard.dto.response.matchingPostResponse;

import com.example.titto_backend.matchingBoard.domain.matchingBoard.MatchingPost;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
@Getter
@AllArgsConstructor
@NoArgsConstructor
Expand Down Expand Up @@ -34,7 +34,7 @@ public static MatchingPostCreateResponseDto of(
matchingPost.getTitle(),
matchingPost.getContent(),
matchingPost.getViewCount(),
matchingPost.getReview_count(),
matchingPost.getReviewCount(),
matchingPost.getCreateDate());
}
}
Expand Down
Loading

0 comments on commit 50e728b

Please sign in to comment.