This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 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
4 changes: 4 additions & 0 deletions
4
...endo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiRequest.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,4 @@ | ||
package com.example.crescendoserver.domain.ai.controller; | ||
|
||
public record AiRequest(String message) { | ||
} |
38 changes: 38 additions & 0 deletions
38
crescendo-server/src/main/java/com/example/crescendoserver/domain/post/Post.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,38 @@ | ||
package com.example.crescendoserver.domain.post; | ||
|
||
|
||
import com.example.crescendoserver.domain.user.domain.User; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@SuperBuilder | ||
@Table(name = "posts") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Post { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String title; | ||
|
||
@Column(nullable = false) | ||
private String content; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "author_id", nullable = false) | ||
private User author; | ||
} |
50 changes: 50 additions & 0 deletions
50
...rver/src/main/java/com/example/crescendoserver/domain/post/controller/PostController.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,50 @@ | ||
package com.example.crescendoserver.domain.post.controller; | ||
|
||
|
||
import com.example.crescendoserver.domain.post.Post; | ||
import com.example.crescendoserver.domain.post.dto.PostCreateRequest; | ||
import com.example.crescendoserver.domain.post.dto.PostEditRequest; | ||
import com.example.crescendoserver.domain.post.service.PostService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/posts") | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
private final PostService postService; | ||
|
||
@GetMapping | ||
public List<Post> getAllPosts() { | ||
return postService.getPosts(); | ||
} | ||
|
||
@GetMapping("/{postId}") | ||
public Post getPost(@PathVariable Long postId) { | ||
return postService.getPost(postId); | ||
} | ||
|
||
@PostMapping | ||
public void createPost(@RequestBody PostCreateRequest request) { | ||
postService.createPost(request); | ||
} | ||
|
||
@DeleteMapping("/{postId}") | ||
public void deletePost(@PathVariable Long postId) { | ||
postService.deletePost(postId); | ||
} | ||
|
||
@PatchMapping("/{postId}") | ||
public void editPost(@PathVariable Long postId, @RequestBody PostEditRequest request) { | ||
postService.editPost(postId, request); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...o-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostCreateRequest.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,4 @@ | ||
package com.example.crescendoserver.domain.post.dto; | ||
|
||
public record PostCreateRequest(String title, String content) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...ndo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostEditRequest.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,4 @@ | ||
package com.example.crescendoserver.domain.post.dto; | ||
|
||
public record PostEditRequest(String title, String content) { | ||
} |
11 changes: 11 additions & 0 deletions
11
...rver/src/main/java/com/example/crescendoserver/domain/post/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,11 @@ | ||
package com.example.crescendoserver.domain.post.repository; | ||
|
||
import com.example.crescendoserver.domain.post.Post; | ||
import com.example.crescendoserver.domain.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
List<Post> findAllByAuthorOrderByIdDesc(User author); | ||
} |
60 changes: 60 additions & 0 deletions
60
...ndo-server/src/main/java/com/example/crescendoserver/domain/post/service/PostService.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,60 @@ | ||
package com.example.crescendoserver.domain.post.service; | ||
|
||
|
||
import com.example.crescendoserver.domain.auth.service.AuthService; | ||
import com.example.crescendoserver.domain.post.Post; | ||
import com.example.crescendoserver.domain.post.dto.PostCreateRequest; | ||
import com.example.crescendoserver.domain.post.dto.PostEditRequest; | ||
import com.example.crescendoserver.domain.post.repository.PostRepository; | ||
import com.example.crescendoserver.domain.user.domain.User; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
private final PostRepository postRepository; | ||
private final AuthService authService; | ||
|
||
@Transactional | ||
public void createPost(PostCreateRequest request) { | ||
User user = authService.getUser(); | ||
Post post = Post.builder() | ||
.title(request.title()) | ||
.content(request.content()) | ||
.author(user) | ||
.build(); | ||
postRepository.save(post); | ||
} | ||
|
||
@Transactional | ||
public void deletePost(Long postId) { | ||
postRepository.deleteById(postId); | ||
} | ||
|
||
|
||
public void editPost(Long postId, PostEditRequest request) { | ||
Post post = postRepository.findById(postId).orElseThrow(); | ||
post.setTitle(request.title()); | ||
post.setContent(request.content()); | ||
postRepository.save(post); | ||
} | ||
|
||
public List<Post> getPosts() { | ||
User author = authService.getUser(); | ||
|
||
return postRepository.findAllByAuthorOrderByIdDesc(author); | ||
} | ||
|
||
|
||
public Post getPost(Long postId) { | ||
Optional<Post> post = postRepository.findById(postId); | ||
|
||
return post.orElse(null); | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ spring: | |
ai: | ||
openai: | ||
api-key: ${CHATGPT_API_KEY} | ||
# chat: | ||
# options: | ||
# model: gpt-4o | ||
|
||
|
||
|
||
|