16 Commits

Author SHA1 Message Date
kkazuya
1369d839af Merge branch 's-choi' of https://gitea.it-college.co.jp/2026_09_5H_Spring_KIZAKI/Monegement into k-kobayashi 2026-09-17 11:16:21 +09:00
kkazuya
7f454b852a [ADD] AllocationRepositoryを作成しました。 2026-09-17 11:16:12 +09:00
s-choi
544ce423d0 [EDIT] userIdによる検索機能に日付も入力するように変更しました。 2026-09-17 11:12:26 +09:00
s-choi
6847f225c4 [EDIT] userIdによる収支検索、期間設定機能がある検索、収入・支出の検索機能のサービス段を作成しました。 2026-09-17 11:01:39 +09:00
s-choi
036a495bc7 [EDIT] userIdによる収支検索、期間設定機能がある検索、収入・支出の検索機能を追加しました。 2026-09-17 11:00:40 +09:00
s-choi
534fc2683d [EDIT] Formクラスへフィールドを追加しました。 2026-09-17 10:57:53 +09:00
s-choi
555d8ceb87 [EDIT] EntityからModelへの変換メソッドを作成しました。 2026-09-17 10:57:22 +09:00
s-choi
60159ea3fe [FIX] Userの主キーの型を修正しました。 2026-09-17 10:56:45 +09:00
kkazuya
f1b5e3eaf8 Merge branch 's-choi' of https://gitea.it-college.co.jp/2026_09_5H_Spring_KIZAKI/Monegement into k-kobayashi 2026-09-17 10:49:58 +09:00
s-choi
3e0c6092aa [EDIT] interfaceにメソッドの宣言を追加しました。 2026-09-17 10:48:42 +09:00
kkazuya
59cec2fed7 Merge branch 's-choi' of https://gitea.it-college.co.jp/2026_09_5H_Spring_KIZAKI/Monegement into k-kobayashi 2026-09-17 10:47:23 +09:00
s-choi
8aba950a5f [EDIT] 特定のカテゴリレコードを取得するサービスメソッドを作成しました。 2026-09-17 10:47:01 +09:00
s-choi
4774badffe Merge remote-tracking branch 'origin/k-kobayashi' into s-choi 2026-09-17 10:36:28 +09:00
kkazuya
743fe20e9a Merge branch 's-choi' of https://gitea.it-college.co.jp/2026_09_5H_Spring_KIZAKI/Monegement into k-kobayashi 2026-09-17 10:36:08 +09:00
s-choi
f3d599e696 [EDIT] EntityからModelへの変換メソッドを作成しました。 2026-09-17 10:35:49 +09:00
s-choi
493dd26367 [ADD] Categoryのビジネスロジック作成のためのサービスクラスを作成しました。 2026-09-17 10:35:22 +09:00
12 changed files with 260 additions and 14 deletions

View File

@@ -1,5 +1,9 @@
package com.example.demo.app.domain.model; package com.example.demo.app.domain.model;
import java.util.List;
import com.example.demo.app.infra.entity.CategoryEntity;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -11,4 +15,16 @@ public class CategoryModel {
private String categoryName; private String categoryName;
private Integer transactionType; private Integer transactionType;
public static CategoryModel convertFrom(CategoryEntity categoryEntity) {
return new CategoryModel(
categoryEntity.getCategoryId(),
categoryEntity.getCategoryName(),
categoryEntity.getTransactionType());
}
public static List<CategoryModel> convertFrom(List<CategoryEntity> categoryEntityList) {
return categoryEntityList.stream().map(CategoryModel::convertFrom).toList();
}
} }

View File

@@ -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();
}
} }

View File

@@ -0,0 +1,12 @@
package com.example.demo.app.domain.service;
import java.util.List;
import com.example.demo.app.domain.model.CategoryModel;
public interface CategoryService {
public List<CategoryModel> findAll();
public CategoryModel findOne(Long categoryId);
}

View File

@@ -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);
} }

View File

@@ -0,0 +1,39 @@
package com.example.demo.app.domain.service.impl;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.CategoryModel;
import com.example.demo.app.domain.service.CategoryService;
import com.example.demo.app.infra.entity.CategoryEntity;
import com.example.demo.app.infra.repository.CategoryRepository;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService{
private final CategoryRepository categoryRepository;
public List<CategoryModel> findAll() {
return CategoryModel.convertFrom(categoryRepository.findAll());
}
public CategoryModel findOne(Long categoryId) {
Optional<CategoryEntity> optionalCategoryModel = categoryRepository.findById(categoryId);
if (optionalCategoryModel.isEmpty()) {
return null;
}
return CategoryModel.convertFrom(optionalCategoryModel.get());
}
}

View File

@@ -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));
}
} }

View File

@@ -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;

View File

@@ -6,4 +6,5 @@ import com.example.demo.app.infra.entity.AllocationEntity;
public interface AllocationRepository extends JpaRepository<AllocationEntity, Long>{ public interface AllocationRepository extends JpaRepository<AllocationEntity, Long>{
} }

View File

@@ -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);
} }

View File

@@ -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 "";
}
} }

View File

@@ -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());
} }
} }