[UPDATE] 2次機能を実装しました。

This commit is contained in:
s-choi
2026-09-25 12:41:52 +09:00
parent 95bfd4ed0e
commit 29d93d5453
23 changed files with 990 additions and 132 deletions

View File

@@ -1,5 +1,12 @@
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.AllocationEntity;
import com.example.demo.app.infra.entity.CategoryEntity;
import com.example.demo.app.infra.entity.CourseEntity;
import com.example.demo.app.presentation.form.AllocationForm;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -11,4 +18,25 @@ public class AllocationModel {
private Long courseId; private Long courseId;
private Integer allocationRatio; private Integer allocationRatio;
public static AllocationModel convertFrom(AllocationEntity allocationEntity) {
return new AllocationModel(allocationEntity.getCategoryEntity().getCategoryId(), allocationEntity.getCourseEntity().getCourseId(), allocationEntity.getAllocationRatio());
}
public static List<AllocationModel> convertFrom(List<AllocationEntity> allocationEntityList){
return allocationEntityList.stream().map(AllocationModel::convertFrom).toList();
}
public static AllocationModel convertFrom1(AllocationForm allocationForm) {
return new AllocationModel(allocationForm.getCategoryId(), allocationForm.getCourseId(), allocationForm.getAllocationRatio());
}
public static List<AllocationModel> convertFrom1(List<AllocationForm> allocationFormList){
return allocationFormList.stream().map(AllocationModel::convertFrom1).toList();
}
public static AllocationEntity convertTo(CategoryEntity categoryEntity, CourseEntity courseEntity, Integer allocationRatio) {
return new AllocationEntity(categoryEntity, courseEntity, allocationRatio);
}
} }

View File

@@ -3,6 +3,7 @@ package com.example.demo.app.domain.model;
import java.util.List; import java.util.List;
import com.example.demo.app.infra.entity.CourseEntity; import com.example.demo.app.infra.entity.CourseEntity;
import com.example.demo.app.infra.entity.UserEntity;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -25,4 +26,12 @@ public class CourseModel {
public static List<CourseModel> convertFrom(List<CourseEntity> courseEntityList) { public static List<CourseModel> convertFrom(List<CourseEntity> courseEntityList) {
return courseEntityList.stream().map(CourseModel::convertFrom).toList(); return courseEntityList.stream().map(CourseModel::convertFrom).toList();
} }
public static CourseEntity convertTo(CourseModel courseModel, List<UserEntity> userEntityList) {
return new CourseEntity(
courseModel.getCourseId(),
courseModel.getCourseName(),
courseModel.getDescription(),
userEntityList);
}
} }

View File

@@ -1,6 +1,8 @@
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.ArrayList;
import java.util.List;
import com.example.demo.app.infra.entity.GoalEntity; import com.example.demo.app.infra.entity.GoalEntity;
import com.example.demo.app.infra.entity.UserEntity; import com.example.demo.app.infra.entity.UserEntity;
@@ -31,6 +33,8 @@ public class GoalModel {
private Long userId; private Long userId;
private List<Integer> savingList;
public static GoalModel convertFrom(GoalEntity goalEntity, int achievementRate, int currentAmount) { public static GoalModel convertFrom(GoalEntity goalEntity, int achievementRate, int currentAmount) {
return new GoalModel( return new GoalModel(
goalEntity.getGoalId(), goalEntity.getGoalId(),
@@ -41,7 +45,8 @@ public class GoalModel {
goalEntity.getGoalComplete(), goalEntity.getGoalComplete(),
achievementRate, achievementRate,
currentAmount, currentAmount,
goalEntity.getUserEntity().getUserId()); goalEntity.getUserEntity().getUserId(),
new ArrayList<>());
} }
public static GoalEntity convertTo(GoalModel goalModel, UserEntity userEntity) { public static GoalEntity convertTo(GoalModel goalModel, UserEntity userEntity) {
@@ -64,7 +69,8 @@ public class GoalModel {
goalForm.getDeadline(), goalForm.getDeadline(),
goalForm.getGoalComplete() goalForm.getGoalComplete()
,null,null, ,null,null,
goalForm.getUserId()); goalForm.getUserId(),
new ArrayList<>());
} }
} }

View File

@@ -8,4 +8,6 @@ public interface AllocationService {
public List<AllocationModel> findAll(); public List<AllocationModel> findAll();
public List<AllocationModel> save(List<AllocationModel> allocationModelList);
} }

