Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit 9035b30

Browse files
author
ge85riz
committed
♻️ Refactor: change guests count calculation for events
1 parent 8e21d1a commit 9035b30

6 files changed

Lines changed: 19 additions & 20 deletions

File tree

backend/src/main/java/com/itestra/eep/exceptions/EventCapacityExceededException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class EventCapacityExceededException extends RuntimeException {
77

88
public EventCapacityExceededException() {
9-
super("Event has more participants than its capacity!");
9+
super("Event capacity exceeded. There are more participants than event capacity.");
1010
}
1111

1212
public EventCapacityExceededException(int emptySlots) {

backend/src/main/java/com/itestra/eep/mappers/EventMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.itestra.eep.dtos.FileDetailsDTO;
77
import com.itestra.eep.models.Event;
88
import com.itestra.eep.models.FileEntity;
9-
import com.itestra.eep.models.Participation;
109
import org.mapstruct.*;
1110

1211
import java.time.LocalDateTime;
@@ -28,20 +27,12 @@ public interface EventMapper {
2827
List<EventDetailsDTO> toDetailsDto(List<Event> events);
2928

3029
@Mappings({
31-
@Mapping(source = "participations", target = "participantCount", qualifiedByName = "countConfirmed"),
3230
@Mapping(source = "date", target = "status", qualifiedByName = "status")
3331
})
3432
EventDetailsDTO toDetailsDto(Event event);
3533

3634
FileDetailsDTO toFileDetailsDto(FileEntity file);
3735

38-
@Named("countConfirmed")
39-
default int map(List<Participation> participations) {
40-
return participations.stream().filter(Participation::isConfirmed)
41-
.mapToInt(p -> p.getGuestCount() + 1)
42-
.sum();
43-
}
44-
4536
@Named("status")
4637
default String status(LocalDateTime eventDate) {
4738
return eventDate.isAfter(LocalDateTime.now()) ? "upcoming" : "completed";

backend/src/main/java/com/itestra/eep/models/Employee.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Employee {
2121
private UUID id;
2222

2323
@MapsId
24-
@OneToOne(fetch = FetchType.LAZY, optional = false)
24+
@OneToOne(fetch = FetchType.LAZY, optional = false, orphanRemoval = true)
2525
@JoinColumn(name = "profile_id", nullable = false)
2626
private Profile profile;
2727

@@ -35,7 +35,7 @@ public class Employee {
3535
@Column(name = "employment_type")
3636
private EmploymentType employmentType;
3737

38-
@OneToMany(mappedBy = "employee")
38+
@OneToMany(mappedBy = "employee", orphanRemoval = true)
3939
private List<Participation> participations = new LinkedList<>();
4040

4141
@ManyToMany

backend/src/main/java/com/itestra/eep/models/Event.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,18 @@ public class Event {
5050
@OneToOne(mappedBy = "event", orphanRemoval = true)
5151
private Schematics schematics;
5252

53+
@Transient
54+
public int getParticipantCount() {
55+
return getParticipantCount(null);
56+
}
57+
58+
@Transient
59+
public int getParticipantCount(Participation excludeParticipation) {
60+
return participations.stream()
61+
.filter(p -> excludeParticipation == null || !p.getId().equals(excludeParticipation.getId()))
62+
.mapToInt(p -> p.getGuestCount() + 1)
63+
.sum();
64+
}
65+
5366
}
5467

backend/src/main/java/com/itestra/eep/services/impl/EventServiceImpl.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ public Event create(EventCreateDTO dto) {
5757
@Override
5858
public Event update(UUID id, EventUpdateDTO dto) {
5959
Event event = eventRepository.findById(id).orElseThrow(EventNotFoundException::new);
60-
eventMapper.updateEventFromDto(dto, event);
61-
62-
if (event.getCapacity() < participationRepository.sumGuestCounts(event, null)) {
60+
if (dto.getCapacity() != null && event.getParticipantCount(null) > dto.getCapacity()) {
6361
throw new EventCapacityExceededException();
6462
}
65-
6663
return eventRepository.save(event);
6764
}
6865

@@ -107,9 +104,7 @@ public void deleteParticipant(UUID participationId) {
107104

108105
private void validateCapacity(Event event, int guestCount, Participation excludeParticipation) {
109106

110-
Integer total = participationRepository.sumGuestCounts(event, excludeParticipation == null ? null : excludeParticipation.getId());
111-
112-
int currentTotal = total != null ? total : 0;
107+
int currentTotal = event.getParticipantCount(excludeParticipation);
113108

114109
int newTotal = currentTotal + guestCount + 1;
115110

backend/src/main/java/com/itestra/eep/webcontroller/SchematicsController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class SchematicsController {
2828
private final SchematicsMapper schematicsMapper;
2929

3030
@GetMapping("/{id}")
31-
@PreAuthorize("hasAnyAuthority('ADMIN', 'EMPLOYEE', 'VISITOR')")
31+
@PreAuthorize("hasAnyAuthority('ADMIN', 'EMPLOYEE')")
3232
public ResponseEntity<SchematicsCreateDTO> getSchematics(@PathVariable UUID id) {
3333
Schematics schematics = schematicsService.findById(id);
3434
return new ResponseEntity<>(schematicsMapper.toSchematicsCreateDTO(schematics), HttpStatus.OK);

0 commit comments

Comments
 (0)