diff --git a/src/main/java/school/faang/user_service/conrtoller/event/EventParticipationController.java b/src/main/java/school/faang/user_service/conrtoller/event/EventParticipationController.java new file mode 100644 index 0000000000..a14cb97def --- /dev/null +++ b/src/main/java/school/faang/user_service/conrtoller/event/EventParticipationController.java @@ -0,0 +1,41 @@ +package school.faang.user_service.conrtoller.event; + +import jakarta.validation.constraints.Positive; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import school.faang.user_service.dto.UserDto; +import school.faang.user_service.service.EventParticipationService; + +import java.util.List; + +@Validated +@RestController +@RequestMapping("/events/{eventId}") +@RequiredArgsConstructor +public class EventParticipationController { + private final EventParticipationService eventParticipationService; + + @ResponseStatus(HttpStatus.NO_CONTENT) + @PostMapping("/participants/{userId}") + public void registerParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) { + eventParticipationService.registerParticipant(eventId, userId); + } + + @ResponseStatus(HttpStatus.NO_CONTENT) + @DeleteMapping("/participants/{userId}") + public void unregisterParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) { + eventParticipationService.unregisterParticipant(eventId, userId); + } + + @GetMapping("/participants") + public List getParticipants(@PathVariable @Positive long eventId) { + return eventParticipationService.getParticipants(eventId); + } + + @GetMapping("/participants/count") + public Integer getParticipantsCount(@PathVariable @Positive long eventId) { + return eventParticipationService.getParticipantsCount(eventId); + } +} \ No newline at end of file diff --git a/src/main/java/school/faang/user_service/dto/UserDto.java b/src/main/java/school/faang/user_service/dto/UserDto.java index 8a48cbb5ee..e298049822 100644 --- a/src/main/java/school/faang/user_service/dto/UserDto.java +++ b/src/main/java/school/faang/user_service/dto/UserDto.java @@ -11,4 +11,4 @@ public class UserDto { private Long id; private String username; private String email; -} \ No newline at end of file +} diff --git a/src/main/java/school/faang/user_service/mapper/ParticipantMapper.java b/src/main/java/school/faang/user_service/mapper/ParticipantMapper.java new file mode 100644 index 0000000000..79ead1b49b --- /dev/null +++ b/src/main/java/school/faang/user_service/mapper/ParticipantMapper.java @@ -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); +} diff --git a/src/main/java/school/faang/user_service/service/EventParticipationService.java b/src/main/java/school/faang/user_service/service/EventParticipationService.java new file mode 100644 index 0000000000..8c3d21f719 --- /dev/null +++ b/src/main/java/school/faang/user_service/service/EventParticipationService.java @@ -0,0 +1,53 @@ +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; + +@Service +@RequiredArgsConstructor +public class EventParticipationService { + private final EventParticipationRepository eventParticipationRepository; + private final ParticipantMapper participantMapper; + + @Transactional + public void registerParticipant(long eventId, long userId) { + if (isParticipantRegistered(eventId, userId)) { + throw new DuplicateRequestException("User is already registered for this event"); + } + eventParticipationRepository.register(eventId, userId); + } + + @Transactional + public void unregisterParticipant(long eventId, long userId) { + if (isParticipantRegistered(eventId, userId)) { + throw new IllegalStateException("User is not registered for this event"); + } + eventParticipationRepository.unregister(eventId, userId); + } + + public List getParticipants(long eventId) { + return eventParticipationRepository + .findAllParticipantsByEventId(eventId) + .stream() + .map(participantMapper::toDto) + .toList(); + } + + 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); + } +}