Merge pull request #5 from lumijiez/daniel

This commit was merged in pull request #5.
This commit is contained in:
Daniel
2023-09-21 13:00:50 +03:00
committed by GitHub
19 changed files with 431 additions and 65 deletions

13
pom.xml
View File

@@ -21,13 +21,26 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
<version>3.1.2</version> <version>3.1.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>

View File

@@ -0,0 +1,26 @@
package com.faf223.expensetrackerfaf.controller;
import com.faf223.expensetrackerfaf.model.BasicEntity;
import com.faf223.expensetrackerfaf.repository.BasicRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/getData")
public class BasicController {
private final BasicRepository basicRepository;
@Autowired
public BasicController(BasicRepository basicRepository) {
this.basicRepository = basicRepository;
}
@GetMapping()
public List<BasicEntity> listDepartments() {
return basicRepository.findAll();
}
}

View File

@@ -0,0 +1,66 @@
package com.faf223.expensetrackerfaf.controller;
import com.faf223.expensetrackerfaf.model.Expense;
import com.faf223.expensetrackerfaf.model.Income;
import com.faf223.expensetrackerfaf.model.Role;
import com.faf223.expensetrackerfaf.model.User;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class MainController {
@GetMapping("/")
public String helloWorld() {
return "Hello, World!";
}
@GetMapping("/users/get/{id}")
public User getUser(@PathVariable int id) {
return new User(id, "Test", null, null, null, null, null, null);
}
@PostMapping("/users/set/{id}/name")
public String setName(@PathVariable int id,
@RequestParam("name") String name) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/email")
public String setEmail(@PathVariable int id,
@RequestParam("email") String email) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/login")
public String setLogin(@PathVariable int id,
@RequestParam("login") String login) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/password")
public String setPassword(@PathVariable int id,
@RequestParam("password") String password) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/role")
public String setRole(@PathVariable int id,
@RequestParam("role") Role role) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/expenses")
public String setExpenses(@PathVariable int id,
@RequestParam("expenses") List<Expense> expenses) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
@PostMapping("/users/set/{id}/incomes")
public String setIncomes(@PathVariable int id,
@RequestParam("incomes")List<Income> incomes) {
throw new UnsupportedOperationException("Waiting for the DB.");
}
}

View File

@@ -0,0 +1,39 @@
package com.faf223.expensetrackerfaf.custom;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class UppercaseStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier; // No modification for catalog name
}
@Override
public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier; // No modification for schema name
}
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
String tableName = name.getText();
return new Identifier(tableName, name.isQuoted());
}
@Override
public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier; // No modification for sequence name
}
@Override
public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier; // No modification for column name
}
@Override
public Identifier toPhysicalTypeName(Identifier logicalName, JdbcEnvironment jdbcEnvironment) {
return PhysicalNamingStrategy.super.toPhysicalTypeName(logicalName, jdbcEnvironment);
}
}

View File

@@ -0,0 +1,37 @@
package com.faf223.expensetrackerfaf.model;
import jakarta.persistence.*;
import java.util.Date;
@Entity
@Table(name = "Department", schema = "HumanResources")
public class BasicEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "DepartmentID")
private Short departmentId;
@Column(name = "GroupName", nullable = false, length = 50)
private String groupName;
@Column(name = "ModifiedDate", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date modifiedDate;
@Column(name = "Name", nullable = false, length = 50)
private String name;
public Short getDepartmentId() {
return departmentId;
}
public String getGroupName() {
return groupName;
}
public Date getModifiedDate() {
return modifiedDate;
}
public String getName() {
return name;
}
}

View File

