Skip to content

Commit

Permalink
fix: fix caching, added logging and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Troha7 committed Jan 23, 2024
1 parent ddd16d7 commit 2ccf684
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions src/main/java/com/chat/yourway/service/ContactServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.chat.yourway.dto.request.ContactRequestDto;
import com.chat.yourway.dto.request.EditContactProfileRequestDto;
import com.chat.yourway.dto.response.ContactProfileResponseDto;
import com.chat.yourway.exception.*;
import com.chat.yourway.exception.ContactNotFoundException;
import com.chat.yourway.exception.PasswordsAreNotEqualException;
import com.chat.yourway.exception.ValueNotUniqException;
import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Role;
import com.chat.yourway.repository.ContactRepository;
import com.chat.yourway.service.interfaces.ContactService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -27,13 +30,15 @@ public class ContactServiceImpl implements ContactService {
@Transactional
@Override
public Contact create(ContactRequestDto contactRequestDto) {
log.trace("Started create contact, contact email: {}", contactRequestDto.getEmail());
log.trace("Started create contact, contact email: [{}]", contactRequestDto.getEmail());

if (isEmailExists(contactRequestDto.getEmail())) {
log.warn("Email [{}] already in use", contactRequestDto.getEmail());
throw new ValueNotUniqException(
String.format("Email %s already in use", contactRequestDto.getEmail()));
String.format("Email [%s] already in use", contactRequestDto.getEmail()));
}

return contactRepository.save(
Contact contact = contactRepository.save(
Contact.builder()
.nickname(contactRequestDto.getNickname())
.avatarId(contactRequestDto.getAvatarId())
Expand All @@ -43,86 +48,109 @@ public Contact create(ContactRequestDto contactRequestDto) {
.isPrivate(true)
.role(Role.USER)
.build());

log.info("New contact with email [{}] was created", contactRequestDto.getEmail());
return contact;
}

@Override
@Cacheable("contacts")
@Transactional(readOnly = true)
@Cacheable(value = "contacts", key = "#email")
public Contact findByEmail(String email) {
log.trace("Started findByEmail: {}", email);
return contactRepository
log.trace("Started findByEmail: [{}]", email);
Contact contact = contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));
.orElseThrow(() -> {
log.warn("Email [{}] wasn't found", email);
return new ContactNotFoundException(String.format("Email [%s] wasn't found", email));
});

log.info("Contact was found by email [{}]", email);
return contact;
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#email")
public void changePasswordByEmail(String password, String email) {
log.trace("Started change password by email [{}]", email);
contactRepository.changePasswordByEmail(passwordEncoder.encode(password), email);
log.info("Password was changed by email [{}]", email);
}

@Override
public void verifyPassword(String password, String encodedPassword) {
log.trace("Started verify password");

if (!passwordEncoder.matches(password, encodedPassword)) {
log.warn("Password was not verify");
throw new PasswordsAreNotEqualException();
}
log.info("Password was verified");
}

@Transactional
@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void updateContactProfile(
EditContactProfileRequestDto editContactProfileRequestDto, UserDetails userDetails) {
log.trace("Started updating contact profile: {}", editContactProfileRequestDto);
log.trace("Started updating contact profile: [{}]", editContactProfileRequestDto);

String email = userDetails.getUsername();
Contact contact =
contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));

Contact contact = findByEmail(email);

contact.setNickname(editContactProfileRequestDto.getNickname());
contact.setAvatarId(editContactProfileRequestDto.getAvatarId());

contact = contactRepository.save(contact);
log.trace("Updated contact: {}", contact);
contactRepository.save(contact);

log.info("Updated contact by email [{}]", email);
}

@Override
public boolean isEmailExists(String email) {
log.trace("Started check is email exists in repository");
return contactRepository.existsByEmailIgnoreCase(email);
}

@Override
public ContactProfileResponseDto getContactProfile(UserDetails userDetails) {
String email = userDetails.getUsername();
Contact contact =
contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));
log.trace("Started get contact profile by email [{}]", email);

Contact contact = findByEmail(email);
ContactProfileResponseDto responseDto = new ContactProfileResponseDto();

responseDto.setNickname(contact.getNickname());
responseDto.setAvatarId(contact.getAvatarId());
responseDto.setEmail(email);
responseDto.setHasPermissionSendingPrivateMessage(contact.isPermittedSendingPrivateMessage());

log.info("Contact profile was got by email [{}]", email);
return responseDto;
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void permitSendingPrivateMessages(UserDetails userDetails) {
log.trace("Started permit sending private messages by email [{}]", userDetails.getUsername());
boolean isPermittedSendingPrivateMessage = true;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
log.info("Permitted sending private messages by email [{}]", userDetails.getUsername());
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void prohibitSendingPrivateMessages(UserDetails userDetails) {
log.trace("Started prohibit sending private messages by email [{}]", userDetails.getUsername());
boolean isPermittedSendingPrivateMessage = false;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
log.info("Prohibited sending private messages by email [{}]", userDetails.getUsername());
}

private void changePermissionSendingPrivateMessages(
Expand Down

0 comments on commit 2ccf684

Please sign in to comment.