Compare commits
165 Commits
security_b
...
dimas_fami
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b48f954cdd | ||
|
|
2f56a5b76d | ||
|
|
3fbd7a1440 | ||
|
|
67f2180f49 | ||
|
|
7f0a173685 | ||
|
|
da1076042e | ||
|
|
d03f3bd552 | ||
|
|
3a79741101 | ||
|
|
5809cf0284 | ||
| 75d081fd88 | |||
|
|
8420f15732 | ||
|
|
ca1c1473a4 | ||
|
|
81fe558dbb | ||
| ee4c885ae6 | |||
|
|
dc0252333e | ||
|
|
a373daa58c | ||
|
|
f98b9ac3e7 | ||
| 3335b1edc4 | |||
| cda5913aa9 | |||
| 9483b7a233 | |||
| 1cc271d590 | |||
| 8103a36394 | |||
| b22f3c32d0 | |||
| d70fd2975b | |||
| 420f9010e4 | |||
| 2e41979897 | |||
| 45b40c8987 | |||
| d750593d24 | |||
| b74cb462cd | |||
| 09fd20d071 | |||
|
|
f4a03e438b | ||
| 1696965557 | |||
|
|
f6fa5542f7 | ||
|
|
5cbee61f71 | ||
|
|
cf9a18cd33 | ||
| 3c724a395b | |||
|
|
2a83718dd6 | ||
|
|
40f7db9dc7 | ||
| 4a25e8fc66 | |||
|
|
673dfa374e | ||
|
|
4fac06b680 | ||
|
|
ee39ac605f | ||
|
|
f70fb1f0a2 | ||
|
|
0eecbd5907 | ||
| 20a2799869 | |||
|
|
48281b46a4 | ||
| b5742b9761 | |||
|
|
0fcacc3e88 | ||
|
|
5a8a8f1197 | ||
| cb6c03fe76 | |||
|
|
71448a6c21 | ||
|
|
fddd02b9ce | ||
|
|
3bf3f92551 | ||
|
|
ee8a61ba8b | ||
|
|
eb93ca73ce | ||
|
|
fa99d42bee | ||
|
|
c45cd0549f | ||
|
|
a21277bfe7 | ||
| bee800428e | |||
| a6ef16b569 | |||
|
|
444f3a07fa | ||
|
|
a484e8e6d2 | ||
|
|
07c9ed63ee | ||
|
|
d03e256425 | ||
|
|
c13056ae7f | ||
|
|
7481f91b11 | ||
|
|
4410694993 | ||
| 4404eedeec | |||
| 021be06f40 | |||
|
|
fb2695e58a | ||
|
|
ab3641dacb | ||
|
|
2d981c5af8 | ||
|
|
f1c8211f7a | ||
|
|
3ba95647b4 | ||
|
|
acbb6285d9 | ||
|
|
04aa41e354 | ||
| 71ef9aefe1 | |||
|
|
f635ca3cb7 | ||
|
|
ffd0fa36ec | ||
|
|
c0e0c21da7 | ||
|
|
eb2d0e53dc | ||
|
|
76b515b129 | ||
|
|
6fc12d22b5 | ||
|
|
55dc6125ad | ||
|
|
1ea883ae5f | ||
|
|
00d56b7e19 | ||
|
|
94ef63253e | ||
|
|
4403d60dc6 | ||
|
|
4fdd67fa21 | ||
|
|
7befb0dac7 | ||
| 525729a725 | |||
|
|
9fe17438f7 | ||
|
|
7bc9a460ed | ||
|
|
801cf60db4 | ||
| ef7c7ec7f8 | |||
| a2b7b38bf1 | |||
|
|
e24ca0d2d5 | ||
| e946eaa9b8 | |||
|
|
eca57b111a | ||
|
|
8f934bdf32 | ||
|
|
6e96029058 | ||
| dfc5fd19a1 | |||
|
|
c399f42e89 | ||
|
|
6a839b7d28 | ||
|
|
f98bd461ee | ||
| 1f92fddd04 | |||
| 34903cd287 | |||
|
|
9c8946f236 | ||
|
|
944669e595 | ||
|
|
ca7eed9154 | ||
|
|
ae6d09b47f | ||
| 163aee19e5 | |||
| b63f156eb7 | |||
| 0a4ff61935 | |||
| 485bd6b9ba | |||
| 3d530c8e7c | |||
|
|
f600f73ec4 | ||
| 12980ae51b | |||
| 5a71be29ff | |||
| 57b21bddc3 | |||
| 5353df8958 | |||
| 8f9476a130 | |||
| d620c6d93f | |||
| 3363d299c3 | |||
| a9d2d914b8 | |||
|
|
c885ccb925 | ||
|
|
1be5250c99 | ||
|
|
e9fd388909 | ||
|
|
ac8e58d052 | ||
|
|
ddeb9544d4 | ||
|
|
06c8757d2a | ||
| c86d76285e | |||
| 5748ba2223 | |||
| c27958cfa1 | |||
| f164d220a1 | |||
| a583f4b765 | |||
| 20dd0f34bf | |||
| 0baac602e4 | |||
| e4de55f255 | |||
| d990d224ab | |||
|
|
56557d5c48 | ||
|
|
65c5c3f2df | ||
|
|
d402514ecb | ||
|
|
4e00244a26 | ||
|
|
84b48afb21 | ||
|
|
0aee8afbac | ||
|
|
4fed3dba9e | ||
|
|
c07e3ca876 | ||
|
|
3f619eb01b | ||
|
|
0afa1b2b7c | ||
|
|
e47f2f8df6 | ||
|
|
0698eb2478 | ||
| 6ca8b861e4 | |||
|
|
e885cc4d6d | ||
|
|
071ec12dd7 | ||
|
|
1009ca7bdb | ||
|
|
6ccf92ecce | ||
|
|
a3bbe4433e | ||
|
|
0b98fe3db4 | ||
|
|
fe3ad761e7 | ||
|
|
f8ec901fa8 | ||
|
|
3160a19fa5 | ||
|
|
090ff315cd | ||
| 00b487e1b3 | |||
|
|
b1b13dc736 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,6 +4,8 @@ target/
|
|||||||
!**/src/main/**/target/
|
!**/src/main/**/target/
|
||||||
!**/src/test/**/target/
|
!**/src/test/**/target/
|
||||||
/src/main/resources/application.properties
|
/src/main/resources/application.properties
|
||||||
|
/src/main/java/web/src/package.json
|
||||||
|
/src/main/java/web/src/package-lock.json
|
||||||
|
|
||||||
### STS ###
|
### STS ###
|
||||||
.apt_generated
|
.apt_generated
|
||||||
@@ -32,5 +34,3 @@ build/
|
|||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
/src/main/resources/application.properties
|
|
||||||
|
|||||||
69
README.md
69
README.md
@@ -1,3 +1,68 @@
|
|||||||
# ExpenseTrackerFAF
|
# Expense Tracker App
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Expense Tracker is a web application that helps you keep track of your expenses and incomes. It provides a user-friendly interface to enter and visualize your financial data, making it easier to manage your finances.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Single-page application for a smooth and responsive user experience.
|
||||||
|
- Reactive graph updates to visualize your financial data.
|
||||||
|
- Ability to add incomes and expenses to your account.
|
||||||
|
- Full-fledged authorization system to secure your data.
|
||||||
|
- Hosted database for seamless data storage and retrieval.
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
|
||||||
|
- Svelte
|
||||||
|
- Chart.js
|
||||||
|
- Axios
|
||||||
|
|
||||||
|
### Backend
|
||||||
|
|
||||||
|
- Spring
|
||||||
|
- Spring Boot
|
||||||
|
- Spring Security
|
||||||
|
|
||||||
|
### Database
|
||||||
|
|
||||||
|
- MySQL
|
||||||
|
- phpMyAdmin
|
||||||
|
|
||||||
|
## Installation Instructions
|
||||||
|
|
||||||
|
To run the Expense Tracker application, follow these steps:
|
||||||
|
|
||||||
|
1. Clone the project repository to your local machine.
|
||||||
|
|
||||||
|
2. Install the required Maven dependencies for the backend. In the project directory, run:
|
||||||
|
|
||||||
|
3. Run the backend Spring application to start the server.
|
||||||
|
|
||||||
|
4. For the frontend, navigate to the `web` directory and run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
This will start the frontend development server and open the application in your web browser.
|
||||||
|
|
||||||
|
Now you can access the Expense Tracker application at http://localhost:5173/auth/login and start tracking your expenses and incomes visually.
|
||||||
|
|
||||||
|
Please note that you need to configure the database connection details and other environment-specific settings in the application properties before running the backend.
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
Make sure to update the configuration files with your specific database settings, security configurations, and other environment variables as needed. You can find these configuration files in the backend project.
|
||||||
|
|
||||||
|
Feel free to customize the application further and adapt it to your specific use case.
|
||||||
|
|
||||||
|
Happy expense tracking!
|
||||||
|
|
||||||
|
|
||||||
Expense tracker project made in Spring by a group of senior full-stack Nobel winner students.
|
|
||||||
|
|||||||
6
package-lock.json
generated
Normal file
6
package-lock.json
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "ExpenseTrackerFAF",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {}
|
||||||
|
}
|
||||||
18
pom.xml
18
pom.xml
@@ -22,6 +22,7 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<version>3.1.4</version>
|
<version>3.1.4</version>
|
||||||
</dependency>
|
</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>
|
||||||
@@ -43,6 +44,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-oauth2-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
@@ -64,13 +69,18 @@
|
|||||||
<version>0.11.5</version>
|
<version>0.11.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
<version>1.18.20</version>
|
</dependency>
|
||||||
<scope>provided</scope>
|
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-web</artifactId>
|
||||||
|
<version>6.1.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
|
<defaultGoal>package</defaultGoal>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.faf223.expensetrackerfaf.security.PersonDetails;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
@@ -19,12 +20,11 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ApplicationConfig {
|
public class ApplicationConfig {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public UserDetailsService userDetailsService() {
|
public UserDetailsService userDetailsService() {
|
||||||
return username -> new PersonDetails(credentialRepository.findByEmail(username).orElseThrow((() -> new UsernameNotFoundException("User not found"))));
|
return username -> new PersonDetails(credentialRepository.findByEmail(username).orElseThrow(() -> new UsernameNotFoundException("User not found")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -44,4 +44,10 @@ public class ApplicationConfig {
|
|||||||
public PasswordEncoder passwordEncoder() {
|
public PasswordEncoder passwordEncoder() {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
}
|
}
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public JwtAuthenticationFilter customJwtAuthenticationFilter(JwtService jwtService, UserDetailsService userDetailsService) {
|
||||||
|
return new JwtAuthenticationFilter(jwtService, userDetailsService);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.config;
|
package com.faf223.expensetrackerfaf.config;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -15,7 +18,6 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||||
@@ -35,23 +37,38 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
final String authHeader = request.getHeader("Authorization");
|
final String authHeader = request.getHeader("Authorization");
|
||||||
final String jwt;
|
final String jwt;
|
||||||
final String userEmail;
|
String userEmail;
|
||||||
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
jwt = authHeader.substring(7);
|
jwt = authHeader.substring(7);
|
||||||
userEmail = jwtService.extractUsername(jwt);
|
|
||||||
if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
try {
|
||||||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
|
userEmail = jwtService.extractUsername(jwt);
|
||||||
if (jwtService.isTokenValid(jwt, userDetails)) {
|
if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||||
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
|
||||||
userDetails, null, userDetails.getAuthorities());
|
if (jwtService.isTokenValid(jwt, userDetails)) {
|
||||||
authToken.setDetails(new WebAuthenticationDetailsSource()
|
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
|
||||||
.buildDetails(request));
|
userDetails, null, userDetails.getAuthorities());
|
||||||
SecurityContextHolder.getContext().setAuthentication(authToken);
|
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(authToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (ExpiredJwtException e) {
|
||||||
|
// Token is expired; return a custom response
|
||||||
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
response.setContentType("application/json");
|
||||||
|
|
||||||
|
ErrorResponse errorResponse = new ErrorResponse("Your session has expired. Refresh your token.");
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
|
||||||
|
|
||||||
|
|
||||||
|
response.getWriter().flush();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import io.jsonwebtoken.Jwts;
|
|||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
import io.jsonwebtoken.io.Decoders;
|
import io.jsonwebtoken.io.Decoders;
|
||||||
import io.jsonwebtoken.security.Keys;
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -16,6 +17,7 @@ import java.util.Map;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class JwtService {
|
public class JwtService {
|
||||||
|
|
||||||
@Value("${application.security.jwt.secret-key}")
|
@Value("${application.security.jwt.secret-key}")
|
||||||
@@ -25,6 +27,7 @@ public class JwtService {
|
|||||||
@Value("${application.security.jwt.refresh-token.expiration}")
|
@Value("${application.security.jwt.refresh-token.expiration}")
|
||||||
private long refreshExpiration;
|
private long refreshExpiration;
|
||||||
|
|
||||||
|
|
||||||
public String extractUsername(String token) {
|
public String extractUsername(String token) {
|
||||||
return extractClaim(token, Claims::getSubject);
|
return extractClaim(token, Claims::getSubject);
|
||||||
}
|
}
|
||||||
@@ -38,6 +41,10 @@ public class JwtService {
|
|||||||
return generateToken(new HashMap<>(), userDetails);
|
return generateToken(new HashMap<>(), userDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String generateRefreshToken(UserDetails userDetails) {
|
||||||
|
return generateRefreshToken(new HashMap<>(), userDetails);
|
||||||
|
}
|
||||||
|
|
||||||
public String generateToken(
|
public String generateToken(
|
||||||
Map<String, Object> extraClaims,
|
Map<String, Object> extraClaims,
|
||||||
UserDetails userDetails
|
UserDetails userDetails
|
||||||
@@ -45,6 +52,13 @@ public class JwtService {
|
|||||||
return buildToken(extraClaims, userDetails, jwtExpiration);
|
return buildToken(extraClaims, userDetails, jwtExpiration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String generateRefreshToken(
|
||||||
|
Map<String, Object> extraClaims,
|
||||||
|
UserDetails userDetails
|
||||||
|
) {
|
||||||
|
return buildToken(extraClaims, userDetails, refreshExpiration);
|
||||||
|
}
|
||||||
|
|
||||||
private String buildToken(Map<String, Object> extraClaims, UserDetails userDetails, long expiration) {
|
private String buildToken(Map<String, Object> extraClaims, UserDetails userDetails, long expiration) {
|
||||||
return Jwts
|
return Jwts
|
||||||
.builder()
|
.builder()
|
||||||
|
|||||||
@@ -1,37 +1,77 @@
|
|||||||
package com.faf223.expensetrackerfaf.config;
|
package com.faf223.expensetrackerfaf.config;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import com.faf223.expensetrackerfaf.controller.auth.JwtAuthenticationSuccessHandler;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
|
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.CorsConfigurationSource;
|
||||||
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.springframework.security.config.Customizer.withDefaults;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@RequiredArgsConstructor
|
@EnableMethodSecurity
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
|
||||||
public class SecurityConfiguration {
|
public class SecurityConfiguration {
|
||||||
|
|
||||||
private final JwtAuthenticationFilter jwtAuthFilter;
|
private final JwtAuthenticationFilter jwtAuthFilter;
|
||||||
private final AuthenticationProvider authenticationProvider;
|
private final AuthenticationProvider authenticationProvider;
|
||||||
|
|
||||||
|
public SecurityConfiguration(JwtAuthenticationFilter jwtAuthFilter,
|
||||||
|
AuthenticationProvider authenticationProvider) {
|
||||||
|
this.jwtAuthFilter = jwtAuthFilter;
|
||||||
|
this.authenticationProvider = authenticationProvider;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
.csrf(csrf -> csrf.disable())
|
.cors(withDefaults())
|
||||||
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/api/v1/auth/**").permitAll()
|
.requestMatchers("/api/v1/auth/*").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
|
//.oauth2Login(withDefaults())
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authenticationProvider(authenticationProvider)
|
.authenticationProvider(authenticationProvider)
|
||||||
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class); // will be executed before UsernamePasswordAuthenticationFilter
|
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JwtAuthenticationSuccessHandler jwtAuthenticationSuccessHandler() {
|
||||||
|
return new JwtAuthenticationSuccessHandler();
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public CorsConfigurationSource corsConfigurationSource() {
|
||||||
|
CorsConfiguration configuration = new CorsConfiguration();
|
||||||
|
configuration.setAllowedOrigins(Arrays.asList("*"));
|
||||||
|
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
|
||||||
|
configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
|
||||||
|
configuration.setExposedHeaders(Arrays.asList("x-auth-token"));
|
||||||
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
|
source.registerCorsConfiguration("/**", configuration);
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationEntryPoint authenticationEntryPoint() {
|
||||||
|
return new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,31 @@ import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
|||||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
|
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.service.ExpenseCategoryService;
|
||||||
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
||||||
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionDoesNotBelongToTheUserException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
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.List;
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -20,45 +37,112 @@ import java.util.stream.Collectors;
|
|||||||
public class ExpenseController {
|
public class ExpenseController {
|
||||||
|
|
||||||
private final ExpenseService expenseService;
|
private final ExpenseService expenseService;
|
||||||
|
private final UserService userService;
|
||||||
private final ExpenseMapper expenseMapper;
|
private final ExpenseMapper expenseMapper;
|
||||||
|
private final ExpenseCategoryService expenseCategoryService;
|
||||||
|
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
public ResponseEntity<List<ExpenseDTO>> getAllExpenses() {
|
public ResponseEntity<List<ExpenseDTO>> getAllExpenses() {
|
||||||
List<ExpenseDTO> expenses = expenseService.getExpenses().stream().map(expenseMapper::toDto).collect(Collectors.toList());
|
List<ExpenseDTO> expenses = expenseService.getTransactions().stream().map(expenseMapper::toDto).collect(Collectors.toList());
|
||||||
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
||||||
else return ResponseEntity.notFound().build();
|
else throw new TransactionsNotFoundException("Transactions not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public ResponseEntity<ExpenseDTO> createNewExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
public ResponseEntity<Map<String, Long>> createNewExpense(@RequestBody @Valid ExpenseCreationDTO expenseDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors())
|
||||||
|
throw new TransactionNotCreatedException("Could not create new expense");
|
||||||
|
|
||||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
Expense expense = expenseMapper.toExpense(expenseDTO);
|
||||||
if (!bindingResult.hasErrors()) {
|
|
||||||
expenseService.createOrUpdateExpense(expense);
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
|
||||||
} else {
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
|
String email = userDetails.getUsername();
|
||||||
|
User user = userService.getUserByEmail(email);
|
||||||
|
expense.setUser(user);
|
||||||
|
|
||||||
|
expenseService.createOrUpdate(expense);
|
||||||
|
Map<String, Long> response = new HashMap<>();
|
||||||
|
response.put("expenseId", expense.getId());
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new TransactionNotCreatedException("Could not create new expense");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping()
|
@PatchMapping("/update/{id}")
|
||||||
public ResponseEntity<ExpenseDTO> updateExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody @Valid ExpenseCreationDTO expenseDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
if(bindingResult.hasErrors())
|
||||||
if (!bindingResult.hasErrors()) {
|
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
expenseService.createOrUpdateExpense(expense);
|
|
||||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
Expense expense = expenseService.getTransactionById(id);
|
||||||
} else {
|
|
||||||
return ResponseEntity.notFound().build();
|
if(expense == null)
|
||||||
|
throw new TransactionsNotFoundException("The expense has not been found");
|
||||||
|
|
||||||
|
if(!expenseService.belongsToUser(expense))
|
||||||
|
throw new TransactionDoesNotBelongToTheUserException("The transaction does not belong to you");
|
||||||
|
|
||||||
|
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
|
||||||
|
expense.setCategory(category);
|
||||||
|
expense.setAmount(expenseDTO.getAmount());
|
||||||
|
|
||||||
|
expenseService.createOrUpdate(expense);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/personal-expenses")
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public ResponseEntity<List<ExpenseDTO>> getExpensesByTimeUnits(@RequestParam Optional<LocalDate> date,
|
||||||
|
@RequestParam Optional<Integer> month,
|
||||||
|
@RequestParam Optional<Integer> startYear,
|
||||||
|
@RequestParam Optional<Integer> endYear,
|
||||||
|
@RequestParam Optional<String> lastUnit) {
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
|
||||||
|
String email = userDetails.getUsername();
|
||||||
|
List<ExpenseDTO> expenses = Collections.emptyList();
|
||||||
|
|
||||||
|
if(date.isPresent())
|
||||||
|
expenses = expenseService.getTransactionsByDate(date.get(), email).stream().map(expenseMapper::toDto).toList();
|
||||||
|
else if(month.isPresent())
|
||||||
|
expenses = expenseService.getTransactionsByMonth(Month.of(month.get()), email).stream().map(expenseMapper::toDto).toList();
|
||||||
|
else if(startYear.isPresent() && endYear.isPresent())
|
||||||
|
expenses = expenseService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(expenseMapper::toDto).toList();
|
||||||
|
else if(lastUnit.isPresent()) {
|
||||||
|
|
||||||
|
if(lastUnit.get().equalsIgnoreCase("week"))
|
||||||
|
expenses = expenseService.getLastWeekTransactions(email).stream().map(expenseMapper::toDto).toList();
|
||||||
|
else if(lastUnit.get().equalsIgnoreCase("month"))
|
||||||
|
expenses = expenseService.getLastMonthTransactions(email).stream().map(expenseMapper::toDto).toList();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
expenses = userService.getUserByEmail(email).getExpenses().stream().map(expenseMapper::toDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(expenses);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new TransactionsNotFoundException("The expenses have not been found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{userUuid}")
|
@GetMapping("/categories")
|
||||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<ExpenseCategory>> getAllCategories() {
|
||||||
List<ExpenseDTO> expenses = expenseService.getExpensesByUserId(userUuid).stream().map(expenseMapper::toDto).collect(Collectors.toList());
|
List<ExpenseCategory> categories = expenseCategoryService.getAllCategories();
|
||||||
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
if (!categories.isEmpty()) return ResponseEntity.ok(categories);
|
||||||
else return ResponseEntity.notFound().build();
|
else throw new TransactionsNotFoundException("The expenses have not been found");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
@DeleteMapping("/delete/{id}")
|
||||||
|
public void deleteCategory(@PathVariable long id) {
|
||||||
|
expenseService.deleteTransactionById(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.*;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleDoesNotBelongException(TransactionDoesNotBelongToTheUserException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleTransactionNotCreatedException(TransactionNotCreatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleTransactionsNotFoundException(TransactionsNotFoundException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleUserNotFoundException(UserNotFoundException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleTransactionNotUpdatedException(TransactionNotUpdatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleUserNotAuthenticatedException(UserNotAuthenticatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleUserNotCreatedException(UserNotCreatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,14 +4,31 @@ import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
|||||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||||
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionDoesNotBelongToTheUserException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
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.List;
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -20,45 +37,112 @@ import java.util.stream.Collectors;
|
|||||||
public class IncomeController {
|
public class IncomeController {
|
||||||
|
|
||||||
private final IncomeService incomeService;
|
private final IncomeService incomeService;
|
||||||
|
private final UserService userService;
|
||||||
private final IncomeMapper incomeMapper;
|
private final IncomeMapper incomeMapper;
|
||||||
|
private final IncomeCategoryService incomeCategoryService;
|
||||||
|
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
public ResponseEntity<List<IncomeDTO>> getAllIncomes() {
|
public ResponseEntity<List<IncomeDTO>> getAllIncomes() {
|
||||||
List<IncomeDTO> incomes = incomeService.getIncomes().stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
List<IncomeDTO> incomes = incomeService.getTransactions().stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||||
else return ResponseEntity.notFound().build();
|
else throw new TransactionsNotFoundException("Transactions not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public ResponseEntity<IncomeDTO> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
public ResponseEntity<Map<String, Long>> createNewIncome(@RequestBody @Valid IncomeCreationDTO incomeDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors())
|
||||||
|
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
Income income = incomeMapper.toIncome(incomeDTO);
|
Income income = incomeMapper.toIncome(incomeDTO);
|
||||||
if (!bindingResult.hasErrors()) {
|
|
||||||
incomeService.createOrUpdateIncome(income);
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
|
||||||
} else {
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
|
String email = userDetails.getUsername();
|
||||||
|
User user = userService.getUserByEmail(email);
|
||||||
|
income.setUser(user);
|
||||||
|
|
||||||
|
incomeService.createOrUpdate(income);
|
||||||
|
Map<String, Long> response = new HashMap<>();
|
||||||
|
response.put("incomeId", income.getId());
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new TransactionNotCreatedException("Could not create new income");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping()
|
@PatchMapping("/update/{id}")
|
||||||
public ResponseEntity<IncomeDTO> updateIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody @Valid IncomeCreationDTO incomeDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Income income = incomeMapper.toIncome(incomeDTO);
|
if(bindingResult.hasErrors())
|
||||||
if (!bindingResult.hasErrors()) {
|
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
incomeService.createOrUpdateIncome(income);
|
|
||||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
Income income = incomeService.getTransactionById(id);
|
||||||
} else {
|
|
||||||
return ResponseEntity.notFound().build();
|
if(income == null)
|
||||||
|
throw new TransactionsNotFoundException("The income has not been found");
|
||||||
|
|
||||||
|
if(!incomeService.belongsToUser(income))
|
||||||
|
throw new TransactionDoesNotBelongToTheUserException("The transaction does not belong to you");
|
||||||
|
|
||||||
|
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
||||||
|
income.setCategory(category);
|
||||||
|
income.setAmount(incomeDTO.getAmount());
|
||||||
|
|
||||||
|
incomeService.createOrUpdate(income);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/personal-incomes")
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public ResponseEntity<List<IncomeDTO>> getIncomesByTimeUnits(@RequestParam Optional<LocalDate> date,
|
||||||
|
@RequestParam Optional<Integer> month,
|
||||||
|
@RequestParam Optional<Integer> startYear,
|
||||||
|
@RequestParam Optional<Integer> endYear,
|
||||||
|
@RequestParam Optional<String> lastUnit) {
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
|
||||||
|
String email = userDetails.getUsername();
|
||||||
|
List<IncomeDTO> incomes = Collections.emptyList();
|
||||||
|
|
||||||
|
if(date.isPresent())
|
||||||
|
incomes = incomeService.getTransactionsByDate(date.get(), email).stream().map(incomeMapper::toDto).toList();
|
||||||
|
else if(month.isPresent())
|
||||||
|
incomes = incomeService.getTransactionsByMonth(Month.of(month.get()), email).stream().map(incomeMapper::toDto).toList();
|
||||||
|
else if(startYear.isPresent() && endYear.isPresent())
|
||||||
|
incomes = incomeService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(incomeMapper::toDto).toList();
|
||||||
|
else if(lastUnit.isPresent()) {
|
||||||
|
|
||||||
|
if(lastUnit.get().equalsIgnoreCase("week"))
|
||||||
|
incomes = incomeService.getLastWeekTransactions(email).stream().map(incomeMapper::toDto).toList();
|
||||||
|
else if(lastUnit.get().equalsIgnoreCase("month"))
|
||||||
|
incomes = incomeService.getLastMonthTransactions(email).stream().map(incomeMapper::toDto).toList();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
incomes = userService.getUserByEmail(email).getIncomes().stream().map(incomeMapper::toDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(incomes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new TransactionsNotFoundException("The expenses have not been found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{userUuid}")
|
@GetMapping("/categories")
|
||||||
public ResponseEntity<List<IncomeDTO>> getIncomesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<IncomeCategory>> getAllCategories() {
|
||||||
List<IncomeDTO> incomes = incomeService.getIncomesByUserId(userUuid).stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
List<IncomeCategory> categories = incomeCategoryService.getAllCategories();
|
||||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
if (!categories.isEmpty()) return ResponseEntity.ok(categories);
|
||||||
else return ResponseEntity.notFound().build();
|
else throw new TransactionsNotFoundException("The expenses have not been found");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
@DeleteMapping("/delete/{id}")
|
||||||
|
public void deleteIncome(@PathVariable long id) {
|
||||||
|
incomeService.deleteTransactionById(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,29 @@
|
|||||||
package com.faf223.expensetrackerfaf.controller;
|
package com.faf223.expensetrackerfaf.controller;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.controller.auth.ChangePasswordRequest;
|
||||||
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
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.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
@@ -20,32 +32,87 @@ public class UserController {
|
|||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
|
private final AuthenticationService authenticationService;
|
||||||
|
|
||||||
@PatchMapping()
|
@PatchMapping()
|
||||||
public ResponseEntity<UserDTO> updateUser(@RequestBody UserCreationDTO userDTO,
|
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if (bindingResult.hasErrors())
|
||||||
|
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
User user = userMapper.toUser(userDTO);
|
User user = userMapper.toUser(userDTO);
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
if (!bindingResult.hasErrors()) {
|
||||||
|
|
||||||
userService.updateUser(user);
|
userService.updateUser(user);
|
||||||
return ResponseEntity.ok(userMapper.toDto(user));
|
return ResponseEntity.ok(userMapper.toDto(user));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException("The user has not been found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{userUuid}")
|
@PatchMapping("/update-password")
|
||||||
public ResponseEntity<UserDTO> getUser(@PathVariable String userUuid) {
|
public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) {
|
||||||
User user = userService.getUserById(userUuid);
|
|
||||||
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
|
authenticationService.updatePassword(password.getPassword());
|
||||||
else return ResponseEntity.notFound().build();
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get-user-data")
|
||||||
|
public ResponseEntity<Map<String, String>> getUser() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user);
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
Map<String, String> userData = new HashMap<>();
|
||||||
|
userData.put("firstname", user.getFirstName());
|
||||||
|
userData.put("lastname", user.getLastName());
|
||||||
|
userData.put("username", user.getUsername());
|
||||||
|
userData.put("userrole", credential.get().getRole().toString()); // Assuming UserRole is an enum
|
||||||
|
|
||||||
|
return ResponseEntity.ok(userData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
}
|
}
|
||||||
|
|
||||||
@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));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
@GetMapping("/delete/{username}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
public ResponseEntity<Void> deleteUserByUsername(@PathVariable String username) {
|
||||||
|
|
||||||
|
userService.deleteByUsername(username);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/promote/{email}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
public ResponseEntity<Void> promoteUser(@PathVariable String email) {
|
||||||
|
|
||||||
|
userService.promoteUser(email);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/demote/{email}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
public ResponseEntity<Void> demoteUser(@PathVariable String email) {
|
||||||
|
|
||||||
|
userService.demoteUser(email);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
package com.faf223.expensetrackerfaf.controller.auth;
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api/v1/auth")
|
@RequestMapping("api/v1/auth")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class AuthenticationController {
|
public class AuthenticationController {
|
||||||
|
|
||||||
private final AuthenticationService service;
|
private final AuthenticationService service;
|
||||||
|
|
||||||
public AuthenticationController(AuthenticationService service) {
|
|
||||||
this.service = service;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public ResponseEntity<AuthenticationResponse> register(@RequestBody RegisterRequest request) {
|
public ResponseEntity<AuthenticationResponse> register(@RequestBody RegisterRequest request) {
|
||||||
return ResponseEntity.ok(service.register(request));
|
return ResponseEntity.ok(service.register(request));
|
||||||
@@ -23,4 +21,9 @@ public class AuthenticationController {
|
|||||||
public ResponseEntity<AuthenticationResponse> authenticate(@RequestBody AuthenticationRequest request) {
|
public ResponseEntity<AuthenticationResponse> authenticate(@RequestBody AuthenticationRequest request) {
|
||||||
return ResponseEntity.ok(service.authenticate(request));
|
return ResponseEntity.ok(service.authenticate(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/refreshtoken")
|
||||||
|
public ResponseEntity<AuthenticationResponse> refreshAccessToken(@RequestBody TokenRefreshRequest request) {
|
||||||
|
return ResponseEntity.ok(service.refreshAccessToken(request));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.faf223.expensetrackerfaf.controller.auth;
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -11,5 +12,8 @@ import lombok.NoArgsConstructor;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class AuthenticationResponse {
|
public class AuthenticationResponse {
|
||||||
|
|
||||||
private String token;
|
@JsonProperty("access_token")
|
||||||
|
private String accessToken;
|
||||||
|
@JsonProperty("refresh_token")
|
||||||
|
private String refreshToken;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class ChangePasswordRequest {
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class JwtAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
||||||
|
|
||||||
|
super.onAuthenticationSuccess(request, response, authentication);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OAuth2SuccessController {
|
||||||
|
|
||||||
|
private final AuthenticationService jwtService;
|
||||||
|
|
||||||
|
@GetMapping()
|
||||||
|
public AuthenticationResponse getUser(@AuthenticationPrincipal OAuth2User oAuth2User) {
|
||||||
|
|
||||||
|
AuthenticationResponse response = jwtService.register(oAuth2User);
|
||||||
|
System.out.println("Response: " + response);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,10 +13,10 @@ import lombok.NoArgsConstructor;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class RegisterRequest {
|
public class RegisterRequest {
|
||||||
|
|
||||||
private String firstname; // Change field name to match JSON
|
private String firstname;
|
||||||
private String lastname; // Change field name to match JSON
|
private String lastname;
|
||||||
private String username; // Change field name to match JSON
|
private String username;
|
||||||
private String email; // Change field name to match JSON
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
private Role role;
|
private Role role;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.controller.auth;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class TokenRefreshRequest {
|
||||||
|
private String refreshToken;
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ExpenseCreationDTO {
|
public class ExpenseCreationDTO {
|
||||||
private long expenseId;
|
@NotNull(message = "Category must not be null")
|
||||||
private User user;
|
private int expenseCategory;
|
||||||
private ExpenseCategory expenseCategory;
|
|
||||||
private LocalDate date;
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -10,9 +12,19 @@ import java.time.LocalDate;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ExpenseDTO {
|
public class ExpenseDTO {
|
||||||
|
@NotNull(message = "ID must not be null")
|
||||||
private long expenseId;
|
private long expenseId;
|
||||||
|
|
||||||
|
@NotNull(message = "User must not be null")
|
||||||
private UserDTO userDTO;
|
private UserDTO userDTO;
|
||||||
|
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
private ExpenseCategory expenseCategory;
|
private ExpenseCategory expenseCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Date must not be null")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class IncomeCreationDTO {
|
public class IncomeCreationDTO {
|
||||||
private long incomeId;
|
@NotNull(message = "Category must not be null")
|
||||||
private User user;
|
private int incomeCategory;
|
||||||
private IncomeCategory category;
|
|
||||||
private LocalDate date;
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -10,9 +12,19 @@ import java.time.LocalDate;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class IncomeDTO {
|
public class IncomeDTO {
|
||||||
|
@NotNull(message = "ID must not be null")
|
||||||
private long incomeId;
|
private long incomeId;
|
||||||
|
|
||||||
|
@NotNull(message = "User must not be null")
|
||||||
private UserDTO userDTO;
|
private UserDTO userDTO;
|
||||||
private IncomeCategory category;
|
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
|
private IncomeCategory incomeCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Date must not be null")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,28 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class UserCreationDTO {
|
public class UserCreationDTO {
|
||||||
|
@NotNull(message = "First name must not be null")
|
||||||
|
@NotEmpty(message = "First name must not be empty")
|
||||||
private String firstname;
|
private String firstname;
|
||||||
|
@NotNull(message = "Last name must not be null")
|
||||||
|
@NotEmpty(message = "Last name must not be empty")
|
||||||
private String lastname;
|
private String lastname;
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
@NotNull(message = "Email must not be null")
|
||||||
|
@NotEmpty(message = "Email must not be empty")
|
||||||
|
@Email(message = "Email must be valid")
|
||||||
private String email;
|
private String email;
|
||||||
|
@NotNull(message = "Password must not be null")
|
||||||
|
@NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class UserDTO {
|
public class UserDTO {
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
private String name;
|
private String name;
|
||||||
|
@NotNull(message = "Surname must not be null")
|
||||||
|
@NotEmpty(message = "Surname must not be empty")
|
||||||
private String surname;
|
private String surname;
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,32 +3,29 @@ package com.faf223.expensetrackerfaf.dto.mappers;
|
|||||||
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
|
import com.faf223.expensetrackerfaf.service.ExpenseCategoryService;
|
||||||
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ExpenseMapper {
|
public class ExpenseMapper {
|
||||||
|
|
||||||
private final ExpenseService expenseService;
|
private final ExpenseCategoryService expenseCategoryService;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public ExpenseMapper(ExpenseService expenseService, UserMapper userMapper) {
|
|
||||||
this.expenseService = expenseService;
|
|
||||||
this.userMapper = userMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpenseDTO toDto(Expense expense) {
|
public ExpenseDTO toDto(Expense expense) {
|
||||||
return new ExpenseDTO(expense.getExpenseId(), userMapper.toDto(expense.getUser()),
|
return new ExpenseDTO(expense.getId(), userMapper.toDto(expense.getUser()),
|
||||||
expense.getCategory(), expense.getDate(), expense.getAmount());
|
expense.getCategory(), expense.getDate(), expense.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Expense toExpense(ExpenseCreationDTO expenseDTO) {
|
public Expense toExpense(ExpenseCreationDTO expenseDTO) {
|
||||||
Expense expense = expenseService.getExpenseById(expenseDTO.getExpenseId());
|
|
||||||
if(expense == null) return new Expense(expenseDTO.getExpenseId(), expenseDTO.getUser(),
|
return new Expense(expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory()), LocalDate.now(), expenseDTO.getAmount());
|
||||||
expenseDTO.getExpenseCategory(), expenseDTO.getDate(), expenseDTO.getAmount());
|
|
||||||
return expense;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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,34 +2,27 @@ 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.IncomeService;
|
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class IncomeMapper {
|
public class IncomeMapper {
|
||||||
|
|
||||||
private final IncomeService incomeService;
|
private final IncomeCategoryService incomeCategoryService;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public IncomeMapper(IncomeService incomeService, UserMapper userMapper) {
|
|
||||||
this.incomeService = incomeService;
|
|
||||||
this.userMapper = userMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IncomeDTO toDto(Income income) {
|
public IncomeDTO toDto(Income income) {
|
||||||
return new IncomeDTO(income.getIncomeId(), userMapper.toDto(income.getUser()),
|
return new IncomeDTO(income.getId(), userMapper.toDto(income.getUser()),
|
||||||
income.getCategory(), income.getDate(), income.getAmount());
|
income.getCategory(), income.getDate(), income.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Income toIncome(IncomeCreationDTO incomeDTO) {
|
public Income toIncome(IncomeCreationDTO incomeDTO) {
|
||||||
Income income = incomeService.getIncomeById(incomeDTO.getIncomeId());
|
return new Income(incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory()), LocalDate.now(), incomeDTO.getAmount());
|
||||||
if(income == null) return new Income(incomeDTO.getIncomeId(), incomeDTO.getUser(),
|
|
||||||
incomeDTO.getCategory(), incomeDTO.getDate(), incomeDTO.getAmount());
|
|
||||||
return income;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -1,35 +1,35 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity(name = "credentials")
|
@Entity(name = "credentials")
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Credential {
|
public class Credential {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long credentialId;
|
private Long credentialId;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "user_uuid")
|
@JoinColumn(name = "user_uuid")
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Role role;
|
private Role role;
|
||||||
|
|
||||||
public Credential(User user, String email, String password) {
|
public Credential(User user, String email, String password) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
|
||||||
this.role = Role.ROLE_USER;
|
this.role = Role.ROLE_USER;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import lombok.Data;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.ToString;
|
import lombok.*;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -13,10 +13,12 @@ import java.time.LocalDate;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Entity(name = "expenses")
|
@Entity(name = "expenses")
|
||||||
public class Expense {
|
@Builder
|
||||||
|
public class Expense implements IMoneyTransaction {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long expenseId;
|
@Column(name = "expense_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne()
|
@ManyToOne()
|
||||||
@JoinColumn(name = "user_uuid")
|
@JoinColumn(name = "user_uuid")
|
||||||
@@ -26,9 +28,19 @@ public class Expense {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "category_id")
|
@JoinColumn(name = "category_id")
|
||||||
|
@NotNull
|
||||||
private ExpenseCategory category;
|
private ExpenseCategory category;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
private BigDecimal amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false)
|
||||||
|
private BigDecimal amount;
|
||||||
|
|
||||||
|
public Expense(ExpenseCategory expenseCategory, LocalDate date, BigDecimal amount) {
|
||||||
|
this.category = expenseCategory;
|
||||||
|
this.date = date;
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity(name = "expense_categories")
|
@Entity(name = "expense_categories")
|
||||||
public class ExpenseCategory {
|
public class ExpenseCategory implements IMoneyTransactionCategory {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long categoryId;
|
@Column(name = "category_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
private String categoryName;
|
@Column(name = "category_name")
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
|
private String name;
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/main/java/com/faf223/expensetrackerfaf/model/Family.java
Normal file
42
src/main/java/com/faf223/expensetrackerfaf/model/Family.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Entity(name = "family")
|
||||||
|
@Builder
|
||||||
|
public class Family {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "family_id")
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public interface IMoneyTransaction {
|
||||||
|
|
||||||
|
Long getId();
|
||||||
|
LocalDate getDate();
|
||||||
|
User getUser();
|
||||||
|
BigDecimal getAmount();
|
||||||
|
IMoneyTransactionCategory getCategory();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
public interface IMoneyTransactionCategory {
|
||||||
|
Long getId();
|
||||||
|
String getName();
|
||||||
|
}
|
||||||
@@ -2,10 +2,10 @@ package com.faf223.expensetrackerfaf.model;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import lombok.Data;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.ToString;
|
import lombok.*;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@@ -13,10 +13,12 @@ import java.time.LocalDate;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Entity(name = "incomes")
|
@Entity(name = "incomes")
|
||||||
public class Income {
|
@Builder
|
||||||
|
public class Income implements IMoneyTransaction {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long incomeId;
|
@Column(name = "income_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "user_uuid")
|
@JoinColumn(name = "user_uuid")
|
||||||
@@ -26,8 +28,19 @@ public class Income {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "category_id")
|
@JoinColumn(name = "category_id")
|
||||||
|
@NotNull
|
||||||
private IncomeCategory category;
|
private IncomeCategory category;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false)
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
|
|
||||||
|
public Income(IncomeCategory incomeCategory, LocalDate date, BigDecimal amount) {
|
||||||
|
this.category = incomeCategory;
|
||||||
|
this.date = date;
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,27 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.persistence.Id;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity(name = "income_categories")
|
@Entity(name = "income_categories")
|
||||||
public class IncomeCategory {
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class IncomeCategory implements IMoneyTransactionCategory {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long categoryId;
|
@Column(name = "category_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
private String categoryName;
|
@Column(name = "category_name")
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
|
private String name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
public enum Role {
|
public enum Role {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -18,22 +20,36 @@ public class User {
|
|||||||
private String userUuid;
|
private String userUuid;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
|
@NotNull(message = "First name must not be null")
|
||||||
|
@NotEmpty(message = "First name must not be empty")
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "surname")
|
@Column(name = "surname")
|
||||||
|
@NotNull(message = "Last name must not be null")
|
||||||
|
@NotEmpty(message = "Last name must not be empty")
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@Column(name = "username")
|
@Column(name = "username")
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
|
// @NotNull(message = "Password must not be null")
|
||||||
|
// @NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Expense> expenses;
|
private List<Expense> expenses;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.repository;
|
package com.faf223.expensetrackerfaf.repository;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@@ -9,4 +10,8 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
||||||
Optional<Credential> findByEmail(String email);
|
Optional<Credential> findByEmail(String email);
|
||||||
|
|
||||||
|
Optional<Credential> findByUser(User user);
|
||||||
|
|
||||||
|
void deleteByEmail(String email);
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,28 @@ package com.faf223.expensetrackerfaf.repository;
|
|||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.query.Procedure;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ExpenseRepository extends JpaRepository<Expense, Long> {
|
public interface ExpenseRepository extends JpaRepository<Expense, Long> {
|
||||||
List<Expense> findByUser(User user);
|
List<Expense> findByUser(User user);
|
||||||
|
List<Expense> findByDate(LocalDate date);
|
||||||
|
|
||||||
|
List<Expense> findByDateBetween(LocalDate start, LocalDate end);
|
||||||
|
|
||||||
|
@Procedure(procedureName = "get_expenses_by_month")
|
||||||
|
List<Expense> filterByMonth(int month);
|
||||||
|
|
||||||
|
@Procedure(procedureName = "get_last_week_expenses")
|
||||||
|
List<Expense> findLastWeek();
|
||||||
|
|
||||||
|
@Procedure(procedureName = "get_last_month_expenses")
|
||||||
|
List<Expense> findLastMonth();
|
||||||
|
|
||||||
|
@Procedure(procedureName = "get_expenses_by_year_interval")
|
||||||
|
List<Expense> filterByYearInterval(int start, int end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.repository;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Family;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface FamilyRepository extends JpaRepository<Family, Long> {
|
||||||
|
}
|
||||||
@@ -6,4 +6,4 @@ import org.springframework.stereotype.Repository;
|
|||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface IncomeCategoryRepository extends JpaRepository<IncomeCategory, Long> {
|
public interface IncomeCategoryRepository extends JpaRepository<IncomeCategory, Long> {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,33 @@ package com.faf223.expensetrackerfaf.repository;
|
|||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.query.Procedure;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface IncomeRepository extends JpaRepository<Income, Long> {
|
public interface IncomeRepository extends JpaRepository<Income, Long> {
|
||||||
List<Income> findByUser(User user);
|
List<Income> findByUser(User user);
|
||||||
|
List<Income> findByDate(LocalDate date);
|
||||||
|
|
||||||
|
List<Income> findByDateBetween(LocalDate start, LocalDate end);
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
@Procedure(procedureName = "get_incomes_by_month")
|
||||||
|
List<Income> filterByMonth(int month);
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
@Procedure(procedureName = "get_last_week_incomes")
|
||||||
|
List<Income> findLastWeek();
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
@Procedure(procedureName = "get_last_month_incomes")
|
||||||
|
List<Income> findLastMonth();
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
@Procedure(procedureName = "get_incomes_by_year_interval")
|
||||||
|
List<Income> filterByYearInterval(int start, int end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User, String> {
|
public interface UserRepository extends JpaRepository<User, String> {
|
||||||
Optional<User> getUserByUserUuid(String userUuid);
|
Optional<User> getUserByUserUuid(String userUuid);
|
||||||
|
|
||||||
|
Optional<User> findByUsername(String username);
|
||||||
|
|
||||||
|
void deleteByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,27 +4,37 @@ import com.faf223.expensetrackerfaf.config.JwtService;
|
|||||||
import com.faf223.expensetrackerfaf.controller.auth.AuthenticationRequest;
|
import com.faf223.expensetrackerfaf.controller.auth.AuthenticationRequest;
|
||||||
import com.faf223.expensetrackerfaf.controller.auth.AuthenticationResponse;
|
import com.faf223.expensetrackerfaf.controller.auth.AuthenticationResponse;
|
||||||
import com.faf223.expensetrackerfaf.controller.auth.RegisterRequest;
|
import com.faf223.expensetrackerfaf.controller.auth.RegisterRequest;
|
||||||
|
import com.faf223.expensetrackerfaf.controller.auth.TokenRefreshRequest;
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AuthenticationService {
|
public class AuthenticationService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final UserService userService;
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
private final PasswordGenerator passwordGenerator;
|
||||||
|
|
||||||
public AuthenticationResponse register(RegisterRequest request) {
|
public AuthenticationResponse register(RegisterRequest request) {
|
||||||
|
|
||||||
@@ -38,21 +48,110 @@ public class AuthenticationService {
|
|||||||
Credential credential = new Credential(user, request.getEmail(), passwordEncoder.encode(request.getPassword()));
|
Credential credential = new Credential(user, request.getEmail(), passwordEncoder.encode(request.getPassword()));
|
||||||
credentialRepository.save(credential);
|
credentialRepository.save(credential);
|
||||||
|
|
||||||
String jwtToken = jwtService.generateToken(new PersonDetails(credential));
|
UserDetails userDetails = new PersonDetails(credential);
|
||||||
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
|
String refreshToken = jwtService.generateRefreshToken(userDetails);
|
||||||
|
|
||||||
return AuthenticationResponse.builder()
|
return AuthenticationResponse.builder()
|
||||||
.token(jwtToken)
|
.accessToken(jwtToken)
|
||||||
|
.refreshToken(refreshToken)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthenticationResponse register(OAuth2User oAuth2User) {
|
||||||
|
String userEmail = oAuth2User.getAttribute("email");
|
||||||
|
|
||||||
|
// Check if the user is already registered
|
||||||
|
Optional<Credential> existingCredential = credentialRepository.findByEmail(userEmail);
|
||||||
|
if (existingCredential.isPresent()) {
|
||||||
|
UserDetails userDetails = new PersonDetails(existingCredential.get());
|
||||||
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
|
String refreshToken = jwtService.generateRefreshToken(userDetails);
|
||||||
|
|
||||||
|
return AuthenticationResponse.builder()
|
||||||
|
.accessToken(jwtToken)
|
||||||
|
.refreshToken(refreshToken)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String givenName = oAuth2User.getAttribute("given_name");
|
||||||
|
String familyName = oAuth2User.getAttribute("family_name");
|
||||||
|
String email = oAuth2User.getAttribute("email");
|
||||||
|
|
||||||
|
User user = User.builder()
|
||||||
|
.firstName(givenName)
|
||||||
|
.lastName(familyName)
|
||||||
|
.username(email)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String randomPassword = passwordGenerator.generateRandomPassword(8);
|
||||||
|
|
||||||
|
user.setPassword(passwordEncoder.encode(randomPassword));
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
Credential credential = new Credential(user, email, passwordEncoder.encode(randomPassword));
|
||||||
|
credentialRepository.save(credential);
|
||||||
|
|
||||||
|
UserDetails userDetails = new PersonDetails(credential);
|
||||||
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
|
String refreshToken = jwtService.generateRefreshToken(userDetails);
|
||||||
|
|
||||||
|
System.out.println("New user: " + user);
|
||||||
|
System.out.println("New credentials: " + credential);
|
||||||
|
|
||||||
|
return AuthenticationResponse.builder()
|
||||||
|
.accessToken(jwtToken)
|
||||||
|
.refreshToken(refreshToken)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthenticationResponse authenticate(AuthenticationRequest request) {
|
public AuthenticationResponse authenticate(AuthenticationRequest request) {
|
||||||
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
|
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
|
||||||
|
|
||||||
Credential credential = credentialRepository.findByEmail(request.getEmail()).orElseThrow((() -> new UsernameNotFoundException("User not found")));
|
Credential credential = credentialRepository.findByEmail(request.getEmail()).orElseThrow((() -> new UserNotFoundException("User not found")));
|
||||||
|
|
||||||
String jwtToken = jwtService.generateToken(new PersonDetails(credential));
|
UserDetails userDetails = new PersonDetails(credential);
|
||||||
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
|
String refreshToken = jwtService.generateRefreshToken(userDetails);
|
||||||
return AuthenticationResponse.builder()
|
return AuthenticationResponse.builder()
|
||||||
.token(jwtToken)
|
.accessToken(jwtToken)
|
||||||
|
.refreshToken(refreshToken)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AuthenticationResponse refreshAccessToken(TokenRefreshRequest refreshRequest) {
|
||||||
|
String refreshToken = refreshRequest.getRefreshToken();
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(jwtService.extractUsername(refreshToken));
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
UserDetails userDetails = new PersonDetails(credential.get());
|
||||||
|
|
||||||
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
|
return AuthenticationResponse.builder()
|
||||||
|
.accessToken(jwtToken)
|
||||||
|
.refreshToken(refreshToken)
|
||||||
|
.build();
|
||||||
|
} else {
|
||||||
|
throw new UserNotAuthenticatedException("Invalid or expired refresh token");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePassword(String newPassword) {
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
User user = userService.getUserByEmail(userDetails.getUsername());
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user);
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
Credential updatedCredential = credential.get();
|
||||||
|
updatedCredential.setPassword(passwordEncoder.encode(newPassword));
|
||||||
|
credentialRepository.save(updatedCredential);
|
||||||
|
}
|
||||||
|
} else throw new UserNotFoundException("User not found!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CredentialService {
|
||||||
|
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
|
public Optional<Credential> findByEmail(String email) {
|
||||||
|
return credentialRepository.findByEmail(email);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.ExpenseCategoryRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class ExpenseCategoryService implements ICategoryService {
|
||||||
|
|
||||||
|
private final ExpenseCategoryRepository expenseCategoryRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ExpenseCategory> getAllCategories() {
|
||||||
|
return expenseCategoryRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExpenseCategory getCategoryById(long id) {
|
||||||
|
return expenseCategoryRepository.getReferenceById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,43 +1,140 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.util.TransactionFilter;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ExpenseService {
|
public class ExpenseService implements ITransactionService {
|
||||||
|
|
||||||
private final ExpenseRepository expenseRepository;
|
private final ExpenseRepository expenseRepository;
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final TransactionFilter transactionFilter;
|
||||||
|
|
||||||
public void createOrUpdateExpense(Expense expense) {
|
public void createOrUpdate(IMoneyTransaction expense) {
|
||||||
expenseRepository.save(expense);
|
expenseRepository.save((Expense) expense);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Expense> getExpensesByUserId(String userUuid) {
|
public List<Expense> getTransactionsByEmail(String email) {
|
||||||
|
|
||||||
Optional<User> user = userRepository.getUserByUserUuid(userUuid);
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
if (user.isPresent()) {
|
if (credential.isPresent()) {
|
||||||
return expenseRepository.findByUser(user.get());
|
return expenseRepository.findByUser(credential.get().getUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Expense> getExpenses() {
|
@Override
|
||||||
|
public List<Expense> getTransactionsByDate(LocalDate date) {
|
||||||
|
return expenseRepository.findByDate(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Expense> getTransactionsByDate(LocalDate date, String email) {
|
||||||
|
return (List<Expense>) transactionFilter.filterByEmail(getTransactionsByDate(date), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getTransactionsByMonth(Month month) {
|
||||||
|
System.out.println(expenseRepository.filterByMonth(month.getValue()));
|
||||||
|
return expenseRepository.filterByMonth(month.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Expense> getTransactionsByMonth(Month month, String email) {
|
||||||
|
return (List<Expense>) transactionFilter.filterByEmail(getTransactionsByMonth(month), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getLastWeekTransactions() {
|
||||||
|
return expenseRepository.findLastWeek();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Expense> getLastWeekTransactions(String email) {
|
||||||
|
return (List<Expense>) transactionFilter.filterByEmail(getLastWeekTransactions(), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getLastMonthTransactions() {
|
||||||
|
return expenseRepository.findLastMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Expense> getLastMonthTransactions(String email) {
|
||||||
|
return (List<Expense>) transactionFilter.filterByEmail(getLastMonthTransactions(), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getYearIntervalTransactions(int start, int end) {
|
||||||
|
return expenseRepository.filterByYearInterval(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Expense> getYearIntervalTransactions(String email, int start, int end) {
|
||||||
|
return (List<Expense>) transactionFilter.filterByEmail(getYearIntervalTransactions(start, end), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Expense> getTransactions() {
|
||||||
return expenseRepository.findAll();
|
return expenseRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Expense getExpenseById(long id) {
|
public Expense getTransactionById(long id) {
|
||||||
return expenseRepository.findById(id).orElse(null);
|
return expenseRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteTransactionById(long id) {
|
||||||
|
expenseRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean belongsToUser(IMoneyTransaction transaction) {
|
||||||
|
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().getExpenses().contains((Expense) transaction);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.IMoneyTransactionCategory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ICategoryService {
|
||||||
|
|
||||||
|
List<? extends IMoneyTransactionCategory> getAllCategories();
|
||||||
|
IMoneyTransactionCategory getCategoryById(long id);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ITransactionService {
|
||||||
|
|
||||||
|
void createOrUpdate(IMoneyTransaction transaction);
|
||||||
|
List<? extends IMoneyTransaction> getTransactions();
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByEmail(String email);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByDate(LocalDate date);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByDate(LocalDate date, String email);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByMonth(Month month);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByMonth(Month month, String email);
|
||||||
|
List<? extends IMoneyTransaction> getLastWeekTransactions();
|
||||||
|
List<? extends IMoneyTransaction> getLastWeekTransactions(String email);
|
||||||
|
List<? extends IMoneyTransaction> getLastMonthTransactions();
|
||||||
|
List<? extends IMoneyTransaction> getLastMonthTransactions(String email);
|
||||||
|
List<? extends IMoneyTransaction> getYearIntervalTransactions(int start, int end);
|
||||||
|
List<? extends IMoneyTransaction> getYearIntervalTransactions(String email, int start, int end);
|
||||||
|
IMoneyTransaction getTransactionById(long id);
|
||||||
|
void deleteTransactionById(long it);
|
||||||
|
boolean belongsToUser(IMoneyTransaction transaction);
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.IncomeCategoryRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class IncomeCategoryService implements ICategoryService {
|
||||||
|
|
||||||
|
private final IncomeCategoryRepository incomeCategoryRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IncomeCategory> getAllCategories() {
|
||||||
|
return incomeCategoryRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IncomeCategory getCategoryById(long id) {
|
||||||
|
return incomeCategoryRepository.getReferenceById(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,43 +1,138 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.util.TransactionFilter;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class IncomeService {
|
public class IncomeService implements ITransactionService {
|
||||||
|
|
||||||
private final IncomeRepository incomeRepository;
|
private final IncomeRepository incomeRepository;
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final TransactionFilter transactionFilter;
|
||||||
|
|
||||||
public void createOrUpdateIncome(Income income) {
|
public void createOrUpdate(IMoneyTransaction income) {
|
||||||
incomeRepository.save(income);
|
incomeRepository.save((Income) income);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Income> getIncomes() {
|
public List<Income> getTransactions() {
|
||||||
return incomeRepository.findAll();
|
return incomeRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Income> getIncomesByUserId(String userUuid) {
|
public List<Income> getTransactionsByEmail(String email) {
|
||||||
|
|
||||||
Optional<User> user = userRepository.getUserByUserUuid(userUuid);
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
if (user.isPresent()) {
|
if (credential.isPresent()) {
|
||||||
return incomeRepository.findByUser(user.get());
|
return incomeRepository.findByUser(credential.get().getUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Income getIncomeById(long id) {
|
@Override
|
||||||
|
public List<Income> getTransactionsByDate(LocalDate date) {
|
||||||
|
return incomeRepository.findByDate(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Income> getTransactionsByDate(LocalDate date, String email) {
|
||||||
|
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByDate(date), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getTransactionsByMonth(Month month) {
|
||||||
|
return incomeRepository.filterByMonth(month.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Income> getTransactionsByMonth(Month month, String email) {
|
||||||
|
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByMonth(month), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getLastWeekTransactions() {
|
||||||
|
return incomeRepository.findLastWeek();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Income> getLastWeekTransactions(String email) {
|
||||||
|
return (List<Income>) transactionFilter.filterByEmail(getLastWeekTransactions(), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getLastMonthTransactions() {
|
||||||
|
return incomeRepository.findLastMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Income> getLastMonthTransactions(String email) {
|
||||||
|
return (List<Income>) transactionFilter.filterByEmail(getLastMonthTransactions(), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getYearIntervalTransactions(int start, int end) {
|
||||||
|
return incomeRepository.filterByYearInterval(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<Income> getYearIntervalTransactions(String email, int start, int end) {
|
||||||
|
return (List<Income>) transactionFilter.filterByEmail(getYearIntervalTransactions(start, end), email);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Income getTransactionById(long id) {
|
||||||
return incomeRepository.findById(id).orElse(null);
|
return incomeRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteTransactionById(long id) {
|
||||||
|
incomeRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean belongsToUser(IMoneyTransaction transaction) {
|
||||||
|
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().getIncomes().contains((Income) transaction);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PasswordGenerator {
|
||||||
|
|
||||||
|
private final SecureRandom secureRandom = new SecureRandom();
|
||||||
|
|
||||||
|
public String generateRandomPassword(int length) {
|
||||||
|
byte[] randomBytes = new byte[length];
|
||||||
|
secureRandom.nextBytes(randomBytes);
|
||||||
|
return Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,23 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Role;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
public void updateUser(User user) {
|
public void updateUser(User user) {
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
@@ -24,4 +30,51 @@ public class UserService {
|
|||||||
public User getUserById(String userUuid) {
|
public User getUserById(String userUuid) {
|
||||||
return userRepository.findById(userUuid).orElse(null);
|
return userRepository.findById(userUuid).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getUserByEmail(String email) {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
Optional<User> user = userRepository.findById(credential.get().getUser().getUserUuid());
|
||||||
|
return user.orElse(null);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteByUsername(String username) {
|
||||||
|
|
||||||
|
Optional<User> user = userRepository.findByUsername(username);
|
||||||
|
if (user.isPresent()) {
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user.get());
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
credentialRepository.deleteByEmail(credential.get().getEmail());
|
||||||
|
userRepository.deleteByUsername(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void promoteUser(String email) {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
System.out.println(email);
|
||||||
|
Credential updatedCredential = credential.get();
|
||||||
|
updatedCredential.setRole(Role.ROLE_ADMIN);
|
||||||
|
credentialRepository.save(updatedCredential);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void demoteUser(String email) {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
System.out.println(email);
|
||||||
|
Credential updatedCredential = credential.get();
|
||||||
|
updatedCredential.setRole(Role.ROLE_USER);
|
||||||
|
credentialRepository.save(updatedCredential);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package com.faf223.expensetrackerfaf.util;
|
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
|
||||||
|
|
||||||
public interface IMoneyTransaction {
|
|
||||||
|
|
||||||
User getUser();
|
|
||||||
int getAmount();
|
|
||||||
String getCategory();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
|
import com.faf223.expensetrackerfaf.service.CredentialService;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TransactionFilter {
|
||||||
|
|
||||||
|
private final CredentialService credentialService;
|
||||||
|
|
||||||
|
public List<? extends IMoneyTransaction> filterByEmail(List<? extends IMoneyTransaction> transactions, String email) {
|
||||||
|
Optional<Credential> credential = credentialService.findByEmail(email);
|
||||||
|
if(credential.isEmpty())
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
|
|
||||||
|
return transactions
|
||||||
|
.stream()
|
||||||
|
.filter(transaction -> credential.get().getUser().equals(transaction.getUser()))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.errors;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ErrorResponse {
|
||||||
|
private String message;
|
||||||
|
private long timestamp;
|
||||||
|
|
||||||
|
public ErrorResponse(String message, long timestamp) {
|
||||||
|
this.message = message;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorResponse(String message) {
|
||||||
|
this.message = message;
|
||||||
|
this.timestamp = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ErrorResponse from(BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors()) {
|
||||||
|
StringBuilder errorMessage = new StringBuilder();
|
||||||
|
|
||||||
|
List<FieldError> errors = bindingResult.getFieldErrors();
|
||||||
|
for(FieldError fieldError : errors)
|
||||||
|
errorMessage.append(fieldError.getField())
|
||||||
|
.append(" - ")
|
||||||
|
.append(fieldError.getDefaultMessage())
|
||||||
|
.append(";");
|
||||||
|
|
||||||
|
return new ErrorResponse(errorMessage.toString(), System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ErrorResponse("No error message was provided", System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class TransactionDoesNotBelongToTheUserException extends RuntimeException {
|
||||||
|
public TransactionDoesNotBelongToTheUserException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class TransactionNotCreatedException extends RuntimeException {
|
||||||
|
public TransactionNotCreatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class TransactionNotUpdatedException extends RuntimeException {
|
||||||
|
public TransactionNotUpdatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class TransactionsNotFoundException extends RuntimeException {
|
||||||
|
public TransactionsNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class UserNotAuthenticatedException extends RuntimeException {
|
||||||
|
public UserNotAuthenticatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class UserNotCreatedException extends RuntimeException {
|
||||||
|
public UserNotCreatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class UserNotFoundException extends RuntimeException {
|
||||||
|
public UserNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main/java/com/faf223/expensetrackerfaf/web/.eslintignore
Normal file
13
src/main/java/com/faf223/expensetrackerfaf/web/.eslintignore
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
.DS_Store
|
||||||
|
node_modules
|
||||||
|
/build
|
||||||
|
/.svelte-kit
|
||||||
|
/package
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
|
||||||
|
# Ignore files for PNPM, NPM and YARN
|
||||||
|
pnpm-lock.yaml
|
||||||
|
package-lock.json
|
||||||
|
yarn.lock
|
||||||
14
src/main/java/com/faf223/expensetrackerfaf/web/.eslintrc.cjs
Normal file
14
src/main/java/com/faf223/expensetrackerfaf/web/.eslintrc.cjs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
module.exports = {
|
||||||
|
root: true,
|
||||||
|
extends: ['eslint:recommended', 'plugin:svelte/recommended', 'prettier'],
|
||||||
|
parserOptions: {
|
||||||
|
sourceType: 'module',
|
||||||
|
ecmaVersion: 2020,
|
||||||
|
extraFileExtensions: ['.svelte']
|
||||||
|
},
|
||||||
|
env: {
|
||||||
|
browser: true,
|
||||||
|
es2017: true,
|
||||||
|
node: true
|
||||||
|
}
|
||||||
|
};
|
||||||
12
src/main/java/com/faf223/expensetrackerfaf/web/.gitignore
vendored
Normal file
12
src/main/java/com/faf223/expensetrackerfaf/web/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
.DS_Store
|
||||||
|
node_modules
|
||||||
|
/build
|
||||||
|
/.svelte-kit
|
||||||
|
/package
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
.vercel
|
||||||
|
.output
|
||||||
|
vite.config.js.timestamp-*
|
||||||
|
vite.config.ts.timestamp-*
|
||||||
1
src/main/java/com/faf223/expensetrackerfaf/web/.npmrc
Normal file
1
src/main/java/com/faf223/expensetrackerfaf/web/.npmrc
Normal file
@@ -0,0 +1 @@
|
|||||||
|
engine-strict=true
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
.DS_Store
|
||||||
|
node_modules
|
||||||
|
/build
|
||||||
|
/.svelte-kit
|
||||||
|
/package
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
|
||||||
|
# Ignore files for PNPM, NPM and YARN
|
||||||
|
pnpm-lock.yaml
|
||||||
|
package-lock.json
|
||||||
|
yarn.lock
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"useTabs": true,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "none",
|
||||||
|
"printWidth": 100,
|
||||||
|
"plugins": ["prettier-plugin-svelte"],
|
||||||
|
"pluginSearchDirs": ["."],
|
||||||
|
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
|
||||||
|
}
|
||||||
6
src/main/java/com/faf223/expensetrackerfaf/web/README.md
Normal file
6
src/main/java/com/faf223/expensetrackerfaf/web/README.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# ExpenseTracker App
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Responsive flexbox dashboard made with Chart.js and Svelte
|
||||||
|
|
||||||
2625
src/main/java/com/faf223/expensetrackerfaf/web/package-lock.json
generated
Normal file
2625
src/main/java/com/faf223/expensetrackerfaf/web/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
38
src/main/java/com/faf223/expensetrackerfaf/web/package.json
Normal file
38
src/main/java/com/faf223/expensetrackerfaf/web/package.json
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
{
|
||||||
|
"name": "expensetracker",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite dev --host",
|
||||||
|
"devs": "vite dev --host --https",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview",
|
||||||
|
"lint": "prettier --plugin-search-dir . --check . && eslint .",
|
||||||
|
"format": "prettier --plugin-search-dir . --write ."
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@fontsource/fira-mono": "^4.5.10",
|
||||||
|
"@neoconfetti/svelte": "^1.0.0",
|
||||||
|
"@sveltejs/adapter-auto": "^2.0.0",
|
||||||
|
"@sveltejs/kit": "^1.20.4",
|
||||||
|
"eslint": "^8.28.0",
|
||||||
|
"eslint-config-prettier": "^8.5.0",
|
||||||
|
"eslint-plugin-svelte": "^2.30.0",
|
||||||
|
"prettier": "^2.8.0",
|
||||||
|
"prettier-plugin-svelte": "^2.10.1",
|
||||||
|
"svelte": "^4.0.5",
|
||||||
|
"vite": "^4.4.2"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
|
"dependencies": {
|
||||||
|
"@fortawesome/free-brands-svg-icons": "^6.4.2",
|
||||||
|
"@fortawesome/free-solid-svg-icons": "^6.4.2",
|
||||||
|
"axios": "^1.5.1",
|
||||||
|
"bootstrap": "^5.3.2",
|
||||||
|
"chart.js": "^4.4.0",
|
||||||
|
"email-validator": "^2.0.4",
|
||||||
|
"svelte-cookie": "^1.0.1",
|
||||||
|
"svelte-fa": "^3.0.4",
|
||||||
|
"svelte-simple-modal": "^1.6.1",
|
||||||
|
"svelte-spa-router": "^3.3.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/main/java/com/faf223/expensetrackerfaf/web/src/app.html
Normal file
20
src/main/java/com/faf223/expensetrackerfaf/web/src/app.html
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<link rel="icon" href="/favicon.png" />
|
||||||
|
<meta name="viewport" content="width=device-width" />
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
|
<style>
|
||||||
|
body, html {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
%sveltekit.head%
|
||||||
|
</head>
|
||||||
|
<body data-sveltekit-preload-data="hover">
|
||||||
|
<div style="display: contents">%sveltekit.body%</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
src/main/java/com/faf223/expensetrackerfaf/web/src/favicon.png
Normal file
BIN
src/main/java/com/faf223/expensetrackerfaf/web/src/favicon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
@@ -0,0 +1,8 @@
|
|||||||
|
<script>
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
window.location.href = '/auth/login';
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<script>
|
||||||
|
import LoginForm from './LoginForm.svelte';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div id="wrapper">
|
||||||
|
<LoginForm />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#wrapper {
|
||||||
|
background-color: #041721;
|
||||||
|
margin:0;
|
||||||
|
padding:0;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
<script>
|
||||||
|
import axios from "axios";
|
||||||
|
import {onMount} from "svelte";
|
||||||
|
import { getCookie, setCookie } from 'svelte-cookie';
|
||||||
|
|
||||||
|
let isErrorVisible = false;
|
||||||
|
let username, password;
|
||||||
|
let message = ""
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
|
||||||
|
const access_token = getCookie('access_token');
|
||||||
|
const refresh_token = getCookie('refresh_token');
|
||||||
|
|
||||||
|
if (access_token && refresh_token) {
|
||||||
|
window.location.href = '/dashboard';
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
async function submitForm(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.post('https://trackio.online:8081/api/v1/auth/authenticate', {
|
||||||
|
email: username,
|
||||||
|
password: password,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(response.data)
|
||||||
|
|
||||||
|
const { access_token, refresh_token } = response.data;
|
||||||
|
|
||||||
|
setCookie('access_token', access_token);
|
||||||
|
setCookie('refresh_token', refresh_token);
|
||||||
|
|
||||||
|
window.location.href = '/dashboard'
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Login failed:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="animated bounceInDown">
|
||||||
|
<div class="container">
|
||||||
|
{#if isErrorVisible}
|
||||||
|
<span class="error animated tada" id="msg">{message}</span>
|
||||||
|
{/if}
|
||||||
|
<form name="loginForm" class="loginForm" on:submit={submitForm}>
|
||||||
|
<h1 id="formTitle">Track<span>.io</span></h1>
|
||||||
|
<h5>Sign in to your account.</h5>
|
||||||
|
<input id="usernameInput" type="text" name="email" placeholder="Email or Username" autocomplete="off" on:input={
|
||||||
|
event => {username = event.target.value}
|
||||||
|
}>
|
||||||
|
<input id="passwordInput" type="password" name="password" placeholder="Password" autocomplete="off" on:input={
|
||||||
|
event => {password = event.target.value}
|
||||||
|
}>
|
||||||
|
<a href="/auth/recovery" class="recoveryPass">Forgot your password?</a>
|
||||||
|
<input type="submit" value="Sign in" class="submitButton">
|
||||||
|
</form>
|
||||||
|
<a href="/auth/register" class="noAccount">Don't have an account? Sign up</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<svelte:head>
|
||||||
|
<link rel="icon" type="image/x-icon" href="../favicon.png" />
|
||||||
|
<title>Login into Track.IO</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
@import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400');
|
||||||
|
|
||||||
|
.container {
|
||||||
|
margin: 0;
|
||||||
|
top: 50px;
|
||||||
|
left: 50%;
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
position: absolute;
|
||||||
|
text-align: center;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
background-color: rgb(33, 41, 66);
|
||||||
|
border-radius: 9px;
|
||||||
|
border-top: 10px solid #79a6fe;
|
||||||
|
border-bottom: 10px solid #8BD17C;
|
||||||
|
width: 400px;
|
||||||
|
height: 500px;
|
||||||
|
box-shadow: 1px 1px 108.8px 19.2px rgb(25, 31, 53);
|
||||||
|
}
|
||||||
|
|
||||||
|
#formTitle {
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
color: #5c6bc0;
|
||||||
|
margin-top: 94px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#formTitle span {
|
||||||
|
color: #dfdeee;
|
||||||
|
font-weight: lighter;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loginForm h5 {
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #a1a4ad;
|
||||||
|
letter-spacing: 1.5px;
|
||||||
|
margin-top: -15px;
|
||||||
|
margin-bottom: 70px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loginForm input[type="text"],
|
||||||
|
.loginForm input[type="password"] {
|
||||||
|
display: block;
|
||||||
|
margin: 20px auto;
|
||||||
|
background: #262e49;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 14px 10px;
|
||||||
|
width: 320px;
|
||||||
|
outline: none;
|
||||||
|
color: #d6d6d6;
|
||||||
|
-webkit-transition: all .2s ease-out;
|
||||||
|
-moz-transition: all .2s ease-out;
|
||||||
|
-ms-transition: all .2s ease-out;
|
||||||
|
-o-transition: all .2s ease-out;
|
||||||
|
transition: all .2s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loginForm input[type="text"]:focus,
|
||||||
|
.loginForm input[type="password"]:focus {
|
||||||
|
border: 1px solid #79A6FE;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #5c7fda;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submitButton {
|
||||||
|
border: 0;
|
||||||
|
background: #7f5feb;
|
||||||
|
color: #dfdeee;
|
||||||
|
border-radius: 100px;
|
||||||
|
width: 340px;
|
||||||
|
height: 49px;
|
||||||
|
font-size: 16px;
|
||||||
|
position: absolute;
|
||||||
|
top: 79%;
|
||||||
|
left: 8%;
|
||||||
|
transition: 0.3s;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submitButton:hover {
|
||||||
|
background: #5d33e6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.recoveryPass {
|
||||||
|
position: relative;
|
||||||
|
float: right;
|
||||||
|
right: 28px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.noAccount {
|
||||||
|
position: absolute;
|
||||||
|
top: 92%;
|
||||||
|
left: 24%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
text-align: center;
|
||||||
|
width: 337px;
|
||||||
|
height: 20px;
|
||||||
|
padding: 2px;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin: 10px auto 10px;
|
||||||
|
position: absolute;
|
||||||
|
top: 31%;
|
||||||
|
left: 7.2%;
|
||||||
|
color: rgb(190, 67, 29);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<script>
|
||||||
|
import RegisterForm from './RegisterForm.svelte';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div id="wrapper">
|
||||||
|
<RegisterForm />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#wrapper {
|
||||||
|
background-color: #041721;
|
||||||
|
margin:0;
|
||||||
|
padding:0;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
<script>
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
import * as EmailValidator from 'email-validator';
|
||||||
|
import {onMount} from "svelte";
|
||||||
|
import {getCookie, setCookie} from "svelte-cookie";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
let isErrorVisible = false;
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
let username, email, password, name, surname;
|
||||||
|
let message = ""
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
|
||||||
|
const access_token = getCookie('access_token');
|
||||||
|
const refresh_token = getCookie('refresh_token');
|
||||||
|
|
||||||
|
if (access_token && refresh_token) {
|
||||||
|
window.location.href = '/dashboard';
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
async function submitForm(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = {
|
||||||
|
firstname: name,
|
||||||
|
lastname: surname,
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(data)
|
||||||
|
|
||||||
|
const response = await axios.post('https://trackio.online:8081/api/v1/auth/register', data);
|
||||||
|
|
||||||
|
const { access_token, refresh_token } = response.data;
|
||||||
|
|
||||||
|
setCookie('access_token', access_token);
|
||||||
|
setCookie('refresh_token', refresh_token);
|
||||||
|
|
||||||
|
window.location.href = '/dashboard'
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Login failed:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// function submitForm(event) {
|
||||||
|
// event.preventDefault();
|
||||||
|
// // console.log("Tried to submit!");
|
||||||
|
// // console.log("Valid? ", (validateEmail() && validateUsername() && validatePassword() ? "Yes" : "No"));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// function validateEmail() {
|
||||||
|
// let valid = EmailValidator.validate(username);
|
||||||
|
// isErrorVisible = valid ? false : true;
|
||||||
|
// message = isErrorVisible ? "Invalid e-mail!" : "";
|
||||||
|
// return valid;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function validatePassword() {
|
||||||
|
// let valid = password.value != '';
|
||||||
|
// isErrorVisible = valid ? false : true;
|
||||||
|
// message = isErrorVisible ? "Invalid password!" : "";
|
||||||
|
// return valid;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function validateUsername() {
|
||||||
|
// let valid = username.value != '';
|
||||||
|
// isErrorVisible = valid ? false : true;
|
||||||
|
// message = isErrorVisible ? "Invalid password!" : "";
|
||||||
|
// return valid;
|
||||||
|
// }
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:head>
|
||||||
|
<link rel="icon" type="image/x-icon" href="../favicon.png" />
|
||||||
|
<title>Register into Track.IO</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="animated bounceInDown">
|
||||||
|
<div class="container">
|
||||||
|
{#if isErrorVisible}
|
||||||
|
<span class="error animated tada" id="msg">{message}</span>
|
||||||
|
{/if}
|
||||||
|
<form name="registerForm" class="registerForm" on:submit={submitForm}>
|
||||||
|
<h1 id="formTitle">Track<span>.io</span></h1>
|
||||||
|
<h5>Sign up for a new account.</h5>
|
||||||
|
<input id="usernameInput" type="text" name="username" placeholder="Username" autocomplete="off" on:input={
|
||||||
|
event => {username = event.target.value}
|
||||||
|
}>
|
||||||
|
<input id="nameInput" type="text" name="name" placeholder="Name" autocomplete="off" on:input={
|
||||||
|
event => {name = event.target.value}
|
||||||
|
}>
|
||||||
|
<input id="surnameInput" type="text" name="surname" placeholder="Surname" autocomplete="off" on:input={
|
||||||
|
event => {surname = event.target.value}
|
||||||
|
}>
|
||||||
|
<input id="emailInput" type="text" name="email" placeholder="Email" autocomplete="off" on:input={
|
||||||
|
event => {email = event.target.value}
|
||||||
|
}>
|
||||||
|
<input id="passwordInput" type="password" name="password" placeholder="Password" autocomplete="off" on:input={
|
||||||
|
event => {password = event.target.value}
|
||||||
|
}>
|
||||||
|
<a href="/auth/recovery" class="recoveryPass">Forgot your password?</a>
|
||||||
|
<input type="submit" value="Sign up" class="submitButton">
|
||||||
|
</form>
|
||||||
|
<a href="/auth/login" class="noAccount">Already have an account? Sign in</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
@import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400');
|
||||||
|
|
||||||
|
.container {
|
||||||
|
margin: 0;
|
||||||
|
top: 50px;
|
||||||
|
left: 50%;
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
position: absolute;
|
||||||
|
text-align: center;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
background-color: rgb(33, 41, 66);
|
||||||
|
border-radius: 9px;
|
||||||
|
border-top: 10px solid #79a6fe;
|
||||||
|
border-bottom: 10px solid #8BD17C;
|
||||||
|
width: 400px;
|
||||||
|
height: 750px;
|
||||||
|
box-shadow: 1px 1px 108.8px 19.2px rgb(25, 31, 53);
|
||||||
|
}
|
||||||
|
|
||||||
|
#formTitle {
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
color: #5c6bc0;
|
||||||
|
margin-top: 94px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#formTitle span {
|
||||||
|
color: #dfdeee;
|
||||||
|
font-weight: lighter;
|
||||||
|
}
|
||||||
|
|
||||||
|
.registerForm h5 {
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #a1a4ad;
|
||||||
|
letter-spacing: 1.5px;
|
||||||
|
margin-top: -15px;
|
||||||
|
margin-bottom: 70px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.registerForm input[type="text"],
|
||||||
|
.registerForm input[type="password"] {
|
||||||
|
display: block;
|
||||||
|
margin: 20px auto;
|
||||||
|
background: #262e49;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 14px 10px;
|
||||||
|
width: 320px;
|
||||||
|
outline: none;
|
||||||
|
color: #d6d6d6;
|
||||||
|
-webkit-transition: all .2s ease-out;
|
||||||
|
-moz-transition: all .2s ease-out;
|
||||||
|
-ms-transition: all .2s ease-out;
|
||||||
|
-o-transition: all .2s ease-out;
|
||||||
|
transition: all .2s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.registerForm input[type="text"]:focus,
|
||||||
|
.registerForm input[type="password"]:focus {
|
||||||
|
border: 1px solid #79A6FE;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: #5c7fda;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submitButton {
|
||||||
|
border: 0;
|
||||||
|
background: #7f5feb;
|
||||||
|
color: #dfdeee;
|
||||||
|
border-radius: 100px;
|
||||||
|
width: 340px;
|
||||||
|
height: 49px;
|
||||||
|
font-size: 16px;
|
||||||
|
position: absolute;
|
||||||
|
top: 79%;
|
||||||
|
left: 8%;
|
||||||
|
transition: 0.3s;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.submitButton:hover {
|
||||||
|
background: #5d33e6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.recoveryPass {
|
||||||
|
position: relative;
|
||||||
|
float: right;
|
||||||
|
right: 28px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.noAccount {
|
||||||
|
position: absolute;
|
||||||
|
top: 92%;
|
||||||
|
left: 24%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
text-align: center;
|
||||||
|
width: 337px;
|
||||||
|
height: 20px;
|
||||||
|
padding: 2px;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin: 10px auto 10px;
|
||||||
|
position: absolute;
|
||||||
|
top: 31%;
|
||||||
|
left: 7.2%;
|
||||||
|
color: rgb(190, 67, 29);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<script>
|
||||||
|
import Dashboard from './board/Dashboard.svelte';
|
||||||
|
import SideMenu from './menu/SideMenu.svelte';
|
||||||
|
import {selectedTab} from "./stores.js";
|
||||||
|
import {globalStyles} from "./styles.js";
|
||||||
|
import StickyMenu from "./menu/StickyMenu.svelte";
|
||||||
|
import {onMount} from "svelte";
|
||||||
|
|
||||||
|
function handleTabClick(tab) {
|
||||||
|
selectedTab.set(tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
let screenWidth;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
screenWidth = window.innerWidth;
|
||||||
|
const handleResize = () => {
|
||||||
|
console.log(screenWidth);
|
||||||
|
screenWidth = window.innerWidth;
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('resize', handleResize);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('resize', handleResize);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div id="wrapper" style="background-color: {$globalStyles.mainColor}">
|
||||||
|
{#if screenWidth < 900}
|
||||||
|
<StickyMenu onTabClick={handleTabClick} />
|
||||||
|
{:else}
|
||||||
|
<SideMenu onTabClick={handleTabClick} />
|
||||||
|
{/if}
|
||||||
|
<Dashboard />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
@import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400');
|
||||||
|
|
||||||
|
#wrapper {
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: stretch;
|
||||||
|
min-height: 100vh;
|
||||||
|
max-height: 100%;
|
||||||
|
transition: all 0.3s cubic-bezier(.25,.8,.25,1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 900px) {
|
||||||
|
#wrapper {
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<h1>ADMIN PANEL</h1>
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
<script>
|
||||||
|
import { getCookie } from "svelte-cookie";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
import ExpenseDashboard from "./ExpenseDashboard.svelte";
|
||||||
|
import IncomeDashboard from "./IncomeDashboard.svelte";
|
||||||
|
import Settings from "./Settings.svelte";
|
||||||
|
import {
|
||||||
|
incomeData,
|
||||||
|
expenseData,
|
||||||
|
incomeTypes,
|
||||||
|
expenseTypes,
|
||||||
|
selectedTab,
|
||||||
|
monthIncome,
|
||||||
|
monthExpense,
|
||||||
|
tempExpense,
|
||||||
|
tempIncome
|
||||||
|
} from "../stores.js";
|
||||||
|
import {globalStyles} from "../styles.js";
|
||||||
|
|
||||||
|
let componentStyles;
|
||||||
|
|
||||||
|
$: {
|
||||||
|
console.log("got here")
|
||||||
|
componentStyles = $globalStyles;
|
||||||
|
}
|
||||||
|
|
||||||
|
import axios from "axios";
|
||||||
|
import Statistics from "./Statistics.svelte";
|
||||||
|
import AdminPanel from "./AdminPanel.svelte";
|
||||||
|
import Profile from "./Profile.svelte";
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
const token = getCookie('access_token');
|
||||||
|
|
||||||
|
if (token === '') {
|
||||||
|
window.location.href = '/auth/login';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const currentDate = new Date();
|
||||||
|
const currentMonth = currentDate.getMonth() + 1;
|
||||||
|
const [incomeResponse, expenseResponse, incomeTypesResponse, expenseTypesResponse] = await Promise.all([
|
||||||
|
axios.get('https://trackio.online:8081/incomes/personal-incomes?month=' + currentMonth , config),
|
||||||
|
axios.get('https://trackio.online:8081/expenses/personal-expenses?month=' + currentMonth, config),
|
||||||
|
axios.get('https://trackio.online:8081/incomes/categories', config),
|
||||||
|
axios.get('https://trackio.online:8081/expenses/categories', config)
|
||||||
|
]);
|
||||||
|
|
||||||
|
console.log("Data", incomeResponse.data);
|
||||||
|
|
||||||
|
incomeData.set(incomeResponse.data);
|
||||||
|
expenseData.set(expenseResponse.data);
|
||||||
|
|
||||||
|
incomeTypes.set(incomeTypesResponse.data);
|
||||||
|
expenseTypes.set(expenseTypesResponse.data);
|
||||||
|
|
||||||
|
tempExpense.set(expenseResponse.data);
|
||||||
|
tempIncome.set(incomeResponse.data);
|
||||||
|
|
||||||
|
monthIncome.set(incomeResponse.data);
|
||||||
|
monthExpense.set(expenseResponse.data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:head>
|
||||||
|
<link rel="icon" type="image/x-icon" href="../favicon.png" />
|
||||||
|
<title>Track.IO</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
|
<div id="dashboard" style="background-color: {componentStyles.dashColor}; color: {componentStyles.color}">
|
||||||
|
{#if $selectedTab === 'expenses'}
|
||||||
|
<ExpenseDashboard />
|
||||||
|
{:else if $selectedTab === 'incomes'}
|
||||||
|
<IncomeDashboard />
|
||||||
|
{:else if $selectedTab === 'settings'}
|
||||||
|
<Settings />
|
||||||
|
{:else if $selectedTab === 'statistics'}
|
||||||
|
<Statistics />
|
||||||
|
{:else if $selectedTab === 'admin'}
|
||||||
|
<AdminPanel />
|
||||||
|
{:else if $selectedTab === 'profile'}
|
||||||
|
<Profile />
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
#dashboard {
|
||||||
|
font-family: 'Source Sans Pro', sans-serif;
|
||||||
|
border-radius: 20px;
|
||||||
|
margin: 20px;
|
||||||
|
min-width: 100px;
|
||||||
|
display: flex;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: stretch;
|
||||||
|
justify-content: stretch;
|
||||||
|
transition: all 0.3s cubic-bezier(.25,.8,.25,1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 900px) {
|
||||||
|
#dashboard {
|
||||||
|
margin: 0;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<script>
|
||||||
|
import DashHeader from "./expenses/other/DashHeader.svelte";
|
||||||
|
import QuickInfobar from "./expenses/other/QuickInfobar.svelte";
|
||||||
|
import Expenses from "./expenses/infolists/Expenses.svelte";
|
||||||
|
import Graph3 from "./expenses/graphs/Graph3.svelte";
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="expenseContainer">
|
||||||
|
<div class="dataHalf">
|
||||||
|
<div>
|
||||||
|
<DashHeader />
|
||||||
|
<QuickInfobar />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="graphs">
|
||||||
|
<Graph3 />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Expenses />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
@media only screen and (max-width: 900px) {
|
||||||
|
.expenseContainer {
|
||||||
|
flex-wrap: wrap;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.graphs {
|
||||||
|
display:flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: stretch;
|
||||||
|
min-width: 0;
|
||||||
|
min-height: 0;
|
||||||
|
height: 100% !important;
|
||||||
|
width: 100% !important;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
}
|
||||||
|
.expenseContainer {
|
||||||
|
display: flex;
|
||||||
|
height: 100%;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dataHalf {
|
||||||
|
display:flex;
|
||||||
|
min-height: 0;
|
||||||
|
min-width: 0;
|
||||||
|
flex-direction: column;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
background-color: #212942;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<script>
|
||||||
|
import DashHeader from "./incomes/other/DashHeader.svelte";
|
||||||
|
import DataMenu from "./incomes/other/DataMenu.svelte";
|
||||||
|
import QuickInfobar from "./incomes/other/QuickInfobar.svelte";
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<DashHeader />
|
||||||
|
<QuickInfobar />
|
||||||
|
<DataMenu />
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<h1>PROFILE</h1>
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
<script>
|
||||||
|
import {globalStyles} from "../styles.js";
|
||||||
|
import {themeDark} from "../styles.js";
|
||||||
|
import {themeDefault} from "../styles.js";
|
||||||
|
import {themeColorful} from "../styles.js";
|
||||||
|
|
||||||
|
function theme_dark() {
|
||||||
|
$globalStyles = themeDark;
|
||||||
|
}
|
||||||
|
|
||||||
|
function theme_default() {
|
||||||
|
$globalStyles = themeDefault;
|
||||||
|
}
|
||||||
|
|
||||||
|
function theme_colorful() {
|
||||||
|
$globalStyles = themeColorful;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div style="color: {$globalStyles.dashTextColor}">
|
||||||
|
<h1>Settings</h1>
|
||||||
|
<div id="buttonContainer">
|
||||||
|
<div class="settingEntry">
|
||||||
|
<span><svg style="fill: yellow" xmlns="http://www.w3.org/2000/svg" height="16" width="16" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M361.5 1.2c5 2.1 8.6 6.6 9.6 11.9L391 121l107.9 19.8c5.3 1 9.8 4.6 11.9 9.6s1.5 10.7-1.6 15.2L446.9 256l62.3 90.3c3.1 4.5 3.7 10.2 1.6 15.2s-6.6 8.6-11.9 9.6L391 391 371.1 498.9c-1 5.3-4.6 9.8-9.6 11.9s-10.7 1.5-15.2-1.6L256 446.9l-90.3 62.3c-4.5 3.1-10.2 3.7-15.2 1.6s-8.6-6.6-9.6-11.9L121 391 13.1 371.1c-5.3-1-9.8-4.6-11.9-9.6s-1.5-10.7 1.6-15.2L65.1 256 2.8 165.7c-3.1-4.5-3.7-10.2-1.6-15.2s6.6-8.6 11.9-9.6L121 121 140.9 13.1c1-5.3 4.6-9.8 9.6-11.9s10.7-1.5 15.2 1.6L256 65.1 346.3 2.8c4.5-3.1 10.2-3.7 15.2-1.6zM160 256a96 96 0 1 1 192 0 96 96 0 1 1 -192 0zm224 0a128 128 0 1 0 -256 0 128 128 0 1 0 256 0z"/></svg></span>
|
||||||
|
<h3 class="settingName">Light Mode</h3>
|
||||||
|
<button class="button-32" on:click={() => theme_default()}>Select</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="settingEntry">
|
||||||
|
<span><svg style="fill:darkblue" xmlns="http://www.w3.org/2000/svg" height="16" width="12" viewBox="0 0 384 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M223.5 32C100 32 0 132.3 0 256S100 480 223.5 480c60.6 0 115.5-24.2 155.8-63.4c5-4.9 6.3-12.5 3.1-18.7s-10.1-9.7-17-8.5c-9.8 1.7-19.8 2.6-30.1 2.6c-96.9 0-175.5-78.8-175.5-176c0-65.8 36-123.1 89.3-153.3c6.1-3.5 9.2-10.5 7.7-17.3s-7.3-11.9-14.3-12.5c-6.3-.5-12.6-.8-19-.8z"/></svg></span>
|
||||||
|
<h3 class="settingName">Dark Mode</h3>
|
||||||
|
<button class="button-32" on:click={() => theme_dark()}>Select</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="settingEntry">
|
||||||
|
<span><svg style="fill:pink" xmlns="http://www.w3.org/2000/svg" height="16" width="10" viewBox="0 0 320 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.--><path d="M160 0a48 48 0 1 1 0 96 48 48 0 1 1 0-96zM88 384H70.2c-10.9 0-18.6-10.7-15.2-21.1L93.3 248.1 59.4 304.5c-9.1 15.1-28.8 20-43.9 10.9s-20-28.8-10.9-43.9l53.6-89.2c20.3-33.7 56.7-54.3 96-54.3h11.6c39.3 0 75.7 20.6 96 54.3l53.6 89.2c9.1 15.1 4.2 34.8-10.9 43.9s-34.8 4.2-43.9-10.9l-33.9-56.3L265 362.9c3.5 10.4-4.3 21.1-15.2 21.1H232v96c0 17.7-14.3 32-32 32s-32-14.3-32-32V384H152v96c0 17.7-14.3 32-32 32s-32-14.3-32-32V384z"/></svg></span>
|
||||||
|
<h3 class="settingName">Pinky Theme</h3>
|
||||||
|
<button class="button-32" on:click={() => theme_colorful()}>Select</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.button-32 {
|
||||||
|
background-color: #fff000;
|
||||||
|
border-radius: 12px;
|
||||||
|
color: #000;
|
||||||
|
cursor: pointer;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 10px 15px;
|
||||||
|
text-align: center;
|
||||||
|
transition: 200ms;
|
||||||
|
width: 100px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
border: 0;
|
||||||
|
font-size: 16px;
|
||||||
|
user-select: none;
|
||||||
|
-webkit-user-select: none;
|
||||||
|
touch-action: manipulation;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-32:not(:disabled):hover,
|
||||||
|
.button-32:not(:disabled):focus {
|
||||||
|
outline: 0;
|
||||||
|
background: #f4e603;
|
||||||
|
box-shadow: 0 0 0 2px rgba(0,0,0,.2), 0 3px 8px 0 rgba(0,0,0,.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-32:disabled {
|
||||||
|
filter: saturate(0.2) opacity(0.5);
|
||||||
|
-webkit-filter: saturate(0.2) opacity(0.5);
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
#buttonContainer {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settingEntry {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settingName {
|
||||||
|
margin: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<h1>ACT LIKE THERE'S A NEURAL NETWORK HERE</h1>
|
||||||
|
<h1>IT'S REALLY IN THE WORKS THOUGH</h1>
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
<script>
|
||||||
|
import Chart from 'chart.js/auto';
|
||||||
|
import {onMount} from 'svelte';
|
||||||
|
import {expenseData} from "../../../stores.js";
|
||||||
|
import {globalStyles} from "../../../styles.js";
|
||||||
|
|
||||||
|
let ctx;
|
||||||
|
let chartCanvas;
|
||||||
|
let chart = null;
|
||||||
|
|
||||||
|
function groupAndSumByCategory() {
|
||||||
|
const groupedData = new Map();
|
||||||
|
$expenseData.forEach(expense => {
|
||||||
|
const category = expense.expenseCategory.name;
|
||||||
|
if (groupedData.has(category)) {
|
||||||
|
groupedData.set(category, groupedData.get(category) + parseInt(expense.amount));
|
||||||
|
} else {
|
||||||
|
groupedData.set(category, parseInt(expense.amount));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Map([...groupedData.entries()].sort());
|
||||||
|
}
|
||||||
|
|
||||||
|
function createGraph() {
|
||||||
|
try {
|
||||||
|
const groupedExpenseData = groupAndSumByCategory();
|
||||||
|
console.log("============= here start")
|
||||||
|
console.log(groupedExpenseData);
|
||||||
|
|
||||||
|
const chartLabels = [];
|
||||||
|
const chartValues = [];
|
||||||
|
|
||||||
|
for (const [label, value] of groupedExpenseData.entries()) {
|
||||||
|
chartLabels.push(label);
|
||||||
|
chartValues.push(value);
|
||||||
|
}
|
||||||
|
console.log(chartLabels)
|
||||||
|
console.log(chartValues)
|
||||||
|
console.log("============= here end")
|
||||||
|
ctx = chartCanvas.getContext('2d');
|
||||||
|
|
||||||
|
if (!chart) {
|
||||||
|
chart = new Chart(ctx, {
|
||||||
|
type: 'bar',
|
||||||
|
data: {
|
||||||
|
labels: chartLabels,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Spendings',
|
||||||
|
backgroundColor: [
|
||||||
|
'rgb(107, 80, 107)',
|
||||||
|
'rgb(171, 61, 169)',
|
||||||
|
'rgb(222, 37, 218)',
|
||||||
|
'rgb(235, 68, 232)',
|
||||||
|
'rgb(255, 128, 255)'],
|
||||||
|
data: chartValues
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
responsive: true,
|
||||||
|
maintainAspectRatio: false,
|
||||||
|
plugins: {
|
||||||
|
legend: {
|
||||||
|
labels: {
|
||||||
|
font: {
|
||||||
|
weight: 'bold'
|
||||||
|
},
|
||||||
|
color: '#fff'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
chart.data.labels = chartLabels;
|
||||||
|
chart.data.datasets[0].data = chartValues;
|
||||||
|
chart.update();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if ($expenseData) {
|
||||||
|
createGraph();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
createGraph();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div id="chart" style="background-color: {$globalStyles.mainColor}">
|
||||||
|
<canvas bind:this={chartCanvas}></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chart {
|
||||||
|
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
|
||||||
|
transition: all 0.3s cubic-bezier(.25,.8,.25,1);
|
||||||
|
flex: 1;
|
||||||
|
border-radius: 0 0 10px 10px;
|
||||||
|
margin: 0 0 10px 10px;
|
||||||
|
min-width: 0;
|
||||||
|
min-height:0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chart:hover {
|
||||||
|
box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
<script>
|
||||||
|
import Chart from 'chart.js/auto';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import {monthIncome, monthExpense, isCategorizedExpense, categorizedExpense} from "../../../stores.js";
|
||||||
|
import { globalStyles } from "../../../styles.js";
|
||||||
|
|
||||||
|
let ctx;
|
||||||
|
let chartCanvas;
|
||||||
|
let chart = null;
|
||||||
|
|
||||||
|
let generatedData;
|
||||||
|
|
||||||
|
function createGraph() {
|
||||||
|
try {
|
||||||
|
// const allDates = [...new Set([...$monthIncome, ...$monthExpense].map(item => item.date))];
|
||||||
|
// const uniqueDates = allDates.sort((a, b) => new Date(a) - new Date(b));
|
||||||
|
//
|
||||||
|
// const incomeValues = uniqueDates.map(date => $monthIncome.filter(item => item.date === date).reduce((total, item) => total + item.amount, 0));
|
||||||
|
// const expenseValues = uniqueDates.map(date => $monthExpense.filter(item => item.date === date).reduce((total, item) => total + item.amount, 0));
|
||||||
|
|
||||||
|
if (chartCanvas.getContext('2d') !== undefined) {
|
||||||
|
ctx = chartCanvas.getContext('2d');
|
||||||
|
if (!chart) {
|
||||||
|
chart = new Chart(ctx, {
|
||||||
|
type: 'line',
|
||||||
|
data: generatedData,
|
||||||
|
options: {
|
||||||
|
responsive: true,
|
||||||
|
maintainAspectRatio: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if ($isCategorizedExpense === true) {
|
||||||
|
chart.data.labels = generatedData.labels;
|
||||||
|
chart.data.datasets = generatedData.datasets;
|
||||||
|
} else {
|
||||||
|
generatedData.datasets = generatedData.datasets.filter(dataset => dataset.label !== "Category");
|
||||||
|
chart.data.labels = generatedData.labels;
|
||||||
|
chart.data.datasets = generatedData.datasets;
|
||||||
|
}
|
||||||
|
|
||||||
|
chart.update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if (isCategorizedExpense) {
|
||||||
|
const allDates = [...new Set([...$monthExpense, ...$categorizedExpense].map(item => item.date))];
|
||||||
|
const uniqueDates = allDates.sort((a, b) => new Date(a) - new Date(b));
|
||||||
|
|
||||||
|
const categorizedValues = uniqueDates.map(date => $categorizedExpense.filter(item => item.date === date).reduce((total, item) => total + item.amount, 0));
|
||||||
|
const expenseValues = uniqueDates.map(date => $monthExpense.filter(item => item.date === date).reduce((total, item) => total + item.amount, 0));
|
||||||
|
|
||||||
|
generatedData = {
|
||||||
|
labels: uniqueDates,
|
||||||
|
datasets: [
|
||||||
|
{
|
||||||
|
label: "Category",
|
||||||
|
backgroundColor: "rgba(21, 194, 58, 0.4)",
|
||||||
|
borderColor: "rgba(21, 194, 58, 1)",
|
||||||
|
data: categorizedValues,
|
||||||
|
tension: 0.2,
|
||||||
|
fill: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Expense",
|
||||||
|
backgroundColor: "rgba(194, 21, 96, 0.4)",
|
||||||
|
borderColor: "rgba(194, 21, 96, 1)",
|
||||||
|
data: expenseValues,
|
||||||
|
tension: 0.4,
|
||||||
|
fill: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const allDates = [...new Set([...$monthExpense].map(item => item.date))];
|
||||||
|
const uniqueDates = allDates.sort((a, b) => new Date(a) - new Date(b));
|
||||||
|
|
||||||
|
const expenseValues = uniqueDates.map(date => $monthExpense.filter(item => item.date === date).reduce((total, item) => total + item.amount, 0));
|
||||||
|
|
||||||
|
generatedData = {
|
||||||
|
labels: uniqueDates,
|
||||||
|
datasets:
|
||||||
|
{
|
||||||
|
label: "Expense",
|
||||||
|
backgroundColor: "rgba(194, 21, 96, 0.4)",
|
||||||
|
borderColor: "rgba(194, 21, 96, 1)",
|
||||||
|
data: expenseValues,
|
||||||
|
tension: 0.4,
|
||||||
|
fill: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($monthIncome || $monthExpense || $isCategorizedExpense || $categorizedExpense) {
|
||||||
|
createGraph();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
createGraph();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div id="chart" style="background-color: {$globalStyles.mainColor}">
|
||||||
|
<canvas id="canvas" bind:this={chartCanvas}></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chart {
|
||||||
|
min-width: 0;
|
||||||
|
min-height:0;
|
||||||
|
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
|
||||||
|
transition: all 0.3s cubic-bezier(.25, .8, .25, 1);
|
||||||
|
display: flex;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
border-radius: 0 0 10px 10px;
|
||||||
|
margin: 0 0 10px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chart:hover {
|
||||||
|
box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user