View File

@@ -11,4 +11,6 @@ public interface CourseService {
public CourseModel findById(Long courseId); public CourseModel findById(Long courseId);
public Long findByName(String courName); public Long findByName(String courName);
public CourseModel save(CourseModel courseModel, Long userId);
} }

View File

@@ -16,6 +16,8 @@ public interface GoalService {
public void delete(Long goalId); public void delete(Long goalId);
public void updateGoalComplete(Long userId);
// void deleteGoal(Long goalId); // void deleteGoal(Long goalId);
} }

View File

@@ -1,5 +1,6 @@
package com.example.demo.app.domain.service.impl; package com.example.demo.app.domain.service.impl;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -8,6 +9,8 @@ import com.example.demo.app.domain.model.AllocationModel;
import com.example.demo.app.domain.service.AllocationService; import com.example.demo.app.domain.service.AllocationService;
import com.example.demo.app.infra.entity.AllocationEntity; import com.example.demo.app.infra.entity.AllocationEntity;
import com.example.demo.app.infra.repository.AllocationRepository; import com.example.demo.app.infra.repository.AllocationRepository;
import com.example.demo.app.infra.repository.CategoryRepository;
import com.example.demo.app.infra.repository.CourseRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -15,10 +18,34 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public class AllocationServiceImpl implements AllocationService { public class AllocationServiceImpl implements AllocationService {
AllocationRepository allocationRepository; private final AllocationRepository allocationRepository;
private final CategoryRepository categoryRepository;
private final CourseRepository courseRepository;
public List<AllocationModel> findAll(){ public List<AllocationModel> findAll() {
return AllocationEntity.createAllocationModelList(allocationRepository.findAll()); return AllocationEntity.createAllocationModelList(allocationRepository.findAll());
} }
public List<AllocationModel> save(List<AllocationModel> allocationModelList) {
List<AllocationEntity> allocationEntityList = new ArrayList<>();
allocationModelList.forEach(allocationModel -> {
allocationEntityList.add(
AllocationModel.convertTo(
categoryRepository.findById(allocationModel.getCategoryId()).get(),
courseRepository.findById(allocationModel.getCourseId()).get(),
allocationModel.getAllocationRatio()));
});
List<AllocationModel> result = new ArrayList<>();
for (AllocationEntity allocationEntity : allocationEntityList) {
result.add(AllocationModel.convertFrom(allocationRepository.save(allocationEntity)));
}
return result;
}
} }

View File

