Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat :: post
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Aug 26, 2024
1 parent 1516dc2 commit 2328a4c
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}
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) {
}
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;
}
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);
}
}
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) {
}
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) {
}
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);
}
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions crescendo-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ spring:
ai:
openai:
api-key: ${CHATGPT_API_KEY}
# chat:
# options:
# model: gpt-4o



Expand Down

0 comments on commit 2328a4c

Please sign in to comment.