diff --git a/build.gradle b/build.gradle index 8c915ed..eaf0bd4 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'com.h2database:h2' + implementation 'io.jsonwebtoken:jjwt-api:0.12.5' + runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.5' + runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.5' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/src/main/java/com/lk/paopao/controller/PostController.java b/src/main/java/com/lk/paopao/controller/PostController.java new file mode 100644 index 0000000..c04771d --- /dev/null +++ b/src/main/java/com/lk/paopao/controller/PostController.java @@ -0,0 +1,27 @@ +package com.lk.paopao.controller; + +import com.lk.paopao.dto.PostRequest; +import com.lk.paopao.entity.Post; +import com.lk.paopao.service.PostService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/v1/post") +public class PostController { + @Autowired + private PostService postService; + + @PostMapping + public Map create(@RequestBody PostRequest postRequest, @RequestAttribute("username") String username){ + Post post = postService.create(postRequest, username); + Map result= new HashMap<>(); + result.put("code", 0); + result.put("msg", "success"); + result.put("data", post); + return result; + } +} diff --git a/src/main/java/com/lk/paopao/dto/PostContentRequest.java b/src/main/java/com/lk/paopao/dto/PostContentRequest.java new file mode 100644 index 0000000..01eb48a --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/PostContentRequest.java @@ -0,0 +1,31 @@ +package com.lk.paopao.dto; + +public class PostContentRequest { + private String content; + private int type; + private int sort; + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public int getSort() { + return sort; + } + + public void setSort(int sort) { + this.sort = sort; + } +} diff --git a/src/main/java/com/lk/paopao/dto/PostRequest.java b/src/main/java/com/lk/paopao/dto/PostRequest.java new file mode 100644 index 0000000..68dfdb8 --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/PostRequest.java @@ -0,0 +1,56 @@ +package com.lk.paopao.dto; + +import java.util.List; + +public class PostRequest { + private List contents; + + private List tags; + + private List users; + + private int attachmentPrice; + + private int visibility; + + + public List getContents() { + return contents; + } + + public void setContents(List contents) { + this.contents = contents; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public int getAttachmentPrice() { + return attachmentPrice; + } + + public void setAttachmentPrice(int attachmentPrice) { + this.attachmentPrice = attachmentPrice; + } + + public int getVisibility() { + return visibility; + } + + public void setVisibility(int visibility) { + this.visibility = visibility; + } +} diff --git a/src/main/java/com/lk/paopao/entity/Post.java b/src/main/java/com/lk/paopao/entity/Post.java new file mode 100644 index 0000000..805818d --- /dev/null +++ b/src/main/java/com/lk/paopao/entity/Post.java @@ -0,0 +1,132 @@ +package com.lk.paopao.entity; + +import jakarta.persistence.*; + +import java.util.List; + +@Entity +public class Post { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToMany(mappedBy = "post", cascade = CascadeType.PERSIST) + private List contents; + + @ManyToOne + private User user; + + private Long createOn; + + private String location; + + private int stars = 0; + + private int collections = 0; + + private int comments = 0; + + private int visibility = 0; + + private int attachmentPrice; + + private String tags; + + private String atUsers; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getContents() { + return contents; + } + + public void setContents(List contents) { + this.contents = contents; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Long getCreateOn() { + return createOn; + } + + public void setCreateOn(Long createOn) { + this.createOn = createOn; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public int getStars() { + return stars; + } + + public void setStars(int stars) { + this.stars = stars; + } + + public int getCollections() { + return collections; + } + + public void setCollections(int collections) { + this.collections = collections; + } + + public int getComments() { + return comments; + } + + public void setComments(int comments) { + this.comments = comments; + } + + public int getVisibility() { + return visibility; + } + + public void setVisibility(int visibility) { + this.visibility = visibility; + } + + public int getAttachmentPrice() { + return attachmentPrice; + } + + public void setAttachmentPrice(int attachmentPrice) { + this.attachmentPrice = attachmentPrice; + } + + public String getTags() { + return tags; + } + + public void setTags(String tags) { + this.tags = tags; + } + + public String getAtUsers() { + return atUsers; + } + + public void setAtUsers(String atUsers) { + this.atUsers = atUsers; + } +} diff --git a/src/main/java/com/lk/paopao/entity/PostContent.java b/src/main/java/com/lk/paopao/entity/PostContent.java new file mode 100644 index 0000000..e8ee9dd --- /dev/null +++ b/src/main/java/com/lk/paopao/entity/PostContent.java @@ -0,0 +1,63 @@ +package com.lk.paopao.entity; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; + +@Entity +public class PostContent { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String content; + + private int type; + + private int sort; + + @ManyToOne + @JsonIgnore + private Post post; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public int getSort() { + return sort; + } + + public void setSort(int sort) { + this.sort = sort; + } + + public Post getPost() { + return post; + } + + public void setPost(Post post) { + this.post = post; + } +} diff --git a/src/main/java/com/lk/paopao/repository/PostContentRepository.java b/src/main/java/com/lk/paopao/repository/PostContentRepository.java new file mode 100644 index 0000000..793e6dd --- /dev/null +++ b/src/main/java/com/lk/paopao/repository/PostContentRepository.java @@ -0,0 +1,7 @@ +package com.lk.paopao.repository; + +import com.lk.paopao.entity.PostContent; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostContentRepository extends JpaRepository { +} diff --git a/src/main/java/com/lk/paopao/repository/PostRepository.java b/src/main/java/com/lk/paopao/repository/PostRepository.java new file mode 100644 index 0000000..b39eb71 --- /dev/null +++ b/src/main/java/com/lk/paopao/repository/PostRepository.java @@ -0,0 +1,7 @@ +package com.lk.paopao.repository; + +import com.lk.paopao.entity.Post; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRepository extends JpaRepository { +} diff --git a/src/main/java/com/lk/paopao/service/PostService.java b/src/main/java/com/lk/paopao/service/PostService.java new file mode 100644 index 0000000..b725ccc --- /dev/null +++ b/src/main/java/com/lk/paopao/service/PostService.java @@ -0,0 +1,48 @@ +package com.lk.paopao.service; + +import com.lk.paopao.dto.PostContentRequest; +import com.lk.paopao.dto.PostRequest; +import com.lk.paopao.entity.Post; +import com.lk.paopao.entity.PostContent; +import com.lk.paopao.repository.PostRepository; +import com.lk.paopao.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class PostService { + + @Autowired + private PostRepository postRepository; + + @Autowired + private UserRepository userRepository; + + public Post create(PostRequest postRequest, String username) { + + Post post = new Post(); + post.setCreateOn(System.currentTimeMillis()); + post.setUser(userRepository.findByUsername(username)); + post.setAttachmentPrice(postRequest.getAttachmentPrice()); + post.setVisibility(postRequest.getVisibility()); + + post.setTags(String.join(",", postRequest.getTags())); + post.setAtUsers(String.join(",", postRequest.getUsers())); + + List contents = new ArrayList<>(); + for(PostContentRequest content : postRequest.getContents()){ + PostContent postContent = new PostContent(); + postContent.setContent(content.getContent()); + postContent.setSort(content.getSort()); + postContent.setType(content.getType()); + postContent.setPost(post); + contents.add(postContent); + } + post.setContents(contents); + return postRepository.save(post); + + } +}