[ADD]目標を管理するため機能の追加
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
package com.example.demo.app.domain.model;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import com.example.demo.app.infra.entity.GoalEntity;
|
||||||
|
import com.example.demo.app.presentation.form.GoalForm;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class GoalModel {
|
||||||
|
|
||||||
|
private Long goalId;
|
||||||
|
|
||||||
|
private String goalTitle;
|
||||||
|
|
||||||
|
private String goalSubtitle;
|
||||||
|
|
||||||
|
private Integer targetAmount;
|
||||||
|
|
||||||
|
private LocalDate deadline;
|
||||||
|
|
||||||
|
private Integer goalComplete;
|
||||||
|
|
||||||
|
public static GoalModel convertFrom(GoalEntity goalEntity) {
|
||||||
|
return new GoalModel(
|
||||||
|
goalEntity.getGoalId(),
|
||||||
|
goalEntity.getGoalTitle(),
|
||||||
|
goalEntity.getGoalSubtitle(),
|
||||||
|
goalEntity.getTargetAmount(),
|
||||||
|
goalEntity.getDeadline(),
|
||||||
|
goalEntity.getGoalComplete());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GoalEntity convertTo(GoalModel goalModel) {
|
||||||
|
return new GoalEntity(
|
||||||
|
goalModel.getGoalId(),
|
||||||
|
goalModel.getGoalTitle(),
|
||||||
|
goalModel.getGoalSubtitle(),
|
||||||
|
goalModel.getTargetAmount(),
|
||||||
|
goalModel.getDeadline(),
|
||||||
|
goalModel.getGoalComplete(),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GoalModel convertFrom(GoalForm goalForm) {
|
||||||
|
return new GoalModel(
|
||||||
|
goalForm.getGoalId(),
|
||||||
|
goalForm.getGoalTitle(),
|
||||||
|
goalForm.getGoalSubtitle(),
|
||||||
|
goalForm.getTargetAmount(),
|
||||||
|
goalForm.getDeadline(),
|
||||||
|
goalForm.getGoalComplete());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.demo.app.domain.service;
|
||||||
|
|
||||||
|
import com.example.demo.app.domain.model.GoalModel;
|
||||||
|
|
||||||
|
public interface GoalService {
|
||||||
|
|
||||||
|
GoalModel getGoalById(Long goalId);
|
||||||
|
|
||||||
|
GoalModel createGoal(GoalModel goalModel);
|
||||||
|
|
||||||
|
GoalModel updateGoal(GoalModel goalModel);
|
||||||
|
|
||||||
|
// void deleteGoal(Long goalId);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.example.demo.app.domain.service.impl;
|
||||||
|
|
||||||
|
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.repository.GoalRepository;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class GoalServiceImpl implements GoalService {
|
||||||
|
|
||||||
|
private final GoalRepository goalRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GoalModel getGoalById(Long goalId) {
|
||||||
|
|
||||||
|
GoalEntity goalEntity = goalRepository.findById(goalId).orElse(null);
|
||||||
|
|
||||||
|
return GoalModel.convertFrom(goalEntity);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GoalModel createGoal(GoalModel goalModel) {
|
||||||
|
|
||||||
|
GoalEntity goalEntity = GoalModel.convertTo(goalModel);
|
||||||
|
|
||||||
|
GoalEntity savedGoalEntity = goalRepository.save(goalEntity);
|
||||||
|
|
||||||
|
return GoalModel.convertFrom(savedGoalEntity);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GoalModel updateGoal(GoalModel goalModel) {
|
||||||
|
|
||||||
|
GoalEntity goalEntity = GoalModel.convertTo(goalModel);
|
||||||
|
|
||||||
|
GoalEntity updatedGoalEntity = goalRepository.save(goalEntity);
|
||||||
|
|
||||||
|
return GoalModel.convertFrom(updatedGoalEntity);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.example.demo.app.presentation.controller;
|
||||||
|
|
||||||
|
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.service.GoalService;
|
||||||
|
import com.example.demo.app.presentation.form.GoalForm;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RequestMapping("/goal")
|
||||||
|
public class GoalController {
|
||||||
|
|
||||||
|
private final GoalService goalService;
|
||||||
|
|
||||||
|
@GetMapping("/{goalId}")
|
||||||
|
public String getGoal(@PathVariable Long goalId, Model model) {
|
||||||
|
|
||||||
|
GoalModel goalModel = goalService.getGoalById(goalId);
|
||||||
|
|
||||||
|
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
||||||
|
|
||||||
|
model.addAttribute("goalForm", goalForm);
|
||||||
|
|
||||||
|
return "goal_view";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{goalId}/edit")
|
||||||
|
public String editGoal(@PathVariable Long goalId, Model model) {
|
||||||
|
|
||||||
|
GoalModel goalModel = goalService.getGoalById(goalId);
|
||||||
|
|
||||||
|
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
||||||
|
|
||||||
|
model.addAttribute("goalForm", goalForm);
|
||||||
|
|
||||||
|
return "goal_edit";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/update")
|
||||||
|
public String updateGoal(@ModelAttribute GoalForm goalForm) {
|
||||||
|
|
||||||
|
GoalModel goalModel = GoalModel.convertFrom(goalForm);
|
||||||
|
|
||||||
|
goalService.updateGoal(goalModel);
|
||||||
|
|
||||||
|
return "redirect:/goal/" + goalForm.getGoalId();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package com.example.demo.app.presentation.form;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import com.example.demo.app.domain.model.GoalModel;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class GoalForm {
|
||||||
|
|
||||||
|
private Long goalId;
|
||||||
|
|
||||||
|
private String goalTitle;
|
||||||
|
|
||||||
|
private String goalSubtitle;
|
||||||
|
|
||||||
|
private Integer targetAmount;
|
||||||
|
|
||||||
|
private LocalDate deadline;
|
||||||
|
|
||||||
|
private Integer goalComplete;
|
||||||
|
|
||||||
|
public static GoalForm convertFrom(GoalModel goalModel) {
|
||||||
|
return new GoalForm(
|
||||||
|
goalModel.getGoalId(),
|
||||||
|
goalModel.getGoalTitle(),
|
||||||
|
goalModel.getGoalSubtitle(),
|
||||||
|
goalModel.getTargetAmount(),
|
||||||
|
goalModel.getDeadline(),
|
||||||
|
goalModel.getGoalComplete());
|
||||||
|
}
|
||||||
|
|
||||||
|
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {
|
||||||
|
// return goalModelList.stream().map(GoalForm::convertFrom).toList();
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
41
src/main/resources/templates/goal_edit.html
Normal file
41
src/main/resources/templates/goal_edit.html
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>目標編集</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>目標編集</h1>
|
||||||
|
|
||||||
|
<form th:action="@{/goal/update}" th:object="${goalForm}" method="post">
|
||||||
|
|
||||||
|
<input type="hidden" th:field="*{goalId}">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label>目標名</label>
|
||||||
|
<input type="text" th:field="*{goalTitle}">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label>目標補足説明</label>
|
||||||
|
<textarea th:field="*{goalSubtitle}"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label>目標金額</label>
|
||||||
|
<input type="number" th:field="*{targetAmount}">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label>目標達成期限</label>
|
||||||
|
<input type="date" th:field="*{deadline}">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<button type="submit">更新する</button>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
33
src/main/resources/templates/goal_view.html
Normal file
33
src/main/resources/templates/goal_view.html
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>目標詳細</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>目標詳細</h1>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>目標名</th>
|
||||||
|
<td th:text="${goalForm.goalTitle}"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>目標補足説明</th>
|
||||||
|
<td th:text="${goalForm.goalSubtitle}"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>目標金額</th>
|
||||||
|
<td th:text="${goalForm.targetAmount}"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>目標達成期限</th>
|
||||||
|
<td th:text="${goalForm.deadline}"></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<a th:href="@{/goal/{goalId}/edit(goalId=${goalForm.goalId})}">編集する</a>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user