fix feature crud user
This commit is contained in:
parent
fe1e7b138e
commit
015157bce6
@ -2,6 +2,7 @@ package com.dh7789dev.xpeditis.controller.api.v1;
|
||||
|
||||
import com.dh7789dev.xpeditis.UserService;
|
||||
import com.dh7789dev.xpeditis.dto.request.ChangePasswordRequest;
|
||||
import com.dh7789dev.xpeditis.dto.app.UserAccount;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -10,8 +11,14 @@ import org.springframework.web.bind.annotation.PatchMapping;
|
||||
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.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
@ -35,4 +42,35 @@ public class UserRestController {
|
||||
service.changePassword(request, connectedUser);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Operation(summary = "Create a user")
|
||||
@PostMapping
|
||||
public ResponseEntity<UserAccount> create(@RequestBody UserAccount user) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(service.create(user));
|
||||
}
|
||||
|
||||
@Operation(summary = "Update a user")
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<UserAccount> update(@PathVariable Long id, @RequestBody UserAccount user) {
|
||||
return ResponseEntity.ok(service.update(id, user));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get a user by id")
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<UserAccount> getById(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(service.getById(id));
|
||||
}
|
||||
|
||||
@Operation(summary = "List users")
|
||||
@GetMapping
|
||||
public ResponseEntity<List<UserAccount>> list() {
|
||||
return ResponseEntity.ok(service.list());
|
||||
}
|
||||
|
||||
@Operation(summary = "Delete a user")
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable Long id) {
|
||||
service.delete(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,22 @@
|
||||
package com.dh7789dev.xpeditis;
|
||||
|
||||
import com.dh7789dev.xpeditis.dto.request.ChangePasswordRequest;
|
||||
import com.dh7789dev.xpeditis.dto.app.UserAccount;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void changePassword(ChangePasswordRequest request, Principal connectedUser);
|
||||
|
||||
UserAccount create(UserAccount user);
|
||||
|
||||
UserAccount update(Long id, UserAccount user);
|
||||
|
||||
UserAccount getById(Long id);
|
||||
|
||||
List<UserAccount> list();
|
||||
|
||||
void delete(Long id);
|
||||
}
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
package com.dh7789dev.xpeditis;
|
||||
|
||||
import com.dh7789dev.xpeditis.dto.request.ChangePasswordRequest;
|
||||
import com.dh7789dev.xpeditis.dto.app.UserAccount;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
@ -18,4 +20,29 @@ public class UserServiceImpl implements UserService {
|
||||
public void changePassword(ChangePasswordRequest request, Principal connectedUser) {
|
||||
userRepository.changePassword(request, connectedUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount create(UserAccount user) {
|
||||
return userRepository.create(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount update(Long id, UserAccount user) {
|
||||
return userRepository.update(id, user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount getById(Long id) {
|
||||
return userRepository.getById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserAccount> list() {
|
||||
return userRepository.list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
userRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,22 @@
|
||||
package com.dh7789dev.xpeditis;
|
||||
|
||||
import com.dh7789dev.xpeditis.dto.request.ChangePasswordRequest;
|
||||
import com.dh7789dev.xpeditis.dto.app.UserAccount;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
|
||||
void changePassword(ChangePasswordRequest request, Principal connectedUser);
|
||||
|
||||
UserAccount create(UserAccount user);
|
||||
|
||||
UserAccount update(Long id, UserAccount user);
|
||||
|
||||
UserAccount getById(Long id);
|
||||
|
||||
List<UserAccount> list();
|
||||
|
||||
void delete(Long id);
|
||||
}
|
||||
|
||||
@ -4,12 +4,18 @@ import com.dh7789dev.xpeditis.UserRepository;
|
||||
import com.dh7789dev.xpeditis.dao.UserDao;
|
||||
import com.dh7789dev.xpeditis.dto.request.ChangePasswordRequest;
|
||||
import com.dh7789dev.xpeditis.entity.UserEntity;
|
||||
import com.dh7789dev.xpeditis.dto.app.UserAccount;
|
||||
import com.dh7789dev.xpeditis.entity.Role;
|
||||
import com.dh7789dev.xpeditis.dao.CompanyDao;
|
||||
import com.dh7789dev.xpeditis.mapper.UserMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Repository
|
||||
public class UserJpaRepository implements UserRepository {
|
||||
@ -17,11 +23,15 @@ public class UserJpaRepository implements UserRepository {
|
||||
private final UserDao userDao;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final CompanyDao companyDao;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public UserJpaRepository(UserDao userDao, PasswordEncoder passwordEncoder) {
|
||||
public UserJpaRepository(UserDao userDao, PasswordEncoder passwordEncoder, CompanyDao companyDao, UserMapper userMapper) {
|
||||
this.userDao = userDao;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.companyDao = companyDao;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -42,4 +52,55 @@ public class UserJpaRepository implements UserRepository {
|
||||
userEntity.setPassword(passwordEncoder.encode(request.getNewPassword()));
|
||||
userDao.save(userEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount create(UserAccount user) {
|
||||
UserEntity entity = userMapper.userAccountToUserEntity(user);
|
||||
if (user.getPassword() != null) {
|
||||
entity.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
}
|
||||
if (user.getRole() != null) {
|
||||
entity.setRole(Role.valueOf(user.getRole()));
|
||||
}
|
||||
if (user.getCompany() != null && user.getCompany().getId() != null) {
|
||||
companyDao.findById(user.getCompany().getId()).ifPresent(entity::setCompany);
|
||||
}
|
||||
entity.setEnabled(true);
|
||||
UserEntity saved = userDao.save(entity);
|
||||
return userMapper.userEntityToUserAccount(saved);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount update(Long id, UserAccount user) {
|
||||
UserEntity entity = userDao.findById(id).orElseThrow();
|
||||
if (user.getFirstName() != null) entity.setFirstName(user.getFirstName());
|
||||
if (user.getLastName() != null) entity.setLastName(user.getLastName());
|
||||
if (user.getEmail() != null) entity.setEmail(user.getEmail());
|
||||
if (user.getUsername() != null) entity.setUsername(user.getUsername());
|
||||
if (user.getRole() != null) entity.setRole(Role.valueOf(user.getRole()));
|
||||
if (user.getCompany() != null && user.getCompany().getId() != null) {
|
||||
companyDao.findById(user.getCompany().getId()).ifPresent(entity::setCompany);
|
||||
}
|
||||
UserEntity saved = userDao.save(entity);
|
||||
return userMapper.userEntityToUserAccount(saved);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserAccount getById(Long id) {
|
||||
return userDao.findById(id)
|
||||
.map(userMapper::userEntityToUserAccount)
|
||||
.orElseThrow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserAccount> list() {
|
||||
return userDao.findAll().stream()
|
||||
.map(userMapper::userEntityToUserAccount)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
userDao.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user