Time : 5:38 AM

Unless we do what we want to do. How will we lead others.

So let’s see

Till now we have decided we’ll go with postgres and user table is designed , postgres is running.

Means today we have to write the db class it’s called repository class and user service class and controller class

Feature Based

├── com.app
    └── company
        ├── Company
        ├── CompanyController
        ├── CompanyRepository        
        └── CompanyService
    └── product
        ├── Product   
        ├── ProductController
        ├── ProductRepository
        └── ProductService
    └── util
    └── user
        ├── User   
        ├── UserController
        ├── UserRepository
        └── UserService
    |__ common
	    |- Middleware 
		    |- Exception
		    |- Logger
		    |- Response Handler
	└── core 
		├── base/
            ├── BaseService.java
            ├── BaseRepository.java
            └── BaseEntity.java
	    └── config/
	│       └── AppConfig.java
Link to original

My main goal with designing the base classes is to keep it as flexible as possible

6:00 break | 6:15 Back

This Means We have to design the base classes first

Now the

Entity Class

package in.abhi8290.helloworld.user;  
  
import in.abhi8290.helloworld.core.base.BaseEntity;  
import jakarta.persistence.Column;  
import jakarta.persistence.Entity;  
import jakarta.persistence.Table;  
  
@Entity  
@Table(name = "users")  
public class User extends BaseEntity {  
  
    @Column(nullable = false)  
    private String firstName;  
  
    @Column(nullable = false)  
    private String lastName;  
  
    @Column(nullable = false)  
    private String password;  
  
    @Column(unique = true, nullable = false)  
    private String email;  
  
    public User() {}  
  
    public User(String firstName, String lastName, String email, String password) {  
        this.firstName = firstName;  
        this.lastName  = lastName;  
        this.email     = email;  
        this.password  = password;  
    }  
  
    // Getters and Setters  
  
    public String getFirstName() {  
        return firstName;  
    }  
  
    public void setFirstName(String firstName) {  
        this.firstName = firstName;  
    }  
  
    public String getLastName() {  
        return lastName;  
    }  
  
    public void setLastName(String lastName) {  
        this.lastName = lastName;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }  
  
    public String getEmail() {  
        return email;  
    }  
  
    public void setEmail(String email) {  
        this.email = email;  
    }  
}
Link to original

Repo Class

  • This is the most interesting All we need to write a function called findByEmail and JPA will go and make write the query for us check this out JPA Repository
package in.abhi8290.helloworld.user;  
  
import in.abhi8290.helloworld.core.base.BaseRepository;  
import org.springframework.stereotype.Repository;  
  
@Repository  
public interface UserRepository extends BaseRepository<User, String> {  
  
    User findByEmail(String email);  
  
    boolean existsByEmail(String email);  
  
    User findByFirstName(String firstName);  
  
  
}
Link to original

User-Service#service-class-v0

Controller Class

package in.abhi8290.helloworld.user;  
  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.web.bind.annotation.*;  
  
import java.util.List;  
import java.util.Optional;  
import in.abhi8290.helloworld.user.*;  
  
@RestController  
@RequestMapping("/users")  
public class UserController {  
  
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);  
  
    private final UserService userService;  
  
    // Spring will inject this for you  
    public UserController(UserService userService) {  
        this.userService = userService;  
    }  
  
    // GET all users  
    @GetMapping  
    public List<User> getAllUsers() {  
        return userService.findAll();  
    }  
  
    // GET user by email  
    @GetMapping("/by-email")  
    public Optional<User> getUserByEmail(@RequestParam String email) {  
        return userService.findByEmail(email);  
    }  
  
    // POST create a user  
    @PostMapping  
    public User createUser(@RequestBody User user) {  
        logger.info("Creating user with email: {}", user.getEmail());  
        return userService.save(user);  
    }  
}
Link to original

This is a very simple crud

  • get user : Done
  • create User : Done
  • get users : Done
  • get user by email : Done

To Do

  • Understand Dependency Injection
  • Add the DTO Layer
    • Something called pojo classes are there
  • Save the hashed password
  • Make the auth handler
  • Validate the user before allowing him the access
  • Addition Of Roles into the System

I’m thinking to Start Working on the project

Priority

  • Auth Service