Skip to content

Commit

Permalink
Merge pull request #32 from olmangjolmang/OMJM-46-mynote
Browse files Browse the repository at this point in the history
마이페이지-티클노트 수정 삭제 기능 추가
  • Loading branch information
lej8924 authored Jul 18, 2024
2 parents 1db05a7 + 166558a commit 5f63d2c
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/ticle/server/ServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class ServerApplication {

public static void main(String[] args) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/ticle/server/memo/domain/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ public class Memo extends BaseTimeEntity {
@JoinColumn(name = "post_id")
private Post post;

public void updateNote(String content){
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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.dto.response.NoteResponse;
import com.ticle.server.mypage.dto.request.NoteUpdateRequest;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -10,10 +11,7 @@
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 org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -25,13 +23,31 @@ public class NoteController {

private final MyPageService myPageService;

@Operation(summary="티클노트",description = "userId에 해당하는 note들을 불러옴")
@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);
List<NoteResponse> noteResponses = myPageService.getMyNotes(userId);
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(myNoteDtos));
.body(ResponseTemplate.from(noteResponses));
}

@Operation(summary="티클노트 수정",description = "noteId에 해당하는 note들을 수정함")
@PutMapping("/my-note/{id}")
public ResponseEntity<ResponseTemplate<Object>> updateMyNotes(@AuthenticationPrincipal CustomUserDetails customUserDetails,@PathVariable("id") Long id, @RequestBody NoteUpdateRequest noteUpdateRequest){
myPageService.updateNote(customUserDetails,id,noteUpdateRequest);
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(id + "번 노트가 수정되었습니다."));
}

