[EDIT]serviceとcontrollerの修正

This commit is contained in:
2026-09-17 12:59:17 +09:00
parent 1170c710e3
commit 79cbbc1f2b
3 changed files with 17 additions and 11 deletions

View File

@@ -1,9 +1,9 @@
package com.example.demo.app.domain.service;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.domain.model.LoginModel;
public interface LoginService {
UserEntity login(String mailAddress, String password);
LoginModel login(String mailAddress, String password);
}

View File

@@ -2,6 +2,7 @@ package com.example.demo.app.domain.service.impl;
import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.LoginService;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.UserRepository;
@@ -15,11 +16,16 @@ public class LoginServiceImpl implements LoginService {
private final UserRepository userRepository;
@Override
public UserEntity login(String mailAddress, String password) {
public LoginModel login(String mailAddress, String password) {
return userRepository.findByMailAddressAndPassword(
UserEntity userEntity = userRepository.findByMailAddressAndPassword(
mailAddress,
password).orElse(null);
if (userEntity == null) {
return null;
}
return new LoginModel(userEntity.getUserId());
}
}

View File

@@ -8,8 +8,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.LoginService;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.presentation.form.LoginForm;
import lombok.RequiredArgsConstructor;
@@ -33,13 +33,14 @@ public class LoginController {
@PostMapping("/login")
public String login(
@ModelAttribute LoginForm loginForm,
Model model, HttpSession session) {
Model model,
HttpSession session) {
UserEntity loginUser = loginService.login(
LoginModel loginModel = loginService.login(
loginForm.getMailAddress(),
loginForm.getPassword());
if (loginUser == null) {
if (loginModel == null) {
model.addAttribute(
"errorMessage",
"Email address or password is incorrect.");
@@ -47,9 +48,8 @@ public class LoginController {
return "login";
}
session.setAttribute("loginUser", loginUser);
session.setAttribute("loginUser", loginModel);
return "menu";
}
}