-
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.
Ensure existin user cannot create an account
- Loading branch information
1 parent
1aeeec8
commit 11703ce
Showing
27 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
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
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
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,36 @@ | ||
package com.michael.controller; | ||
|
||
import com.michael.exception.UserException; | ||
import com.michael.model.Post; | ||
import com.michael.model.User; | ||
import com.michael.request.PostRequest; | ||
import com.michael.response.PostResponse; | ||
import com.michael.service.contracts.IPostService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping(path = "posts") | ||
public class PostController { | ||
|
||
@Autowired | ||
IPostService postService; | ||
|
||
@GetMapping() | ||
|
||
@PostMapping(value = "create") | ||
public ResponseEntity<PostResponse> store(@Valid @RequestBody PostRequest request, HttpSession session) { | ||
|
||
User authUser = (User) session.getAttribute("user_session"); | ||
if (authUser == null) throw new UserException("Please login before you can create post"); | ||
Post post = postService.addPost(request, authUser); | ||
|
||
return new ResponseEntity<>(new PostResponse(post, "Post Added Successfully!"), HttpStatus.OK); | ||
} | ||
} |
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,24 @@ | ||
package com.michael.model; | ||
|
||
import com.michael.utils.AutoDate; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.*; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "posts") | ||
public class Post extends AutoDate { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
private String title; | ||
@Column(columnDefinition = "TEXT") | ||
private String body; | ||
} |
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.michael.repository; | ||
|
||
import com.michael.model.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
} |
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
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,17 @@ | ||
package com.michael.request; | ||
|
||
import com.michael.model.User; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
@Getter | ||
@Setter | ||
public class PostRequest { | ||
private User user; | ||
@NotEmpty(message = "Title field cannot be empty") | ||
private String title; | ||
@NotEmpty(message = "Body field cannot be empty") | ||
private String body; | ||
} |
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,16 @@ | ||
package com.michael.response; | ||
|
||
import com.michael.model.Post; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class PostResponse { | ||
private Post post; | ||
private String message; | ||
} |
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
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,26 @@ | ||
package com.michael.service; | ||
|
||
import com.michael.model.Post; | ||
import com.michael.model.User; | ||
import com.michael.repository.PostRepository; | ||
import com.michael.request.PostRequest; | ||
import com.michael.service.contracts.IPostService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class PostService implements IPostService { | ||
|
||
@Autowired | ||
PostRepository postRepository; | ||
|
||
@Override | ||
public Post addPost(PostRequest request, User user) { | ||
Post post = new Post(); | ||
post.setUser(user); | ||
post.setTitle(request.getTitle()); | ||
post.setBody(request.getBody()); | ||
|
||
return postRepository.save(post); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/michael/service/contracts/IPostService.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,10 @@ | ||
package com.michael.service.contracts; | ||
|
||
import com.michael.model.Post; | ||
import com.michael.model.User; | ||
import com.michael.request.PostRequest; | ||
|
||
public interface IPostService { | ||
Post addPost(PostRequest request, User user); | ||
|
||
} |
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
Binary file modified
BIN
+439 Bytes
(120%)
target/classes/com/michael/controller/AuthenticationController.class
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+306 Bytes
(120%)
target/classes/com/michael/exception/CustomException.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+64 Bytes
(110%)
target/classes/com/michael/repository/UserRepository.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+70 Bytes
(120%)
target/classes/com/michael/service/contracts/IUserService.class
Binary file not shown.
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