@Operation(summary="티클노트 삭제",description = "noteId에 해당하는 note들을 삭제함")
@DeleteMapping("/my-note/{id}")
public ResponseEntity<ResponseTemplate<Object>> deleteMyNotes(@AuthenticationPrincipal CustomUserDetails customUserDetails, @PathVariable("id") Long id){
myPageService.deleteNote(customUserDetails,id);
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(id + "번 노트가 삭제되었습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ticle.server.mypage.controller;

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.dto.response.QuestionResponse;
import com.ticle.server.mypage.dto.request.CommentUpdateRequest;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.jwt.JwtTokenProvider;
import com.ticle.server.user.service.CustomUserDetails;
Expand Down Expand Up @@ -32,31 +32,28 @@ public class QuestionController {
@GetMapping("/my-question")
public ResponseEntity<ResponseTemplate<Object>> getMyQuestions(@AuthenticationPrincipal CustomUserDetails customUserDetails){

// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// if (authentication == null || !authentication.isAuthenticated()) {
// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ResponseTemplate.EMPTY_RESPONSE);
// }

Long userId = customUserDetails.getUserId();

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

return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(myQuestionDtos));
.body(ResponseTemplate.from(questionResponses));

}
@Operation(summary = "마이물어봥 수정",description = "question_id에서 수정하기")
@PutMapping("/my-question/{id}")
public ResponseEntity<ResponseTemplate<Object>> updateQuestion(@AuthenticationPrincipal CustomUserDetails customUserDetails,@PathVariable("id") Long questionId, @RequestBody UpdateCommentDto updateCommentDto) {
public ResponseEntity<ResponseTemplate<Object>> updateQuestion(@AuthenticationPrincipal CustomUserDetails customUserDetails,@PathVariable("id") Long questionId, @RequestBody CommentUpdateRequest commentUpdateRequest) {
Long userId = customUserDetails.getUserId();
myPageService.updateComment(userId, questionId, updateCommentDto.getContent());
myPageService.updateComment(userId, questionId, commentUpdateRequest.getContent());

return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from("질문이 성공적으로 수정되었습니다."));
}




}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.ticle.server.mypage.controller;

import com.ticle.server.global.dto.ResponseTemplate;
import com.ticle.server.mypage.dto.SavedTicleDto;
import com.ticle.server.mypage.dto.response.SavedTicleResponse;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.domain.type.Category;
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.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
Expand All @@ -35,16 +33,16 @@ public class SavedTicleController {
@GetMapping("/saved-ticles")
public ResponseEntity<ResponseTemplate<Object>> getSavedTicles(@AuthenticationPrincipal CustomUserDetails customUserDetails, @RequestParam(value = "category",required = false) Category category, @PageableDefault(page=0,size=9,sort="id",direction = Sort.Direction.DESC)Pageable pageable){
Long userId = customUserDetails.getUserId();
List<SavedTicleDto> savedTicleDtos;
List<SavedTicleResponse> savedTicleResponses;

if(category != null){
savedTicleDtos = myPageService.getSavedArticlesByCategory(userId,category,pageable);
savedTicleResponses = myPageService.getSavedArticlesByCategory(userId,category,pageable);
}else{
savedTicleDtos = myPageService.getSavedArticles(userId,pageable);
savedTicleResponses = myPageService.getSavedArticles(userId,pageable);
}
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseTemplate.from(savedTicleDtos));
.body(ResponseTemplate.from(savedTicleResponses));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ticle.server.mypage.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CommentUpdateRequest {
private String content;

@Builder
public CommentUpdateRequest(String content){
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.ticle.server.mypage.dto;
package com.ticle.server.mypage.dto.request;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class UpdateCommentDto {
public class NoteUpdateRequest {

private String content;

@Builder
public UpdateCommentDto(String content){
public NoteUpdateRequest(String content){
this.content = content;
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.ticle.server.mypage.dto;
package com.ticle.server.mypage.dto.response;

import com.ticle.server.memo.domain.Memo;
import com.ticle.server.opinion.domain.Opinion;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@AllArgsConstructor
@Builder
public class MyNoteDto {
public class NoteResponse {
private Long noteId;
private String content;
private LocalDateTime memoDate;
private Long postId;
private String postTitle;

public static MyNoteDto toDto(Memo memo){
return MyNoteDto.builder()
public static NoteResponse toDto(Memo memo){
return NoteResponse.builder()
.noteId(memo.getMemoId())
.content(memo.getContent())
.memoDate(memo.getCreatedDate())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.mypage.dto;
package com.ticle.server.mypage.dto.response;

import com.ticle.server.opinion.domain.Opinion;
import lombok.AllArgsConstructor;
Expand All @@ -8,14 +8,14 @@
@Getter
@AllArgsConstructor
@Builder
public class MyQuestionDto {
public class QuestionResponse {
private Long questionId;
private String question;
private Long viewCount;
private Long commentCount;

public static MyQuestionDto toDto(Opinion opinion){
return MyQuestionDto.builder()
public static QuestionResponse toDto(Opinion opinion){
return QuestionResponse.builder()
.questionId(opinion.getOpinionId())
.question(opinion.getQuestion())
.viewCount(opinion.getViewCount())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.mypage.dto;
package com.ticle.server.mypage.dto.response;

import com.ticle.server.global.domain.S3Info;
import com.ticle.server.post.domain.Post;
Expand All @@ -10,7 +10,7 @@
@Getter
@AllArgsConstructor
@Builder
public class SavedTicleDto {
public class SavedTicleResponse {
private Long postId;
private String title;
private String content;
Expand All @@ -19,8 +19,8 @@ public class SavedTicleDto {
private Category postCategory;
private S3Info image;

public static SavedTicleDto toDto(Post post) {
return SavedTicleDto.builder()
public static SavedTicleResponse toDto(Post post) {
return SavedTicleResponse.builder()
.postId(post.getPostId())
.title(post.getTitle())
.content(post.getContent())
Expand Down
53 changes: 42 additions & 11 deletions src/main/java/com/ticle/server/mypage/service/MyPageService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.ticle.server.mypage.service;

import com.ticle.server.memo.domain.Memo;
import com.ticle.server.mypage.dto.MyNoteDto;
import com.ticle.server.mypage.dto.MyQuestionDto;
import com.ticle.server.mypage.dto.SavedTicleDto;
import com.ticle.server.mypage.dto.request.NoteUpdateRequest;
import com.ticle.server.mypage.dto.response.NoteResponse;
import com.ticle.server.mypage.dto.response.QuestionResponse;
import com.ticle.server.mypage.dto.response.SavedTicleResponse;
import com.ticle.server.mypage.repository.NoteRepository;
import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.repository.CommentRepository;
Expand All @@ -15,6 +16,7 @@
import com.ticle.server.user.domain.User;
import com.ticle.server.user.domain.type.Category;
import com.ticle.server.user.repository.UserRepository;
import com.ticle.server.user.service.CustomUserDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -38,33 +40,35 @@ public class MyPageService {
private final CommentRepository commentRepository;
private final int SIZE = 9;

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

return scraps.stream()
.map(scrap -> postRepository.findById(scrap.getPost().getPostId()).orElse(null))
.filter(post -> post != null)
.map(SavedTicleDto::toDto)
.map(SavedTicleResponse::toDto)
.collect(Collectors.toList());
}

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

return scraps.stream()
.map(scrap -> postRepository.findById(scrap.getPost().getPostId()).orElse(null))
.filter(post -> post != null)
.map(SavedTicleDto::toDto)
.map(SavedTicleResponse::toDto)
.collect(Collectors.toList());
}


public List<MyQuestionDto> getMyQuestions(Long userId) {
//////////////////////////////////////////////티클문답///////////////////////////////////////////////////////////////

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

Expand All @@ -79,11 +83,38 @@ public void updateComment(Long userId, Long opinionId, String newContent) {

}

public List<MyNoteDto> getMyNotes(Long userId) {
//////////////////////////////////////////////티클노트///////////////////////////////////////////////////////////////

public List<NoteResponse> getMyNotes(Long userId) {
List<Memo> memos = noteRepository.findByUserId(userId);
return memos.stream()
.map(MyNoteDto::toDto)
.map(NoteResponse::toDto)
.collect(toList());
}

@Transactional
public void updateNote(CustomUserDetails customUserDetails, Long noteId, NoteUpdateRequest noteUpdateRequest){
Memo memo = noteRepository.findById(noteId)
.orElseThrow(() -> new RuntimeException("Memo not found"));

if (!memo.getUser().getId().equals(customUserDetails.getUserId())) {
throw new RuntimeException("You do not have permission to edit this memo");
}

memo.updateNote(noteUpdateRequest.getContent());
noteRepository.save(memo);
}

@Transactional
public void deleteNote(CustomUserDetails customUserDetails, Long noteId) {
Memo memo = noteRepository.findById(noteId)
.orElseThrow(() -> new RuntimeException("Memo not found"));

if (!memo.getUser().getId().equals(customUserDetails.getUserId())) {
throw new RuntimeException("You do not have permission to delete this memo");
}

noteRepository.delete(memo);
}

}
Loading

0 comments on commit 5f63d2c

Please sign in to comment.