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 ea2c3b7..294f7ac 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 @@ -5,6 +5,8 @@ import com.example.demo.app.domain.model.GoalModel; public interface GoalService { GoalModel getGoalById(Long goalId); + + GoalModel getGoalByUserId(Long userId); GoalModel createGoal(GoalModel goalModel); 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 c7063f3..56f4ea4 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 @@ -5,7 +5,9 @@ import org.springframework.stereotype.Service; import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.service.GoalService; import com.example.demo.app.infra.entity.GoalEntity; +import com.example.demo.app.infra.entity.UserEntity; import com.example.demo.app.infra.repository.GoalRepository; +import com.example.demo.app.infra.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -14,14 +16,20 @@ import lombok.RequiredArgsConstructor; public class GoalServiceImpl implements GoalService { private final GoalRepository goalRepository; + private final UserRepository userRepository; @Override public GoalModel getGoalById(Long goalId) { - GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null); - return GoalModel.convertFrom(goalEntity); + } + @Override + public GoalModel getGoalByUserId(Long userId) { + + UserEntity userEntity = userRepository.findById(userId).orElseThrow(); + GoalEntity goalEntity = userEntity.getGoalEntity(); + return GoalModel.convertFrom(goalEntity); } @Override @@ -32,7 +40,6 @@ public class GoalServiceImpl implements GoalService { GoalEntity savedGoalEntity = goalRepository.save(goalEntity); return GoalModel.convertFrom(savedGoalEntity); - } @Override @@ -43,7 +50,6 @@ public class GoalServiceImpl implements GoalService { GoalEntity updatedGoalEntity = goalRepository.save(goalEntity); return GoalModel.convertFrom(updatedGoalEntity); - } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/infra/entity/GoalEntity.java b/src/main/java/com/example/demo/app/infra/entity/GoalEntity.java index f4bbe50..2996fe6 100644 --- a/src/main/java/com/example/demo/app/infra/entity/GoalEntity.java +++ b/src/main/java/com/example/demo/app/infra/entity/GoalEntity.java @@ -38,7 +38,7 @@ public class GoalEntity { @Column(name = "deadline", nullable = false) private LocalDate deadline; - @Column(name = "goal_complete", nullable = false) + @Column(name = "goal_complete", nullable = true) private Integer goalComplete; @OneToOne(mappedBy = "goalEntity") 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 96fe4ac..827e05b 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,14 +1,16 @@ package com.example.demo.app.presentation.controller; +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.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.example.demo.app.domain.model.GoalModel; +import com.example.demo.app.domain.model.LoginModel; import com.example.demo.app.domain.service.GoalService; import com.example.demo.app.presentation.form.GoalForm; @@ -21,41 +23,59 @@ public class GoalController { private final GoalService goalService; - @GetMapping("/{goalId}") - public String getGoal(@PathVariable Long goalId, Model model) { - - GoalModel goalModel = goalService.getGoalById(goalId); + @GetMapping("") + public String getGoal(Model model, HttpSession session) { + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + + Long userId = loginUser.getUserId(); + //テスト用 +// Long userId = 1L; + + GoalModel goalModel = goalService.getGoalByUserId(userId); GoalForm goalForm = GoalForm.convertFrom(goalModel); model.addAttribute("goalForm", goalForm); - return "goal_view"; - } - @GetMapping("/{goalId}/edit") - public String editGoal(@PathVariable Long goalId, Model model) { + @GetMapping("/edit") + public String editGoal(Model model, HttpSession session) { - GoalModel goalModel = goalService.getGoalById(goalId); + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + Long userId = loginUser.getUserId(); + + //テスト用 +// Long userId = 1L; + GoalModel goalModel = goalService.getGoalByUserId(userId); GoalForm goalForm = GoalForm.convertFrom(goalModel); model.addAttribute("goalForm", goalForm); - return "goal_edit"; - } @PostMapping("/update") - public String updateGoal(@ModelAttribute GoalForm goalForm) { + public String updateGoal(@ModelAttribute GoalForm goalForm, HttpSession session) { + + LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + + Long userId = loginUser.getUserId(); + //テスト用 +// Long userId = 1L; + + + // 本人の目標かどうかを確認してから更新する + GoalModel currentGoal = goalService.getGoalByUserId(userId); + + if (!currentGoal.getGoalId().equals(goalForm.getGoalId())) { + return "redirect:/goal"; // 他人のデータを更新しようとしたら弾く + } GoalModel goalModel = GoalModel.convertFrom(goalForm); - goalService.updateGoal(goalModel); - return "redirect:/goal/" + goalForm.getGoalId(); - + return "redirect:/goal"; } } \ No newline at end of file diff --git a/src/main/resources/templates/goal_edit.html b/src/main/resources/templates/goal_edit.html index b3b59d3..0d3601e 100644 --- a/src/main/resources/templates/goal_edit.html +++ b/src/main/resources/templates/goal_edit.html @@ -9,18 +9,15 @@

目標編集

- -
- +
@@ -33,8 +30,6 @@ - -
diff --git a/src/main/resources/templates/goal_view.html b/src/main/resources/templates/goal_view.html index 75c8cde..348f7e4 100644 --- a/src/main/resources/templates/goal_view.html +++ b/src/main/resources/templates/goal_view.html @@ -9,16 +9,14 @@

目標詳細

- - - + - + @@ -31,7 +29,7 @@
目標補足説明目標説明
- 編集する - + 編集する + \ No newline at end of file