mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-04 00:47:37 +08:00
Add Post module
This commit is contained in:
@@ -63,6 +63,7 @@ public class SecurityConfig {
|
|||||||
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers(HttpMethod.POST, "/api/auth/**").permitAll()
|
.requestMatchers(HttpMethod.POST, "/api/auth/**").permitAll()
|
||||||
|
.requestMatchers(HttpMethod.GET, "/api/posts/**").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
|
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|||||||
64
src/main/java/com/openisle/controller/PostController.java
Normal file
64
src/main/java/com/openisle/controller/PostController.java
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package com.openisle.controller;
|
||||||
|
|
||||||
|
import com.openisle.model.Post;
|
||||||
|
import com.openisle.service.PostService;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/posts")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) {
|
||||||
|
Post post = postService.createPost(auth.getName(), req.getTitle(), req.getContent());
|
||||||
|
return ResponseEntity.ok(toDto(post));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<PostDto> getPost(@PathVariable Long id) {
|
||||||
|
Post post = postService.getPost(id);
|
||||||
|
return ResponseEntity.ok(toDto(post));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<PostDto> listPosts() {
|
||||||
|
return postService.listPosts().stream().map(this::toDto).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private PostDto toDto(Post post) {
|
||||||
|
PostDto dto = new PostDto();
|
||||||
|
dto.setId(post.getId());
|
||||||
|
dto.setTitle(post.getTitle());
|
||||||
|
dto.setContent(post.getContent());
|
||||||
|
dto.setCreatedAt(post.getCreatedAt());
|
||||||
|
dto.setAuthor(post.getAuthor().getUsername());
|
||||||
|
dto.setViews(post.getViews());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
private static class PostRequest {
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
private static class PostDto {
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private String author;
|
||||||
|
private long views;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
src/main/java/com/openisle/model/Post.java
Normal file
40
src/main/java/com/openisle/model/Post.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.openisle.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Table(name = "posts")
|
||||||
|
public class Post {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(nullable = false, columnDefinition = "TEXT")
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
@ManyToOne(optional = false, fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "author_id")
|
||||||
|
private User author;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private long views = 0;
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
protected void onCreate() {
|
||||||
|
this.createdAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.openisle.repository;
|
||||||
|
|
||||||
|
import com.openisle.model.Post;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||||
|
}
|
||||||
38
src/main/java/com/openisle/service/PostService.java
Normal file
38
src/main/java/com/openisle/service/PostService.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.model.Post;
|
||||||
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.repository.PostRepository;
|
||||||
|
import com.openisle.repository.UserRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class PostService {
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
public Post createPost(String username, String title, String content) {
|
||||||
|
User author = userRepository.findByUsername(username)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||||
|
Post post = new Post();
|
||||||
|
post.setTitle(title);
|
||||||
|
post.setContent(content);
|
||||||
|
post.setAuthor(author);
|
||||||
|
return postRepository.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post getPost(Long id) {
|
||||||
|
Post post = postRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
|
||||||
|
post.setViews(post.getViews() + 1);
|
||||||
|
return postRepository.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Post> listPosts() {
|
||||||
|
return postRepository.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user