-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alina Tarasova
committed
Aug 18, 2024
1 parent
5fcaf9f
commit d63ba78
Showing
9 changed files
with
543 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/hexlet/code/controller/UserTaskController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package hexlet.code.controller; | ||
|
||
import hexlet.code.dto.tasks.TaskCreateDTO; | ||
import hexlet.code.dto.tasks.TaskDTO; | ||
import hexlet.code.dto.users.UserCreateDTO; | ||
import hexlet.code.dto.users.UserDTO; | ||
import hexlet.code.dto.userstasks.UserTaskUpdateDTO; | ||
import hexlet.code.service.UserTaskService; | ||
import jakarta.validation.Valid; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
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.PathVariable; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/api") | ||
@AllArgsConstructor | ||
public class UserTaskController { | ||
private final UserTaskService userTaskService; | ||
|
||
|
||
//@PostMapping(path = "/create/user") | ||
@GetMapping(path = "/create/user") | ||
//@ResponseStatus(HttpStatus.CREATED) | ||
public UserDTO create(@Valid @RequestBody UserCreateDTO taskCreateDTO) { | ||
return userTaskService.createUser(taskCreateDTO); | ||
} | ||
|
||
//@PostMapping(path = "/create/task") | ||
@GetMapping(path = "/create/task") | ||
//@ResponseStatus(HttpStatus.CREATED) | ||
public TaskDTO create(@Valid @RequestBody TaskCreateDTO taskCreateDTO) { | ||
return userTaskService.createTask(taskCreateDTO); | ||
} | ||
|
||
// @PutMapping("/user/{userId}/task/{taskId}") | ||
// @ResponseStatus(HttpStatus.OK) | ||
// public void updateUserAndTask(@PathVariable Long userId, | ||
// @PathVariable Long taskId, | ||
// @RequestBody UserWithTaskDTO userTaskDTO) { | ||
// userTaskService.updateUserAndTask(userId, taskId, userTaskDTO); | ||
// } | ||
|
||
@PutMapping("/user/{userId}/task/{taskId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public void updateUserAndTask(@PathVariable Long userId, | ||
@PathVariable Long taskId, | ||
@RequestBody UserTaskUpdateDTO userTaskDTO) { | ||
userTaskService.updateUserAndTask(userId, taskId, userTaskDTO); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/hexlet/code/dto/userstasks/UserTaskUpdateDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hexlet.code.dto.userstasks; | ||
|
||
import hexlet.code.dto.tasks.TaskUpdateDTO; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.openapitools.jackson.nullable.JsonNullable; | ||
|
||
|
||
@Getter | ||
@Setter | ||
public class UserTaskUpdateDTO { | ||
|
||
@NotBlank | ||
private JsonNullable<String> email; | ||
|
||
@NotBlank | ||
private JsonNullable<String> firstName; | ||
|
||
@NotBlank | ||
private JsonNullable<String> lastName; | ||
|
||
@NotBlank | ||
@Size(min = 3) | ||
private JsonNullable<String> password; | ||
|
||
private TaskUpdateDTO taskUpdateDTO; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/hexlet/code/dto/userstasks/UserWithTaskDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package hexlet.code.dto.userstasks; | ||
|
||
import hexlet.code.dto.tasks.TaskDTO; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class UserWithTaskDTO { | ||
private long id; | ||
private String email; | ||
private String firstName; | ||
private String lastName; | ||
private TaskDTO taskDTO; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package hexlet.code.mapper; | ||
|
||
import hexlet.code.dto.tasks.TaskDTO; | ||
import hexlet.code.dto.userstasks.UserWithTaskDTO; | ||
import hexlet.code.exception.ResourceNotFoundException; | ||
import hexlet.code.model.Task; | ||
import hexlet.code.model.TaskStatus; | ||
import hexlet.code.model.User; | ||
import hexlet.code.repository.TaskStatusRepository; | ||
import hexlet.code.repository.UserRepository; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingConstants; | ||
import org.mapstruct.NullValuePropertyMappingStrategy; | ||
import org.mapstruct.ReportingPolicy; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
|
||
@Mapper( | ||
uses = {JsonNullableMapper.class, ReferenceMapper.class}, | ||
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, | ||
componentModel = MappingConstants.ComponentModel.SPRING, | ||
unmappedTargetPolicy = ReportingPolicy.IGNORE | ||
) | ||
public abstract class UserTaskMapper { | ||
@Autowired | ||
private TaskStatusRepository taskStatusRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
public User map(UserWithTaskDTO userTaskDTO) { | ||
User user = new User(); | ||
user.setId(userTaskDTO.getId()); | ||
user.setFirstName(userTaskDTO.getFirstName()); | ||
user.setLastName(userTaskDTO.getLastName()); | ||
user.setEmail(userTaskDTO.getEmail()); | ||
user.getTasks().add(map(userTaskDTO.getTaskDTO())); | ||
return user; | ||
} | ||
|
||
public Task map(TaskDTO taskDTO) { | ||
Task task = new Task(); | ||
task.setId(taskDTO.getId()); | ||
task.setName(taskDTO.getTitle()); | ||
task.setDescription(taskDTO.getContent()); | ||
task.setTaskStatus(slugToTaskStatus(taskDTO.getStatus())); | ||
task.setAssignee(idToAssignee(taskDTO.getAssigneeId())); | ||
return task; | ||
} | ||
|
||
private TaskStatus slugToTaskStatus(String slug) { | ||
return taskStatusRepository.findBySlugWithEagerUpload(slug).orElseThrow( | ||
() -> new ResourceNotFoundException("TaskStatus with slug " + slug + " not found")); | ||
} | ||
|
||
private User idToAssignee(Long assigneeId) { | ||
return userRepository.findById(assigneeId).orElseThrow( | ||
() -> new ResourceNotFoundException("User with id " + assigneeId + " not found")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package hexlet.code.service; | ||
|
||
import hexlet.code.dto.tasks.TaskCreateDTO; | ||
import hexlet.code.dto.tasks.TaskDTO; | ||
import hexlet.code.dto.tasks.TaskUpdateDTO; | ||
import hexlet.code.dto.users.UserCreateDTO; | ||
import hexlet.code.dto.users.UserDTO; | ||
import hexlet.code.dto.users.UserUpdateDTO; | ||
import hexlet.code.dto.userstasks.UserTaskUpdateDTO; | ||
import hexlet.code.exception.ResourceNotFoundException; | ||
import hexlet.code.mapper.TaskMapper; | ||
import hexlet.code.mapper.UserMapper; | ||
import hexlet.code.mapper.UserTaskMapper; | ||
import hexlet.code.model.Task; | ||
import hexlet.code.model.User; | ||
import hexlet.code.repository.TaskRepository; | ||
import hexlet.code.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
|
||
@Service | ||
@AllArgsConstructor | ||
public class UserTaskService { | ||
private final UserRepository userRepository; | ||
private final TaskRepository taskRepository; | ||
private final UserMapper userMapper; | ||
private final TaskMapper taskMapper; | ||
private final UserTaskMapper mapper; | ||
|
||
|
||
public UserDTO createUser(UserCreateDTO userCreateDTO) { | ||
var user = userMapper.map(userCreateDTO); | ||
userRepository.save(user); | ||
return userMapper.map(user); | ||
} | ||
|
||
|
||
public TaskDTO createTask(TaskCreateDTO taskCreateDTO) { | ||
var task = taskMapper.map(taskCreateDTO); | ||
taskRepository.save(task); | ||
return taskMapper.map(task); | ||
} | ||
|
||
@Transactional | ||
public void updateUserAndTask(Long userId, Long taskId, UserTaskUpdateDTO userTaskUpdateDTO) { | ||
|
||
User user = userRepository.findByIdWithEagerUpload(userId).orElseThrow( | ||
() -> new ResourceNotFoundException("User With Id: " + userId + " Not Found")); | ||
|
||
Task task = taskRepository.findByIdWithEagerUpload(taskId).orElseThrow( | ||
() -> new ResourceNotFoundException("Task With Id: " + taskId + " Not Found")); | ||
|
||
UserUpdateDTO userUpdateDTO = new UserUpdateDTO(); | ||
userUpdateDTO.setLastName(userTaskUpdateDTO.getLastName()); | ||
|
||
TaskUpdateDTO taskUpdateDTO = userTaskUpdateDTO.getTaskUpdateDTO(); | ||
|
||
userMapper.update(userUpdateDTO, user); | ||
taskMapper.update(taskUpdateDTO, task); | ||
|
||
user.addTask(task); // MERGE | ||
userRepository.save(user); | ||
|
||
ResponseEntity.ok().build(); | ||
} | ||
|
||
|
||
// public void updateUserAndTask(Long userId, Long taskId, UserWithTaskDTO userTaskDTO) { | ||
// | ||
// User user = userRepository.findByIdWithEagerUpload(userId).orElseThrow( | ||
// () -> new ResourceNotFoundException("User With Id: " + userId + " Not Found")); | ||
// | ||
// Task task = taskRepository.findByIdWithEagerUpload(taskId).orElseThrow( | ||
// () -> new ResourceNotFoundException("Task With Id: " + taskId + " Not Found")); | ||
// | ||
// user.setLastName(userTaskDTO.getLastName()); | ||
// task.setName(userTaskDTO.getTaskDTO().getTitle()); | ||
// | ||
//// User updatedUser = mapper.map(userTaskDTO); | ||
//// Task updatedTask = mapper.map(userTaskDTO.getTaskDTO()); | ||
//// user.setLastName(updatedUser.getLastName()); | ||
//// task.setName(updatedTask.getName()); | ||
// | ||
// user.addTask(task); // MERGE | ||
// userRepository.save(user); | ||
// | ||
// ResponseEntity.ok().build(); | ||
// } | ||
} |
Oops, something went wrong.