Tinder-Clone/src/ru/charm/back/service/ProfileService.java

59 lines
2.0 KiB
Java

package ru.charm.back.service;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import ru.charm.back.dao.ProfileDao;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.dto.ProfileUpdateDto;
import ru.charm.back.dto.RegistrationDto;
import ru.charm.back.mapper.ProfileToProfileGetDtoMapper;
import ru.charm.back.mapper.ProfileUpdateDtoToProfileMapper;
import ru.charm.back.mapper.RegistrationDtoToProfileMapper;
import ru.charm.back.model.exception.DuplicateEmailException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ProfileService {
private static final ProfileService INSTANCE = new ProfileService();
private final ProfileDao dao = ProfileDao.getInstance();
private final ProfileToProfileGetDtoMapper profileToProfileGetDtoMapper = ProfileToProfileGetDtoMapper.getInstance();
private final ProfileUpdateDtoToProfileMapper profileUpdateDtoToProfileMapper = ProfileUpdateDtoToProfileMapper.getInstance();
private final RegistrationDtoToProfileMapper registrationDtoToProfileMapper = RegistrationDtoToProfileMapper.getInstance();
public static ProfileService getInstance() {
return INSTANCE;
}
public Long save(RegistrationDto dto) {
return dao.save(registrationDtoToProfileMapper.map(dto)).getId();
}
public Optional<ProfileGetDto> findById(Long id) {
return dao.findById(id).map(profileToProfileGetDtoMapper::map);
}
public List<ProfileGetDto> findAll() {
return dao.findAll().stream().map(profileToProfileGetDtoMapper::map).toList();
}
public void update(ProfileUpdateDto dto) {
dao.findById(dto.getId())
.ifPresent(profile -> {
dao.update(profileUpdateDtoToProfileMapper.map(dto, profile));
}
);
}
public boolean delete(Long id) {
return dao.delete(id);
}
}