qcounter
...
├── src
├── main
│ ├── java
│ │ └── com
│ │ └── proximity
│ │ └── labs
│ │ └── qcounter
│ │ ├── advice
│ │ ├── annotation
│ │ ├── cache
│ │ ├── config
│ │ ├── controllers
│ │ ├── data
│ │ │ ├── dto
│ │ │ │ ├── request
│ │ │ │ └── response
│ │ │ ├── models
│ │ │ │ ├── audit
│ │ │ │ ├── token
│ │ │ │ └── user
│ │ │ └── repositories
│ │ ├── event
│ │ │ └── listener
│ │ ├── exception
│ │ ├── security
│ │ ├── service
│ │ └── util
│ └── resources
│ ├── static
│ └── templates
...
contains classes with methods to throw proper error message. this classes are grouped based on their purpose, like AuthControllerAdvice.java
contains methods to throw auth related errors.
contains files that combine annotations or create a new one.
All the packages above are self explanatory.
contains classes used to exchange information client-server
contains database entity classes and it's helpers.
contains interface to write queries. each entity must have a repostory.
contains classes to write bussiness logic, usually before making changes to db. but this can also be used to write any other bussiness logic that doesn't involes db.
-
where to query db?
-
create abstract query method in an entity respected repository interface.
Example:
-
public interface UserRepository extends JpaRepository<User, Long> {
//...
Optional<User> findByEmail(String email);
//...
}
-
create a query method in an entity respected service class
Example:
public class UserService implements UserDetailsService{
private static final Logger logger = Logger.getLogger(UserService.class);
private final UserRepository userRepository;
private final UserDeviceService userDeviceService;
private final RefreshTokenService refreshTokenService;
@Autowired
public UserService(UserRepository userRepository, UserDeviceService userDeviceService, RefreshTokenService refreshTokenService) {
this.userRepository = userRepository;
this.userDeviceService = userDeviceService;
this.refreshTokenService = refreshTokenService;
}
/**
* Finds a user in the database by email
*/
public Optional<User> findByEmail(String email) {
return userRepository.findByEmail(email);
}
//...
}
-
Inject the service class to where you need it. like in a controller class.
Example on how to inject:
public class UserController { private static final Logger logger = Logger.getLogger(UserController.class); private final AuthService authService; private final UserService userService; private final ApplicationEventPublisher applicationEventPublisher; @Autowired public UserController(AuthService authService, UserService userService, ApplicationEventPublisher applicationEventPublisher) { this.authService = authService; this.userService = userService; this.applicationEventPublisher = applicationEventPublisher; } }