[EDIT] 目標の一覧を受け取るように修正・作成と削除機能の追加
This commit is contained in:
@@ -14,6 +14,8 @@ public interface GoalService {
|
|||||||
|
|
||||||
GoalModel updateGoal(GoalModel goalModel, Long userId);
|
GoalModel updateGoal(GoalModel goalModel, Long userId);
|
||||||
|
|
||||||
|
public void delete(Long goalId);
|
||||||
|
|
||||||
// void deleteGoal(Long goalId);
|
// void deleteGoal(Long goalId);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -79,4 +79,8 @@ public class GoalServiceImpl implements GoalService {
|
|||||||
return GoalModel.convertFrom(updatedGoalEntity, 0, 0);
|
return GoalModel.convertFrom(updatedGoalEntity, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delete(Long goalId) {
|
||||||
|
goalRepository.deleteById(goalId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.demo.app.presentation.controller;
|
package com.example.demo.app.presentation.controller;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@@ -32,22 +34,21 @@ public class GoalController {
|
|||||||
|
|
||||||
Long userId = loginUser.getUserId();
|
Long userId = loginUser.getUserId();
|
||||||
|
|
||||||
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
List<GoalModel> goalModelList = goalService.getGoalByUserId(userId);
|
||||||
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
List<GoalForm> goalFormList = GoalForm.convertFrom(goalModelList);
|
||||||
|
|
||||||
model.addAttribute("goalForm", goalForm);
|
model.addAttribute("goalFormList", goalFormList);
|
||||||
return "goal_view";
|
return "goal_view";
|
||||||
}
|
}
|
||||||
|
|
||||||
@SessionCheck
|
@SessionCheck
|
||||||
@GetMapping("/edit")
|
@GetMapping("/edit")
|
||||||
public String editGoal(Model model, HttpSession session) {
|
public String editGoal(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();
|
||||||
|
|
||||||
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
GoalForm goalForm = GoalForm.convertFrom(goalService.getGoalById(goalId));
|
||||||
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
|
||||||
|
|
||||||
model.addAttribute("goalForm", goalForm);
|
model.addAttribute("goalForm", goalForm);
|
||||||
return "goal_edit";
|
return "goal_edit";
|
||||||
@@ -61,12 +62,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);
|
||||||
@@ -74,4 +75,36 @@ public class GoalController {
|
|||||||
return "redirect:/goal";
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user