Search in sources :

Example 1 with UserDTO

use of com.rafaelvieira.letmebuy.dto.UserDTO in project letmebuy by rafaelrok.

the class UserService method update.

@Transactional
public UserDTO update(Long id, UserUpdateDTO dto) {
    try {
        User entity = repository.getOne(id);
        copyDtoToEntity(dto, entity);
        entity = repository.save(entity);
        return new UserDTO(entity);
    } catch (EntityNotFoundException e) {
        throw new ResourceNotFoundException("Id not found " + id);
    }
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserDTO

use of com.rafaelvieira.letmebuy.dto.UserDTO in project letmebuy by rafaelrok.

the class UserService method findById.

@Transactional(readOnly = true)
public UserDTO findById(Long id) {
    authService.validateSelfOrAdmin(id);
    Optional<User> obj = repository.findById(id);
    User entity = obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found"));
    return new UserDTO(entity);
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserDTO

use of com.rafaelvieira.letmebuy.dto.UserDTO in project letmebuy by rafaelrok.

the class UserService method save.

@Transactional
public UserDTO save(UserInsertDTO dto) {
    User entity = new User();
    copyDtoToEntity(dto, entity);
    entity.setPassword(passwordEncoder.encode(dto.getPassword()));
    entity = repository.save(entity);
    return new UserDTO(entity);
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserDTO

use of com.rafaelvieira.letmebuy.dto.UserDTO in project letmebuy by rafaelrok.

the class UserController method insert.

@PostMapping
public ResponseEntity<UserDTO> insert(@RequestBody @Valid UserInsertDTO dto) {
    UserDTO newDto = service.save(dto);
    URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(newDto.getId()).toUri();
    return ResponseEntity.created(uri).body(newDto);
}
Also used : UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) URI(java.net.URI) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

UserDTO (com.rafaelvieira.letmebuy.dto.UserDTO)4 User (com.rafaelvieira.letmebuy.entities.User)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)2 URI (java.net.URI)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1