Split up UserDTO.java into UserRespondDTO.java and UserRequestDTO.java, this commit also includes a change to DocumentRequestDTO.java which now takes in a whole file in with MultipartFile
This commit is contained in:
parent
c37139287f
commit
fbc24d309e
|
@ -1,7 +1,7 @@
|
||||||
package com.mixel.docusphere.controller;
|
package com.mixel.docusphere.controller;
|
||||||
|
|
||||||
import com.mixel.docusphere.dto.UserDTO;
|
import com.mixel.docusphere.dto.request.UserRequestDTO;
|
||||||
import com.mixel.docusphere.entity.User;
|
import com.mixel.docusphere.dto.response.UserRespondDTO;
|
||||||
import com.mixel.docusphere.service.UserService;
|
import com.mixel.docusphere.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
@ -23,24 +23,24 @@ public class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<UserDTO> getAllUsers() {
|
public List<UserRespondDTO> getAllUsers() {
|
||||||
return userService.findAll();
|
return userService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public ResponseEntity<UserDTO> getUserById(@PathVariable UUID id) {
|
public ResponseEntity<UserRespondDTO> getUserById(@PathVariable UUID id) {
|
||||||
Optional<UserDTO> user = userService.findById(id);
|
Optional<UserRespondDTO> user = userService.findById(id);
|
||||||
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public UserDTO createUser(@RequestBody UserDTO userDTO) {
|
public UserRespondDTO createUser(@RequestBody UserRequestDTO userRequestDTO) {
|
||||||
return userService.save(userDTO);
|
return userService.save(userRequestDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public ResponseEntity<UserDTO> updateUser(@PathVariable UUID id, @RequestBody UserDTO userDTO) {
|
public ResponseEntity<UserRespondDTO> updateUser(@PathVariable UUID id, @RequestBody UserRequestDTO userRequestDTO) {
|
||||||
Optional<UserDTO> updatedUser = userService.update(id, userDTO);
|
Optional<UserRespondDTO> updatedUser = userService.update(id, userRequestDTO);
|
||||||
return updatedUser.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
return updatedUser.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.mixel.docusphere.dto;
|
package com.mixel.docusphere.dto.request;
|
||||||
|
|
||||||
public class DocumentDTO {
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
public class DocumentRequestDTO {
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private String s3Path;
|
|
||||||
private String userId;
|
private String userId;
|
||||||
|
private MultipartFile file;
|
||||||
|
|
||||||
// Getters and Setters
|
// Getters and Setters
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -23,14 +25,6 @@ public class DocumentDTO {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getS3Path() {
|
|
||||||
return s3Path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setS3Path(String s3Path) {
|
|
||||||
this.s3Path = s3Path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUserId() {
|
public String getUserId() {
|
||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
@ -38,4 +32,12 @@ public class DocumentDTO {
|
||||||
public void setUserId(String userId) {
|
public void setUserId(String userId) {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MultipartFile getFile() {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFile(MultipartFile file) {
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
package com.mixel.docusphere.dto;
|
package com.mixel.docusphere.dto.request;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public class UserDTO {
|
public class UserRequestDTO {
|
||||||
private String username;
|
private String username;
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.mixel.docusphere.dto.response;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UserRespondDTO {
|
||||||
|
private UUID userId;
|
||||||
|
private String username;
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public UUID getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(UUID userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) {
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getUpdatedAt() {
|
||||||
|
return updatedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUpdatedAt(LocalDateTime updatedAt) {
|
||||||
|
this.updatedAt = updatedAt;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,38 @@
|
||||||
package com.mixel.docusphere.mapper;
|
package com.mixel.docusphere.mapper;
|
||||||
|
|
||||||
import com.mixel.docusphere.dto.UserDTO;
|
import com.mixel.docusphere.dto.request.UserRequestDTO;
|
||||||
|
import com.mixel.docusphere.dto.response.UserRespondDTO;
|
||||||
import com.mixel.docusphere.entity.User;
|
import com.mixel.docusphere.entity.User;
|
||||||
|
|
||||||
public class UserMapper {
|
public class UserMapper {
|
||||||
public static UserDTO toDTO(User user) {
|
public static UserRequestDTO toRequestDTO(User user) {
|
||||||
UserDTO userDTO = new UserDTO();
|
UserRequestDTO userRequestDTO = new UserRequestDTO();
|
||||||
userDTO.setUsername(user.getUsername());
|
userRequestDTO.setUsername(user.getUsername());
|
||||||
userDTO.setName(user.getName());
|
userRequestDTO.setName(user.getName());
|
||||||
userDTO.setEmail(user.getEmail());
|
userRequestDTO.setEmail(user.getEmail());
|
||||||
userDTO.setCreatedAt(user.getCreatedAt());
|
userRequestDTO.setCreatedAt(user.getCreatedAt());
|
||||||
userDTO.setUpdatedAt(user.getUpdatedAt());
|
userRequestDTO.setUpdatedAt(user.getUpdatedAt());
|
||||||
|
|
||||||
return userDTO;
|
return userRequestDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User toEntity(UserDTO userDTO) {
|
public static UserRespondDTO toRespondDTO(User user) {
|
||||||
|
UserRespondDTO userRespondDTO = new UserRespondDTO();
|
||||||
|
userRespondDTO.setUserId(user.getUserId());
|
||||||
|
userRespondDTO.setUsername(user.getUsername());
|
||||||
|
userRespondDTO.setName(user.getName());
|
||||||
|
userRespondDTO.setEmail(user.getEmail());
|
||||||
|
userRespondDTO.setCreatedAt(user.getCreatedAt());
|
||||||
|
userRespondDTO.setUpdatedAt(user.getUpdatedAt());
|
||||||
|
|
||||||
|
return userRespondDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static User toEntity(UserRequestDTO userRequestDTO) {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setUsername(userDTO.getUsername());
|
user.setUsername(userRequestDTO.getUsername());
|
||||||
user.setName(userDTO.getName());
|
user.setName(userRequestDTO.getName());
|
||||||
user.setEmail(userDTO.getEmail());
|
user.setEmail(userRequestDTO.getEmail());
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.mixel.docusphere.service;
|
package com.mixel.docusphere.service;
|
||||||
|
|
||||||
import com.mixel.docusphere.dto.UserDTO;
|
import com.mixel.docusphere.dto.request.UserRequestDTO;
|
||||||
|
import com.mixel.docusphere.dto.response.UserRespondDTO;
|
||||||
import com.mixel.docusphere.entity.User;
|
import com.mixel.docusphere.entity.User;
|
||||||
import com.mixel.docusphere.mapper.UserMapper;
|
import com.mixel.docusphere.mapper.UserMapper;
|
||||||
import com.mixel.docusphere.repository.UserRepository;
|
import com.mixel.docusphere.repository.UserRepository;
|
||||||
|
@ -25,40 +26,40 @@ public class UserService {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UserDTO> findAll() {
|
public List<UserRespondDTO> findAll() {
|
||||||
return userRepository.findAll().stream()
|
return userRepository.findAll().stream()
|
||||||
.map(UserMapper::toDTO)
|
.map(UserMapper::toRespondDTO)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<UserDTO> findById(UUID id) {
|
public Optional<UserRespondDTO> findById(UUID id) {
|
||||||
return userRepository.findById(id)
|
return userRepository.findById(id)
|
||||||
.map(UserMapper::toDTO);
|
.map(UserMapper::toRespondDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserDTO save(UserDTO userDTO) {
|
public UserRespondDTO save(UserRequestDTO userRequestDTO) {
|
||||||
User user = UserMapper.toEntity(userDTO);
|
User user = UserMapper.toEntity(userRequestDTO);
|
||||||
isPasswordAlreadySet(userDTO, user);
|
isPasswordAlreadySet(userRequestDTO, user);
|
||||||
return UserMapper.toDTO(userRepository.save(user));
|
return UserMapper.toRespondDTO(userRepository.save(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<UserDTO> update(UUID id, UserDTO userDTO){
|
public Optional<UserRespondDTO> update(UUID id, UserRequestDTO userRequestDTO){
|
||||||
Optional<User> userOptional = userRepository.findById(id);
|
Optional<User> userOptional = userRepository.findById(id);
|
||||||
if (userOptional.isPresent()) {
|
if (userOptional.isPresent()) {
|
||||||
User user = userOptional.get();
|
User user = userOptional.get();
|
||||||
user.setUsername(userDTO.getUsername());
|
user.setUsername(userRequestDTO.getUsername());
|
||||||
user.setName(userDTO.getName());
|
user.setName(userRequestDTO.getName());
|
||||||
user.setEmail(userDTO.getEmail());
|
user.setEmail(userRequestDTO.getEmail());
|
||||||
|
|
||||||
isPasswordAlreadySet(userDTO, user);
|
isPasswordAlreadySet(userRequestDTO, user);
|
||||||
return Optional.of(UserMapper.toDTO(userRepository.save(user)));
|
return Optional.of(UserMapper.toRespondDTO(userRepository.save(user)));
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void isPasswordAlreadySet(UserDTO userDTO, User user) {
|
private void isPasswordAlreadySet(UserRequestDTO userRequestDTO, User user) {
|
||||||
if (userDTO.getPassword() != null && !userDTO.getPassword().isEmpty()) {
|
if (userRequestDTO.getPassword() != null && !userRequestDTO.getPassword().isEmpty()) {
|
||||||
user.setPasswordHash(passwordEncoder.encode(userDTO.getPassword()));
|
user.setPasswordHash(passwordEncoder.encode(userRequestDTO.getPassword()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue