Skip to content

Commit

Permalink
feat: overall refactoring #1
Browse files Browse the repository at this point in the history
  • Loading branch information
wwingyou committed Sep 22, 2024
1 parent d80ebd7 commit c4a5378
Show file tree
Hide file tree
Showing 42 changed files with 998 additions and 855 deletions.
56 changes: 56 additions & 0 deletions src/main/java/kr/tgwing/tech/admin/service/AdminService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kr.tgwing.tech.admin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kr.tgwing.tech.admin.dto.AdminCheckUserDto;
import kr.tgwing.tech.user.entity.TempUser;
import kr.tgwing.tech.user.entity.User;
import kr.tgwing.tech.user.exception.UserNotFoundException;
import kr.tgwing.tech.user.repository.TempUserRepository;
import kr.tgwing.tech.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class AdminService {

private final UserRepository userRepository;
private final TempUserRepository tempUserRepository;

public List<AdminCheckUserDto> checkUser() {
List<TempUser> allUser = tempUserRepository.findAll();
if(allUser.isEmpty()) return null; // 여기 값은 예외로 보내야하느건지, 어떻게 보내야하는거지?

List<AdminCheckUserDto> dtoList = new ArrayList<>();
for(TempUser user : allUser) {
AdminCheckUserDto dto = user.toAdminCheckUserDto(user);
dtoList.add(dto);
}

return dtoList;
}

@Transactional
public Long registerUsers(Long id) {
TempUser notUser = tempUserRepository.findById(id).orElseThrow(UserNotFoundException::new);
User user = notUser.toUser(notUser);
tempUserRepository.deleteById(id);

user.setRole("ROLE_USER");
userRepository.save(user);

return user.getStudentId();
}

public Long refuseUsers(Long id) {
TempUser user = tempUserRepository.findById(id).orElseThrow(UserNotFoundException::new);
userRepository.deleteById(user.getStudentId());

return user.getStudentId();
}
}
186 changes: 132 additions & 54 deletions src/main/java/kr/tgwing/tech/blog/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,163 @@
package kr.tgwing.tech.blog.controller;

import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import kr.tgwing.tech.blog.exception.post.PathHasNoPostIdException;
import kr.tgwing.tech.blog.service.PostServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.security.Principal;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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;

import java.security.Principal;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import kr.tgwing.tech.blog.dto.CommentForm;
import kr.tgwing.tech.blog.dto.CommentView;
import kr.tgwing.tech.blog.dto.PostDetail;
import kr.tgwing.tech.blog.dto.PostForm;
import kr.tgwing.tech.blog.dto.PostOverview;
import kr.tgwing.tech.blog.dto.PostQuery;
import kr.tgwing.tech.blog.dto.ReplyForm;
import kr.tgwing.tech.blog.dto.ReplyView;
import kr.tgwing.tech.blog.service.PostService;

