Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

@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);
}
}
18 changes: 18 additions & 0 deletions src/main/java/school/faang/user_service/dto/UserDto.java
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;
@NotEmpty(message = "Username is required")
private String username;
@Email(message = "Email must be valid")
private String email;
}
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);
}
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) {
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<UserDto> getParticipants(long eventId) {
return eventParticipationRepository
.findAllParticipantsByEventId(eventId)
.stream()
.map(participantMapper::toDto)
.collect(Collectors.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);
}
}