Skip to content

Commit

Permalink
Ensure existin user cannot create an account
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ikpefua committed May 25, 2021
1 parent 1aeeec8 commit 11703ce
Show file tree
Hide file tree
Showing 27 changed files with 158 additions and 7 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
Expand All @@ -24,9 +21,13 @@ public class AuthenticationController {
@Autowired
IUserService userService;

@RequestMapping(path = "register")
@PostMapping(path = "register")
public ResponseEntity<UserResponse> register(@Valid @RequestBody UserRegisterRequest request, HttpSession session) {

boolean isUserExists = userService.checkIfUserExist(request.getEmail());
if (isUserExists) {
throw new UserException("User email already exist. Please Login");
}
User user = userService.register(request);
session.setAttribute("user_session", user);

Expand All @@ -36,6 +37,7 @@ public ResponseEntity<UserResponse> register(@Valid @RequestBody UserRegisterReq

@GetMapping(path = "login")
public ResponseEntity<?> login(@Valid @RequestBody UserLoginRequest request, HttpSession session) {

User user = userService.login(request);
if (user == null) {
throw new UserException("Check Email or Password");
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/michael/controller/PostController.java
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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/michael/model/Post.java
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;
}
9 changes: 9 additions & 0 deletions src/main/java/com/michael/repository/PostRepository.java
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> {
}
4 changes: 4 additions & 0 deletions src/main/java/com/michael/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmailAndPassword(String email, String password);

// boolean existsByEmail(String email);

boolean existsUsersByEmail(String email);

}
17 changes: 17 additions & 0 deletions src/main/java/com/michael/request/PostRequest.java
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;
}
16 changes: 16 additions & 0 deletions src/main/java/com/michael/response/PostResponse.java
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;
}
1 change: 0 additions & 1 deletion src/main/java/com/michael/response/UserResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.http.HttpStatus;

@Getter
@Setter
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/michael/service/PostService.java
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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/michael/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public User login(UserLoginRequest request) {

return user;
}

public boolean checkIfUserExist(String email) {
return userRepository.existsUsersByEmail(email);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/michael/service/contracts/IPostService.java
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public interface IUserService {

boolean checkIfUserExist(String email);
User register(UserRegisterRequest userRegisterRequest);
User login(UserLoginRequest userLoginRequest);
}
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/com/michael/exception/CustomException.class
Binary file not shown.
Binary file added target/classes/com/michael/model/Post.class
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/com/michael/repository/UserRepository.class
Binary file not shown.
Binary file not shown.
Binary file added target/classes/com/michael/response/PostResponse.class
Binary file not shown.
Binary file modified target/classes/com/michael/response/UserResponse.class
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/com/michael/service/UserService.class
Binary file not shown.
Binary file not shown.
Binary file modified target/classes/com/michael/service/contracts/IUserService.class
Binary file not shown.
4 changes: 3 additions & 1 deletion week-9-task-michael-ikpefua.iml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<configuration>
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="Hibernate" />
<datasource-mapping />
<datasource-mapping>
<factory-entry name="entityManagerFactory" />
</datasource-mapping>
<naming-strategy-map />
</configuration>
</facet>
Expand Down

0 comments on commit 11703ce

Please sign in to comment.