added users
This commit is contained in:
parent
afe847c255
commit
1c197a1555
@ -0,0 +1,64 @@
|
|||||||
|
package com.ip.library.users.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.ip.library.core.configuration.Constants;
|
||||||
|
import com.ip.library.users.model.UserEntity;
|
||||||
|
import com.ip.library.users.service.UserService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(Constants.API_URL + "/user")
|
||||||
|
public class UserController {
|
||||||
|
private final UserService userService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public UserController(UserService userService, ModelMapper modelMapper) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserDto toDto(UserEntity entity) {
|
||||||
|
return modelMapper.map(entity, UserDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserEntity toEntity(UserDto dto) {
|
||||||
|
return modelMapper.map(dto, UserEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<UserDto> getAll() {
|
||||||
|
return userService.getAll().stream().map(this::toDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public UserDto get(@PathVariable(name = "id") Long id) {
|
||||||
|
return toDto(userService.get(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public UserDto create(@RequestBody @Valid UserDto dto) {
|
||||||
|
return toDto(userService.create(toEntity(dto)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
|
||||||
|
return toDto(userService.update(id, toEntity(dto)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public UserDto delete(@PathVariable(name = "id") Long id) {
|
||||||
|
return toDto(userService.delete(id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.ip.library.users.api;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
public class UserDto {
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(String role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.ip.library.users.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.ip.library.core.model.BaseEntity;
|
||||||
|
|
||||||
|
public class UserEntity extends BaseEntity {
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
public UserEntity() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity(Long id, String name, String password, String role) {
|
||||||
|
super(id);
|
||||||
|
this.name = name;
|
||||||
|
this.password = password;
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(String role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name, password, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final UserEntity other = (UserEntity) obj;
|
||||||
|
return Objects.equals(other.getId(), id)
|
||||||
|
&& Objects.equals(other.getName(), name)
|
||||||
|
&& Objects.equals(other.getPassword(), password)
|
||||||
|
&& Objects.equals(other.getRole(), role);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.ip.library.users.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.ip.library.core.repository.MapRepository;
|
||||||
|
import com.ip.library.users.model.UserEntity;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserRepository extends MapRepository<UserEntity> {
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.ip.library.users.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.ip.library.core.error.NotFoundException;
|
||||||
|
import com.ip.library.users.model.UserEntity;
|
||||||
|
import com.ip.library.users.repository.UserRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
private final UserRepository repository;
|
||||||
|
|
||||||
|
public UserService(UserRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserEntity> getAll() {
|
||||||
|
return repository.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity get(Long id) {
|
||||||
|
return Optional.ofNullable(repository.get(id))
|
||||||
|
.orElseThrow(() -> new NotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity create(UserEntity entity) {
|
||||||
|
return repository.create(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity update(Long id, UserEntity entity) {
|
||||||
|
final UserEntity existsEntity = get(id);
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
return repository.update(existsEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity delete(Long id) {
|
||||||
|
final UserEntity existsEntity = get(id);
|
||||||
|
return repository.delete(existsEntity);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user