Skip to content

Commit dc855ca

Browse files
JolyGoldenJolyGoldenKateryna
authored
BJS2-77373 | Участвовать в событии (#2952)
* create CRUD operations for event participants * pr fixes * pr fixes --------- Co-authored-by: JolyGolden <[email protected]> Co-authored-by: Kateryna <[email protected]>
1 parent a26ab3a commit dc855ca

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package school.faang.user_service.conrtoller.event;
2+
3+
import jakarta.validation.constraints.Positive;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.validation.annotation.Validated;
7+
import org.springframework.web.bind.annotation.*;
8+
import school.faang.user_service.dto.UserDto;
9+
import school.faang.user_service.service.EventParticipationService;
10+
11+
import java.util.List;
12+
13+
@Validated
14+
@RestController
15+
@RequestMapping("/events/{eventId}")
16+
@RequiredArgsConstructor
17+
public class EventParticipationController {
18+
private final EventParticipationService eventParticipationService;
19+
20+
@ResponseStatus(HttpStatus.NO_CONTENT)
21+
@PostMapping("/participants/{userId}")
22+
public void registerParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) {
23+
eventParticipationService.registerParticipant(eventId, userId);
24+
}
25+
26+
@ResponseStatus(HttpStatus.NO_CONTENT)
27+
@DeleteMapping("/participants/{userId}")
28+
public void unregisterParticipant(@PathVariable @Positive long eventId, @PathVariable @Positive long userId) {
29+
eventParticipationService.unregisterParticipant(eventId, userId);
30+
}
31+
32+
@GetMapping("/participants")
33+
public List<UserDto> getParticipants(@PathVariable @Positive long eventId) {
34+
return eventParticipationService.getParticipants(eventId);
35+
}
36+
37+
@GetMapping("/participants/count")
38+
public Integer getParticipantsCount(@PathVariable @Positive long eventId) {
39+
return eventParticipationService.getParticipantsCount(eventId);
40+
}
41+
}

src/main/java/school/faang/user_service/dto/UserDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ public class UserDto {
1111
private Long id;
1212
private String username;
1313
private String email;
14-
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package school.faang.user_service.mapper;
2+
3+
import org.mapstruct.Mapper;
4+
import school.faang.user_service.dto.UserDto;
5+
import school.faang.user_service.entity.User;
6+
7+
@Mapper(componentModel = "spring")
8+
public interface ParticipantMapper {
9+
UserDto toDto(User user);
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package school.faang.user_service.service;
2+
3+
import com.sun.jdi.request.DuplicateRequestException;
4+
import jakarta.transaction.Transactional;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Service;
7+
import school.faang.user_service.dto.UserDto;
8+
import school.faang.user_service.mapper.ParticipantMapper;
9+
import school.faang.user_service.repository.event.EventParticipationRepository;
10+
11+
import java.util.List;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
public class EventParticipationService {
16+
private final EventParticipationRepository eventParticipationRepository;
17+
private final ParticipantMapper participantMapper;
18+
19+
@Transactional
20+
public void registerParticipant(long eventId, long userId) {
21+
if (isParticipantRegistered(eventId, userId)) {
22+
throw new DuplicateRequestException("User is already registered for this event");
23+
}
24+
eventParticipationRepository.register(eventId, userId);
25+
}
26+
27+
@Transactional
28+
public void unregisterParticipant(long eventId, long userId) {
29+
if (isParticipantRegistered(eventId, userId)) {
30+
throw new IllegalStateException("User is not registered for this event");
31+
}
32+
eventParticipationRepository.unregister(eventId, userId);
33+
}
34+
35+
public List<UserDto> getParticipants(long eventId) {
36+
return eventParticipationRepository
37+
.findAllParticipantsByEventId(eventId)
38+
.stream()
39+
.map(participantMapper::toDto)
40+
.toList();
41+
}
42+
43+
public Integer getParticipantsCount(long eventId) {
44+
return eventParticipationRepository.countParticipants(eventId);
45+
}
46+
47+
private boolean isParticipantRegistered(long eventId, long userId) {
48+
return eventParticipationRepository
49+
.findAllParticipantsByEventId(eventId)
50+
.stream()
51+
.anyMatch(user -> user.getId() == userId);
52+
}
53+
}

0 commit comments

Comments
 (0)