update families
This commit is contained in:
@@ -98,7 +98,7 @@ public class ExpenseController {
|
||||
|
||||
@GetMapping("/personal-expenses")
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByTimeUnits(@RequestParam Optional<LocalDate> date,
|
||||
@RequestParam Optional<Integer> month,
|
||||
@RequestParam Optional<Integer> startYear,
|
||||
@RequestParam Optional<Integer> endYear,
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.FamilyCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.FamilyDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.FamilyMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Family;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.service.FamilyService;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.*;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/families")
|
||||
@RequiredArgsConstructor
|
||||
public class FamilyController {
|
||||
|
||||
private final FamilyService familyService;
|
||||
private final FamilyMapper familyMapper;
|
||||
private final UserService userService;
|
||||
|
||||
@GetMapping()
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<List<FamilyDTO>> getAllFamilies() {
|
||||
List<FamilyDTO> families = familyService.getFamilies().stream().map(familyMapper::toDto).collect(Collectors.toList());
|
||||
if (!families.isEmpty()) return ResponseEntity.ok(families);
|
||||
else throw new FamiliesNotFoundException("Families not found");
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Map<String, Long>> createNewFamily(@RequestBody @Valid FamilyCreationDTO familyCreationDTO,
|
||||
BindingResult bindingResult) {
|
||||
if(bindingResult.hasErrors())
|
||||
throw new FamilyNotCreatedException("Could not create new family");
|
||||
|
||||
Family family = familyMapper.toFamily(familyCreationDTO);
|
||||
|
||||
familyService.createOrUpdate(family);
|
||||
Map<String, Long> response = new HashMap<>();
|
||||
response.put("familyId", family.getId());
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(response);
|
||||
}
|
||||
|
||||
@PatchMapping("/update/{id}")
|
||||
public ResponseEntity<Void> updateFamily(@PathVariable long id, @RequestBody @Valid FamilyCreationDTO familyDTO,
|
||||
BindingResult bindingResult) {
|
||||
if(bindingResult.hasErrors())
|
||||
throw new FamilyNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||
|
||||
Family family = familyService.getFamilyById(id);
|
||||
|
||||
if(family == null)
|
||||
throw new FamiliesNotFoundException("The family has not been found");
|
||||
|
||||
if(!familyService.containsMember(family))
|
||||
throw new NotAMemberOfTheFamily("You are not a member of this family");
|
||||
|
||||
family.setName(familyDTO.getName());
|
||||
|
||||
familyService.createOrUpdate(family);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void deleteFamily(@PathVariable long id) {
|
||||
familyService.deleteFamilyById(id);
|
||||
}
|
||||
|
||||
@PatchMapping("/add-member/{id}")
|
||||
public ResponseEntity<Void> addFamilyMember(@PathVariable long id, @RequestParam Optional<String> email) {
|
||||
if(email.isEmpty())
|
||||
throw new UserNotFoundException("You have not specified the user email");
|
||||
|
||||
Family family = familyService.getFamilyById(id);
|
||||
|
||||
if(family == null)
|
||||
throw new FamiliesNotFoundException("The family has not been found");
|
||||
|
||||
if(!familyService.containsMember(family))
|
||||
throw new NotAMemberOfTheFamily("You are not a member of this family");
|
||||
|
||||
User user = userService.getUserByEmail(email.get());
|
||||
|
||||
if(user == null)
|
||||
throw new UserNotFoundException("User with the specified email has not been found");
|
||||
|
||||
family.getMembers().add(user);
|
||||
|
||||
familyService.createOrUpdate(family);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
@PatchMapping("/remove-member/{id}")
|
||||
public ResponseEntity<Void> removeFamilyMember(@PathVariable long id, @RequestParam Optional<String> email) {
|
||||
if(email.isEmpty())
|
||||
throw new UserNotFoundException("You have not specified the user email");
|
||||
|
||||
Family family = familyService.getFamilyById(id);
|
||||
|
||||
if(family == null)
|
||||
throw new FamiliesNotFoundException("The family has not been found");
|
||||
|
||||
if(!familyService.containsMember(family))
|
||||
throw new NotAMemberOfTheFamily("You are not a member of this family");
|
||||
|
||||
User user = userService.getUserByEmail(email.get());
|
||||
|
||||
if(user == null)
|
||||
throw new UserNotFoundException("User with the specified email has not been found");
|
||||
|
||||
family.getMembers().remove(user);
|
||||
|
||||
familyService.createOrUpdate(family);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class IncomeController {
|
||||
|
||||
@GetMapping("/personal-incomes")
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<List<IncomeDTO>> getIncomesByUser(@RequestParam Optional<LocalDate> date,
|
||||
public ResponseEntity<List<IncomeDTO>> getIncomesByTimeUnits(@RequestParam Optional<LocalDate> date,
|
||||
@RequestParam Optional<Integer> month,
|
||||
@RequestParam Optional<Integer> startYear,
|
||||
@RequestParam Optional<Integer> endYear,
|
||||
|
||||
@@ -5,11 +5,12 @@ import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity(name = "families")
|
||||
@Entity(name = "family")
|
||||
@Builder
|
||||
public class Family {
|
||||
|
||||
@@ -19,10 +20,23 @@ public class Family {
|
||||
private Long id;
|
||||
|
||||
@NotEmpty
|
||||
@Column(name = "family_name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "family", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
@ToString.Exclude
|
||||
private List<User> members;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Family family = (Family) o;
|
||||
return Objects.equals(id, family.id) && Objects.equals(name, family.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.faf223.expensetrackerfaf.repository;
|
||||
|
||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||
import com.faf223.expensetrackerfaf.model.Family;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface FamilyRepository extends JpaRepository<IncomeCategory, Long> {
|
||||
public interface FamilyRepository extends JpaRepository<Family, Long> {
|
||||
}
|
||||
|
||||
@@ -130,6 +130,8 @@ public class ExpenseService implements ITransactionService {
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||
|
||||
@@ -1,13 +1,67 @@
|
||||
package com.faf223.expensetrackerfaf.service;
|
||||
|
||||
import com.faf223.expensetrackerfaf.model.Credential;
|
||||
import com.faf223.expensetrackerfaf.model.Family;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||
import com.faf223.expensetrackerfaf.repository.FamilyRepository;
|
||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FamilyService {
|
||||
|
||||
private final FamilyRepository familyRepository;
|
||||
private final CredentialRepository credentialRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public List<Family> getFamilies() {
|
||||
return familyRepository.findAll();
|
||||
}
|
||||
|
||||
public void createOrUpdate(Family family) {
|
||||
familyRepository.save(family);
|
||||
}
|
||||
|
||||
public Family getFamilyById(long id) {
|
||||
return familyRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public boolean containsMember(Family family) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||
|
||||
if(authentication.getAuthorities().stream().noneMatch(authority -> authority.getAuthority().equals("ADMIN"))) {
|
||||
|
||||
Optional<Credential> credential = credentialRepository.findByEmail(userDetails.getUsername());
|
||||
if(credential.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||
Optional<User> user = userRepository.findById(credential.get().getUser().getUserUuid());
|
||||
if(user.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||
|
||||
return user.get().getFamily().equals(family);
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||
}
|
||||
|
||||
public void deleteFamilyById(long id) {
|
||||
familyRepository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -129,6 +129,8 @@ public class IncomeService implements ITransactionService {
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||
|
||||
public class FamiliesNotFoundException extends RuntimeException {
|
||||
|
||||
public FamiliesNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||
|
||||
public class FamilyNotCreatedException extends RuntimeException {
|
||||
|
||||
public FamilyNotCreatedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||
|
||||
public class FamilyNotUpdatedException extends RuntimeException {
|
||||
|
||||
public FamilyNotUpdatedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||
|
||||
public class NotAMemberOfTheFamily extends RuntimeException {
|
||||
|
||||
public NotAMemberOfTheFamily(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user