[EDIT] 目標を表示する機能を追加しました。

This commit is contained in:
2026-09-18 13:32:14 +09:00
parent be9abffeaa
commit 15bf2949bc
6 changed files with 55 additions and 34 deletions

View File

@@ -5,6 +5,8 @@ import com.example.demo.app.domain.model.GoalModel;
public interface GoalService { public interface GoalService {
GoalModel getGoalById(Long goalId); GoalModel getGoalById(Long goalId);
GoalModel getGoalByUserId(Long userId);
GoalModel createGoal(GoalModel goalModel); GoalModel createGoal(GoalModel goalModel);

View File

@@ -5,7 +5,9 @@ import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.GoalModel; import com.example.demo.app.domain.model.GoalModel;
import com.example.demo.app.domain.service.GoalService; 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.repository.GoalRepository; import com.example.demo.app.infra.repository.GoalRepository;
import com.example.demo.app.infra.repository.UserRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -14,14 +16,20 @@ import lombok.RequiredArgsConstructor;
public class GoalServiceImpl implements GoalService { public class GoalServiceImpl implements GoalService {
private final GoalRepository goalRepository; private final GoalRepository goalRepository;
private final UserRepository userRepository;
@Override @Override
public GoalModel getGoalById(Long goalId) { public GoalModel getGoalById(Long goalId) {
GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null); GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null);
return GoalModel.convertFrom(goalEntity); 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 @Override
@@ -32,7 +40,6 @@ public class GoalServiceImpl implements GoalService {
GoalEntity savedGoalEntity = goalRepository.save(goalEntity); GoalEntity savedGoalEntity = goalRepository.save(goalEntity);
return GoalModel.convertFrom(savedGoalEntity); return GoalModel.convertFrom(savedGoalEntity);
} }
@Override @Override
@@ -43,7 +50,6 @@ public class GoalServiceImpl implements GoalService {
GoalEntity updatedGoalEntity = goalRepository.save(goalEntity); GoalEntity updatedGoalEntity = goalRepository.save(goalEntity);
return GoalModel.convertFrom(updatedGoalEntity); return GoalModel.convertFrom(updatedGoalEntity);
} }
} }

View File

@@ -38,7 +38,7 @@ public class GoalEntity {
@Column(name = "deadline", nullable = false) @Column(name = "deadline", nullable = false)
private LocalDate deadline; private LocalDate deadline;
@Column(name = "goal_complete", nullable = false) @Column(name = "goal_complete", nullable = true)
private Integer goalComplete; private Integer goalComplete;
@OneToOne(mappedBy = "goalEntity") @OneToOne(mappedBy = "goalEntity")

View File

@@ -1,14 +1,16 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
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.service.GoalService; import com.example.demo.app.domain.service.GoalService;
import com.example.demo.app.presentation.form.GoalForm; import com.example.demo.app.presentation.form.GoalForm;
@@ -21,41 +23,59 @@ public class GoalController {
private final GoalService goalService; private final GoalService goalService;
@GetMapping("/{goalId}") @GetMapping("")
public String getGoal(@PathVariable Long goalId, Model model) { public String getGoal(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); GoalForm goalForm = GoalForm.convertFrom(goalModel);
model.addAttribute("goalForm", goalForm); model.addAttribute("goalForm", goalForm);
return "goal_view"; return "goal_view";
} }
@GetMapping("/{goalId}/edit") @GetMapping("/edit")
public String editGoal(@PathVariable Long goalId, Model model) { 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); GoalForm goalForm = GoalForm.convertFrom(goalModel);
model.addAttribute("goalForm", goalForm); model.addAttribute("goalForm", goalForm);
return "goal_edit"; return "goal_edit";
} }
@PostMapping("/update") @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); GoalModel goalModel = GoalModel.convertFrom(goalForm);
goalService.updateGoal(goalModel); goalService.updateGoal(goalModel);
return "redirect:/goal/" + goalForm.getGoalId(); return "redirect:/goal";
} }
} }

View File

@@ -9,18 +9,15 @@
<h1>目標編集</h1> <h1>目標編集</h1>
<form th:action="@{/goal/update}" th:object="${goalForm}" method="post"> <form th:action="@{/goal/update}" th:object="${goalForm}" method="post">
<input type="hidden" th:field="*{goalId}"> <input type="hidden" th:field="*{goalId}">
<!--
<div> <div>
<label>目標名</label> <label>目標名</label>
<input type="text" th:field="*{goalTitle}"> <input type="text" th:field="*{goalTitle}">
</div> </div>
-->
<div> <div>
<label>目標補足説明</label> <label>目標説明</label>
<textarea th:field="*{goalSubtitle}"></textarea> <textarea th:field="*{goalSubtitle}"></textarea>
</div> </div>
@@ -33,8 +30,6 @@
<label>目標達成期限</label> <label>目標達成期限</label>
<input type="date" th:field="*{deadline}"> <input type="date" th:field="*{deadline}">
</div> </div>
<button type="submit">更新する</button> <button type="submit">更新する</button>
</form> </form>

View File

@@ -9,16 +9,14 @@
<h1>目標詳細</h1> <h1>目標詳細</h1>
<table> <table>
<!--
<tr> <tr>
<th>目標名</th> <th>目標名</th>
<td th:text="${goalForm.goalTitle}"></td> <td th:text="${goalForm.goalTitle}"></td>
</tr> </tr>
-->
<tr> <tr>
<th>目標補足説明</th> <th>目標説明</th>
<td th:text="${goalForm.goalSubtitle}"></td> <td th:text="${goalForm.goalSubtitle}"></td>
</tr> </tr>
<tr> <tr>
@@ -31,7 +29,7 @@
</tr> </tr>
</table> </table>
<a th:href="@{/goal/{goalId}/edit(goalId=${goalForm.goalId})}">編集する</a> <a th:href="@{/goal/edit}">編集する</a>
</body> </body>
</html> </html>