Skip to content

Commit

Permalink
Merge pull request #28 from olmangjolmang/OMJM-46-mynote
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeyeonKong authored Jul 15, 2024
2 parents 27cba50 + e60fd63 commit bc2b0f5
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ticle.server.mypage.controller;

import com.ticle.server.global.dto.ResponseTemplate;
import com.ticle.server.mypage.dto.MyNoteDto;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name="TicleNote",description = "티클노트 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mypage")
public class NoteController {

private final MyPageService myPageService;

@Operation(summary="티클노트",description = "userId에 해당하는 note들을 불러옴")
@GetMapping("/my-note")
public ResponseEntity<ResponseTemplate<Object>> getMyNotes(@AuthenticationPrincipal CustomUserDetails customUserDetails){
Long userId = customUserDetails.getUserId();
List<MyNoteDto> myNoteDtos = myPageService.getMyNotes(userId);
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(myNoteDtos));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import com.ticle.server.global.dto.ResponseTemplate;
import com.ticle.server.mypage.dto.MyQuestionDto;
import com.ticle.server.mypage.dto.UpdateCommentDto;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.jwt.JwtTokenProvider;
import com.ticle.server.user.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -25,7 +23,7 @@
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/mypage")
public class MyQuestionController {
public class QuestionController {

private final MyPageService myPageService;
private final JwtTokenProvider jwtTokenProvider;
Expand All @@ -43,7 +41,6 @@ public ResponseEntity<ResponseTemplate<Object>> getMyQuestions(@AuthenticationPr

List<MyQuestionDto> myQuestionDtos;
myQuestionDtos = myPageService.getMyQuestions(userId);


return ResponseEntity
.status(HttpStatus.OK)
Expand Down

This file was deleted.

This file was deleted.

23 changes: 15 additions & 8 deletions src/main/java/com/ticle/server/mypage/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,42 @@
import com.ticle.server.mypage.dto.MyQuestionDto;
import com.ticle.server.mypage.dto.SavedTicleDto;
import com.ticle.server.mypage.repository.NoteRepository;
import com.ticle.server.mypage.repository.QuestionRepository;
import com.ticle.server.mypage.repository.ScrapRepository;
import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.repository.CommentRepository;
import com.ticle.server.opinion.repository.OpinionRepository;
import com.ticle.server.post.repository.PostRepository;
import com.ticle.server.scrapped.domain.Scrapped;
import com.ticle.server.opinion.domain.Opinion;
import com.ticle.server.scrapped.repository.ScrappedRepository;
import com.ticle.server.user.domain.User;
import com.ticle.server.user.domain.type.Category;
import com.ticle.server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static java.util.stream.Collectors.toList;

@Service
@RequiredArgsConstructor
public class MyPageService {
private final ScrapRepository scrapRepository;
private final QuestionRepository questionRepository;
private final UserRepository userRepository;
private final ScrappedRepository scrappedRepository;
private final OpinionRepository opinionRepository;
private final NoteRepository noteRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final int SIZE = 9;

public List<SavedTicleDto> getSavedArticles(Long userId,Pageable pageable) {
// Pageable pageable = PageRequest.of(page-1,SIZE);
Page<Scrapped> scraps = scrapRepository.findByUserId(userId,pageable);
Page<Scrapped> scraps = scrappedRepository.findByUserId(userId,pageable);

return scraps.stream()
.map(scrap -> postRepository.findById(scrap.getPost().getPostId()).orElse(null))
Expand All @@ -47,7 +51,7 @@ public List<SavedTicleDto> getSavedArticles(Long userId,Pageable pageable) {

public List<SavedTicleDto> getSavedArticlesByCategory(Long userId, Category category,Pageable pageable) {
// Pageable pageable = PageRequest.of(page-1,SIZE);
Page<Scrapped> scraps = scrapRepository.findByUserIdAndPostCategory(userId, category,pageable);
Page<Scrapped> scraps = scrappedRepository.findByUserIdAndPostCategory(userId, category,pageable);

return scraps.stream()
.map(scrap -> postRepository.findById(scrap.getPost().getPostId()).orElse(null))
Expand All @@ -58,15 +62,18 @@ public List<SavedTicleDto> getSavedArticlesByCategory(Long userId, Category cate


public List<MyQuestionDto> getMyQuestions(Long userId) {
List<Opinion> questions = questionRepository.findByUserId(userId);
List<Opinion> questions = opinionRepository.findByUserId(userId);
return questions.stream()
.map(MyQuestionDto::toDto)
.collect(toList());
}

@Transactional
public void updateComment(Long userId, Long opinionId, String newContent) {
Comment comment = commentRepository.findByUserIdAndOpinionId(userId, opinionId);
Optional<User> user = userRepository.findById(userId);
Optional<Opinion> opinion = opinionRepository.findByOpinionIdWithFetch(opinionId);
Comment comment = commentRepository.findByUserAndOpinion(user.get(), opinion.get());




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.domain.Opinion;
import com.ticle.server.user.domain.User;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {

@EntityGraph(attributePaths = {"hearts", "user"})
List<Comment> findAllByOpinion(Opinion opinion, Sort sort);

Comment findByUserIdAndOpinionId(Long userId, Long opinionId);
Comment findByUserAndOpinion(User user,Opinion opinion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -20,4 +21,8 @@ public interface OpinionRepository extends JpaRepository<Opinion, Long> {

@EntityGraph(attributePaths = {"comments", "comments.user"})
Page<Opinion> findAll(Pageable pageable);

List<Opinion> findByUserId(Long userId);

// Optional<Opinion> findByOpinionId(Long opinionId);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.ticle.server.scrapped.repository;

import com.ticle.server.scrapped.domain.Scrapped;
import com.ticle.server.user.domain.type.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.Optional;


public interface ScrappedRepository extends JpaRepository<Scrapped, Long> {
Optional<Scrapped> findByUserIdAndPost_PostId(Long userId, Long postId);

Page<Scrapped> findByUserId(Long userId, Pageable pageable);
Page<Scrapped> findByUserIdAndPostCategory(@Param("userid") Long userId, @Param("category") Category category, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface UserRepository extends JpaRepository<User,Long> {
boolean existsByEmail(String email);
boolean existsBynickName(String nickName);
Optional<User> findByEmail(String email);

Optional<User> findById(Long id);
}

0 comments on commit bc2b0f5

Please sign in to comment.