-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jorbush/post
Implementing `Post` resources
- Loading branch information
Showing
15 changed files
with
684 additions
and
8 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
postrify-backend/src/main/java/com/postrify/postrifybackend/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
postrify-backend/src/main/java/com/postrify/postrifybackend/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.postrify.postrifybackend.controller; | ||
|
||
import com.postrify.postrifybackend.dto.PostRequest; | ||
import com.postrify.postrifybackend.dto.PostResponseDTO; | ||
import com.postrify.postrifybackend.model.Post; | ||
import com.postrify.postrifybackend.model.User; | ||
import com.postrify.postrifybackend.service.PostService; | ||
import com.postrify.postrifybackend.service.UserService; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/posts") | ||
public class PostController { | ||
|
||
@Autowired private PostService postService; | ||
|
||
@Autowired private UserService userService; | ||
|
||
@GetMapping | ||
public List<PostResponseDTO> getAllPosts() { | ||
return postService.getAllPosts(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<PostResponseDTO> getPostById(@PathVariable final Long id) { | ||
Optional<PostResponseDTO> post = postService.getPostById(id); | ||
return post.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build()); | ||
} | ||
|
||
@GetMapping("/user/{userId}") | ||
public List<PostResponseDTO> getPostsByUser(@PathVariable final Long userId) { | ||
return postService.getPostsByUser(userId); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<PostResponseDTO> createPost( | ||
@RequestBody final PostRequest postRequest, final Authentication authentication) { | ||
String username = authentication.getName(); | ||
User user = userService.findByUsername(username); | ||
|
||
Post post = new Post(); | ||
post.setTitle(postRequest.getTitle()); | ||
post.setContent(postRequest.getContent()); | ||
post.setUser(user); | ||
|
||
PostResponseDTO createdPost = postService.createPost(post); | ||
return ResponseEntity.ok(createdPost); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<PostResponseDTO> updatePost( | ||
@PathVariable final Long id, | ||
@RequestBody final PostRequest postRequest, | ||
final Authentication authentication) { | ||
String username = authentication.getName(); | ||
User user = userService.findByUsername(username); | ||
|
||
Post postDetails = new Post(); | ||
postDetails.setTitle(postRequest.getTitle()); | ||
postDetails.setContent(postRequest.getContent()); | ||
|
||
PostResponseDTO updatedPost = postService.updatePost(id, postDetails, user); | ||
return ResponseEntity.ok(updatedPost); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deletePost( | ||
@PathVariable final Long id, final Authentication authentication) { | ||
String username = authentication.getName(); | ||
User user = userService.findByUsername(username); | ||
postService.deletePost(id, user); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...fy/postrifybackend/model/JwtResponse.java → ...rify/postrifybackend/dto/JwtResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...y/postrifybackend/model/LoginRequest.java → ...ify/postrifybackend/dto/LoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ostrifybackend/model/MessageResponse.java → .../postrifybackend/dto/MessageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
postrify-backend/src/main/java/com/postrify/postrifybackend/dto/PostRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.postrify.postrifybackend.dto; | ||
|
||
public class PostRequest { | ||
private String title; | ||
private String content; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(final String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(final String content) { | ||
this.content = content; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
postrify-backend/src/main/java/com/postrify/postrifybackend/dto/PostResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.postrify.postrifybackend.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class PostResponseDTO { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
private UserDTO user; | ||
private LocalDateTime createdAt; | ||
private LocalDateTime updatedAt; | ||
|
||
public PostResponseDTO( | ||
final Long id, | ||
final String title, | ||
final String content, | ||
final UserDTO user, | ||
final LocalDateTime createdAt, | ||
final LocalDateTime updatedAt) { | ||
this.id = id; | ||
this.title = title; | ||
this.content = content; | ||
this.user = user; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public UserDTO getUser() { | ||
return user; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public LocalDateTime getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public void setId(final Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setTitle(final String title) { | ||
this.title = title; | ||
} | ||
|
||
public void setContent(final String content) { | ||
this.content = content; | ||
} | ||
|
||
public void setUser(final UserDTO user) { | ||
this.user = user; | ||
} | ||
|
||
public void setCreatedAt(final LocalDateTime createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public void setUpdatedAt(final LocalDateTime updatedAt) { | ||
this.updatedAt = updatedAt; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../postrifybackend/model/SignUpRequest.java → ...fy/postrifybackend/dto/SignUpRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
postrify-backend/src/main/java/com/postrify/postrifybackend/dto/UserDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.postrify.postrifybackend.dto; | ||
|
||
public class UserDTO { | ||
private Long id; | ||
private String username; | ||
private String email; | ||
|
||
public UserDTO(final Long id, final String username, final String email) { | ||
this.id = id; | ||
this.username = username; | ||
this.email = email; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setId(final Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setUsername(final String username) { | ||
this.username = username; | ||
} | ||
|
||
public void setEmail(final String email) { | ||
this.email = email; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
postrify-backend/src/main/java/com/postrify/postrifybackend/model/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.postrify.postrifybackend.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import jakarta.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@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; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
@JsonIgnore | ||
private User user; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime createdAt; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime updatedAt; | ||
|
||
public Post() {} | ||
|
||
public Post(final String title, final String content, final User user) { | ||
this.title = title; | ||
this.content = content; | ||
this.user = user; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public LocalDateTime getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public void setTitle(final String title) { | ||
this.title = title; | ||
} | ||
|
||
public void setContent(final String content) { | ||
this.content = content; | ||
} | ||
|
||
public void setUser(final User user) { | ||
this.user = user; | ||
} | ||
|
||
@PrePersist | ||
protected void onCreate() { | ||
this.createdAt = LocalDateTime.now(); | ||
this.updatedAt = LocalDateTime.now(); | ||
} | ||
|
||
@PreUpdate | ||
protected void onUpdate() { | ||
this.updatedAt = LocalDateTime.now(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
postrify-backend/src/main/java/com/postrify/postrifybackend/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.postrify.postrifybackend.repository; | ||
|
||
import com.postrify.postrifybackend.model.Post; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
List<Post> findByUserId(Long userId); | ||
} |
Oops, something went wrong.