@@ -1,4 +1,5 @@
package entities;
package com.faf223.expensetrackerfaf.model;
public class Categories { public class Categories {

View File

@@ -1,24 +1,43 @@
package entities;
package com.faf223.expensetrackerfaf.model;
import com.faf223.expensetrackerfaf.util.IMoneyTransaction;
import jakarta.persistence.*;
@Entity
@Table(name = "expense")
public class Expense implements IMoneyTransaction { public class Expense implements IMoneyTransaction {
private int userId; @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 int amount;
private Categories.ExpenseCategory category; private Categories.ExpenseCategory category;
public Expense(int userId, int amount, Categories.ExpenseCategory category) { public Expense(User user, int amount, Categories.ExpenseCategory category) {
this.userId = userId; this.user = user;
this.amount = amount; this.amount = amount;
this.category = category; this.category = category;
} }
public Expense() {}
@Override @Override
public long getUserId() { public User getUser() {
return userId; return user;
} }
public void setUserId(int userId) { public void setUserId(int userId) {
this.userId = userId; this.user = user;
}
@Override
public long getUserId() {
return 0;
} }
@Override @Override

View File

@@ -0,0 +1,60 @@
package com.faf223.expensetrackerfaf.model;
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 long getUserId() {
return 0;
}
@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

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

View File

@@ -1,12 +1,25 @@
package entities;
package com.faf223.expensetrackerfaf.model;
import jakarta.persistence.*;
import java.util.List; import java.util.List;
@Entity
@Table(name = "User")
public class User { public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private long id;
private String name, email, login, password; private String name;
private String email;
private String login;
private String password;
private Role role; private Role role;
@OneToMany(mappedBy = "user")
private List<Expense> expenses; private List<Expense> expenses;
@OneToMany(mappedBy = "user")
private List<Income> incomes; private List<Income> incomes;
public User(long id, String name, String email, String login, String password, Role role, List<Expense> expenses, List<Income> incomes) { public User(long id, String name, String email, String login, String password, Role role, List<Expense> expenses, List<Income> incomes) {

View File

@@ -0,0 +1,12 @@
package com.faf223.expensetrackerfaf.repository;
import com.faf223.expensetrackerfaf.model.BasicEntity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BasicRepository extends JpaRepository<BasicEntity, Short> {
// You can define custom query methods here if needed
}

View File

@@ -0,0 +1,59 @@
package com.faf223.expensetrackerfaf.security;
import com.faf223.expensetrackerfaf.model.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,9 @@
package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.BasicEntity;
import java.util.List;
public interface BasicService {
List<BasicEntity> getAllData();
}

View File

@@ -0,0 +1,23 @@
package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.BasicEntity;
import com.faf223.expensetrackerfaf.repository.BasicRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BasicServiceImpl implements BasicService {
private final BasicRepository basicRepository;
@Autowired
public BasicServiceImpl(BasicRepository basicRepository) {
this.basicRepository = basicRepository;
}
@Override
public List<BasicEntity> getAllData() {
return basicRepository.findAll();
}
}

View File

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

View File

@@ -1,9 +0,0 @@
package entities;
public interface IMoneyTransaction {
long getUserId();
int getAmount();
String getCategory();
}

View File

@@ -1,41 +0,0 @@
package entities;
public class Income implements IMoneyTransaction {
private long userId;
private int amount;
private Categories.IncomeCategory category;
public Income(long userId, int amount, Categories.IncomeCategory category) {
this.userId = userId;
this.amount = amount;
this.category = category;
}
@Override
public long getUserId() {
return userId;
}
@Override
public int getAmount() {
return amount;
}
@Override
public String getCategory() {
return category.getEffectiveName();
}
public void setUserId(long userId) {
this.userId = userId;
}
public void setAmount(int amount) {
this.amount = amount;
}
public void setCategory(Categories.IncomeCategory category) {
this.category = category;
}
}

View File

@@ -1,3 +1,5 @@
spring.datasource.url=jdbc:sqlserver://DESKTOP-53DT8GT\\SQLEXPRESS:1433;databaseName=ExpensesApp;integratedSecurity=true spring.datasource.url=jdbc:sqlserver://DANIEL-LAPTOP\\SQLEXPRESS01:1433;databaseName=AdventureWorks2022;user=Daniel1;password=daniel;encrypt=true;trustServerCertificate=true;
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.hibernate.naming.physical-strategy=com.faf223.expensetrackerfaf.custom.UppercaseStrategy

View File

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Department List</title>
</head>
<body>
<h1>Department List</h1>
<table>
<thead>
<tr>
<th>Department ID</th>
<th>Name</th>
<th>Group Name</th>
<th>Modified Date</th>
</tr>
</thead>
<tbody>
<tr th:each="department : ${basic}">
<td th:text="${basic.actorName}"></td>
</tr>
</tbody>
</table>
</body>
</html>