Cześć,
napisałem kod odpowiedzialny za usuwanie użytkownika. Mam problem z oceną, czy zrobiłem to tak, jak należy, a dokładniej chodzi mi o jedną rzecz. Funkcjonalność klas takich jak RoleDeleterImpl
, SessionDeleterImpl
, ogranicza się do wykorzystania metod zdefiniowanych w DAO. Zrobiłem to po to, żeby można było to łatwiej przetestować. Otrzymałem komentarz, że stworzyłem dużo bezwartościowego kodu, ponieważ te moje klasy robią praktycznie to samo, co DAO i tak nie powinno być. Jaka jest Wasza opinia na ten temat? Czy mogę i powinienem tak robić? Ewentualnie co powinienem zmienić.
Dzięki za pomoc
UserDAO:
public interface UserDAO {
@SqlQuery("SELECT * FROM users WHERE id = :id")
@RegisterBeanMapper(User.class)
Optional<User> findById(@Bind("id") int id);
@SqlUpdate("DELETE FROM users WHERE id = :id")
void delete(@BindBean User user);
@SqlUpdate("DELETE FROM user_roles WHERE user_id = :userId")
void deleteUserRoles(@Bind("userId") int userId);
}
SessionDAO:
public interface SessionDAO {
@SqlUpdate("DELETE FROM sessions WHERE user_id = :userId")
void deleteSessionsByUserId(@Bind("userId") int userId);
}
Klasy:
@Service
public class RoleDeleterImpl implements RoleDeleter {
private final UserDAO userDAO;
public RoleDeleterImpl(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public void deleteUserRoles(int userId) {
userDAO.deleteUserRoles(userId);
}
}
@Service
public class SessionDeleterImpl implements SessionDeleter {
private final SessionDAO sessionDAO;
public SessionDeleterImpl(SessionDAO sessionDAO) {
this.sessionDAO = sessionDAO;
}
@Override
public void deleteUserSessions(int userId) {
sessionDAO.deleteSessionsByUserId(userId);
}
}
@Service
public class UserRetrievalImpl implements UserRetrieval {
private final UserDAO userDAO;
public UserRetrievalImpl(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public User findByUserId(int userId) {
return userDAO.findById(userId).orElseThrow(() -> new UserNotFoundException(
"User not found with id: " + userId));
}
}
@Service
public class UserDeleterImpl implements UserDeleter {
private final UserDAO userDAO;
private final UserRetrieval userRetrieval;
public UserDeleterImpl(UserDAO userDAO, UserRetrieval userRetrieval) {
this.userDAO = userDAO;
this.userRetrieval = userRetrieval;
}
@Override
public void deleteUser(int userId) {
User user = userRetrieval.findByUserId(userId);
userDAO.delete(user);
}
}
@Service
public class UserDeletionManagerImpl implements UserDeletionManager {
private final RoleDeleter roleDeleter;
private final UserDeleter userDeleter;
private final SessionDeleter sessionDeleter;
public UserDeletionManager(RoleDeleterImpl roleDeleter,
UserDeleterImpl userDeleter,
SessionDeleterImpl sessionDeleter) {
this.roleDeleter = roleDeleter;
this.userDeleter = userDeleter;
this.sessionDeleter = sessionDeleter;
}
@Transactional
public void executeDeletion(int userId) {
roleDeleter.deleteUserRoles(userId);
sessionDeleter.deleteUserSessions(userId);
userDeleter.deleteUser(userId);
}
}
@Service
@Slf4j
class DeleteUserServiceImpl implements DeleteUserService {
private final UserDeletionManager deletionManager;
private final ApplicationMessageService messageService;
public DeleteUserServiceImpl(UserDeletionManager deletionManager, ApplicationMessageService messageService) {
this.deletionManager = deletionManager;
this.messageService = messageService;
}
@Override
public ApiResponse deleteUser(int userId) throws DatabaseException {
deletionManager.executeDeletion(userId);
log.info("User with ID {} deleted successfully", userId);
return new ApiResponse(messageService.getMessage("user.delete.success", userId));
}
}