@RestController
@RequestMapping("/post")
@RequiredArgsConstructor
@Slf4j
public class PostController {

private final PostServiceImpl postService;

@GetMapping("/blog") // 블로그 전체 가져오기 - GET, /api/blog
public ResponseEntity<Page> getAllPostswithSearch(@RequestParam(value = "text", required = false) String text,
@PageableDefault Pageable pageable) {
log.info("-- Retrieve All of Posts --");
private final PostService postService;

Page<PostDto> postsInPage = postService.getPostsInPage(text, pageable);

return ResponseEntity.ok(postsInPage);
@GetMapping // 블로그 전체 가져오기 - GET, /api/blog
public Page<PostOverview> getAllPostswithSearch(
@ModelAttribute PostQuery query,
@PageableDefault Pageable pageable
) {
return postService.getPostOverviews(query, pageable);
}

@GetMapping("/blog/{postId}") // 특정 블로그 가져오기 - GET, /api/blog/{postId}
public ResponseEntity<PostDto> getPost(@PathVariable(required = false, name = "postId") Optional<Long> optional) {
log.info("-- Retreive Specific Post by Id --");
// 특정 게시글 id에 대한 post 정보를 모아 반환
@GetMapping("{postId}") // 특정 블로그 가져오기 - GET, /api/blog/{postId}
public PostDetail getPost(@PathVariable Long postId) {
return postService.getPost(postId);
}

Long postId = optional.orElseThrow(PathHasNoPostIdException::new);
@PostMapping // 블로그 작성 - POST, /api/blog
public PostDetail createPost(
@RequestBody PostForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.createPost(form, studentId);
}

PostDto post = postService.getPost(postId);
return ResponseEntity.ok(post);
// @CrossOrigin
@PutMapping("{postId}") // 블로그 수정 - PUT, /api/blog/{postId}
public PostDetail updatePost(
@PathVariable Long postId,
@RequestBody PostForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.updatePost(postId, form, studentId);
}

@PostMapping("/blog") // 블로그 작성 - POST, /api/blog
public ResponseEntity<PostDto> post(@RequestBody PostCreationDto requestDto,
Principal principal) {
log.info("-- Post new post --");
log.info("Request Dto = " + requestDto);
// RequestDTO : writer, title, content, thumbnailUri
@DeleteMapping("{postId}") // 블로그 삭제 - DELETE, /api/blog/{postid}
public void delete(
@PathVariable Long postId,
Principal principal
) {
String studentId = principal.getName();
postService.deletePost(postId, studentId);
}

String utilStudentId = principal.getName();
@GetMapping("{postId}/comment")
public Page<CommentView> getComments(
@PathVariable Long postId,
@PageableDefault Pageable pageable
) {
return postService.getComments(postId, pageable);
}

PostDto responseDto = postService.createPost(requestDto, utilStudentId);
return ResponseEntity.ok(responseDto);
@PostMapping("{postId}/comment")
public CommentView createComment(
@PathVariable Long postId,
@RequestBody CommentForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.createComment(postId, form, studentId);
}

// @CrossOrigin
@PutMapping("/blog/{postId}") // 블로그 수정 - PUT, /api/blog/{postId}
public ResponseEntity<PostDto> modify(@RequestBody PostDto requestDto,
@PathVariable Long postId,
Principal principal) {
log.info("-- Modify (title + content) of post --");
// repository에 대해서 해당 id를 가진 엔티티를 가져오고,
// 그 엔티티의 내용을 dto 내용으로 수정 및 다시 repository에 저장한다
@PutMapping("{postId}/comment/{commentId}")
public CommentView updateComment(
@PathVariable Long postId,
@PathVariable Long commentId,
@RequestBody CommentForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.updateComment(postId, commentId, form, studentId);
}

log.info("Request Dto = " + requestDto);
@DeleteMapping("{postId}/comment/{commentId}")
public void deletePost(
@PathVariable Long postId,
@PathVariable Long commentId,
Principal principal
) {
String studentId = principal.getName();
postService.deleteComment(postId, commentId, studentId);
}

String utilStudentId = principal.getName();
PostDto responseDto = postService.updatePost(requestDto, postId, utilStudentId);
@GetMapping("{postId}/comment/{commentId}/reply")
public Page<ReplyView> getReplies(
@PathVariable Long postId,
@PathVariable Long commentId,
@PageableDefault Pageable pageable
) {
return postService.getReplies(postId, commentId, pageable);
}

return ResponseEntity.ok(responseDto);
@PostMapping("{postId}/comment/{commentId}/reply")
public ReplyView createReply(
@PathVariable Long postId,
@PathVariable Long commentId,
@RequestBody ReplyForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.createReply(postId, commentId, form, studentId);
}

@DeleteMapping("/blog/{postId}") // 블로그 삭제 - DELETE, /api/blog/{postid}
public ResponseEntity<Void> delete(@PathVariable Long postId,
Principal principal) {
log.info("-- Delete Specific Post --");
@PutMapping("{postId}/comment/{commentId}/reply/{replyId}")
public ReplyView updateReply(
@PathVariable Long postId,
@PathVariable Long commentId,
@PathVariable Long replyId,
@RequestBody ReplyForm form,
Principal principal
) {
String studentId = principal.getName();
return postService.updateReply(postId, commentId, replyId, form, studentId);
}

String utilStudentId = principal.getName();
postService.deletePost(postId, utilStudentId);
return ResponseEntity.noContent().build();
@DeleteMapping("{postId}/comment/{commentId}/reply/{replyId}")
public void deletePost(
@PathVariable Long postId,
@PathVariable Long commentId,
@PathVariable Long replyId,
Principal principal
) {
String studentId = principal.getName();
postService.deleteReply(postId, commentId, replyId, studentId);
}
}

57 changes: 0 additions & 57 deletions src/main/java/kr/tgwing/tech/blog/controller/ReplyController.java

This file was deleted.

11 changes: 11 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/dto/CommentForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.tgwing.tech.blog.dto;

import lombok.Data;

@Data
public class CommentForm {

private String content;

}

27 changes: 27 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/dto/CommentView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kr.tgwing.tech.blog.dto;

import java.time.LocalDateTime;

import lombok.Builder;
import lombok.Data;

import kr.tgwing.tech.blog.entity.Comment;
import kr.tgwing.tech.user.dto.profiledto.ProfileDTO;

@Data
@Builder
public class CommentView {

private Long id;
private String content;
private ProfileDTO writer;
private LocalDateTime modDate;

public static CommentView of(Comment comment) {
return CommentView.builder()
.content(comment.getContent())
.writer(ProfileDTO.of(comment.getWriter()))
.modDate(comment.getModDate())
.build();
}
}
Loading

0 comments on commit c4a5378

Please sign in to comment.