@@ -1,5 +1,6 @@
package com.example.demo.app.domain.service.impl; package com.example.demo.app.domain.service.impl;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -7,7 +8,9 @@ import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.CourseModel; import com.example.demo.app.domain.model.CourseModel;
import com.example.demo.app.domain.service.CourseService; import com.example.demo.app.domain.service.CourseService;
import com.example.demo.app.infra.entity.CourseEntity; import com.example.demo.app.infra.entity.CourseEntity;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.CourseRepository; import com.example.demo.app.infra.repository.CourseRepository;
import com.example.demo.app.infra.repository.UserRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -16,6 +19,7 @@ import lombok.RequiredArgsConstructor;
public class CourseServiceImpl implements CourseService { public class CourseServiceImpl implements CourseService {
private final CourseRepository courseRepository; private final CourseRepository courseRepository;
private final UserRepository userRepository;
public List<CourseModel> findAll(){ public List<CourseModel> findAll(){
@@ -29,4 +33,13 @@ public class CourseServiceImpl implements CourseService {
public Long findByName(String courseName) { public Long findByName(String courseName) {
return courseRepository.findByCourseName(courseName).getCourseId(); return courseRepository.findByCourseName(courseName).getCourseId();
} }
public CourseModel save(CourseModel courseModel, Long userId) {
List<UserEntity> userEntityList = new ArrayList<>();
userEntityList.add(userRepository.findById(userId).get());
return CourseModel.convertFrom( courseRepository.save( CourseModel.convertTo(courseModel, userEntityList)));
}
} }

View File

@@ -3,6 +3,8 @@ package com.example.demo.app.domain.service.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
@@ -10,6 +12,7 @@ import com.example.demo.app.domain.service.GoalService;
import com.example.demo.app.infra.entity.GoalEntity; import com.example.demo.app.infra.entity.GoalEntity;
import com.example.demo.app.infra.entity.UserEntity; import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.GoalRepository; import com.example.demo.app.infra.repository.GoalRepository;
import com.example.demo.app.infra.repository.TransactionRepository;
import com.example.demo.app.infra.repository.UserRepository; import com.example.demo.app.infra.repository.UserRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -20,6 +23,7 @@ public class GoalServiceImpl implements GoalService {
private final GoalRepository goalRepository; private final GoalRepository goalRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
private final TransactionRepository transactionRepository;
@Override @Override
public GoalModel getGoalById(Long goalId) { public GoalModel getGoalById(Long goalId) {
@@ -83,4 +87,25 @@ public class GoalServiceImpl implements GoalService {
goalRepository.deleteById(goalId); goalRepository.deleteById(goalId);
} }
@Transactional
public void updateGoalComplete(Long userId) {
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
List<GoalEntity> goalEntityList = goalRepository.findAllByUserId(userId);
int transactionTotal = userEntity.getTransactionEntityList().stream()
.mapToInt(t -> {
int sign = t.getCategoryEntity().getTransactionType() == 1 ? 1 : -1;
return sign * t.getAmount();
})
.sum();
int currentAmount = userEntity.getSaving() + transactionTotal;
goalEntityList.stream().forEach(goalEntity -> {
if (goalEntity.getTargetAmount() <= currentAmount) {
goalRepository.updateCompleteByGoalId(goalEntity.getGoalId());
}
});
}
} }

View File

@@ -3,6 +3,7 @@ package com.example.demo.app.infra.repository;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@@ -14,4 +15,8 @@ public interface GoalRepository extends JpaRepository<GoalEntity, Long> {
@Query("select g from GoalEntity g where g.userEntity.userId = :userId") @Query("select g from GoalEntity g where g.userEntity.userId = :userId")
public List<GoalEntity> findAllByUserId(Long userId); public List<GoalEntity> findAllByUserId(Long userId);
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("update GoalEntity g set g.goalComplete = 1 where g.goalId = :goalId")
public int updateCompleteByGoalId(Long goalId);
} }

View File

@@ -1,5 +1,8 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@@ -10,30 +13,31 @@ 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.aop.aspect.SessionCheck; import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.AllocationModel;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.AllocationService;
import com.example.demo.app.domain.service.CourseInfoService; import com.example.demo.app.domain.service.CourseInfoService;
import com.example.demo.app.domain.service.CourseService; import com.example.demo.app.domain.service.CourseService;
import com.example.demo.app.domain.service.UserService; import com.example.demo.app.domain.service.UserService;
import com.example.demo.app.presentation.form.AllocationFormList;
import com.example.demo.app.presentation.form.CourseForm; import com.example.demo.app.presentation.form.CourseForm;
import com.example.demo.app.presentation.form.CourseInfoForm; import com.example.demo.app.presentation.form.CourseInfoForm;
import com.example.demo.app.presentation.form.UserForm; import com.example.demo.app.presentation.form.UserForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequiredArgsConstructor @RequiredArgsConstructor
public class CourseController { public class CourseController {
private final CourseService courseService; private final CourseService courseService;
private final CourseInfoService courseInfoService; private final CourseInfoService courseInfoService;
private final UserService userService; private final UserService userService;
private final AllocationService allocationService;
@PostMapping("/user/register/course")
public String showSelectCourse(@ModelAttribute UserForm userForm, Model model, HttpSession session) {
@PostMapping("/user/register/course")
public String showSelectCourse(@ModelAttribute UserForm userForm, Model model, HttpSession session) {
session.setAttribute("userForm", userForm); session.setAttribute("userForm", userForm);
model.addAttribute("mode", "regist"); model.addAttribute("mode", "regist");
@@ -41,15 +45,13 @@ public class CourseController {
model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll())); model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll()));
model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll())); model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll()));
return "select_course"; return "select_course";
}
} @SessionCheck
@GetMapping("/course")
@SessionCheck public String showChangeCourse(Model model, HttpSession session) {
@GetMapping("/course")
public String showChangeCourse(Model model, HttpSession session) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId(); Long userId = loginUser.getUserId();
@@ -60,22 +62,64 @@ public class CourseController {
model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll())); model.addAttribute("courseList", CourseForm.convertFrom(courseService.findAll()));
model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll())); model.addAttribute("courseInfoList", CourseInfoForm.convertFrom(courseInfoService.findAll()));
return "select_course"; return "select_course";
}
} @SessionCheck
@PostMapping("/change")
public String changeCourse(@RequestParam Long courseId, HttpSession httpSession) {
@SessionCheck LoginModel loginUser = (LoginModel) httpSession.getAttribute("loginUser");
@PostMapping("/change")
public String changeCourse(@RequestParam Long courseId, HttpSession httpSession) {
LoginModel loginUser = (LoginModel) httpSession.getAttribute("loginUser"); userService.updateCourse(loginUser.getUserId(), courseId);
userService.updateCourse(loginUser.getUserId(), courseId); return "redirect:/menu";
}
return "redirect:/menu"; @SessionCheck
} @GetMapping("/course/create")
public String createCourseGet(@ModelAttribute CourseForm courseForm, HttpSession session) {
return "course_create";
}
@SessionCheck
@PostMapping("/course/create/ratio")
public String createCourse(@ModelAttribute CourseForm courseForm, @ModelAttribute AllocationFormList allocationFormList,
HttpSession session, Model model) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
CourseForm form = CourseForm.convertFrom(courseService.save(CourseForm.convertTo(courseForm), loginUser.getUserId()));
session.setAttribute("courseId", form.getCourseId());
return "course_create_ratio";
}
@SessionCheck
@PostMapping("/course/create")
public String createCoursePost(@ModelAttribute AllocationFormList allocationFormList, HttpSession session) {
List<AllocationModel> allocationModelList = new ArrayList<>();
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId1(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio1()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId2(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio2()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId3(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio3()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId4(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio4()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId5(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio5()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId6(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio6()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId7(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio7()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId8(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio8()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId9(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio9()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId10(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio10()));
allocationModelList.add(new AllocationModel(allocationFormList.getCategoryId11(), (Long) session.getAttribute("courseId"), allocationFormList.getAllocationRatio11()));
allocationService.save(allocationModelList);
return "redirect:/course";
}
} }

View File

@@ -1,6 +1,13 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
@@ -14,8 +21,15 @@ import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.aop.aspect.SessionCheck; import com.example.demo.aop.aspect.SessionCheck;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.CategoryService;
import com.example.demo.app.domain.service.GoalService; import com.example.demo.app.domain.service.GoalService;
import com.example.demo.app.domain.service.TransactionService;
import com.example.demo.app.domain.service.UserService;
import com.example.demo.app.presentation.form.CategoryForm;
import com.example.demo.app.presentation.form.GoalForm; import com.example.demo.app.presentation.form.GoalForm;
import com.example.demo.app.presentation.form.MonthlyAchievementForm;
import com.example.demo.app.presentation.form.MonthlySavingForm;
import com.example.demo.app.presentation.form.TransactionForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -25,19 +39,183 @@ import lombok.RequiredArgsConstructor;
public class GoalController { public class GoalController {
private final GoalService goalService; private final GoalService goalService;
private final TransactionService transactionService;
private final UserService userService;
private final CategoryService categoryService;
@SessionCheck @SessionCheck
@GetMapping("") @GetMapping("")
public String getGoal(Model model, HttpSession session) { public String getGoal(
Model model,
HttpSession session,
Long goalId) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId(); Long userId = loginUser.getUserId();
goalService.updateGoalComplete(userId);
List<GoalModel> goalModelList = goalService.getGoalByUserId(userId); List<GoalModel> goalModelList = goalService.getGoalByUserId(userId);
List<GoalForm> goalFormList = GoalForm.convertFrom(goalModelList);
model.addAttribute("goalFormList", goalFormList); List<GoalForm> goalFormList1 = GoalForm.convertFrom(goalModelList);
List<GoalForm> goalFormList = goalFormList1.stream()
.filter(goalForm -> goalForm.getGoalComplete() == 0)
.collect(Collectors.toList());
model.addAttribute(
"goalFormList",
goalFormList);
if (goalFormList.isEmpty()) {
return "goal_view";
}
GoalForm graphGoal;
if (goalId == null) {
graphGoal = goalFormList.getFirst();
} else {
graphGoal = goalFormList.stream()
.filter(goal -> goal.getGoalId().equals(goalId))
.findFirst()
.orElse(
goalFormList.getFirst());
}
model.addAttribute(
"graphGoal",
graphGoal);
int currentAmount = graphGoal.getCurrentAmount();
int targetAmount = graphGoal.getTargetAmount();
LocalDate startDate = LocalDate.of(
LocalDate.now().getYear(),
1,
1);
LocalDate today = LocalDate.now();
Map<LocalDate, List<TransactionForm>> transactionFormMap = new HashMap<>();
transactionService
.findAllByDate(
startDate,
today,
userId)
.forEach((date, list) -> {
List<TransactionForm> transactionFormList = list.stream()
.map(transaction -> {
CategoryForm categoryForm = CategoryForm.convertFrom(
categoryService.findOne(
transaction.getCategoryId()));
return TransactionForm.convertFrom(
transaction,
categoryForm.getCategoryName(),
transactionService.findTotalExpense(
startDate,
today,
loginUser.getUserId()),
transactionService.findTotalIncome(
startDate,
today,
loginUser.getUserId()),
categoryForm.getTransactionType());
})
.collect(Collectors.toList());
transactionFormMap.put(
date,
transactionFormList);
});
List<MonthlySavingForm> monthlySavingList = new ArrayList<>();
YearMonth startMonth = YearMonth.from(startDate);
YearMonth currentMonth = YearMonth.now();
YearMonth month = currentMonth;
while (!month.isBefore(startMonth)) {
int income = 0;
int expense = 0;
for (Map.Entry<LocalDate, List<TransactionForm>> entry : transactionFormMap.entrySet()) {
YearMonth transactionMonth = YearMonth.from(entry.getKey());
if (!transactionMonth.equals(month)) {
continue;
}
for (TransactionForm transaction : entry.getValue()) {
if (transaction.getAmount() == null) {
continue;
}
if (transaction.getTransactionType() == 1) {
income += transaction.getAmount();
} else if (transaction.getTransactionType() == 0) {
expense += transaction.getAmount();
}
}
}
monthlySavingList.add(
new MonthlySavingForm(
month,
income,
expense,
currentAmount));
currentAmount = currentAmount
- income
+ expense;
month = month.minusMonths(1);
}
Collections.reverse(monthlySavingList);
List<MonthlyAchievementForm> achievementList = new ArrayList<>();
for (MonthlySavingForm monthlySaving : monthlySavingList) {
int savingAmount = monthlySaving.getSaving();
int achievementRate = 0;
if (targetAmount > 0) {
achievementRate = (int) ((double) savingAmount
/ targetAmount
* 100);
}
achievementList.add(
new MonthlyAchievementForm(
monthlySaving.getMonth(),
savingAmount,
achievementRate));
}
model.addAttribute(
"achievementList",
achievementList);
return "goal_view"; return "goal_view";
} }
@@ -62,12 +240,12 @@ public class GoalController {
Long userId = loginUser.getUserId(); Long userId = loginUser.getUserId();
// // 本人の目標かどうかを確認してから更新する // // 本人の目標かどうかを確認してから更新する
// GoalModel currentGoal = goalService.getGoalByUserId(userId); // GoalModel currentGoal = goalService.getGoalByUserId(userId);
// //
// if (!currentGoal.getGoalId().equals(goalForm.getGoalId())) { // if (!currentGoal.getGoalId().equals(goalForm.getGoalId())) {
// return "redirect:/goal"; // 他人のデータを更新しようとしたら弾く // return "redirect:/goal"; // 他人のデータを更新しようとしたら弾く
// } // }
GoalModel goalModel = GoalModel.convertFrom(goalForm); GoalModel goalModel = GoalModel.convertFrom(goalForm);
goalService.updateGoal(goalModel, userId); goalService.updateGoal(goalModel, userId);
@@ -75,7 +253,6 @@ public class GoalController {
return "redirect:/goal"; return "redirect:/goal";
} }
@SessionCheck @SessionCheck
@GetMapping("/create") @GetMapping("/create")
public String createGoalGet(@ModelAttribute GoalForm goalForm, HttpSession session) { public String createGoalGet(@ModelAttribute GoalForm goalForm, HttpSession session) {
@@ -97,14 +274,44 @@ public class GoalController {
} }
@SessionCheck @SessionCheck
@PostMapping("/delete") // フラグ考慮してないため、今度修正必要 @PostMapping("/delete") // フラグ考慮してないため、今度修正必要
public String deleteGoal(Long goalId) { public String deleteGoal(Long goalId, Long record) {
goalService.delete(goalId); goalService.delete(goalId);
return "redirect:/goal"; if (record == null) {
return "redirect:/goal";
} else {
return "redirect:/goal/record";
}
} }
@SessionCheck
@GetMapping("/record")
public String getRecord(HttpSession session, Model model) {
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
Long userId = loginUser.getUserId();
goalService.updateGoalComplete(userId);
List<GoalModel> goalModelList = goalService.getGoalByUserId(userId);
List<GoalForm> goalFormList = GoalForm.convertFrom(goalModelList);
List<GoalForm> goalCompleteFormList = new ArrayList<>();
goalFormList.stream().forEach(goalForm -> {
if (goalForm.getGoalComplete() == 1) {
goalCompleteFormList.add(goalForm);
}
});
model.addAttribute(
"goalCompleteFormList",
goalCompleteFormList);
return "goal_record";
}
} }

View File

@@ -0,0 +1,45 @@
package com.example.demo.app.presentation.form;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AllocationFormList {
private Long categoryId1 = 1L;
private Integer allocationRatio1;
private Long categoryId2 = 2L;
private Integer allocationRatio2;
private Long categoryId3 = 3L;
private Integer allocationRatio3;
private Long categoryId4 = 4L;
private Integer allocationRatio4;
private Long categoryId5 = 5L;
private Integer allocationRatio5;
private Long categoryId6 = 6L;
private Integer allocationRatio6;
private Long categoryId7 = 7L;
private Integer allocationRatio7;
private Long categoryId8 = 8L;
private Integer allocationRatio8;
private Long categoryId9 = 9L;
private Integer allocationRatio9;
private Long categoryId10 = 10L;
private Integer allocationRatio10;
private Long categoryId11 = 11L;
private Integer allocationRatio11;
}

View File

@@ -30,4 +30,12 @@ public class CourseForm {
return courseModelList.stream().map(CourseForm::convertFrom).toList(); return courseModelList.stream().map(CourseForm::convertFrom).toList();
} }
public static CourseModel convertTo(CourseForm courseForm) {
return new CourseModel(
courseForm.getCourseId(),
courseForm.getCourseName(),
courseForm.getDescription()
);
}
} }

View File

@@ -0,0 +1,17 @@
package com.example.demo.app.presentation.form;
import java.time.YearMonth;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class MonthlyAchievementForm {
private YearMonth month;
private Integer saving;
private Integer achievementRate;
}

View File

@@ -0,0 +1,19 @@
package com.example.demo.app.presentation.form;
import java.time.YearMonth;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class MonthlySavingForm {
private YearMonth month;
private Integer income;
private Integer expense;
private Integer saving;
}

View File

@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/menu.css}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<h1>コース登録</h1>
<form th:action="@{/course/create/ratio}" th:object="${courseForm}" method="post">
<div>
<label for="courseName">コース名</label>
<input type="text" id="courseName" th:field="*{courseName}">
</div>
<br>
<div>
<label for="description">説明</label>
<textarea id="description" th:field="*{description}" placeholder="説明を入力してください">
</textarea>
</div>
<br>
<button type="submit">登録</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/menu.css}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<h1>コース登録</h1>
<form th:action="@{/course/create}" th:object="${allocationFormList}" method="post">
<div>
<label for="allocationRatio1">給与</label>
<input type="number" id="allocationRatio1" th:field="*{allocationRatio1}">
</div>
<br>
<div>
<label for="allocationRatio2">賞与</label>
<input type="number" id="allocationRatio2" th:field="*{allocationRatio2}">
</div>
<br>
<div>
<label for="allocationRatio3">その他</label>
<input type="number" id="allocationRatio3" th:field="*{allocationRatio3}">
</div>
<br>
<div>
<label for="allocationRatio4">固定費</label>
<input type="number" id="allocationRatio4" th:field="*{allocationRatio4}">
</div>
<br>
<div>
<label for="allocationRatio5">食費</label>
<input type="number" id="allocationRatio5" th:field="*{allocationRatio5}">
</div>
<br>
<div>
<label for="allocationRatio6">交通費</label>
<input type="number" id="allocationRatio6" th:field="*{allocationRatio6}">
</div>
<br>
<div>
<label for="allocationRatio7">日用品費</label>
<input type="number" id="allocationRatio7" th:field="*{allocationRatio7}">
</div>
<br>
<div>
<label for="allocationRatio8">被服/美容費</label>
<input type="number" id="allocationRatio8" th:field="*{allocationRatio8}">
</div>
<br>
<div>
<label for="allocationRatio9">医療費</label>
<input type="number" id="allocationRatio9" th:field="*{allocationRatio9}">
</div>
<br>
<div>
<label for="allocationRatio10">趣味/娯楽費</label>
<input type="number" id="allocationRatio10" th:field="*{allocationRatio10}">
</div>
<br>
<div>
<label for="allocationRatio11">その他</label>
<input type="number" id="allocationRatio11" th:field="*{allocationRatio11}">
</div>
<br>
<button type="submit">登録</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>目標履歴</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<h1>目標履歴</h1>
<div>
<ul>
<li th:each="goalForm : ${goalCompleteFormList}" class="transaction-item">
<div>
<p>
目標名:
<span th:text="${goalForm.goalTitle}">
</span>
<br>
目標説明:
<span th:text="${goalForm.goalSubtitle}">
</span>
<br>
目標金額:
<span th:text="${goalForm.targetAmount}">
</span>
<br>
目標達成期限:
<span th:text="${goalForm.deadline}">
</span>
<br>
</p>
</form>
</div>
<div class="transaction-buttons">
<form th:action="@{/goal/delete}" method="post">
<input type="hidden" name="goalId" th:value="${goalForm.goalId}">
<input type="hidden" name="record" th:value="1">
<button type="submit">
削除
</button>
</form>
</div>
</li>
</ul>
</div>
<form th:action="@{/goal}" method="get">
<button type="submit">
戻る
</button>
</form>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>

View File

@@ -6,62 +6,200 @@
<title>目標詳細</title> <title>目標詳細</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.0/chart.umd.min.js"></script>
</head> </head>
<body class="pb-5 pt-5"> <body class="pb-5 pt-5">
<div th:replace="~{header :: header}"></div>
<div th:replace="~{header :: header}"></div>
<h1>目標詳細</h1> <h1>目標詳細</h1>
<div th:if="${graphGoal != null}">
<h2 th:text="${graphGoal.goalTitle}"></h2>
<p>
目標金額:
<span th:text="${graphGoal.targetAmount}"></span>
</p>
</div>
<div style="width: 600px; height: 300px;">
<canvas id="myChart"></canvas>
</div>
<script th:inline="javascript">
const months =
/*[[${achievementList.![month.monthValue]}]]*/[];
const achievementRates =
/*[[${achievementList.![achievementRate]}]]*/[];
const labels =
months.map(month => month + '月');
const myCt =
document.getElementById('myChart');
const myChart =
new Chart(myCt, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: '達成率',
data: achievementRates,
backgroundColor:
'rgba(54, 162, 235, 0.5)',
borderColor:
'rgba(54, 162, 235, 1)',
borderWidth: 1
}
]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
max: 100,
ticks: {
callback: function (value) {
return value + '%';
}
}
}
}
}
});
</script>
<div> <div>
<ul> <ul>
<li th:each="goalForm : ${goalFormList}" class="transaction-item"> <li th:each="goalForm : ${goalFormList}" class="transaction-item">
<div> <div>
<p>
目標名: <form th:action="@{/goal}" method="get">
<span th:text="${goalForm.goalTitle}"></span>
<br> <input type="radio" name="goalId" th:value="${goalForm.goalId}"
目標説明: th:checked="${graphGoal != null and graphGoal.goalId == goalForm.goalId}"
<span th:text="${goalForm.goalSubtitle}"></span> onchange="this.form.submit()">
<br>
目標金額: <p>
<span th:text="${goalForm.targetAmount}"></span>
<br> 目標名:
目標達成期限: <span th:text="${goalForm.goalTitle}">
<span th:text="${goalForm.deadline}"></span> </span>
<br>
達成率: <br>
<span th:text="${goalForm.achievementRate} + '%'"></span>
<br> 目標説明:
</p> <span th:text="${goalForm.goalSubtitle}">
</span>
<br>
目標金額:
<span th:text="${goalForm.targetAmount}">
</span>
<br>
目標達成期限:
<span th:text="${goalForm.deadline}">
</span>
<br>
達成率:
<span th:text="${goalForm.achievementRate} + '%'">
</span>
</p>
</form>
</div> </div>
<div class="transaction-buttons"> <div class="transaction-buttons">
<form th:action="@{/goal/edit}" method="get"> <form th:action="@{/goal/edit}" method="get">
<input type="hidden" name="goalId" th:value="${goalForm.goalId}"> <input type="hidden" name="goalId" th:value="${goalForm.goalId}">
<button type="submit">編集</button>
<button type="submit">
編集
</button>
</form> </form>
<form th:action="@{/goal/delete}" method="post"> <form th:action="@{/goal/delete}" method="post">
<input type="hidden" name="goalId" th:value="${goalForm.goalId}"> <input type="hidden" name="goalId" th:value="${goalForm.goalId}">
<button type="submit">削除</button>
<button type="submit">
削除
</button>
</form> </form>
</div> </div>
</li> </li>
</ul> </ul>
</div> </div>
<form th:action="@{/goal/create}" method="get"> <form th:action="@{/goal/create}" method="get">
<button type="submit">新規登録</button>
<button type="submit">
新規登録
</button>
</form> </form>
<form th:action="@{/goal/record}" method="get">
<button type="submit">
履歴
</button>
</form>
<div th:replace="~{footer :: footer}"></div> <div th:replace="~{footer :: footer}"></div>
</body> </body>
</html> </html>

