currentEventCalculation

This commit is contained in:
2024-11-30 13:11:55 +01:00
parent 5de13e8a2b
commit 620e615e72
8 changed files with 89 additions and 7 deletions
@@ -1,6 +1,7 @@
package com.sasiedzi.event.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sasiedzi.event.domain.enumeration.TransactionType;
import jakarta.persistence.*;
import java.io.Serializable;
@@ -42,6 +43,19 @@ public class Transaction implements Serializable {
@JsonIgnoreProperties(value = { "transaction", "event", "registration" }, allowSetters = true)
private Set<TransactionItem> transactionItems = new HashSet<>();
@Transient
private UserAccount beneficiary;
@JsonProperty("beneficiary")
public UserAccount getBeneficiary() {
return beneficiary;
}
@JsonProperty("beneficiary")
public void setBeneficiary(UserAccount beneficiary) {
this.beneficiary = beneficiary;
}
// jhipster-needle-entity-add-field - JHipster will add fields here
public Long getId() {
@@ -323,6 +323,29 @@ public class EventService {
}
}
public Long getCurrentEventId() {
List<Event> allEvents = eventRepository.findAll();
Event currentEvent = allEvents
.stream()
.filter(event -> event.getDate() != null && event.getDate().isAfter(LocalDate.now()))
.min(Comparator.comparing(Event::getDate))
.orElse(null);
if (currentEvent == null) {
Event latestEvent = allEvents
.stream()
.filter(event -> event.getDate() != null)
.max(Comparator.comparing(Event::getDate))
.orElse(null);
if (latestEvent != null) {
return latestEvent.getId();
} else {
return -1L;
}
} else {
return currentEvent.getId();
}
}
@Autowired
TransactionItemRepository transactionItemRepository;
@@ -0,0 +1,19 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
@Component
public class MyInfoContributor implements InfoContributor {
@Autowired
EventService eventService;
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("currentEventId", eventService.getCurrentEventId());
}
}
@@ -5,6 +5,7 @@ import com.sasiedzi.event.domain.Registration;
import com.sasiedzi.event.domain.User;
import com.sasiedzi.event.repository.RegistrationRepository;
import com.sasiedzi.event.repository.UserRepository;
import com.sasiedzi.event.service.EventService;
import com.sasiedzi.event.service.UserService;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
@@ -51,12 +52,17 @@ public class RegistrationResource {
@Autowired
private UserRepository userRepository;
@Autowired
private EventService eventService;
public RegistrationResource(RegistrationRepository registrationRepository, UserRepository userRepository) {
this.registrationRepository = registrationRepository;
this.userRepository = userRepository;
}
private static Long currentEventId = 2601L;
private Long getCurrentEventId() {
return eventService.getCurrentEventId();
}
@Autowired
CurrentUserHolder currentUser;
@@ -72,7 +78,8 @@ public class RegistrationResource {
public ResponseEntity<Registration> createRegistration(@Valid @RequestBody Registration registration, Principal principal)
throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId)
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
@@ -110,7 +117,8 @@ public class RegistrationResource {
@Valid @RequestBody Registration registration
) throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId)
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
@@ -149,7 +157,8 @@ public class RegistrationResource {
@NotNull @RequestBody Registration registration
) throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId)
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
@@ -232,7 +241,8 @@ public class RegistrationResource {
public ResponseEntity<Void> deleteRegistration(@PathVariable("id") Long id) {
Registration registration = registrationRepository.findById(id).get();
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId)
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}