add families
This commit is contained in:
@@ -23,10 +23,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
@@ -86,7 +83,7 @@ public class UserController {
|
|||||||
|
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
public ResponseEntity<ArrayList<UserDTO>> getAllUsers() {
|
public ResponseEntity<List<UserDTO>> getAllUsers() {
|
||||||
ArrayList<User> users = new ArrayList<>(userService.getUsers());
|
ArrayList<User> users = new ArrayList<>(userService.getUsers());
|
||||||
|
|
||||||
return ResponseEntity.ok(userMapper.toDto(users));
|
return ResponseEntity.ok(userMapper.toDto(users));
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FamilyCreationDTO {
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FamilyDTO {
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.dto.mappers;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.dto.FamilyCreationDTO;
|
||||||
|
import com.faf223.expensetrackerfaf.dto.FamilyDTO;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Family;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FamilyMapper {
|
||||||
|
|
||||||
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
|
public FamilyDTO toDto(Family family) {
|
||||||
|
return new FamilyDTO(family.getId(), family.getName(), userMapper.toDto(family.getMembers()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Family toFamily(FamilyCreationDTO familyCreationDTO) {
|
||||||
|
Family family = new Family();
|
||||||
|
family.setName(familyCreationDTO.getName());
|
||||||
|
return family;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,12 +2,9 @@ package com.faf223.expensetrackerfaf.dto.mappers;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.faf223.expensetrackerfaf.model.User;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UserMapper {
|
public class UserMapper {
|
||||||
@@ -14,11 +15,9 @@ public class UserMapper {
|
|||||||
return new UserDTO(user.getFirstName(), user.getLastName(), user.getUsername());
|
return new UserDTO(user.getFirstName(), user.getLastName(), user.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<UserDTO> toDto(ArrayList<User> user) {
|
public List<UserDTO> toDto(List<User> user) {
|
||||||
|
List<UserDTO> list = new ArrayList<>();
|
||||||
ArrayList<UserDTO> list = new ArrayList<>();
|
for (User u : user)
|
||||||
|
|
||||||
for (User u: user)
|
|
||||||
list.add(toDto(u));
|
list.add(toDto(u));
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
|||||||
@@ -38,9 +38,6 @@ public class Expense implements IMoneyTransaction {
|
|||||||
@DecimalMin(value = "0.0", inclusive = false)
|
@DecimalMin(value = "0.0", inclusive = false)
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
|
|
||||||
public Expense(LocalDate date, BigDecimal amount) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Expense(ExpenseCategory expenseCategory, LocalDate date, BigDecimal amount) {
|
public Expense(ExpenseCategory expenseCategory, LocalDate date, BigDecimal amount) {
|
||||||
this.category = expenseCategory;
|
this.category = expenseCategory;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
|
|||||||
28
src/main/java/com/faf223/expensetrackerfaf/model/Family.java
Normal file
28
src/main/java/com/faf223/expensetrackerfaf/model/Family.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Entity(name = "families")
|
||||||
|
@Builder
|
||||||
|
public class Family {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "family_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@NotEmpty
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "family", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
|
@ToString.Exclude
|
||||||
|
private List<User> members;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import lombok.*;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity(name = "users")
|
@Entity(name = "users")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class User {
|
public class User {
|
||||||
@Id
|
@Id
|
||||||
@Column(name = "user_uuid")
|
@Column(name = "user_uuid")
|
||||||
@GeneratedValue(strategy = GenerationType.UUID)
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
@@ -34,8 +35,8 @@
|
|||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
// @NotNull(message = "Password must not be null")
|
// @NotNull(message = "Password must not be null")
|
||||||
// @NotEmpty(message = "Password must not be empty")
|
// @NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@@ -45,4 +46,10 @@
|
|||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Income> incomes;
|
private List<Income> incomes;
|
||||||
}
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "family_id")
|
||||||
|
@ToString.Exclude
|
||||||
|
@JsonIgnore
|
||||||
|
private Family family;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.repository;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface FamilyRepository extends JpaRepository<IncomeCategory, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.repository.FamilyRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FamilyService {
|
||||||
|
|
||||||
|
private final FamilyRepository familyRepository;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user