From 1c7ff108649f52096fb9e1bc56a3e32a2731b17d Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Sat, 25 Nov 2023 20:25:42 +0100 Subject: [PATCH] use optional in PersonService --- .../y/server/controller/PersonController.java | 10 ++++++++-- .../tjv/y/server/controller/TokenController.java | 11 +++++++++-- .../tjv/y/server/service/PersonService.java | 7 ++++--- .../tjv/y/server/service/PersonServiceImpl.java | 16 +++++++--------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/controller/PersonController.java b/server/src/main/java/com/usatiuk/tjv/y/server/controller/PersonController.java index 7edffcf..0621393 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/controller/PersonController.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/controller/PersonController.java @@ -7,8 +7,12 @@ import com.usatiuk.tjv.y.server.entity.Person; import com.usatiuk.tjv.y.server.service.PersonService; import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException; import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Optional; @RestController @RequestMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE) @@ -33,9 +37,11 @@ public class PersonController { @GetMapping(path = "/{username}") public PersonTo get(@PathVariable String username) throws UserNotFoundException { - Person found = personService.readByUsername(username); + Optional found = personService.readByUsername(username); - return PersonMapper.makeDto(found); + if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND); + + return PersonMapper.makeDto(found.get()); } } diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/controller/TokenController.java b/server/src/main/java/com/usatiuk/tjv/y/server/controller/TokenController.java index ea15538..ee7819e 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/controller/TokenController.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/controller/TokenController.java @@ -6,11 +6,15 @@ import com.usatiuk.tjv.y.server.entity.Person; import com.usatiuk.tjv.y.server.service.PersonService; import com.usatiuk.tjv.y.server.service.TokenService; import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Optional; @RestController @RequestMapping(value = "/token", produces = MediaType.APPLICATION_JSON_VALUE) @@ -25,8 +29,11 @@ public class TokenController { @PostMapping public TokenResponse request(@RequestBody TokenRequest tokenRequest) throws UserNotFoundException { - Person found = personService.login(tokenRequest.username(), tokenRequest.password()); - return new TokenResponse(tokenService.generateToken(found.getId())); + Optional found = personService.login(tokenRequest.username(), tokenRequest.password()); + + if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND); + + return new TokenResponse(tokenService.generateToken(found.get().getId())); } } diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonService.java b/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonService.java index a1df913..73ed610 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonService.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonService.java @@ -2,12 +2,13 @@ package com.usatiuk.tjv.y.server.service; import com.usatiuk.tjv.y.server.entity.Person; import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException; -import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException; + +import java.util.Optional; public interface PersonService extends CrudService { Person signup(Person person) throws UserAlreadyExistsException; - Person login(String username, String password) throws UserNotFoundException; + Optional login(String username, String password); - Person readByUsername(String username) throws UserNotFoundException; + Optional readByUsername(String username); } diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonServiceImpl.java b/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonServiceImpl.java index 9d8f954..254bc6d 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonServiceImpl.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/service/PersonServiceImpl.java @@ -8,6 +8,8 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.util.Optional; + @Service public class PersonServiceImpl extends CrudServiceImpl implements PersonService { private final PersonRepository personRepository; @@ -34,20 +36,16 @@ public class PersonServiceImpl extends CrudServiceImpl implement } @Override - public Person login(String username, String password) throws UserNotFoundException { + public Optional login(String username, String password) { var found = personRepository.findByUsername(username); if (found.isEmpty() || !passwordEncoder.matches(password, found.get().getPassword())) - throw new UserNotFoundException(); + return Optional.empty(); - return found.get(); + return found; } @Override - public Person readByUsername(String username) throws UserNotFoundException { - var found = personRepository.findByUsername(username); - if (found.isEmpty()) - throw new UserNotFoundException(); - - return found.get(); + public Optional readByUsername(String username) { + return personRepository.findByUsername(username); } }