-
Notifications
You must be signed in to change notification settings - Fork 71
BJS2-77373 | Участвовать в событии #2952
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
Merged
Changes from 1 commit
Commits
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
43 changes: 43 additions & 0 deletions
43
src/main/java/school/faang/user_service/conrtoller/event/EventParticipationController.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,43 @@ | ||
| package school.faang.user_service.conrtoller.event; | ||
|
|
||
| import jakarta.validation.constraints.Positive; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import school.faang.user_service.dto.UserDto; | ||
| import school.faang.user_service.service.EventParticipationService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Validated | ||
| @RestController | ||
| @RequestMapping("/events") | ||
| @RequiredArgsConstructor | ||
| public class EventParticipationController { | ||
| private final EventParticipationService eventParticipationService; | ||
|
|
||
| @PostMapping("/{eventId}/participants/{userId}") | ||
| public void registerParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) { | ||
| eventParticipationService.registerParticipant(eventId,userId); | ||
AlexeyAntipin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @DeleteMapping("/{eventId}/participants/{userId}") | ||
| public void unregisterParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) { | ||
| eventParticipationService.unregisterParticipant(eventId, userId); | ||
| } | ||
|
|
||
| @GetMapping("{eventId}/participants") | ||
| public List<UserDto> getParticipants(@PathVariable @Positive long eventId) { | ||
| return eventParticipationService.getParticipants(eventId); | ||
| } | ||
|
|
||
| @GetMapping("{eventId}/participants/count") | ||
| public Integer getParticipantsCount(@PathVariable @Positive long eventId) { | ||
| return eventParticipationService.getParticipantsCount(eventId); | ||
| } | ||
| } | ||
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,18 @@ | ||
| package school.faang.user_service.dto; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Positive; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class UserDto { | ||
| @NotNull(message = "ID is required") | ||
| @Positive(message = "ID must be positive") | ||
| private long id; | ||
AlexeyAntipin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @NotEmpty(message = "Username is required") | ||
| private String username; | ||
| @Email(message = "Email must be valid") | ||
| private String email; | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/main/java/school/faang/user_service/mapper/ParticipantMapper.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,10 @@ | ||
| package school.faang.user_service.mapper; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import school.faang.user_service.dto.UserDto; | ||
| import school.faang.user_service.entity.User; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| public interface ParticipantMapper { | ||
| UserDto toDto(User user); | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/school/faang/user_service/service/EventParticipationService.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,54 @@ | ||
| package school.faang.user_service.service; | ||
|
|
||
| import com.sun.jdi.request.DuplicateRequestException; | ||
| import jakarta.transaction.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import school.faang.user_service.dto.UserDto; | ||
| import school.faang.user_service.mapper.ParticipantMapper; | ||
| import school.faang.user_service.repository.event.EventParticipationRepository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class EventParticipationService { | ||
| private final EventParticipationRepository eventParticipationRepository; | ||
| private final ParticipantMapper participantMapper; | ||
|
|
||
| @Transactional | ||
| public void registerParticipant(long eventId, long userId) { | ||
AlexeyAntipin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (isParticipantRegistered(eventId, userId)) { | ||
| throw new DuplicateRequestException("User is already registered for this event"); | ||
AlexeyAntipin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| eventParticipationRepository.register(eventId, userId); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void unregisterParticipant(long eventId, long userId) { | ||
AlexeyAntipin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (isParticipantRegistered(eventId, userId)) { | ||
| throw new IllegalStateException("User is not registered for this event"); | ||
| } | ||
| eventParticipationRepository.unregister(eventId, userId); | ||
| } | ||
|
|
||
| public List<UserDto> getParticipants(long eventId) { | ||
| return eventParticipationRepository | ||
| .findAllParticipantsByEventId(eventId) | ||
| .stream() | ||
| .map(participantMapper::toDto) | ||
| .collect(Collectors.toList()); | ||
AlexeyAntipin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public Integer getParticipantsCount(long eventId) { | ||
| return eventParticipationRepository.countParticipants(eventId); | ||
| } | ||
|
|
||
| private boolean isParticipantRegistered(long eventId, long userId) { | ||
| return eventParticipationRepository | ||
| .findAllParticipantsByEventId(eventId) | ||
| .stream() | ||
| .anyMatch(user -> user.getId() == userId); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.