Steps to Write an Api Service
Link to original
- 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
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 Name | Data Type | Required | Indexed | Sample |
|---|---|---|---|---|
| string | yes | yes | joe@email.com | |
| name | string | yes | no | joe |
| lastname | string | no | no | biden |
| password | string | yes | no | fnwenbqboojow2 |
| role | string | yes | no | user |
| emailVerifiedAt | timestamp | yes | no | timestamp |
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;
}
}