Compare commits
6 Commits
3e0c6092aa
...
544ce423d0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
544ce423d0 | ||
|
|
6847f225c4 | ||
|
|
036a495bc7 | ||
|
|
534fc2683d | ||
|
|
555d8ceb87 | ||
|
|
60159ea3fe |
@@ -1,6 +1,9 @@
|
|||||||
package com.example.demo.app.domain.model;
|
package com.example.demo.app.domain.model;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.example.demo.app.infra.entity.TransactionEntity;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -19,4 +22,17 @@ public class TransactionModel {
|
|||||||
|
|
||||||
private LocalDate transactionDate;
|
private LocalDate transactionDate;
|
||||||
|
|
||||||
|
public static TransactionModel convertFrom(TransactionEntity transactionEntity) {
|
||||||
|
return new TransactionModel(
|
||||||
|
transactionEntity.getTransactionId(),
|
||||||
|
transactionEntity.getUserEntity().getUserId(),
|
||||||
|
transactionEntity.getCategoryEntity().getCategoryId(),
|
||||||
|
transactionEntity.getAmount(),
|
||||||
|
transactionEntity.getTransactionDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<TransactionModel> convertFrom(List<TransactionEntity> transactionEntityList) {
|
||||||
|
return transactionEntityList.stream().map(TransactionModel::convertFrom).toList();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,19 @@
|
|||||||
package com.example.demo.app.domain.service;
|
package com.example.demo.app.domain.service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.example.demo.app.domain.model.TransactionModel;
|
||||||
|
|
||||||
public interface TransactionService {
|
public interface TransactionService {
|
||||||
|
|
||||||
|
public Map<LocalDate, List<TransactionModel>> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
|
public List<TransactionModel> findByUserIdAndDate(LocalDate todayDate, Long userId);
|
||||||
|
|
||||||
|
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
|
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,55 @@
|
|||||||
package com.example.demo.app.domain.service.impl;
|
package com.example.demo.app.domain.service.impl;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
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.service.TransactionService;
|
import com.example.demo.app.domain.service.TransactionService;
|
||||||
|
import com.example.demo.app.infra.entity.TransactionEntity;
|
||||||
|
import com.example.demo.app.infra.repository.TransactionRepository;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class TransactionServiceImpl implements TransactionService {
|
public class TransactionServiceImpl implements TransactionService {
|
||||||
|
|
||||||
|
private final TransactionRepository transactionRepository;
|
||||||
|
|
||||||
|
public Map<LocalDate, List<TransactionModel>> findAllByDate(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
|
List<TransactionEntity> transactionEntityList = transactionRepository.findAllByDateAndUserId(startDate, endDate,
|
||||||
|
userId);
|
||||||
|
|
||||||
|
List<TransactionModel> transactionModelList = TransactionModel
|
||||||
|
.convertFrom(transactionEntityList);
|
||||||
|
|
||||||
|
Map<LocalDate, List<TransactionModel>> transactionModelMap = transactionModelList.stream()
|
||||||
|
.collect(Collectors.groupingBy(TransactionModel::getTransactionDate));
|
||||||
|
|
||||||
|
return transactionModelMap;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
|
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
||||||
|
|
||||||
|
}
|
||||||
|
public int findTotalexpense(LocalDate startDate, LocalDate endDate, Long userId) {
|
||||||
|
|
||||||
|
return transactionRepository.findTotalIncome(startDate, endDate, userId);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TransactionModel> findByUserIdAndDate(LocalDate todayDate, Long userId) {
|
||||||
|
|
||||||
|
return TransactionModel.convertFrom(transactionRepository.findByUserIdAndDate(todayDate, userId));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class UserEntity {
|
|||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name = "user_id")
|
@Column(name = "user_id")
|
||||||
private Integer userId;
|
private Long userId;
|
||||||
|
|
||||||
@Column(name = "user_name", nullable = false)
|
@Column(name = "user_name", nullable = false)
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
package com.example.demo.app.infra.repository;
|
package com.example.demo.app.infra.repository;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
import com.example.demo.app.infra.entity.TransactionEntity;
|
import com.example.demo.app.infra.entity.TransactionEntity;
|
||||||
|
|
||||||
public interface TransactionRepository extends JpaRepository<TransactionEntity, Long> {
|
public interface TransactionRepository extends JpaRepository<TransactionEntity, Long> {
|
||||||
|
|
||||||
|
@Query("select t from TransactionEntity t where t.userEntity.userId = :userId and t.transactionDate = :transactionDate")
|
||||||
|
public List<TransactionEntity> findByUserIdAndDate(LocalDate todayDate, Long userId);
|
||||||
|
|
||||||
|
@Query("select t from TransactionEntity t where (t.transactionDate between :startDate and :endDate) and (t.userId = :userId) order by t.transactionDate asc")
|
||||||
|
public List<TransactionEntity> findAllByDateAndUserId(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
|
@Query("select sum(t.amount) from TransactionEntity t inner join CategoryEntity c on t.categoryId = c.categoryId where (t.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 0")
|
||||||
|
public Integer findTotalExpense(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
|
@Query("select sum(t.amount) from TransactionEntity t inner join CategoryEntity c on t.categoryId = c.categoryId where (t.userId = :userId and t.transactionDate between :startDate and :endDate) and c.transactionType = 1")
|
||||||
|
public Integer findTotalIncome(LocalDate startDate, LocalDate endDate, Long userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,22 @@
|
|||||||
package com.example.demo.app.presentation.controller;
|
package com.example.demo.app.presentation.controller;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import java.time.LocalDate;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
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.TransactionForm;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
@@ -11,7 +25,59 @@ import lombok.RequiredArgsConstructor;
|
|||||||
public class TransactionController {
|
public class TransactionController {
|
||||||
|
|
||||||
private final TransactionService transactionService;
|
private final TransactionService transactionService;
|
||||||
|
private final CategoryService categoryService;
|
||||||
|
|
||||||
|
// 一覧画面
|
||||||
|
|
||||||
|
@GetMapping("/transaction/list")
|
||||||
|
public String transactionList(@RequestParam LocalDate startDate, @RequestParam LocalDate endDate, Model model,
|
||||||
|
HttpSession session) {
|
||||||
|
|
||||||
|
LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
|
||||||
|
|
||||||
|
Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
transactionService.findAll(startDate, endDate, loginUser.getUserId).forEach((date, list) -> {
|
||||||
|
List<TransactionForm> transactionFormList = list.stream().map(transaction -> {
|
||||||
|
TransactionForm transactionForm = TransactionForm.convertFrom(
|
||||||
|
transaction,
|
||||||
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getCategoryName(),
|
||||||
|
transactionService.findTotalexpense(startDate, endDate, loginUser.getUserId),
|
||||||
|
transactionService.findTotalIncome(startDate, endDate, loginUser.getUserId),
|
||||||
|
CategoryForm.convertFrom(categoryService.findOne(transaction.getCategoryId())).getTransactionType()
|
||||||
|
);
|
||||||
|
return transactionForm;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
transactionFormMap.put(date, transactionFormList);
|
||||||
|
});
|
||||||
|
|
||||||
|
model.addAttribute("transactionFormMap", transactionFormMap);
|
||||||
|
|
||||||
|
return "transactionList";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 詳細画面
|
||||||
|
@GetMapping()
|
||||||
|
public String findOne(@RequestParam LocalDate todayDate, Model model, HttpSession session) {
|
||||||
|
|
||||||
|
LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
|
||||||
|
model.addAttribute("transactionFormList", transactionFormList);
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,17 +24,38 @@ public class TransactionForm {
|
|||||||
|
|
||||||
private LocalDate transactionDate;
|
private LocalDate transactionDate;
|
||||||
|
|
||||||
public static TransactionForm convertFrom(TransactionModel transactionModel) {
|
private String categoryName;
|
||||||
|
|
||||||
|
private Integer income;
|
||||||
|
|
||||||
|
private Integer expense;
|
||||||
|
|
||||||
|
private Integer 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(),
|
||||||
transactionModel.getUserId(),
|
transactionModel.getUserId(),
|
||||||
transactionModel.getCategoryId(),
|
transactionModel.getCategoryId(),
|
||||||
transactionModel.getAmount(),
|
transactionModel.getAmount(),
|
||||||
transactionModel.getTransactionDate());
|
transactionModel.getTransactionDate(),
|
||||||
|
categoryName,
|
||||||
|
income,
|
||||||
|
expense,
|
||||||
|
transactionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<TransactionForm> convertFrom(List<TransactionModel> transactionModelList) {
|
public static List<TransactionForm> convertFrom(List<TransactionModel> transactionModelList, String categoryName, int income, int expense, int transactionType ) {
|
||||||
return transactionModelList.stream().map(TransactionForm::convertFrom).toList();
|
return transactionModelList.stream().map(transactionModel -> TransactionForm.convertFrom(transactionModel, categoryName, income, expense, transactionType)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TransactionModel convertTo(TransactionForm transactionForm) {
|
||||||
|
return new TransactionModel(
|
||||||
|
transactionForm.getTransactionId(),
|
||||||
|
transactionForm.getUserId(),
|
||||||
|
transactionForm.getCategoryId(),
|
||||||
|
transactionForm.getAmount(),
|
||||||
|
transactionForm.getTransactionDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user