diff --git a/src/main/java/com/example/demo/app/domain/model/GoalModel.java b/src/main/java/com/example/demo/app/domain/model/GoalModel.java index 7764281..94a57d2 100644 --- a/src/main/java/com/example/demo/app/domain/model/GoalModel.java +++ b/src/main/java/com/example/demo/app/domain/model/GoalModel.java @@ -2,6 +2,9 @@ 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; @@ -21,4 +24,35 @@ public class GoalModel { 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()); + } + } \ No newline at end of file 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 new file mode 100644 index 0000000..294f7ac --- /dev/null +++ b/src/main/java/com/example/demo/app/domain/service/GoalService.java @@ -0,0 +1,17 @@ +package com.example.demo.app.domain.service; + +import com.example.demo.app.domain.model.GoalModel; + +public interface GoalService { + + GoalModel getGoalById(Long goalId); + + GoalModel getGoalByUserId(Long userId); + + GoalModel createGoal(GoalModel goalModel); + + GoalModel updateGoal(GoalModel goalModel); + +// void deleteGoal(Long goalId); + +} \ No newline at end of file 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 new file mode 100644 index 0000000..56f4ea4 --- /dev/null +++ b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java @@ -0,0 +1,55 @@ +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.entity.UserEntity; +import com.example.demo.app.infra.repository.GoalRepository; +import com.example.demo.app.infra.repository.UserRepository; + +import lombok.RequiredArgsConstructor; + +@Service +@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 + 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); + } + +} \ 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 new file mode 100644 index 0000000..827e05b --- /dev/null +++ b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java @@ -0,0 +1,81 @@ +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.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; + +import lombok.RequiredArgsConstructor; + +@Controller +@RequiredArgsConstructor +@RequestMapping("/goal") +public class GoalController { + + private final GoalService goalService; + + @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("/edit") + public String editGoal(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_edit"; + } + + @PostMapping("/update") + 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"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/presentation/form/GoalForm.java b/src/main/java/com/example/demo/app/presentation/form/GoalForm.java new file mode 100644 index 0000000..42e5368 --- /dev/null +++ b/src/main/java/com/example/demo/app/presentation/form/GoalForm.java @@ -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 convertFrom(List goalModelList) { +// return goalModelList.stream().map(GoalForm::convertFrom).toList(); +// } + +} \ No newline at end of file diff --git a/src/main/resources/templates/goal_edit.html b/src/main/resources/templates/goal_edit.html new file mode 100644 index 0000000..0d3601e --- /dev/null +++ b/src/main/resources/templates/goal_edit.html @@ -0,0 +1,38 @@ + + + + + 目標編集 + + + +

目標編集

+ +
+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/goal_view.html b/src/main/resources/templates/goal_view.html new file mode 100644 index 0000000..348f7e4 --- /dev/null +++ b/src/main/resources/templates/goal_view.html @@ -0,0 +1,35 @@ + + + + + 目標詳細 + + + +

目標詳細

+ + + + + + + + + + + + + + + + + + + + +
目標名
目標説明
目標金額
目標達成期限
+ + 編集する + + + \ No newline at end of file