diff --git a/src/main/java/com/example/demo/app/presentation/controller/FixedCostController.java b/src/main/java/com/example/demo/app/presentation/controller/FixedCostController.java new file mode 100644 index 0000000..5a0809b --- /dev/null +++ b/src/main/java/com/example/demo/app/presentation/controller/FixedCostController.java @@ -0,0 +1,120 @@ +package com.example.demo.app.presentation.controller; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +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.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +import com.example.demo.aop.aspect.SessionCheck; +import com.example.demo.app.domain.model.LoginModel; +import com.example.demo.app.domain.service.CategoryService; +import com.example.demo.app.domain.service.FixedCostService; +import com.example.demo.app.presentation.form.CategoryForm; +import com.example.demo.app.presentation.form.FixedCostForm; + +import lombok.RequiredArgsConstructor; + +@Controller +@RequiredArgsConstructor +public class FixedCostController { + + private final FixedCostService fixedCostService; + private final CategoryService categoryService; + + @SessionCheck + @GetMapping("/fixed-cost/list") + public String getFixedCostList( + HttpSession session, Model model, LocalDate today) { + + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + + if (today == null) { + today = (LocalDate) session.getAttribute("today"); + } + + List fixedCostFormList = FixedCostForm.convertFrom(fixedCostService.findAllByUserId(loginUser.getUserId())); + + fixedCostFormList.forEach(fixedCostForm -> fixedCostForm.setCategoryName(categoryService.findOne(fixedCostForm.getCategoryId()).getCategoryName())); + + model.addAttribute("fixedCostFormList", fixedCostFormList); + + model.addAttribute("today", today); + + return "fixed_cost"; + } + + + @SessionCheck + @GetMapping("/fixed-cost/create") + public String getFixedCostCreate( + HttpSession session, @ModelAttribute FixedCostForm fixedCostForm, Model model, LocalDate today) { + + List categoryFormList = CategoryForm.convertFrom(categoryService.findAll()); + + List expenseList = new ArrayList(); + + List incomeList = new ArrayList(); + + for (CategoryForm categoryForm : categoryFormList) { + + if (categoryForm.getTransactionType() == 0) { + expenseList.add(categoryForm); + } else { + incomeList.add(categoryForm); + } + + } + + model.addAttribute("expenseList", expenseList); + model.addAttribute("incomeList", incomeList); + model.addAttribute("today", today); + + + return "fixed_cost_create"; + } + + + + @SessionCheck + @PostMapping("/fixed-cost/create") + public String postFixedCostCreate( + HttpSession session, @ModelAttribute FixedCostForm fixedCostForm, Model model, LocalDate today) { + + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + + fixedCostForm.setUserId(loginUser.getUserId()); + + fixedCostService.save(FixedCostForm.convertTo(fixedCostForm)); + + session.setAttribute("today", today); + + return "redirect:/fixed-cost/list"; + } + + + @SessionCheck + @PostMapping("/fixed-cost/delete") + public String postFixedCostDelete(Long fixedCostId, LocalDate today, HttpSession session) { + + fixedCostService.delete(fixedCostId); + + session.setAttribute("today", today); + + return "redirect:/fixed-cost/list"; + } + + + + + + + + +}