Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4'

}

group = 'com.example'
Expand Down Expand Up @@ -41,6 +42,7 @@ dependencies {
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
//implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
implementation 'org.springframework.boot:spring-boot-starter-validation'

}

tasks.named('test') {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ForMZ/Server/BookMark/Entity/BookMark.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public class BookMark {
public void settingUser(User user){
this.user = user;
}

public void setBookMarkPostList(BookMarkPost bookMarkPost){
this.bookMarkPostList.add(bookMarkPost);
}
}
2 changes: 2 additions & 0 deletions src/main/java/ForMZ/Server/BookMark/Entity/BookMarkPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public class BookMarkPost {
public BookMarkPost(Post posts, BookMark bookMarks) {
this.posts = posts;
this.bookMarks = bookMarks;
bookMarks.setBookMarkPostList(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import ForMZ.Server.BookMark.Repository.BookMarkPostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BookMarkPostService {
private final BookMarkPostRepository bookMarkPostRepository;

@Transactional
public void saveAll(List<BookMarkPost> bookMarkPostList){
bookMarkPostRepository.saveAll(bookMarkPostList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import ForMZ.Server.BookMark.Entity.BookMark;
import ForMZ.Server.BookMark.Repository.BookMarkRepository;
import ForMZ.Server.User.Entity.User;
import ForMZ.Server.User.Repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BookMarkService {
private final BookMarkRepository bookMarkRepository;

@Transactional
public void save(BookMark bookMark){
bookMarkRepository.save(bookMark);
}

}
32 changes: 32 additions & 0 deletions src/main/java/ForMZ/Server/Category/CategoryController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ForMZ.Server.Category;

import ForMZ.Server.Category.Entity.Category;
import ForMZ.Server.Category.Service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequiredArgsConstructor
public class CategoryController {
private final CategoryService categoryService;

@PostMapping("/create/Category")
public void CreateCategory(){
List<String> names = new ArrayList<>();
names.add("policy");
names.add("housing");
names.add("job");
names.add("foundation");
names.add("free");
names.add("tip");
for (String name : names) {
Category category = new Category(name);
categoryService.save(category);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import ForMZ.Server.Category.Repository.CategoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CategoryService {
private final CategoryRepository categoryRepository;
@Transactional
public void save(Category category){
categoryRepository.save(category);
}
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/ForMZ/Server/Comment/Controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package ForMZ.Server.Comment.Controller;

import ForMZ.Server.Comment.Dto.*;
import ForMZ.Server.Comment.Service.CommentService;
import ForMZ.Server.Configuration.JwtTokenUtil;
import ForMZ.Server.Core.Dto.JoinDto;
import ForMZ.Server.User.Service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class CommentController {
private final CommentService commentService;
private final UserService userService;
private final JwtTokenUtil jwtTokenUtil;

@GetMapping("/api/user/comment")
public ResponseEntity<List<CommentDto>> userComment(HttpServletRequest request) throws Exception {
try {
String accessToken = jwtTokenUtil.resolveAccessToken(request);
Long userId = userService.findUserProfile(accessToken).getId();
List<CommentDto> userComment = commentService.findUserComment(userId);
return ResponseEntity.status(200).body(userComment);
}
catch (Exception e){
return ResponseEntity.status(401).build();
}
}

@PostMapping("/community/comments")
public ResponseEntity<JoinDto> userAddComment(@RequestBody ResCommentDto resCommentDto){
try {
Long commentId = commentService.addComment(resCommentDto);
JoinDto joinDto = new JoinDto(commentId,"λŒ“κΈ€μ΄ μ„±κ³΅μ μœΌλ‘œ μž‘μ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
return ResponseEntity.status(201).body(joinDto);
}
catch (Exception e){
return ResponseEntity.status(404).build();
}
}
@GetMapping("/community/comments/{post-id}")
public ResponseEntity<List<CommentDto>> postComment(@PathVariable("post-id") Long postId){
try {
List<CommentDto> postComment = commentService.findPostComment(postId);
return ResponseEntity.status(200).body(postComment);
}
catch (Exception e){
return ResponseEntity.status(404).build();
}

}
@PatchMapping("/community/comments/{comment-id}")
public ResponseEntity<JoinDto> commentChange(@PathVariable("comment-id") Long commentId, @RequestBody CommentChangeDto commentChangeDto){
try {
System.out.println(commentId);
Long l = commentService.ChangeComment(commentChangeDto.getContent(), commentId);
return ResponseEntity.status(200).body(new JoinDto(l,"λŒ“κΈ€μ΄ μ„±κ³΅μ μœΌλ‘œ μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€."));
} catch (Exception e) {
return ResponseEntity.status(404).build();
}
}

@PostMapping("/community/replies")
public ResponseEntity<JoinDto> AddReply(@RequestBody ResRepliesDto repliesDto,HttpServletRequest request){
try {
String accessToken = jwtTokenUtil.resolveAccessToken(request);
Long userId = userService.findUserProfile(accessToken).getId();
Long replyId = commentService.addReplies(repliesDto, userId);
return ResponseEntity.status(200).body(new JoinDto(replyId,repliesDto.getContent()));
}
catch (Exception e){
return ResponseEntity.status(404).build();
}
}

@GetMapping("/community/replies/{commentId}")
public ResponseEntity<List<ReplyDto>> Reply(@PathVariable("commentId") Long commentId){
try {
List<ReplyDto> reply = commentService.commentReplies(commentId);
return ResponseEntity.status(200).body(reply);
}
catch (Exception e){
return ResponseEntity.status(404).build();
}

}
@PatchMapping("/community/replies/{replyId}")
public ResponseEntity<String> commentReply(@PathVariable("replyId") Long replyId,@RequestBody CommentChangeDto commentChangeDto){
try {
Long l = commentService.ChangeComment(commentChangeDto.getContent(), replyId);
return ResponseEntity.status(200).body("λŒ€λŒ“κΈ€μ΄ μ„±κ³΅μ μœΌλ‘œ μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
} catch (Exception e) {
return ResponseEntity.status(404).build();
}
}
@DeleteMapping("/community/comments/{comment-id}")
public ResponseEntity<String> deleteChange(@PathVariable("comment-id") Long commentId){
try {
commentService.DeleteComment(commentId);
return ResponseEntity.status(200).body("λŒ“κΈ€μ΄ μ„±κ³΅μ μœΌλ‘œ μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
} catch (Exception e) {
return ResponseEntity.status(404).build();
}
}
@DeleteMapping("/community/replies/{replyId}")
public ResponseEntity<String> deleteReply(@PathVariable("replyId") Long replyId){
try {
commentService.DeleteComment(replyId);
return ResponseEntity.status(200).body("λŒ€λŒ“κΈ€μ΄ μ„±κ³΅μ μœΌλ‘œ μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
} catch (Exception e) {
return ResponseEntity.status(404).build();
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/ForMZ/Server/Comment/Dto/CommentChangeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ForMZ.Server.Comment.Dto;

import lombok.Data;

@Data
public class CommentChangeDto {
String content;
}
1 change: 0 additions & 1 deletion src/main/java/ForMZ/Server/Comment/Dto/ResRepliesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@Data
@AllArgsConstructor
public class ResRepliesDto {
Long userId;
Long commentId;
String content;
}
4 changes: 4 additions & 0 deletions src/main/java/ForMZ/Server/Comment/Entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class Comment extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="comment_id")
private Long id;

@NotNull
private String content;

@NotNull
private int like_count;

Expand Down Expand Up @@ -57,4 +60,5 @@ public Comment(String content, User user, Post post) {
public void ChangeContent(String content) {
this.content = content;
}

}
27 changes: 19 additions & 8 deletions src/main/java/ForMZ/Server/Comment/Service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import ForMZ.Server.User.Repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CommentService {
private final CommentRepository commentRepository;
private final UserRepository userRepository;
Expand All @@ -30,44 +32,53 @@ public List<CommentDto> findUserComment(Long userId){
}).toList();
}
//12번
public List<CommentDto> findPostComment(Long postId){
public List<CommentDto> findPostComment(Long postId) throws Exception {
if(postRepository.findById(postId).isEmpty()){
throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” κ²Œμ‹œλ¬Όμž…λ‹ˆλ‹€");
};
List<Comment> postComment = commentRepository.postAllComment(postId);
return postComment.stream().map(c->{
return new CommentDto(c.getId(),c.getContent(),c.getCreatedDate(),c.getLastModifiedDate());
}).toList();
}

public void addComment(ResCommentDto addCommentDto) throws Exception {
@Transactional
public Long addComment(ResCommentDto addCommentDto) throws Exception {
Optional<User> user = userRepository.findById(addCommentDto.getUserId());
if(user.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” μ‚¬μš©μžμž…λ‹ˆλ‹€");
Optional<Post> post = postRepository.findById(addCommentDto.getPostId());
if(post.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” κ²Œμ‹œλ¬Όμž…λ‹ˆλ‹€");
Comment comment = new Comment(addCommentDto.getContent(),user.get(),post.get());
commentRepository.save(comment);
return comment.getId();
}
@Transactional
public void DeleteComment(Long commentId) throws Exception {
Optional<Comment> comment = commentRepository.findById(commentId);
if(comment.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” λŒ“κΈ€μž…λ‹ˆλ‹€");
commentRepository.delete(comment.get());
}//30,35
public void ChangeComment(String content,Long commentId) throws Exception {
@Transactional
public Long ChangeComment(String content, Long commentId) throws Exception {
Optional<Comment> comment = commentRepository.findById(commentId);
if(comment.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” λŒ“κΈ€μž…λ‹ˆλ‹€");
Comment findComment = comment.get();
findComment.ChangeContent(content);
commentRepository.save(findComment);

return findComment.getId();
}
//29,34
public void addReplies(ResRepliesDto resRepliesDto) throws Exception {
@Transactional
public Long addReplies(ResRepliesDto resRepliesDto, Long UserId) throws Exception {
Optional<Comment> findComment = commentRepository.findWithPost(resRepliesDto.getCommentId());
if(findComment.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” λŒ“κΈ€μž…λ‹ˆλ‹€");
Optional<Post> post = postRepository.findById(findComment.get().getId());
Optional<Post> post = postRepository.findById(findComment.get().getPost().getId());
if (post.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” κ²Œμ‹œκΈ€μž…λ‹ˆλ‹€");
Optional<User> user = userRepository.findById(resRepliesDto.getUserId());
Optional<User> user = userRepository.findById(UserId);
if (user.isEmpty()) throw new Exception("μ‘΄μž¬ν•˜μ§€μ•ŠλŠ” μ‚¬μš©μžμž…λ‹ˆλ‹€");
Comment replies = new Comment(resRepliesDto.getContent(),user.get(),post.get());
replies.setParent(findComment.get());
commentRepository.save(replies);
return replies.getId();
}
public List<ReplyDto> commentReplies(Long commentId) throws Exception {
List<Comment> reply = commentRepository.reply(commentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ForMZ.Server.Configuration;

import ForMZ.Server.Post.Repository.PostRepository;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

@RestController
@RequiredArgsConstructor
public class ConfigurationController {
private final JwtTokenUtil jwtTokenUtil;
private final RedisConfig redisConfig;
private final Long expireTimeMs = 1000*60*60L;
@PostMapping("/refresh")
public ResponseEntity<String> refreshToken(HttpServletRequest request){
String AccessToken = jwtTokenUtil.resolveAccessToken(request);
String no1 = jwtTokenUtil.getclaims(AccessToken).getSubject();
Optional<String> refreshToken = Optional.ofNullable(redisConfig.redisTemplate().opsForValue().get(no1));
if(refreshToken.isEmpty()){
return ResponseEntity.status(404).body("λ¦¬ν”„λ ˆμ‹œ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€");
}
Claims getclaims = jwtTokenUtil.getclaims(refreshToken.get());
String id = getclaims.getSubject();
String new_token = jwtTokenUtil.createToken(id, expireTimeMs);
return ResponseEntity.status(200).body(new_token);
}
}
Loading