The Magic you’re looking for is in the work you’re avoiding.

Progress till Now

  • I understood the DTO Part

    Explanation of the DTOs in this flow:

    • LoginRequestDTO:

      • Represents the data sent from the client to the server.

      • Contains username and password fields.

      • Used by Spring to deserialize the incoming JSON request body into a Java object.

      • Helps the controller safely receive only the expected data.

    • LoginResponseDTO:

      • Represents the data sent from the server back to the client.

      • In this case, contains only the username.

      • Used by Spring to serialize the Java object back into JSON for the HTTP response.

      • Helps control exactly what data is exposed to the client (e.g., hiding password).


    Link to original

So Remaining from day 5 is

I’m thinking let’s create a simple user crud apis

Api List

  • Create User
  • Get User
  • Update User
  • Delete User

This will be a user service

User Service


Steps to Write an Api Service

  • Understand the requirement of the Client
  • Fix the database type
  • Design the table (model)
  • Write the Orm Layer
  • Write the service layer
  • Join with the controller and Done
Link to original

Okay So let’s start making this.

Requirement make a user Service

To make a database first we’ll have to decide which type of database

According to this document : How To choose a database for my System

Database for this should be relational so Postgres

Table Name : Users

Field NameData TypeRequiredIndexedSample
emailstringyesyesjoe@email.com
namestringyesnojoe
lastnamestringnonobiden
passwordstringyesnofnwenbqboojow2
rolestringyesnouser
emailVerifiedAttimestampyesnotimestamp

User Service

Service Class : V0

Optional will be used a lot of places

package in.abhi8290.helloworld.user;  
  
import in.abhi8290.helloworld.core.base.BaseService;  
import org.springframework.stereotype.Service;  
  
import java.util.Optional;  
  
@Service  
public class UserService extends BaseService<User, String> {  
  
    private final UserRepository userRepository;  
  
    // Spring will inject this automatically  
    public UserService(UserRepository userRepository) {  
        this.userRepository = userRepository;  
    }  
  
    @Override  
    protected UserRepository getRepository() {  
        return userRepository;  
    }  
  
    public Optional<User> findByEmail(String email) {  
        return Optional.ofNullable(userRepository.findByEmail(email));  
    }  
  
    public boolean emailExists(String email) {  
        return userRepository.existsByEmail(email);  
    }  
}

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);  
    }  
}

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);  
  
  
}

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

🥲 But I don’t have postgres running in my local system Run Postgres Locally

Postgres Running 🔋

Connect to database : How to Connect the Database in spring-boot

Test the connection: - can we print the number of tables in there ?