forked from Team-Traveler/Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/yejineeeeee/Backend into …
…feat/Team-Traveler#86
- Loading branch information
Showing
13 changed files
with
458 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/traveler/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/com/example/traveler/model/dto/CommentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/com/example/traveler/model/dto/CommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/java/com/example/traveler/model/entity/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/traveler/repository/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/com/example/traveler/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
src/main/java/com/example/traveler/service/CommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.