Search in sources :

Example 1 with ObjectNotFoundException

use of pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException in project Flare-event-calendar by PollubCafe.

the class EventServiceImpl method createEvent.

@Override
public SimplifiedEventResponseDTO createEvent(CreateEventReqDTO createEventReqDTO) {
    String authenticatedUserNick = SecurityContextHolder.getContext().getAuthentication().getName();
    User authenticatedUser = userRepository.findByNick(authenticatedUserNick).orElseThrow(() -> new ObjectNotFoundException(User.class, "nick", authenticatedUserNick));
    Address address = Address.builder().town(createEventReqDTO.getAddress_town()).zipCode(createEventReqDTO.getAddress_zipCode()).street(createEventReqDTO.getAddress_street()).province(Province.valueOf(createEventReqDTO.getAddress_province())).blockNumber(createEventReqDTO.getAddress_blockNumber()).houseNumber(createEventReqDTO.getAddress_houseNumber()).additionalInformation(createEventReqDTO.getAddress_additionalInformation()).build();
    Event event = Event.builder().address(address).title(createEventReqDTO.getTitle()).description(createEventReqDTO.getDescription()).duration(createEventReqDTO.getDuration()).dateOfEventApproval(createEventReqDTO.getDateOfEventApproval().toInstant()).isApproved(false).onlyForRegisteredUsers(createEventReqDTO.isOnlyForRegisteredUsers()).dateOfCreation(Instant.now()).build();
    authenticatedUser.addEvent(event);
    Event createdEvent = eventRepository.save(event);
    userRepository.save(authenticatedUser);
    return eventMapper.mapToResponseDTO(createdEvent);
}
Also used : ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException)

Example 2 with ObjectNotFoundException

use of pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException in project Flare-event-calendar by PollubCafe.

the class SecurityServiceImpl method resetPassword.

@Override
@Transactional(rollbackFor = { MessagingException.class, ObjectNotFoundException.class, ResetPasswordException.class })
public void resetPassword(EmailReqDTO emailReqDTO) {
    String recipientAddress = emailReqDTO.getEmail();
    User user = userRepository.findByEmail(recipientAddress).orElseThrow(() -> new ObjectNotFoundException(User.class, "e-mail", emailReqDTO.getEmail()));
    if (!user.isEnabled()) {
        throw new ResetPasswordException(messages.get("login.userAccount.disabled"));
    }
    if (user.isBanned()) {
        throw new ResetPasswordException(messages.get("login.userAccount.locked"));
    }
    String generatedPassword = RandomPasswordGenerator.generate();
    user.setPassword(passwordEncoder.encode(generatedPassword));
    userRepository.save(user);
    Email resetPasswordEmail = emailBuilder.buildResetPasswordEmail(generatedPassword).to(recipientAddress);
    this.sendResetPasswordEmail(resetPasswordEmail);
}
Also used : User(pl.pollub.cs.pentagoncafe.flare.domain.User) Email(pl.pollub.cs.pentagoncafe.flare.component.email.Email) HtmlEmail(pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail) ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException) ResetPasswordException(pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ObjectNotFoundException

use of pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException in project Flare-event-calendar by PollubCafe.

the class EmailSenderServiceImpl method sendEmail.

@Override
public void sendEmail(EmailToEventAuthorReqDTO emailToEventAuthorReqDTO) {
    Event event = eventRepository.findById(emailToEventAuthorReqDTO.getEventID()).orElseThrow(() -> new ObjectNotFoundException(Event.class, "id", emailToEventAuthorReqDTO.getEventID()));
    User eventAuthor = event.getOrganizer();
    String message = emailToEventAuthorReqDTO.getContent();
    String subject = emailToEventAuthorReqDTO.getSubject();
    Email email = emailBuilder.buildEmailToEventAuthor(message, subject).to(eventAuthor.getEmail());
    try {
        checkArgument(email instanceof HtmlEmail);
        emailSender.send((HtmlEmail) email);
    } catch (MessagingException e) {
        e.printStackTrace();
        throw new SendingEmailException("Email to author sending filed.");
    }
}
Also used : User(pl.pollub.cs.pentagoncafe.flare.domain.User) Email(pl.pollub.cs.pentagoncafe.flare.component.email.Email) HtmlEmail(pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail) MessagingException(javax.mail.MessagingException) ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException) HtmlEmail(pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail) Event(pl.pollub.cs.pentagoncafe.flare.domain.Event) SendingEmailException(pl.pollub.cs.pentagoncafe.flare.exception.sendingEmail.SendingEmailException)

Aggregations

ObjectNotFoundException (pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException)3 Email (pl.pollub.cs.pentagoncafe.flare.component.email.Email)2 HtmlEmail (pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail)2 User (pl.pollub.cs.pentagoncafe.flare.domain.User)2 MessagingException (javax.mail.MessagingException)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Event (pl.pollub.cs.pentagoncafe.flare.domain.Event)1 ResetPasswordException (pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException)1 SendingEmailException (pl.pollub.cs.pentagoncafe.flare.exception.sendingEmail.SendingEmailException)1