-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEventService.java
More file actions
70 lines (51 loc) · 2.39 KB
/
EventService.java
File metadata and controls
70 lines (51 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.kipper.eventsmicroservice.services;
import com.kipper.eventsmicroservice.domain.Event;
import com.kipper.eventsmicroservice.domain.Subscription;
import com.kipper.eventsmicroservice.dtos.EmailRequestDTO;
import com.kipper.eventsmicroservice.dtos.EventRequestDTO;
import com.kipper.eventsmicroservice.exceptions.EventFullException;
import com.kipper.eventsmicroservice.exceptions.EventNotFoundException;
import com.kipper.eventsmicroservice.repositories.EventRepository;
import com.kipper.eventsmicroservice.repositories.SubscriptionRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class EventService {
@Autowired
private EventRepository eventRepository;
@Autowired
private SubscriptionRepository subscriptionRepository;
@Autowired
private EmailServiceClient emailServiceClient;
public List<Event> getAllEvents() {
return eventRepository.findAll();
}
public List<Event> getUpcomingEvents() {
return eventRepository.findUpcomingEvents(LocalDateTime.now());
}
public Event createEvent(EventRequestDTO eventRequest) {
Event newEvent = new Event(eventRequest);
return eventRepository.save(newEvent);
}
private Boolean isEventFull(Event event){
return event.getRegisteredParticipants() >= event.getMaxParticipants();
}
// Rollback the transaction, once the EmailServiceClient throws an exception - participantEmail must be verified in SES identities.
@Transactional
public void registerParticipant(String eventId, String participantEmail) {
Event event = eventRepository.findById(eventId).orElseThrow(EventNotFoundException::new);
if(isEventFull(event)) {
throw new EventFullException();
}
Subscription subscription = new Subscription(event, participantEmail);
subscriptionRepository.save(subscription);
event.setRegisteredParticipants(event.getRegisteredParticipants() + 1);
// update the new participant
eventRepository.save(event);
EmailRequestDTO emailRequest = new EmailRequestDTO(participantEmail, "Confirmação de Inscrição", "Você foi inscrito no evento com sucesso!");
emailServiceClient.sendEmail(emailRequest);
}
}