diff --git a/src/main/java/com/example/demo/app/domain/service/GoalService.java b/src/main/java/com/example/demo/app/domain/service/GoalService.java index 500c990..9272331 100644 --- a/src/main/java/com/example/demo/app/domain/service/GoalService.java +++ b/src/main/java/com/example/demo/app/domain/service/GoalService.java @@ -13,6 +13,8 @@ public interface GoalService { public GoalModel createGoal(GoalModel goalModel, Long userId); GoalModel updateGoal(GoalModel goalModel, Long userId); + + public void delete(Long goalId); // void deleteGoal(Long goalId); diff --git a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java index 0d4d75a..0708ec9 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java @@ -78,5 +78,9 @@ public class GoalServiceImpl implements GoalService { return GoalModel.convertFrom(updatedGoalEntity, 0, 0); } + + public void delete(Long goalId) { + goalRepository.deleteById(goalId); + } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java index 3657719..abb05a4 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java @@ -1,5 +1,7 @@ package com.example.demo.app.presentation.controller; +import java.util.List; + import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; @@ -32,22 +34,21 @@ public class GoalController { Long userId = loginUser.getUserId(); - GoalModel goalModel = goalService.getGoalByUserId(userId); - GoalForm goalForm = GoalForm.convertFrom(goalModel); + List goalModelList = goalService.getGoalByUserId(userId); + List goalFormList = GoalForm.convertFrom(goalModelList); - model.addAttribute("goalForm", goalForm); + model.addAttribute("goalFormList", goalFormList); return "goal_view"; } @SessionCheck @GetMapping("/edit") - public String editGoal(Model model, HttpSession session) { + public String editGoal(Model model, HttpSession session, Long goalId) { LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); Long userId = loginUser.getUserId(); - GoalModel goalModel = goalService.getGoalByUserId(userId); - GoalForm goalForm = GoalForm.convertFrom(goalModel); + GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalById(goalId)); model.addAttribute("goalForm", goalForm); return "goal_edit"; @@ -61,17 +62,49 @@ public class GoalController { Long userId = loginUser.getUserId(); - // 本人の目標かどうかを確認してから更新する - GoalModel currentGoal = goalService.getGoalByUserId(userId); - - if (!currentGoal.getGoalId().equals(goalForm.getGoalId())) { - return "redirect:/goal"; // 他人のデータを更新しようとしたら弾く - } +// // 本人の目標かどうかを確認してから更新する +// GoalModel currentGoal = goalService.getGoalByUserId(userId); +// +// if (!currentGoal.getGoalId().equals(goalForm.getGoalId())) { +// return "redirect:/goal"; // 他人のデータを更新しようとしたら弾く +// } GoalModel goalModel = GoalModel.convertFrom(goalForm); goalService.updateGoal(goalModel, userId); return "redirect:/goal"; } + + + @SessionCheck + @GetMapping("/create") + public String createGoalGet(@ModelAttribute GoalForm goalForm, HttpSession session) { + + return "goal_create"; + } + + @SessionCheck + @PostMapping("/create") + public String createGoalPost(@ModelAttribute GoalForm goalForm, HttpSession session) { + + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + + Long userId = loginUser.getUserId(); + + goalService.createGoal(GoalModel.convertFrom(goalForm), userId); + + return "redirect:/goal"; + } + + @SessionCheck + @PostMapping("/delete") // フラグ考慮してないため、今度修正必要 + public String deleteGoal(Long goalId) { + + goalService.delete(goalId); + + return "redirect:/goal"; + } + + } \ No newline at end of file