mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
use optional in PersonService
This commit is contained in:
@@ -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<Person> found = personService.readByUsername(username);
|
||||
|
||||
return PersonMapper.makeDto(found);
|
||||
if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
|
||||
return PersonMapper.makeDto(found.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Person> found = personService.login(tokenRequest.username(), tokenRequest.password());
|
||||
|
||||
if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
|
||||
return new TokenResponse(tokenService.generateToken(found.get().getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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, String> {
|
||||
Person signup(Person person) throws UserAlreadyExistsException;
|
||||
|
||||
Person login(String username, String password) throws UserNotFoundException;
|
||||
Optional<Person> login(String username, String password);
|
||||
|
||||
Person readByUsername(String username) throws UserNotFoundException;
|
||||
Optional<Person> readByUsername(String username);
|
||||
}
|
||||
|
||||
@@ -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<Person, String> implements PersonService {
|
||||
private final PersonRepository personRepository;
|
||||
@@ -34,20 +36,16 @@ public class PersonServiceImpl extends CrudServiceImpl<Person, String> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person login(String username, String password) throws UserNotFoundException {
|
||||
public Optional<Person> 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<Person> readByUsername(String username) {
|
||||
return personRepository.findByUsername(username);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user