Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yejineeeeee/Backend into …
Browse files Browse the repository at this point in the history
  • Loading branch information
ozll-zinni committed Aug 16, 2023
2 parents 7bd284d + 1cc7429 commit d831107
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public enum BaseResponseStatus {
PASSWORD_ENCRYPTION_ERROR(false, 4011, "비밀번호 암호화에 실패하였습니다."),
PASSWORD_DECRYPTION_ERROR(false, 4012, "비밀번호 복호화에 실패하였습니다."),

POST_IS_EMPTY(false,5003, "게시물이 존재하지 않습니다."),
SAVE_COMMENT_FAIL(false, 5500, "댓글 생성 실패했습니다."),

/**
* 6000 : Checklist 오류
*/
SAVE_CATEGORY_FAIL(false, 6001, "체크리스트 생성 실패했습니다"),
SAVE_CATEGORY_FAIL(false, 6001, "체크리스트 생성 실패했습니다."),
UPDATE_CATEGORYNAME_FAIL( false, 6002, "체크리스트 수정 실패하였습니다."),
CHECKLIST_IS_EMPTY(false,6003, "체크리스트가 존재하지 않습니다."),
DELETE_CATEGORY_FAIL(false, 6004, "체크리스트 삭제 실패하였습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.traveler.controller;


import com.example.traveler.config.BaseException;
import com.example.traveler.config.BaseResponse;
import com.example.traveler.model.dto.CommentRequest;
import com.example.traveler.model.dto.CommentResponse;
import com.example.traveler.model.dto.TravelRequest;
import com.example.traveler.model.dto.TravelResponse;
import com.example.traveler.model.entity.Comment;
import com.example.traveler.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post")
public class CommentController {
// @Autowired
// private PostService postService;
@Autowired
private CommentService commentService;

@PostMapping("/{pId}/comment")
public BaseResponse<CommentResponse> saveComment(@RequestHeader("Authorization") String accessToken, @RequestBody CommentRequest commentRequest, @PathVariable("pId") long pId) {
try {
CommentResponse commentResponse = commentService.saveComment(accessToken, pId, commentRequest);
return new BaseResponse<>(commentResponse);
} catch (BaseException exception) {
return new BaseResponse<>(exception.getStatus());
}
}

@GetMapping("/{pId}/comment")
public BaseResponse<List<CommentResponse>> getAllComment(@PathVariable("pId") long pId) {
try {
List<CommentResponse> commentResponses = commentService.getAllComment(pId);
return new BaseResponse<>(commentResponses);
} catch (BaseException exception) {
return new BaseResponse<>(exception.getStatus());
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/example/traveler/model/dto/CommentRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.traveler.model.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Setter
@Getter
@AllArgsConstructor
public class CommentRequest {
String content;
}
21 changes: 21 additions & 0 deletions src/main/java/com/example/traveler/model/dto/CommentResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.traveler.model.dto;

import com.example.traveler.model.entity.DayCourse;
import com.example.traveler.model.entity.Travel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@Getter
@Setter
public class CommentResponse {
long coId;
String content;
long pId;
long uId;
}
31 changes: 31 additions & 0 deletions src/main/java/com/example/traveler/model/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.traveler.model.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long coId;

@ManyToOne
@JoinColumn(name="pId")
private Post post;

private String content;

@ManyToOne
@JoinColumn(name="uId")
private User user;


public Comment(Post post, String content, User user) {
this.post = post;
this.content = content;
this.user = user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class DayCourse {
RecommendTravel recommendTravel;



@OneToOne
@JoinColumn(name="sId1")
Spot spot1;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/example/traveler/model/entity/Post.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.example.traveler.model.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Post {

Expand Down Expand Up @@ -65,4 +70,11 @@ public Post(String title, List<String> hashtags, String oneLineReview, String lo
@CreatedDate
private LocalDateTime createdAt;

@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();


@OneToOne(mappedBy = "post")
private Travel travel;

}
11 changes: 11 additions & 0 deletions src/main/java/com/example/traveler/model/entity/Travel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.traveler.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -40,6 +41,11 @@ public class Travel {

int state;

// RecommendTravel을 통해 작성되면 그 코드 값을, 아니면 0
int code = 0;

int with;

//사용자
@ManyToOne
@JoinColumn(name="uId")
Expand All @@ -48,6 +54,11 @@ public class Travel {
@OneToMany(mappedBy = "travel")
List<DayCourse> courses = new ArrayList<>();

@JsonIgnore
@OneToOne
@JoinColumn(name="pId")
Post post;

public Travel(String title, String destination, Date startDate, Date endDate, int timeStatus, int writeStatus, int noteStatus, int state, User user) {
this.title = title;
this.destination = destination;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.traveler.repository;

import com.example.traveler.model.entity.Comment;
import com.example.traveler.model.entity.Post;
import com.example.traveler.model.entity.Travel;
import com.example.traveler.model.entity.User;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface CommentRepository extends CrudRepository<Comment, Long> {

public Comment findByCoId(long id);
public List<Comment> findAllByPostOrderByCoId(Post post);

public List<Comment> findAllByUserOrderByCoId(User user);
//public List<Travel> findAllByUser()
}
14 changes: 14 additions & 0 deletions src/main/java/com/example/traveler/repository/PostRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.traveler.repository;

import com.example.traveler.model.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

public Post findBypId(Long pId);

}
90 changes: 90 additions & 0 deletions src/main/java/com/example/traveler/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.example.traveler.service;

import com.example.traveler.config.BaseException;
import com.example.traveler.model.dto.CommentRequest;
import com.example.traveler.model.dto.CommentResponse;
import com.example.traveler.model.dto.TravelRequest;
import com.example.traveler.model.dto.TravelResponse;
import com.example.traveler.model.entity.Comment;
import com.example.traveler.model.entity.Post;
import com.example.traveler.model.entity.Travel;
import com.example.traveler.model.entity.User;
import com.example.traveler.repository.CommentRepository;
import com.example.traveler.repository.TravelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static com.example.traveler.config.BaseResponseStatus.*;

@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
@Autowired
private UserService userService;
@Autowired
private PostService postService;
public CommentResponse saveComment(String accessToken, long pId, CommentRequest comment) throws BaseException {
User user = userService.getUserByToken(accessToken);
Post post;
if (user == null) {
throw new BaseException(INVALID_JWT);
}
Comment saveComment = null;
try {
post = postService.getPost(pId);
} catch (Exception e) {
throw new BaseException(POST_IS_EMPTY);
}
try {
Comment newComment = new Comment(post, comment.getContent(), user);
saveComment = commentRepository.save(newComment);
} catch (Exception e) {
throw new BaseException(SAVE_TRAVEL_FAIL);
}
CommentResponse commentResponse = new CommentResponse(saveComment.getCoId(), saveComment.getContent(), saveComment.getPost().getPId(), saveComment.getUser().getId());
return commentResponse;
}



public List<CommentResponse> getAllComment(long pId) throws BaseException {
Post post = null;
try {
post = postService.getPost(pId);
} catch (Exception e) {
throw new BaseException(POST_IS_EMPTY);
}
List<Comment> allComment = commentRepository.findAllByPostOrderByCoId(post);
ArrayList<CommentResponse> allCommentResponse = new ArrayList<>();
for (Comment comment : allComment) {
CommentResponse commentResponse = new CommentResponse(comment.getCoId(), comment.getContent(), comment.getPost().getPId(), comment.getUser().getId());
allCommentResponse.add(commentResponse);
}
return allCommentResponse;
}





public List<CommentResponse> getMyComment(String accessToken) throws BaseException{
User user = userService.getUserByToken(accessToken);
if (user == null) {
throw new BaseException(INVALID_JWT);
}

List<Comment> allMyComment = commentRepository.findAllByUserOrderByCoId(user);
ArrayList<CommentResponse> allMyCommentResponse = new ArrayList<>();
for (Comment comment : allMyComment) {
CommentResponse commentResponse = new CommentResponse(comment.getCoId(), comment.getContent(), comment.getPost().getPId(), comment.getUser().getId());
allMyCommentResponse.add(commentResponse);
}
return allMyCommentResponse;
}


}
Loading

0 comments on commit d831107

Please sign in to comment.