Edit entities, add security package

This commit is contained in:
Dmitrii Cravcenco
2023-09-21 12:44:43 +03:00
parent 1e9a547ffc
commit 248982d128
10 changed files with 165 additions and 64 deletions

View File

@@ -1,6 +1,6 @@
package com.faf223.expensetrackerfaf.controllers;
import entities.User;
import com.faf223.expensetrackerfaf.entities.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -0,0 +1,33 @@
package com.faf223.expensetrackerfaf.entities;
public class Categories {
public enum ExpenseCategory {
CREDIT, BUY, BUSINESS, ENTERTAINMENT, RESTAURANTS_AND_CAFE, COMMUNAL_PAYMENTS, SUPERMARKET, MISC;
public String getEffectiveName() {
return Categories.getEffectiveName(this.name());
}
}
public enum IncomeCategory {
P2P, SALARY, GIFT, CREDIT, MISC;
public String getEffectiveName() {
return Categories.getEffectiveName(this.name());
}
}
private static String getEffectiveName(String name) {
String[] arr = name.split("_");
StringBuilder result = new StringBuilder();
for(String entry : arr) {
String[] entryArr = entry.split("");
StringBuilder builder = new StringBuilder(entryArr[0]);
for(int i = 1; i < entry.length(); i++) builder.append(entryArr[i].toLowerCase());
result.append(builder).append(" ");
}
return result.toString();
}
}

View File

@@ -0,0 +1,55 @@
package com.faf223.expensetrackerfaf.entities;
import com.faf223.expensetrackerfaf.util.IMoneyTransaction;
import jakarta.persistence.*;
@Entity
@Table(name = "expense")
public class Expense implements IMoneyTransaction {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
private int amount;
private Categories.ExpenseCategory category;
public Expense(User user, int amount, Categories.ExpenseCategory category) {
this.user = user;
this.amount = amount;
this.category = category;
}
public Expense() {}
@Override
public User getUser() {
return user;
}
public void setUserId(int userId) {
this.user = user;
}
@Override
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String getCategory() {
return category.getEffectiveName();
}
public void setCategory(Categories.ExpenseCategory category) {
this.category = category;
}
}

View File

@@ -0,0 +1,55 @@
package com.faf223.expensetrackerfaf.entities;
import com.faf223.expensetrackerfaf.util.IMoneyTransaction;
import jakarta.persistence.*;
@Entity
@Table(name = "income")
public class Income implements IMoneyTransaction {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
private int amount;
private Categories.IncomeCategory category;
public Income(User user, int amount, Categories.IncomeCategory category) {
this.user = user;
this.amount = amount;
this.category = category;
}
public Income() {}
@Override
public User getUser() {
return user;
}
@Override
public int getAmount() {
return amount;
}
@Override
public String getCategory() {
return category.getEffectiveName();
}
public void setUserId(User user) {
this.user = user;
}
public void setAmount(int amount) {
this.amount = amount;
}
public void setCategory(Categories.IncomeCategory category) {
this.category = category;
}
}

View File

@@ -0,0 +1,5 @@
package com.faf223.expensetrackerfaf.entities;
public enum Role {
UNREGISTERED, REGISTERED, ADMIN;
}

View File

@@ -0,0 +1,101 @@
package com.faf223.expensetrackerfaf.entities;
import jakarta.persistence.*;
import java.util.List;
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
private String login;
private String password;
private Role role;
@OneToMany(mappedBy = "user")
private List<Expense> expenses;
@OneToMany(mappedBy = "user")
private List<Income> incomes;
public User(long id, String name, String email, String login, String password, Role role, List<Expense> expenses, List<Income> incomes) {
this.id = id;
this.name = name;
this.email = email;
this.login = login;
this.password = password;
this.role = role;
this.expenses = expenses;
this.incomes = incomes;
}
public User() {}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public List<Expense> getExpenses() {
return expenses;
}
public void setExpenses(List<Expense> expenses) {
this.expenses = expenses;
}
public List<Income> getIncomes() {
return incomes;
}
public void setIncomes(List<Income> incomes) {
this.incomes = incomes;
}
}

View File

@@ -0,0 +1,59 @@
package com.faf223.expensetrackerfaf.security;
import com.faf223.expensetrackerfaf.entities.Role;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
public class PersonDetails implements UserDetails {
private final User user;
@Enumerated(EnumType.STRING)
private Role role;
public PersonDetails(User user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(role.name()));
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}

View File

@@ -0,0 +1,12 @@
package com.faf223.expensetrackerfaf.util;
import com.faf223.expensetrackerfaf.entities.User;
public interface IMoneyTransaction {
User getUser();
int getAmount();
String getCategory();
}