View File

@@ -1,43 +1,57 @@
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<title>コース選択</title> <title>コース選択</title>
</head> </head>
<body> <body>
<div th:replace="~{header :: header}"></div> <div th:replace="~{header :: header}"></div>
<h1>コース選択</h1> <h1>コース選択</h1>
<th:block th:if="${mode == 'change'}">
<form th:action="@{/course/create}" method="get">
<button type="submit">新規登録</button>
</form>
</th:block>
<form th:action="${mode == 'regist'} ? @{/user/register/return}: @{/change}" method="post"> <form th:action="${mode == 'regist'} ? @{/user/register/return}: @{/change}" method="post">
<button type="submit">確定</button> <button type="submit">確定</button>
<div class="accordion" id="accordionExample"> <div class="accordion" id="accordionExample">
<div class="accordion-item" th:each="course:${courseList}"> <div class="accordion-item" th:each="course:${courseList}">
<h2 class="accordion-header d-flex align-items-center"> <h2 class="accordion-header d-flex align-items-center">
<input type="radio" name="courseId" th:value="${course.courseId}" th:checked="${course.courseId==currentCourseId}" class="form-check-input ms-3 me-2"> <input type="radio" name="courseId" th:value="${course.courseId}"
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" th:data-bs-target="'#collapse' + ${course.courseId}" aria-expanded="false" aria-controls="'collapse' + ${course.courseId}"> th:checked="${course.courseId==currentCourseId}" class="form-check-input ms-3 me-2">
<span th:text="${course.courseName}"></span> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
</button> th:data-bs-target="'#collapse' + ${course.courseId}" aria-expanded="false"
</h2> aria-controls="'collapse' + ${course.courseId}">
<div th:id="'collapse' + ${course.courseId}" class="accordion-collapse collapse" data-bs-parent="#accordionExample"> <span th:text="${course.courseName}"></span>
<div class="accordion-body"> </button>
<span th:text="${course.description}"></span> </h2>
<table border="1"> <div th:id="'collapse' + ${course.courseId}" class="accordion-collapse collapse"
<tr> data-bs-parent="#accordionExample">
<th>カテゴリ名</th><th>配分(%)</th> <div class="accordion-body">
</tr> <span th:text="${course.description}"></span>
<tr th:each="courseInfo:${courseInfoList}" th:if="${course.courseId == courseInfo.courseId}"> <table border="1">
<td th:text="${courseInfo.categoryName}"></td> <tr>
<td th:text="${courseInfo.allocationRatio}"></td> <th>カテゴリ名</th>
</tr> <th>配分(%)</th>
</table> </tr>
</div> <tr th:each="courseInfo:${courseInfoList}"
</div> th:if="${course.courseId == courseInfo.courseId}">
</div> <td th:text="${courseInfo.categoryName}"></td>
<td th:text="${courseInfo.allocationRatio}"></td>
</tr>
</table>
</div>
</div>
</div>
</div> </div>
</form> </form>
@@ -47,4 +61,5 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>
</body> </body>
</html> </html>