-
Notifications
You must be signed in to change notification settings - Fork 71
BJS2-77992. Добавлено создание цели #2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
20cognosce
merged 16 commits into
hippogriff-master-stream10
from
feature/BJS2-77992-add-goal
May 22, 2025
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f529c50
BJS2-77992. Добавлено создание цели
FROLOV-KI fbf4321
BJS2-77992. Обновлено создание цели
FROLOV-KI 972ce80
BJS2-77992. Добавлено обновление цели
FROLOV-KI 323e88d
BJS2-77992. Добавлено удаление цели
FROLOV-KI 9d1d6f2
BJS2-77992. Удаление цели обновлено
FROLOV-KI 5e767bb
BJS2-77992. Комментарии к ПР
FROLOV-KI 91a5a5b
BJS2-77992. Обновил маппер на использование MapStruct
FROLOV-KI e081b0e
BJS2-77992. Добавил получения списка целей и списка подцелей с фильтрами
FROLOV-KI 242860e
BJS2-77992. Обновлен сервис Целей
FROLOV-KI 4a7c384
Merge branch 'hippogriff-master-stream10' into feature/BJS2-77992-add…
FROLOV-KI d095715
BJS2-77992. Обновлен сервис Целей
FROLOV-KI b4ce6a0
Merge branch 'hippogriff-master-stream10' into feature/BJS2-77992-add…
FROLOV-KI c2148de
BJS2-77992. Обновил фильтры
FROLOV-KI 38546a0
BJS2-77992. Удалил неиспользуемый метод
FROLOV-KI ac6a6e1
BJS2-77992. Код стайл
FROLOV-KI 1b05279
Merge branch 'hippogriff-master-stream10' into feature/BJS2-77992-add…
FROLOV-KI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/school/faang/user_service/controller/goal/GoalController.java
This file contains hidden or 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,47 @@ | ||
| package school.faang.user_service.controller.goal; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| 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 school.faang.user_service.entity.goal.Goal; | ||
| import school.faang.user_service.entity.goal.dto.CreateGoalDto; | ||
| import school.faang.user_service.entity.goal.dto.UpdateGoalDto; | ||
| import school.faang.user_service.entity.goal.mapper.CreateGoalMapperImpl; | ||
| import school.faang.user_service.entity.goal.mapper.UpdateGoalMapperImpl; | ||
| import school.faang.user_service.service.GoalService; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/goal") | ||
| @RequiredArgsConstructor | ||
| public class GoalController { | ||
|
|
||
| private final GoalService goalService; | ||
| private final CreateGoalMapperImpl createGoalMapper; | ||
| private final UpdateGoalMapperImpl updateGoalMapper; | ||
|
|
||
| @PostMapping("/create") | ||
| public ResponseEntity<CreateGoalDto> createGoal(@Valid @RequestBody CreateGoalDto createGoalDto) { | ||
| Goal createdGoal = goalService.createGoal( | ||
| createGoalMapper.dtoToGoal(createGoalDto), | ||
| createGoalDto.skillsId(), | ||
| createGoalDto.parent() | ||
| ); | ||
| return ResponseEntity.ok(createGoalMapper.goalToDto(createdGoal)); | ||
| } | ||
|
|
||
| @PutMapping("/update/{goalId}") | ||
|
||
| public ResponseEntity<UpdateGoalDto> updateGoal(@PathVariable long goalId, @Valid @RequestBody UpdateGoalDto updateGoalDto) { | ||
| Goal createdGoal = goalService.update( | ||
| goalId, | ||
| updateGoalMapper.dtoToGoal(updateGoalDto), | ||
| updateGoalDto.skillsId() | ||
| ); | ||
| return ResponseEntity.ok(updateGoalMapper.goalToDto(createdGoal)); | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
src/main/java/school/faang/user_service/entity/error/DefaultErrorResponse.java
This file contains hidden or 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,3 @@ | ||
| package school.faang.user_service.entity.error; | ||
|
|
||
| public record DefaultErrorResponse(String code, String message) {} |
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
src/main/java/school/faang/user_service/entity/goal/dto/CreateGoalDto.java
This file contains hidden or 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 school.faang.user_service.entity.goal.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record CreateGoalDto( | ||
| Long id, | ||
| @NotBlank(message = "Empty goal title not allowed!") String title, | ||
| String description, | ||
| Long parent, | ||
| List<Long> skillsId | ||
| ) {} |
15 changes: 15 additions & 0 deletions
15
src/main/java/school/faang/user_service/entity/goal/dto/UpdateGoalDto.java
This file contains hidden or 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 school.faang.user_service.entity.goal.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import school.faang.user_service.entity.goal.GoalStatus; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record UpdateGoalDto( | ||
| @NotBlank(message = "Empty goal title not allowed!") String title, | ||
| String description, | ||
| GoalStatus status, | ||
| List<Long> skillsId | ||
| ) {} |
33 changes: 33 additions & 0 deletions
33
src/main/java/school/faang/user_service/entity/goal/mapper/CreateGoalMapperImpl.java
This file contains hidden or 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,33 @@ | ||
| package school.faang.user_service.entity.goal.mapper; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import school.faang.user_service.entity.Skill; | ||
| import school.faang.user_service.entity.goal.Goal; | ||
| import school.faang.user_service.entity.goal.dto.CreateGoalDto; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CreateGoalMapperImpl implements GoalMapper<CreateGoalDto> { | ||
|
|
||
| @Override | ||
| public Goal dtoToGoal(CreateGoalDto createGoalDto) { | ||
| Goal goal = new Goal(); | ||
| goal.setId(createGoalDto.id()); | ||
| goal.setTitle(createGoalDto.title()); | ||
| goal.setDescription(createGoalDto.description()); | ||
| return goal; | ||
| } | ||
|
|
||
| @Override | ||
| public CreateGoalDto goalToDto(Goal goal) { | ||
| return new CreateGoalDto( | ||
| goal.getId(), | ||
| goal.getTitle(), | ||
| goal.getDescription(), | ||
| goal.getParent() != null ? goal.getParent().getId() : null, | ||
| goal.getSkillsToAchieve().stream().map(Skill::getId).toList() | ||
| ); | ||
| } | ||
|
|
||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/school/faang/user_service/entity/goal/mapper/GoalMapper.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.entity.goal.mapper; | ||
|
|
||
| import school.faang.user_service.entity.goal.Goal; | ||
|
|
||
| public interface GoalMapper<T> { | ||
| Goal dtoToGoal(T createGoalDto); | ||
| T goalToDto(Goal goal); | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/school/faang/user_service/entity/goal/mapper/UpdateGoalMapperImpl.java
This file contains hidden or 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 school.faang.user_service.entity.goal.mapper; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import school.faang.user_service.entity.Skill; | ||
| import school.faang.user_service.entity.goal.Goal; | ||
| import school.faang.user_service.entity.goal.dto.UpdateGoalDto; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class UpdateGoalMapperImpl implements GoalMapper<UpdateGoalDto> { | ||
|
|
||
| @Override | ||
| public Goal dtoToGoal(UpdateGoalDto updateGoalDto) { | ||
| Goal goal = new Goal(); | ||
| goal.setTitle(updateGoalDto.title()); | ||
| goal.setDescription(updateGoalDto.description()); | ||
| goal.setStatus(updateGoalDto.status()); | ||
| return goal; | ||
| } | ||
|
|
||
| @Override | ||
| public UpdateGoalDto goalToDto(Goal goal) { | ||
| return new UpdateGoalDto( | ||
| goal.getTitle(), | ||
| goal.getDescription(), | ||
| goal.getStatus(), | ||
| goal.getSkillsToAchieve().stream().map(Skill::getId).toList() | ||
| ); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/school/faang/user_service/exception/goal/GoalNotExistException.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.exception.goal; | ||
|
|
||
| public class GoalNotExistException extends RuntimeException { | ||
|
|
||
| public GoalNotExistException(long goalId) { | ||
| super("Goal with id %d not exist".formatted(goalId)); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/school/faang/user_service/exception/goal/MaxActiveGoalPerUserException.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.exception.goal; | ||
|
|
||
| public class MaxActiveGoalPerUserException extends RuntimeException { | ||
|
|
||
| public MaxActiveGoalPerUserException(Long userId, int goalLimit) { | ||
| super("User with id %s reach max goals active limit of %d".formatted(userId, goalLimit)); | ||
20cognosce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
src/main/java/school/faang/user_service/exception/goal/UpdateComleteGoalException.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.exception.goal; | ||
|
|
||
| public class UpdateComleteGoalException extends RuntimeException { | ||
|
|
||
| public UpdateComleteGoalException(long goalId) { | ||
| super("Goal with id %d is complete. Update not allowed.".formatted(goalId)); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
.../java/school/faang/user_service/exception/goal/UpdateGoalWithActiveSubGoalsException.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.exception.goal; | ||
|
|
||
| public class UpdateGoalWithActiveSubGoalsException extends RuntimeException { | ||
|
|
||
| public UpdateGoalWithActiveSubGoalsException(long goalId, String activeSubGoalsId) { | ||
| super("Goal with id %d have active sub goals [%s]. Update not allowed.".formatted(goalId, activeSubGoalsId)); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/school/faang/user_service/exception/skill/AddedSkillNotExistException.java
This file contains hidden or 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,8 @@ | ||
| package school.faang.user_service.exception.skill; | ||
|
|
||
| public class AddedSkillNotExistException extends RuntimeException { | ||
|
|
||
| public AddedSkillNotExistException(String skillsId) { | ||
| super("Contains skills [%s] that not exist!!".formatted(skillsId)); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
src/main/java/school/faang/user_service/service/GoalService.java
This file contains hidden or 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,88 @@ | ||
| package school.faang.user_service.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import school.faang.user_service.config.context.UserContext; | ||
| import school.faang.user_service.entity.Skill; | ||
| import school.faang.user_service.entity.goal.Goal; | ||
| import school.faang.user_service.entity.goal.GoalStatus; | ||
| import school.faang.user_service.exception.goal.GoalNotExistException; | ||
| import school.faang.user_service.repository.SkillRepository; | ||
| import school.faang.user_service.repository.goal.GoalRepository; | ||
| import school.faang.user_service.validator.goal.GoalValidator; | ||
| import school.faang.user_service.validator.goal.SkillValidator; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GoalService { | ||
|
|
||
| private final GoalRepository goalRepository; | ||
| private final SkillRepository skillRepository; | ||
|
|
||
| private final UserContext userContext; | ||
|
|
||
| private final GoalValidator goalValidator; | ||
| private final SkillValidator skillValidator; | ||
|
|
||
| private final SkillService skillService; | ||
|
|
||
| @Transactional | ||
| public Goal createGoal(Goal newGoalData, List<Long> skillsId, Long parentId) { | ||
| long userId = userContext.getUserId(); | ||
| goalValidator.validateMaxActiveGoalLimit(goalRepository.countActiveGoalsPerUser(userId)); | ||
|
|
||
| if (parentId != null) { | ||
| goalRepository.findById(parentId).orElseThrow(() -> new GoalNotExistException(parentId)); | ||
| } | ||
| Goal newGoal = goalRepository.create(newGoalData.getTitle(), newGoalData.getDescription(), parentId); | ||
FROLOV-KI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| goalRepository.assignGoalToUser(userId, newGoal.getId()); | ||
|
|
||
| if (!skillsId.isEmpty()) { | ||
| skillService.assignSkillToGoal(newGoal.getId(), skillsId); | ||
| } | ||
| return newGoal; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Goal update(long goalId, Goal newGoalData, List<Long> skillsId) { | ||
| Goal dbGoal = goalRepository.findById(goalId).orElseThrow(() -> new GoalNotExistException(goalId)); | ||
| goalValidator.validateUpdateCompleteGoal(dbGoal); | ||
| skillValidator.validateExistingSkills( | ||
| skillsId.stream() | ||
| .filter(skillId -> !skillRepository.existsById(skillId)) | ||
| .toList() | ||
| ); | ||
|
|
||
| dbGoal.setTitle(newGoalData.getTitle()); | ||
| dbGoal.setDescription(newGoalData.getDescription()); | ||
| dbGoal.setStatus(newGoalData.getStatus()); | ||
|
|
||
| boolean skillsChanged = !dbGoal.getSkillsToAchieve() | ||
| .stream() | ||
| .map(Skill::getId) | ||
| .toList() | ||
| .equals(skillsId); | ||
FROLOV-KI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (skillsChanged) { | ||
|
|
||
| skillService.updateSkillForGoal(goalId, skillsId); | ||
| dbGoal.setSkillsToAchieve(skillRepository.findSkillsByGoalId(goalId)); | ||
| } | ||
|
|
||
| if (dbGoal.getStatus().equals(GoalStatus.COMPLETED)) { | ||
FROLOV-KI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| List<Long> involvedUsersId = goalRepository.findUsersByGoalId(dbGoal.getId()); | ||
|
|
||
| goalValidator.validateAllSubGoalsCompleted(goalId, goalRepository.findByParent(goalId)); | ||
|
|
||
| involvedUsersId.forEach(userId -> | ||
| skillService.assignSkillsToUser(userId, dbGoal.getSkillsToAchieve())); | ||
| } | ||
|
|
||
| goalRepository.save(dbGoal); | ||
|
|
||
| return dbGoal; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PostMapping самодостаточный для create, его лучше без параметров оставить, и по этой url /api/v1/goal делать create