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;
|
||||
|
||||
import com.mixel.docusphere.dto.UserDTO;
|
||||
import com.mixel.docusphere.entity.User;
|
||||
import com.mixel.docusphere.dto.request.UserRequestDTO;
|
||||
import com.mixel.docusphere.dto.response.UserRespondDTO;
|
||||
import com.mixel.docusphere.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -23,24 +23,24 @@ public class UserController {
|
|||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDTO> getAllUsers() {
|
||||
public List<UserRespondDTO> getAllUsers() {
|
||||
return userService.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<UserDTO> getUserById(@PathVariable UUID id) {
|
||||
Optional<UserDTO> user = userService.findById(id);
|
||||
public ResponseEntity<UserRespondDTO> getUserById(@PathVariable UUID id) {
|
||||
Optional<UserRespondDTO> user = userService.findById(id);
|
||||
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public UserDTO createUser(@RequestBody UserDTO userDTO) {
|
||||
return userService.save(userDTO);
|
||||
public UserRespondDTO createUser(@RequestBody UserRequestDTO userRequestDTO) {
|
||||
return userService.save(userRequestDTO);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<UserDTO> updateUser(@PathVariable UUID id, @RequestBody UserDTO userDTO) {
|
||||
Optional<UserDTO> updatedUser = userService.update(id, userDTO);
|
||||
public ResponseEntity<UserRespondDTO> updateUser(@PathVariable UUID id, @RequestBody UserRequestDTO userRequestDTO) {
|
||||
Optional<UserRespondDTO> updatedUser = userService.update(id, userRequestDTO);
|
||||
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 description;
|
||||
private String s3Path;
|
||||
private String userId;
|
||||
private MultipartFile file;
|
||||
|
||||
// Getters and Setters
|
||||
public String getName() {
|
||||
|
@ -23,14 +25,6 @@ public class DocumentDTO {
|
|||
this.description = description;
|
||||
}
|
||||
|
||||
public String getS3Path() {
|
||||
return s3Path;
|
||||
}
|
||||
|
||||
public void setS3Path(String s3Path) {
|
||||
this.s3Path = s3Path;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
@ -38,4 +32,12 @@ public class DocumentDTO {
|
|||
public void setUserId(String 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;
|
||||
|
||||
public class UserDTO {
|
||||
public class UserRequestDTO {
|
||||
private String username;
|
||||
private String name;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
public class UserMapper {
|
||||
public static UserDTO toDTO(User user) {
|
||||
UserDTO userDTO = new UserDTO();
|
||||
userDTO.setUsername(user.getUsername());
|
||||
userDTO.setName(user.getName());
|
||||
userDTO.setEmail(user.getEmail());
|
||||
userDTO.setCreatedAt(user.getCreatedAt());
|
||||
userDTO.setUpdatedAt(user.getUpdatedAt());
|
||||
public static UserRequestDTO toRequestDTO(User user) {
|
||||
UserRequestDTO userRequestDTO = new UserRequestDTO();
|
||||
userRequestDTO.setUsername(user.getUsername());
|
||||
userRequestDTO.setName(user.getName());
|
||||
userRequestDTO.setEmail(user.getEmail());
|
||||
userRequestDTO.setCreatedAt(user.getCreatedAt());
|
||||
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.setUsername(userDTO.getUsername());
|
||||
user.setName(userDTO.getName());
|
||||
user.setEmail(userDTO.getEmail());
|
||||
user.setUsername(userRequestDTO.getUsername());
|
||||
user.setName(userRequestDTO.getName());
|
||||
user.setEmail(userRequestDTO.getEmail());
|
||||
|
||||
return user;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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.mapper.UserMapper;
|
||||
import com.mixel.docusphere.repository.UserRepository;
|
||||
|
@ -25,40 +26,40 @@ public class UserService {
|
|||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public List<UserDTO> findAll() {
|
||||
public List<UserRespondDTO> findAll() {
|
||||
return userRepository.findAll().stream()
|
||||
.map(UserMapper::toDTO)
|
||||
.map(UserMapper::toRespondDTO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Optional<UserDTO> findById(UUID id) {
|
||||
public Optional<UserRespondDTO> findById(UUID id) {
|
||||
return userRepository.findById(id)
|
||||
.map(UserMapper::toDTO);
|
||||
.map(UserMapper::toRespondDTO);
|
||||
}
|
||||
|
||||
public UserDTO save(UserDTO userDTO) {
|
||||
User user = UserMapper.toEntity(userDTO);
|
||||
isPasswordAlreadySet(userDTO, user);
|
||||
return UserMapper.toDTO(userRepository.save(user));
|
||||
public UserRespondDTO save(UserRequestDTO userRequestDTO) {
|
||||
User user = UserMapper.toEntity(userRequestDTO);
|
||||
isPasswordAlreadySet(userRequestDTO, 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);
|
||||
if (userOptional.isPresent()) {
|
||||
User user = userOptional.get();
|
||||
user.setUsername(userDTO.getUsername());
|
||||
user.setName(userDTO.getName());
|
||||
user.setEmail(userDTO.getEmail());
|
||||
user.setUsername(userRequestDTO.getUsername());
|
||||
user.setName(userRequestDTO.getName());
|
||||
user.setEmail(userRequestDTO.getEmail());
|
||||
|
||||
isPasswordAlreadySet(userDTO, user);
|
||||
return Optional.of(UserMapper.toDTO(userRepository.save(user)));
|
||||
isPasswordAlreadySet(userRequestDTO, user);
|
||||
return Optional.of(UserMapper.toRespondDTO(userRepository.save(user)));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private void isPasswordAlreadySet(UserDTO userDTO, User user) {
|
||||
if (userDTO.getPassword() != null && !userDTO.getPassword().isEmpty()) {
|
||||
user.setPasswordHash(passwordEncoder.encode(userDTO.getPassword()));
|
||||
private void isPasswordAlreadySet(UserRequestDTO userRequestDTO, User user) {
|
||||
if (userRequestDTO.getPassword() != null && !userRequestDTO.getPassword().isEmpty()) {
|
||||
user.setPasswordHash(passwordEncoder.encode(userRequestDTO.getPassword()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue