This commit is contained in:
wu 2024-12-05 12:29:40 +08:00
parent 6841e2f32e
commit e8b3d0b958
9 changed files with 375 additions and 0 deletions

View File

@ -25,6 +25,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2' 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' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
} }

View File

@ -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<String, Object> create(@RequestBody PostRequest postRequest, @RequestAttribute("username") String username){
Post post = postService.create(postRequest, username);
Map<String, Object> result= new HashMap<>();
result.put("code", 0);
result.put("msg", "success");
result.put("data", post);
return result;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,56 @@
package com.lk.paopao.dto;
import java.util.List;
public class PostRequest {
private List<PostContentRequest> contents;
private List<String> tags;
private List<String> users;
private int attachmentPrice;
private int visibility;
public List<PostContentRequest> getContents() {
return contents;
}
public void setContents(List<PostContentRequest> contents) {
this.contents = contents;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> 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;
}
}

View File

@ -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<PostContent> 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<PostContent> getContents() {
return contents;
}
public void setContents(List<PostContent> 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;
}
}

View File

@ -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;
}
}

View File

@ -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<PostContent, Long> {
}

View File

@ -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<Post, Long> {
}

View File

@ -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<PostContent> 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);
}
}