diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiController.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiController.java index 16584a9..86c348a 100644 --- a/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiController.java +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiController.java @@ -3,20 +3,22 @@ import lombok.RequiredArgsConstructor; import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.web.bind.annotation.GetMapping; +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.RequestParam; import org.springframework.web.bind.annotation.RestController; -@RequestMapping("/ai") +@RequestMapping("/api") @RestController @RequiredArgsConstructor public class AiController { private final OpenAiChatModel openAiChatModel; - @GetMapping("/chat") - public String chat(@RequestBody String message) { - return openAiChatModel.call(String.valueOf(message)); + @PostMapping("/chat") + public String chat(@RequestBody AiRequest request) { + System.out.println(request.message()); + return openAiChatModel.call(String.valueOf(request.message())); } } \ No newline at end of file diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiRequest.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiRequest.java new file mode 100644 index 0000000..c159f49 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/ai/controller/AiRequest.java @@ -0,0 +1,4 @@ +package com.example.crescendoserver.domain.ai.controller; + +public record AiRequest(String message) { +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/Post.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/Post.java new file mode 100644 index 0000000..bb36411 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/Post.java @@ -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; +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/controller/PostController.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/controller/PostController.java new file mode 100644 index 0000000..db8aaa6 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/controller/PostController.java @@ -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 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); + } +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostCreateRequest.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostCreateRequest.java new file mode 100644 index 0000000..28e4c0d --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostCreateRequest.java @@ -0,0 +1,4 @@ +package com.example.crescendoserver.domain.post.dto; + +public record PostCreateRequest(String title, String content) { +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostEditRequest.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostEditRequest.java new file mode 100644 index 0000000..a6545f2 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/dto/PostEditRequest.java @@ -0,0 +1,4 @@ +package com.example.crescendoserver.domain.post.dto; + +public record PostEditRequest(String title, String content) { +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/repository/PostRepository.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/repository/PostRepository.java new file mode 100644 index 0000000..843c435 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/repository/PostRepository.java @@ -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 { + List findAllByAuthorOrderByIdDesc(User author); +} diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/service/PostService.java b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/service/PostService.java new file mode 100644 index 0000000..dafbf31 --- /dev/null +++ b/crescendo-server/src/main/java/com/example/crescendoserver/domain/post/service/PostService.java @@ -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 getPosts() { + User author = authService.getUser(); + + return postRepository.findAllByAuthorOrderByIdDesc(author); + } + + + public Post getPost(Long postId) { + Optional post = postRepository.findById(postId); + + return post.orElse(null); + } +} + diff --git a/crescendo-server/src/main/java/com/example/crescendoserver/global/config/security/SecurityConfig.java b/crescendo-server/src/main/java/com/example/crescendoserver/global/config/security/SecurityConfig.java index 2190de5..76096d5 100644 --- a/crescendo-server/src/main/java/com/example/crescendoserver/global/config/security/SecurityConfig.java +++ b/crescendo-server/src/main/java/com/example/crescendoserver/global/config/security/SecurityConfig.java @@ -49,17 +49,23 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .requestMatchers(HttpMethod.POST, "/auth/signup", "/auth/login", "/auth/reissue").anonymous() .requestMatchers(HttpMethod.GET, "/auth/me").authenticated() .requestMatchers(HttpMethod.GET, "/swagger-ui/**", "/v3/api-docs/**").permitAll() - .requestMatchers(HttpMethod.GET, "/ai/chat").permitAll() + .requestMatchers(HttpMethod.POST, "/api/chat").permitAll() .requestMatchers(HttpMethod.POST, "/ws/chat").permitAll() .requestMatchers(HttpMethod.GET, "/ws/chat").permitAll() .requestMatchers(HttpMethod.POST, "/chat").permitAll() .requestMatchers(HttpMethod.GET, "/chat").permitAll() - .requestMatchers(HttpMethod.GET, "/todos/{todoId}").permitAll() - .requestMatchers(HttpMethod.GET, "/todos").permitAll() - .requestMatchers(HttpMethod.POST, "/todos").permitAll() - .requestMatchers(HttpMethod.PATCH, "/todos/{todoId}/check").permitAll() - .requestMatchers(HttpMethod.PATCH, "/todos/{todoId}").permitAll() - .requestMatchers(HttpMethod.DELETE, "/todos/{todoId}").permitAll() + .requestMatchers(HttpMethod.GET, "/todos/{todoId}").authenticated() + .requestMatchers(HttpMethod.GET, "/todos").authenticated() + .requestMatchers(HttpMethod.POST, "/todos").authenticated() + .requestMatchers(HttpMethod.PATCH, "/todos/{todoId}/check").authenticated() + .requestMatchers(HttpMethod.PATCH, "/todos/{todoId}").authenticated() + .requestMatchers(HttpMethod.DELETE, "/todos/{todoId}").authenticated() + .requestMatchers(HttpMethod.GET, "/posts/{postId}").authenticated() + .requestMatchers(HttpMethod.GET, "/posts").authenticated() + .requestMatchers(HttpMethod.POST, "/posts").authenticated() + .requestMatchers(HttpMethod.PATCH, "/posts/{postId}/check").authenticated() + .requestMatchers(HttpMethod.PATCH, "/posts/{postId}").authenticated() + .requestMatchers(HttpMethod.DELETE, "/posts/{postId}").authenticated() ) .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) diff --git a/crescendo-server/src/main/resources/application.yml b/crescendo-server/src/main/resources/application.yml index 33a72ee..45380cf 100644 --- a/crescendo-server/src/main/resources/application.yml +++ b/crescendo-server/src/main/resources/application.yml @@ -17,6 +17,9 @@ spring: ai: openai: api-key: ${CHATGPT_API_KEY} +# chat: +# options: +# model: gpt-4o