Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/jwttoken #5

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.fianancetracker.backend.SecurityConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import org.fianancetracker.backend.util.Validation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
Expand Down Expand Up @@ -38,4 +37,15 @@ public ResponseEntity<String> addExpense(@RequestBody ExpenseDTO expenseDTO) {
}
}

@GetMapping("/getAllExpenses/{username}")
public ResponseEntity<List<ExpenseDTO>> getAllExpense(@PathVariable("username") String username) {
log.info(this.getClass().getName() + "get All Expense");
List<ExpenseDTO> expense = expenseService.getAllExpense(username);
if (expense.isEmpty()) {
return new ResponseEntity<>(expense, HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(expense, HttpStatus.FOUND);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.fianancetracker.backend.service.UserService;
import org.fianancetracker.backend.util.MessageVarList;
import org.fianancetracker.backend.util.Validation;
import org.fianancetracker.backend.webToken.JWTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,6 +22,8 @@ public class LoginController {
@Autowired
UserService userService;

JWTService jwtService = new JWTService();

@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody UserDTO userDTO) {
log.info("LoginController - register");
Expand All @@ -44,9 +47,9 @@ public ResponseEntity<String> login(@RequestBody UserDTO userDTO) {
}
message = userService.loginUser(userDTO);
if (message.isEmpty()) {
return new ResponseEntity<>(MessageVarList.VALID_lOGIN, HttpStatus.CREATED);
return new ResponseEntity<>(jwtService.generateToken(userDTO), HttpStatus.CREATED);
}
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(message, HttpStatus.BAD_GATEWAY);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.fianancetracker.backend.dto;

import jakarta.persistence.Column;
import lombok.Data;
import org.springframework.stereotype.Component;

Expand All @@ -10,13 +9,15 @@ public class CategoryDTO {
private String id;
private String description;
private String type;
private String username;

public CategoryDTO() {
}

public CategoryDTO(Long id, String description, String type) {
public CategoryDTO(Long id, String description, String type, String username) {
this.id = String.valueOf(id);
this.description = description;
this.type = type;
this.username = username;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.fianancetracker.backend.dto;

import lombok.Data;
import org.fianancetracker.backend.models.Category;
import org.springframework.stereotype.Component;

import java.sql.Date;

@Data
@Component

public class ExpenseDTO {

private Long id;
private Long userId;
private String userId;
private String description;
private Long amount;
private String type;
private Long type;
private Date date;

public ExpenseDTO(Long id, String userId, String description, Long amount, Long type) {
this.id = id;
this.userId = userId;
this.description = description;
this.amount = amount;
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import jakarta.persistence.*;
import lombok.Data;

import java.sql.Date;

@Entity
@Table(name = "Category")
@Data
Expand All @@ -18,5 +20,10 @@ public class Category {
@Column(name = "type")
private String type;

@ManyToOne
@JoinColumn(name = "user_name", nullable = false, referencedColumnName = "user_name")
private User user;

@Column(name = "create_date")
private Date date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class Expense {
@Column(name = "description")
private String description;

@Column(name = "type")
private String type;
@ManyToOne
@JoinColumn(name = "type", nullable = false)
private Category type;

@Column(name = "amount")
private Long amount;
Expand All @@ -27,7 +28,7 @@ public class Expense {
private Date date;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id", nullable = false , referencedColumnName = "user_name")
private User user;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.fianancetracker.backend.repository;

import org.fianancetracker.backend.models.Expense;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public interface ExpenseRepository extends JpaRepository<Expense, Long> {

@Query("SELECT E FROM Expense E WHERE E.user.userName = :username AND E.date BETWEEN :startDate AND :endDate")
List<Expense> findExpenseByMonth(
@Param("username") String username,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import org.fianancetracker.backend.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

User getReferenceByEmail(String email);

User findByUserName(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.fianancetracker.backend.service;

import org.fianancetracker.backend.models.User;
import org.springframework.stereotype.Service;

@Service
public interface CommonService {

User findUserByUserName(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.fianancetracker.backend.dto.ExpenseDTO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface ExpenseService {

String addExpense(ExpenseDTO expenseDTO);

List<ExpenseDTO> getAllExpense(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import org.fianancetracker.backend.dto.CategoryDTO;
import org.fianancetracker.backend.models.Category;
import org.fianancetracker.backend.repository.CategoryRepository;
import org.fianancetracker.backend.repository.UserRepository;
import org.fianancetracker.backend.service.CategoryService;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -16,8 +19,11 @@ public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;

public CategoryServiceImpl(CategoryRepository categoryRepository) {
private final UserRepository userRepository;

public CategoryServiceImpl(CategoryRepository categoryRepository, UserRepository userRepository) {
this.categoryRepository = categoryRepository;
this.userRepository = userRepository;
}

@Override
Expand All @@ -27,6 +33,9 @@ public String addCategory(CategoryDTO categoryDTO) {
Category category = new Category();
category.setDescription(categoryDTO.getDescription());
category.setType(categoryDTO.getType());
LocalDate currentDate = LocalDate.now();
category.setDate(Date.valueOf(currentDate));
category.setUser(userRepository.findByUserName(categoryDTO.getUsername()));
categoryRepository.save(category);
} catch (Exception e) {
message = e.getMessage();
Expand Down Expand Up @@ -80,6 +89,7 @@ public List<CategoryDTO> getAllCategories() {
.map(category -> new CategoryDTO(
category.getId(),
category.getDescription(),
category.getUser().getUserName(),
category.getType()))
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -112,6 +122,7 @@ public List<CategoryDTO> getCategoryByType(String type) {
.map(category -> new CategoryDTO(
category.getId(),
category.getDescription(),
category.getUser().getUserName(),
category.getType()))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.fianancetracker.backend.service.Impl;

import org.fianancetracker.backend.models.User;
import org.fianancetracker.backend.repository.UserRepository;
import org.fianancetracker.backend.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommonServiceImpl implements CommonService {

@Autowired
UserRepository userRepository;

@Override
public User findUserByUserName(String username) {
User user = userRepository.findByUserName(username);
return user;
}
}
Loading
Loading