a
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.sasiedzi.event.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import jakarta.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -43,6 +46,15 @@ public class UserAccount implements Serializable {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Transient
|
||||
// @JsonSerialize
|
||||
BigDecimal balance = BigDecimal.ZERO;
|
||||
|
||||
@JsonProperty("balance")
|
||||
public BigDecimal getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public UserAccount id(Long id) {
|
||||
this.setId(id);
|
||||
return this;
|
||||
@@ -88,6 +100,10 @@ public class UserAccount implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setBalance(BigDecimal balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public Set<TransactionItem> getTransactionItems() {
|
||||
return this.transactionItems;
|
||||
}
|
||||
|
||||
@@ -39,4 +39,7 @@ public interface TransactionItemRepository extends JpaRepository<TransactionItem
|
||||
"select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event where transactionItem.id =:id"
|
||||
)
|
||||
Optional<TransactionItem> findOneWithToOneRelationships(@Param("id") Long id);
|
||||
|
||||
@Query("select transactionItem from TransactionItem transactionItem where transactionItem.userAccount.id =:accountId")
|
||||
List<TransactionItem> getTransactionItemsForAccountId(@Param("accountId") Long accountId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sasiedzi.event.repository;
|
||||
|
||||
import com.sasiedzi.event.domain.Charge;
|
||||
import com.sasiedzi.event.domain.UserAccount;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -27,4 +28,8 @@ public interface UserAccountRepository extends UserAccountRepositoryWithBagRelat
|
||||
default Page<UserAccount> findAllWithEagerRelationships(Pageable pageable) {
|
||||
return this.fetchBagRelationships(this.findAll(pageable));
|
||||
}
|
||||
|
||||
@Query("select userAccount from UserAccount userAccount join fetch userAccount.users")
|
||||
// List<UserAccount> findByUserLogin(String login);
|
||||
List<UserAccount> findAllFetchAccounts();
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@ package com.sasiedzi.event.service;
|
||||
|
||||
import com.sasiedzi.event.domain.*;
|
||||
import com.sasiedzi.event.domain.enumeration.TransactionType;
|
||||
import com.sasiedzi.event.repository.EventRepository;
|
||||
import com.sasiedzi.event.repository.TransactionRepository;
|
||||
import com.sasiedzi.event.repository.UserAccountRepository;
|
||||
import com.sasiedzi.event.web.rest.TransactionResource;
|
||||
import com.sasiedzi.event.repository.*;
|
||||
import com.sasiedzi.event.web.rest.AccountResource;
|
||||
import jakarta.validation.Valid;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -16,6 +14,8 @@ import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.AuthenticatedPrincipal;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -30,6 +30,15 @@ public class EventService {
|
||||
|
||||
private final EventRepository eventRepository;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountResource accountResource;
|
||||
|
||||
public EventService(EventRepository eventRepository) {
|
||||
this.eventRepository = eventRepository;
|
||||
}
|
||||
@@ -186,10 +195,7 @@ public class EventService {
|
||||
String login = registration.getUser().getLogin();
|
||||
UserAccount userAccount = accountsByLogin.get(login);
|
||||
if (userAccount == null) {
|
||||
userAccount = new UserAccount();
|
||||
userAccount.setName(registration.getUser().getName());
|
||||
userAccount.getUsers().add(registration.getUser());
|
||||
userAccount = userAccountRepository.save(userAccount);
|
||||
userAccount = createNewAccountForLogin(login);
|
||||
accountsByLogin.put(login, userAccount);
|
||||
}
|
||||
accountsToCharge.add(userAccount);
|
||||
@@ -227,4 +233,72 @@ public class EventService {
|
||||
|
||||
return eventRepository.findById(event.getId());
|
||||
}
|
||||
|
||||
public String getCurrentAuthenticatedUserAsDTO() {
|
||||
if (SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||
return null;
|
||||
}
|
||||
if (SecurityContextHolder.getContext() == null) {
|
||||
return null;
|
||||
}
|
||||
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
if (principal instanceof AuthenticatedPrincipal) {
|
||||
return ((AuthenticatedPrincipal) principal).getName();
|
||||
// return userService.getUserFromAuthentication((AbstractAuthenticationToken) principal);
|
||||
} else {
|
||||
throw new RuntimeException("User could not be found");
|
||||
}
|
||||
}
|
||||
|
||||
public UserAccount createNewAccountForLogin(String login) {
|
||||
UserAccount entity = new UserAccount();
|
||||
Optional<User> oneByLogin = userRepository.findOneByLogin(login);
|
||||
if (oneByLogin.isPresent()) {
|
||||
entity.setName(oneByLogin.get().getName());
|
||||
entity.getUsers().add(oneByLogin.get());
|
||||
entity = userAccountRepository.save(entity);
|
||||
return entity;
|
||||
} else {
|
||||
throw new RuntimeException("user not found");
|
||||
}
|
||||
}
|
||||
|
||||
public UserAccount getOrCreateForCurrentUser() {
|
||||
// if (true) return new UserAccount();
|
||||
String username = getCurrentAuthenticatedUserAsDTO();
|
||||
if (username != null) {
|
||||
// List<UserAccount> userAccounts = userAccountRepository.findByUserLogin(currentAuthenticatedUserAsDTO.getLogin());
|
||||
List<UserAccount> userAccounts = userAccountRepository
|
||||
.findAllFetchAccounts()
|
||||
.stream()
|
||||
.filter(accountResource -> {
|
||||
return accountResource.getUsers().stream().anyMatch(user -> user.getLogin().equals(username));
|
||||
})
|
||||
.toList();
|
||||
if (userAccounts.isEmpty()) {
|
||||
return createNewAccountForLogin(username);
|
||||
} else {
|
||||
UserAccount userAccount = userAccounts.get(0);
|
||||
List<TransactionItem> transactionItemsForAccountId = transactionItemRepository.getTransactionItemsForAccountId(
|
||||
userAccount.getId()
|
||||
);
|
||||
|
||||
BigDecimal totalBalance = transactionItemsForAccountId
|
||||
.stream()
|
||||
.map(TransactionItem::getAmount)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
userAccount.setBalance(totalBalance);
|
||||
|
||||
return userAccount;
|
||||
}
|
||||
} else {
|
||||
UserAccount userAccount = new UserAccount();
|
||||
userAccount.setName("Unauthenticated User");
|
||||
return userAccount;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
TransactionItemRepository transactionItemRepository;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sasiedzi.event.service.dto;
|
||||
|
||||
import com.sasiedzi.event.domain.User;
|
||||
import jakarta.persistence.Transient;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.sasiedzi.event.web.rest;
|
||||
|
||||
import com.sasiedzi.event.domain.User;
|
||||
import com.sasiedzi.event.domain.UserAccount;
|
||||
import com.sasiedzi.event.repository.UserAccountRepository;
|
||||
import com.sasiedzi.event.service.EventService;
|
||||
import com.sasiedzi.event.service.UserService;
|
||||
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -10,8 +13,11 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import tech.jhipster.web.util.HeaderUtil;
|
||||
@@ -166,6 +172,12 @@ public class UserAccountResource {
|
||||
return ResponseUtil.wrapOrNotFound(userAccount);
|
||||
}
|
||||
|
||||
@GetMapping("/currentUser")
|
||||
public UserAccount getCurrentUserAccount() {
|
||||
UserAccount userAccount = eventService.getOrCreateForCurrentUser();
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /user-accounts/:id} : delete the "id" userAccount.
|
||||
*
|
||||
@@ -180,4 +192,7 @@ public class UserAccountResource {
|
||||
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
EventService eventService;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user