Skip to content

Commit

Permalink
feat: add email sender func
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusVict committed Nov 26, 2023
1 parent e655beb commit 63a7294
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public ResponseEntity<UserResponseDTO> getUserByUuid(@PathVariable String uuid)
UserResponseDTO response = UserResponseDTO.toResponse(user);
return ResponseEntity.ok(response);
}

@GetMapping("/themselves")
@Operation(summary = "Get user by token")
@Tag(name = "user")
@OkResponse
@ForbiddenResponse
@NotFoundResponse
public ResponseEntity<UserResponseDTO> getUserByToken( @AuthenticationPrincipal UserDetails userDetails) {
public ResponseEntity<UserResponseDTO> getUserByToken(@AuthenticationPrincipal UserDetails userDetails) {
User user = userService.getUserByEmail(userDetails.getUsername());
UserResponseDTO response = UserResponseDTO.toResponse(user);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package com.pet.foundation.pataamiga.service.impl;

import com.pet.foundation.pataamiga.domain.email.dto.EmailDTO;
import com.pet.foundation.pataamiga.domain.email.dto.MailType;
import com.pet.foundation.pataamiga.domain.user.User;
import com.pet.foundation.pataamiga.domain.user.dto.UserCreateDTO;
import com.pet.foundation.pataamiga.domain.user.dto.UserUpdateDTO;
import com.pet.foundation.pataamiga.exceptions.EmailAlreadyExists;
import com.pet.foundation.pataamiga.exceptions.UserNotFoundException;
import com.pet.foundation.pataamiga.repositories.UserRepository;
import com.pet.foundation.pataamiga.service.EmailService;
import com.pet.foundation.pataamiga.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import static com.pet.foundation.pataamiga.utils.email.EmailValues.*;

@AllArgsConstructor
@Service
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;
private final EmailService emailService;

@Override
public String createUser(UserCreateDTO userCreateDTO) {
if (emailAlreadyExists(userCreateDTO.email()))
throw new EmailAlreadyExists("Email already exists");

User userSaved = userRepository.save(userCreateDTO.toEntity());

EmailDTO emailToUser = EmailDTO.builder()
.emailTo(userSaved.getEmail())
.emailFrom(EMAIL_FROM)
.subject(CREATE_USER_EMAIL_SUBJECT)
.text(CREATE_USER_EMAIL_TEXT)
.mailType(MailType.CREATE_USER)
.build();

emailService.sendEmail(emailToUser);
return userSaved.getUuid();
}

Expand Down

0 comments on commit 63a7294

Please sign in to comment.