Compare commits
5 Commits
42bb3c608f
...
19989111ec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19989111ec | ||
|
|
4f49c24c99 | ||
|
|
8763f88de4 | ||
|
|
5ab8109504 | ||
|
|
d31d6aaf2d |
@@ -3,7 +3,9 @@ package com.example.demo.app.domain.model;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.example.demo.app.infra.entity.CategoryEntity;
|
||||||
import com.example.demo.app.infra.entity.TransactionEntity;
|
import com.example.demo.app.infra.entity.TransactionEntity;
|
||||||
|
import com.example.demo.app.infra.entity.UserEntity;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -35,4 +37,13 @@ public class TransactionModel {
|
|||||||
return transactionEntityList.stream().map(TransactionModel::convertFrom).toList();
|
return transactionEntityList.stream().map(TransactionModel::convertFrom).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static TransactionEntity convertTo(TransactionModel transactionModel, UserEntity userEntity, CategoryEntity categoryEntity) {
|
||||||
|
return new TransactionEntity(
|
||||||
|
transactionModel.getTransactionId(),
|
||||||
|
userEntity,
|
||||||
|
categoryEntity,
|
||||||
|
transactionModel.getAmount(),
|
||||||
|
transactionModel.getTransactionDate());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,4 +9,6 @@ public interface CategoryService {
|
|||||||
public List<CategoryModel> findAll();
|
public List<CategoryModel> findAll();
|
||||||
|
|
||||||
public CategoryModel findOne(Long categoryId);
|
public CategoryModel findOne(Long categoryId);
|
||||||
|
|
||||||
|
public CategoryModel findOneByCategoryName(String categoryName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,8 @@ public interface TransactionService {
|
|||||||
|
|
||||||
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId);
|
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
|
public int createTransaction(TransactionModel transactionModel);
|
||||||
|
|
||||||
|
public int updateTransaction(TransactionModel transactionModel);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,26 +14,38 @@ import lombok.RequiredArgsConstructor;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CategoryServiceImpl implements CategoryService{
|
public class CategoryServiceImpl implements CategoryService {
|
||||||
|
|
||||||
private final CategoryRepository categoryRepository;
|
private final CategoryRepository categoryRepository;
|
||||||
|
|
||||||
public List<CategoryModel> findAll() {
|
public List<CategoryModel> findAll() {
|
||||||
|
|
||||||
return CategoryModel.convertFrom(categoryRepository.findAll());
|
return CategoryModel.convertFrom(categoryRepository.findAll());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CategoryModel findOne(Long categoryId) {
|
public CategoryModel findOne(Long categoryId) {
|
||||||
|
|
||||||
Optional<CategoryEntity> optionalCategoryModel = categoryRepository.findById(categoryId);
|
Optional<CategoryEntity> optionalCategoryModel = categoryRepository.findById(categoryId);
|
||||||
|
|
||||||
if (optionalCategoryModel.isEmpty()) {
|
if (optionalCategoryModel.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CategoryModel.convertFrom(optionalCategoryModel.get());
|
return CategoryModel.convertFrom(optionalCategoryModel.get());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CategoryModel findOneByCategoryName(String categoryName) {
|
||||||
|
|
||||||
|
CategoryEntity categoryModel = categoryRepository.findByCategoryName(categoryName);
|
||||||
|
|
||||||
|
if (categoryModel == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CategoryModel.convertFrom(categoryModel);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,19 @@ package com.example.demo.app.domain.service.impl;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.example.demo.app.domain.model.TransactionModel;
|
import com.example.demo.app.domain.model.TransactionModel;
|
||||||
import com.example.demo.app.domain.service.TransactionService;
|
import com.example.demo.app.domain.service.TransactionService;
|
||||||
|
import com.example.demo.app.infra.entity.CategoryEntity;
|
||||||
import com.example.demo.app.infra.entity.TransactionEntity;
|
import com.example.demo.app.infra.entity.TransactionEntity;
|
||||||
|
import com.example.demo.app.infra.entity.UserEntity;
|
||||||
|
import com.example.demo.app.infra.repository.CategoryRepository;
|
||||||
import com.example.demo.app.infra.repository.TransactionRepository;
|
import com.example.demo.app.infra.repository.TransactionRepository;
|
||||||
|
import com.example.demo.app.infra.repository.UserRepository;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@@ -19,6 +24,8 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class TransactionServiceImpl implements TransactionService {
|
public class TransactionServiceImpl implements TransactionService {
|
||||||
|
|
||||||
private final TransactionRepository transactionRepository;
|
private final TransactionRepository transactionRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final CategoryRepository categoryRepository;
|
||||||
|
|
||||||
public Map<LocalDate, List<TransactionModel>> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId) {
|
public Map<LocalDate, List<TransactionModel>> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
@@ -34,16 +41,17 @@ public class TransactionServiceImpl implements TransactionService {
|
|||||||
return transactionModelMap;
|
return transactionModelMap;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId) {
|
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId) {
|
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TransactionModel> findByUserIdAndDate(LocalDate todayDate, Long userId) {
|
public List<TransactionModel> findByUserIdAndDate(LocalDate todayDate, Long userId) {
|
||||||
@@ -52,4 +60,59 @@ public class TransactionServiceImpl implements TransactionService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int createTransaction(TransactionModel transactionModel) {
|
||||||
|
|
||||||
|
Optional<UserEntity> userEntityOptional = userRepository.findById(transactionModel.getUserId());
|
||||||
|
|
||||||
|
if (userEntityOptional.isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CategoryEntity> categoryEntityOptional = categoryRepository.findById(transactionModel.getCategoryId());
|
||||||
|
if (categoryEntityOptional.isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
TransactionEntity transactionEntity = TransactionModel.convertTo(
|
||||||
|
transactionModel,
|
||||||
|
userEntityOptional.get(),
|
||||||
|
categoryEntityOptional.get());
|
||||||
|
if (transactionRepository.save(transactionEntity) != null) {
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int updateTransaction(TransactionModel transactionModel) {
|
||||||
|
|
||||||
|
if (!transactionRepository.existsById(transactionModel.getTransactionId())) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<UserEntity> userEntityOptional = userRepository.findById(transactionModel.getUserId());
|
||||||
|
|
||||||
|
if (userEntityOptional.isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CategoryEntity> categoryEntityOptional = categoryRepository.findById(transactionModel.getCategoryId());
|
||||||
|
if (categoryEntityOptional.isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
TransactionEntity transactionEntity = TransactionModel.convertTo(
|
||||||
|
transactionModel,
|
||||||
|
userEntityOptional.get(),
|
||||||
|
categoryEntityOptional.get());
|
||||||
|
if (transactionRepository.save(transactionEntity) != null) {
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ public class TransactionEntity {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name = "transaction_id")
|
@Column(name = "transaction_id")
|
||||||
private Long transactionId;
|
private Long transactionId;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity userEntity;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "category_id")
|
||||||
|
private CategoryEntity categoryEntity;
|
||||||
|
|
||||||
@Column(name = "amount", nullable = false)
|
@Column(name = "amount", nullable = false)
|
||||||
private Integer amount;
|
private Integer amount;
|
||||||
@@ -34,12 +42,4 @@ public class TransactionEntity {
|
|||||||
@Column(name = "transaction_date", nullable = false)
|
@Column(name = "transaction_date", nullable = false)
|
||||||
private LocalDate transactionDate;
|
private LocalDate transactionDate;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
|
||||||
@JoinColumn(name = "user_id")
|
|
||||||
private UserEntity userEntity;
|
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
|
||||||
@JoinColumn(name = "category_id")
|
|
||||||
private CategoryEntity categoryEntity;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package com.example.demo.app.infra.repository;
|
package com.example.demo.app.infra.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
import com.example.demo.app.infra.entity.CategoryEntity;
|
import com.example.demo.app.infra.entity.CategoryEntity;
|
||||||
|
|
||||||
public interface CategoryRepository extends JpaRepository<CategoryEntity, Long> {
|
public interface CategoryRepository extends JpaRepository<CategoryEntity, Long> {
|
||||||
|
|
||||||
|
|
||||||
|
@Query("select c from CategoryEntity c where c.categoryName = :categoryName")
|
||||||
|
public CategoryEntity findByCategoryName(@Param("categoryName") String categoryName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,11 @@ import jakarta.servlet.http.HttpSession;
|
|||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
import com.example.demo.app.domain.model.LoginModel;
|
||||||
import com.example.demo.app.domain.service.CategoryService;
|
import com.example.demo.app.domain.service.CategoryService;
|
||||||
import com.example.demo.app.domain.service.TransactionService;
|
import com.example.demo.app.domain.service.TransactionService;
|
||||||
import com.example.demo.app.presentation.form.CategoryForm;
|
import com.example.demo.app.presentation.form.CategoryForm;
|
||||||
@@ -26,27 +29,32 @@ public class TransactionController {
|
|||||||
|
|
||||||
private final TransactionService transactionService;
|
private final TransactionService transactionService;
|
||||||
private final CategoryService categoryService;
|
private final CategoryService categoryService;
|
||||||
|
|
||||||
// 一覧画面
|
// 一覧画面
|
||||||
|
|
||||||
@GetMapping("/transaction/list")
|
@GetMapping("/transaction/list")
|
||||||
public String transactionList(@RequestParam LocalDate startDate, @RequestParam LocalDate endDate, Model model,
|
public String transactionList(@RequestParam(required = false) LocalDate startDates,
|
||||||
|
@RequestParam(required = false) LocalDate endDates, Model model,
|
||||||
HttpSession session) {
|
HttpSession session) {
|
||||||
|
|
||||||
LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
|
LocalDate startDate = (startDates == null) ? LocalDate.now().withDayOfMonth(1) : startDates;
|
||||||
|
|
||||||
|
LocalDate endDate = (endDates == null) ? LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth())
|
||||||
|
: endDates;
|
||||||
|
|
||||||
|
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||||
|
|
||||||
Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>();
|
Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>();
|
||||||
|
|
||||||
|
transactionService.findAllByDate(startDate, endDate, loginUser.getUserId()).forEach((date, list) -> {
|
||||||
transactionService.findAll(startDate, endDate, loginUser.getUserId).forEach((date, list) -> {
|
|
||||||
List<TransactionForm> transactionFormList = list.stream().map(transaction -> {
|
List<TransactionForm> transactionFormList = list.stream().map(transaction -> {
|
||||||
TransactionForm transactionForm = TransactionForm.convertFrom(
|
TransactionForm transactionForm = TransactionForm.convertFrom(
|
||||||
transaction,
|
transaction,
|
||||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getCategoryName(),
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||||
transactionService.findTotalexpense(startDate, endDate, loginUser.getUserId),
|
.getCategoryName(),
|
||||||
transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId),
|
transactionService.findTotalexpense(startDate, endDate, loginUser.getUserId()),
|
||||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getTransactionType()
|
transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId()),
|
||||||
);
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||||
|
.getTransactionType());
|
||||||
return transactionForm;
|
return transactionForm;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
transactionFormMap.put(date, transactionFormList);
|
transactionFormMap.put(date, transactionFormList);
|
||||||
@@ -59,25 +67,64 @@ public class TransactionController {
|
|||||||
|
|
||||||
// 詳細画面
|
// 詳細画面
|
||||||
@GetMapping()
|
@GetMapping()
|
||||||
public String findOne(@RequestParam LocalDate todayDate, Model model, HttpSession session) {
|
public String findOne(@RequestParam(required = false) LocalDate todayDates, Model model, HttpSession session) {
|
||||||
|
|
||||||
LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
|
LocalDate todayDate = (todayDates == null) ? LocalDate.now() : todayDates;
|
||||||
|
|
||||||
List<TransactionForm> transactionFormList = transactionService.findByUserIdAndDate(todayDate, ((LoginUser)session.getAttribute("loginUser")).getUserId).stream().map(transaction -> {
|
|
||||||
TransactionForm transactionForm = TransactionForm.convertFrom(
|
|
||||||
transaction,
|
|
||||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getCategoryName(),
|
|
||||||
transactionService.findTotalexpense(todayDate, todayDate, loginUser.getUserId),
|
|
||||||
transactionService.findTotalIncome(todayDate, todayDate, loginUser.getUserId),
|
|
||||||
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getTransactionType()
|
|
||||||
);
|
|
||||||
return transactionForm;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
|
|
||||||
|
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||||
|
|
||||||
|
List<TransactionForm> transactionFormList = transactionService
|
||||||
|
.findByUserIdAndDate(todayDate, loginUser.getUserId()).stream()
|
||||||
|
.map(transaction -> {
|
||||||
|
TransactionForm transactionForm = TransactionForm.convertFrom(
|
||||||
|
transaction,
|
||||||
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||||
|
.getCategoryName(),
|
||||||
|
transactionService.findTotalexpense(todayDate, todayDate, loginUser.getUserId()),
|
||||||
|
transactionService.findTotalIncome(todayDate, todayDate, loginUser.getUserId()),
|
||||||
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId()))
|
||||||
|
.getTransactionType());
|
||||||
|
return transactionForm;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
|
||||||
model.addAttribute("transactionFormList", transactionFormList);
|
model.addAttribute("transactionFormList", transactionFormList);
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("")
|
||||||
|
public String createOneGet(@ModelAttribute TransactionForm transactionForm) {
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public String createOnePost(@ModelAttribute TransactionForm transactionForm, Model model) {
|
||||||
|
|
||||||
|
if (transactionService.createTransaction(TransactionForm.convertTo(transactionForm)) != 0) {
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("")
|
||||||
|
public String updateOneGet(@ModelAttribute TransactionForm transactionForm) {
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public String updateOnePost(@ModelAttribute TransactionForm transactionForm, Model model) {
|
||||||
|
|
||||||
|
if (transactionService.updateTransaction(TransactionForm.convertTo(transactionForm)) != 0) {
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ public class TransactionForm {
|
|||||||
|
|
||||||
private Integer transactionType;
|
private Integer transactionType;
|
||||||
|
|
||||||
|
|
||||||
public static TransactionForm convertFrom(TransactionModel transactionModel, String categoryName, int income, int expense, int transactionType) {
|
public static TransactionForm convertFrom(TransactionModel transactionModel, String categoryName, int income, int expense, int transactionType) {
|
||||||
return new TransactionForm(
|
return new TransactionForm(
|
||||||
transactionModel.getTransactionId(),
|
transactionModel.getTransactionId(),
|
||||||
@@ -45,6 +46,7 @@ public class TransactionForm {
|
|||||||
transactionType);
|
transactionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<TransactionForm> convertFrom(List<TransactionModel> transactionModelList, String categoryName, int income, int expense, int transactionType ) {
|
public static List<TransactionForm> convertFrom(List<TransactionModel> transactionModelList, String categoryName, int income, int expense, int transactionType ) {
|
||||||
return transactionModelList.stream().map(transactionModel -> TransactionForm.convertFrom(transactionModel, categoryName, income, expense, transactionType)).toList();
|
return transactionModelList.stream().map(transactionModel -> TransactionForm.convertFrom(transactionModel, categoryName, income, expense, transactionType)).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user