Skip to content
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

feat: add clubs + subscriptions #19

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.chessgrinder.chessgrinder.trf.dto.PlayerTrfLineDto.TrfMatchResult;
import com.chessgrinder.chessgrinder.trf.line.MissingPlayersTrfLineParser;
import com.chessgrinder.chessgrinder.trf.line.PlayerTrfLineParser;
import com.chessgrinder.chessgrinder.utils.Const;
import javafo.api.JaVaFoApi;
import org.springframework.stereotype.Component;

Expand All @@ -21,13 +22,9 @@ public class JavafoPairingStrategyImpl implements PairingStrategy {
* JaVaFo is no thread-safe library, therefore to avoid concurrency problems,
* the requests to JaVaFo are synchronized on this monitor.
*/
private static final Object JAVAFO_MONITOR = new Object();

private final PlayerTrfLineParser playerTrfLineParser = new PlayerTrfLineParser();
private final MissingPlayersTrfLineParser missingPlayersTrfLineParser = new MissingPlayersTrfLineParser();
private static final int DEFAULT_RATING = 1000;
private static final String NEWLINE_REGEX = "\\r?\\n|\\r";
private static final int STANDARD_PAIRING_CODE = 1000;
vladimirshefer marked this conversation as resolved.
Show resolved Hide resolved

@Override
public List<MatchDto> makePairings(List<ParticipantDto> participants, List<List<MatchDto>> matchHistory, Integer roundsNumber, boolean recalculateResults) {
Expand Down Expand Up @@ -59,15 +56,15 @@ public List<MatchDto> makePairings(List<ParticipantDto> participants, List<List<
});

String pairingsFileContent;
synchronized (JAVAFO_MONITOR) {
synchronized (Const.Javafo.JAVAFO_MONITOR) {
try {
pairingsFileContent = JaVaFoApi.exec(STANDARD_PAIRING_CODE, new ByteArrayInputStream(stringBuilder.toString().getBytes()));
pairingsFileContent = JaVaFoApi.exec(Const.Javafo.STANDARD_PAIRING_CODE, new ByteArrayInputStream(stringBuilder.toString().getBytes()));
} catch (Exception e) {
throw new RuntimeException("Could not do pairing via javafo. \n" + stringBuilder, e);
}
}

List<MatchDto> result = Arrays.stream(pairingsFileContent.split(NEWLINE_REGEX))
List<MatchDto> result = Arrays.stream(pairingsFileContent.split(Const.Javafo.NEWLINE_REGEX))
.skip(1)
.map(it -> it.split(" "))
.map(it ->
Expand Down Expand Up @@ -118,7 +115,7 @@ private static PlayerTrfLineDto toTrfDto(
PlayerTrfLineDto playerTrfLineDto = PlayerTrfLineDto.builder()
.startingRank(playerId)
.name(participant.getUserFullName() != null ? participant.getUserFullName() : participant.getName())
.rating(DEFAULT_RATING)
.rating(Const.Javafo.DEFAULT_RATING)
.points(participant.getScore().floatValue())
.matches(matches.stream()
.map(match -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.chessgrinder.chessgrinder.dto.ListDto;
import com.chessgrinder.chessgrinder.dto.UserDto;
import com.chessgrinder.chessgrinder.entities.BadgeEntity;
import com.chessgrinder.chessgrinder.entities.RoleEntity;
import com.chessgrinder.chessgrinder.entities.UserEntity;
import com.chessgrinder.chessgrinder.mappers.BadgeMapper;
import com.chessgrinder.chessgrinder.mappers.UserMapper;
import com.chessgrinder.chessgrinder.repositories.BadgeRepository;
import com.chessgrinder.chessgrinder.repositories.UserRepository;
import com.chessgrinder.chessgrinder.service.UserService;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
Expand All @@ -33,7 +33,7 @@ public ListDto<BadgeDto> getBadges() {
return ListDto.<BadgeDto>builder().values(badgeMapper.toDto(badgeRepository.findAll())).build();
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(com.chessgrinder.chessgrinder.utils.Const.Roles.ADMIN)
@PostMapping
public BadgeDto create(@RequestBody BadgeDto badgeDto) {
return badgeMapper.toDto(badgeRepository.save(BadgeEntity.builder()
Expand All @@ -51,7 +51,7 @@ public BadgeDto getBadge(
return badgeMapper.toDto(badgeRepository.findById(badgeId).orElseThrow());
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@DeleteMapping("/{badgeId}")
public void delete(
@PathVariable UUID badgeId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.chessgrinder.chessgrinder.repositories.RoleRepository;
import com.chessgrinder.chessgrinder.repositories.UserRepository;
import com.chessgrinder.chessgrinder.repositories.UserRoleRepository;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -35,17 +36,17 @@ public Object getAdminRole(
if (authentication == null || !authentication.isAuthenticated()) {
throw new ResponseStatusException(401, "Please log in", null);
}
if (!authentication.getAuthorities().stream().anyMatch(it -> it.getAuthority().equals(RoleEntity.Roles.ADMIN))) {
if (!authentication.getAuthorities().stream().anyMatch(it -> it.getAuthority().equals(Const.Roles.ADMIN))) {
UserEntity user = userRepository.findByUsername(authentication.getName());
if (user == null) {
throw new ResponseStatusException(401, "User does not exist", null);
}
if (user.getRoles().stream().anyMatch(it -> it.getName().equals(RoleEntity.Roles.ADMIN))) {
if (user.getRoles().stream().anyMatch(it -> it.getName().equals(Const.Roles.ADMIN))) {
return "User already has admin role";
}

RoleEntity adminRole = roleRepository.findByName(RoleEntity.Roles.ADMIN)
.orElseGet(() -> roleRepository.save(RoleEntity.builder().name(RoleEntity.Roles.ADMIN).build()));
RoleEntity adminRole = roleRepository.findByName(Const.Roles.ADMIN)
.orElseGet(() -> roleRepository.save(RoleEntity.builder().name(Const.Roles.ADMIN).build()));
userRoleRepository.save(UserRoleEntity.builder()
.user(user)
.role(adminRole)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.chessgrinder.chessgrinder.controller;

import com.chessgrinder.chessgrinder.dto.*;
import com.chessgrinder.chessgrinder.entities.*;
import com.chessgrinder.chessgrinder.mappers.ClubMapper;
import com.chessgrinder.chessgrinder.repositories.ClubRepository;
import com.chessgrinder.chessgrinder.service.ClubService;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/clubs")
@RequiredArgsConstructor
public class ClubController {

private final ClubRepository clubRepository;

private final ClubService clubService;

private final ClubMapper clubMapper;

@GetMapping
public ListDto<ClubDto> getAllClubs() {
vladimirshefer marked this conversation as resolved.
Show resolved Hide resolved
List<ClubDto> allClubs = clubService.getAllClubs();
return ListDto.<ClubDto>builder().values(allClubs).build();
}

@GetMapping("/{clubId}")
public ClubDto getClubById(
@PathVariable UUID clubId
) {
final var club = clubRepository.findById(clubId).orElseThrow();
return clubMapper.toDto(club);
}

@Secured(Const.Roles.ADMIN)
@PostMapping
public void createClub(
@RequestBody ClubDto clubDto
) {
ClubEntity club = ClubEntity.builder()
.id(UUID.randomUUID())
.name(clubDto.getName())
.description(clubDto.getDescription())
.location(clubDto.getLocation())
.build();

clubRepository.save(club);
}

@Secured(Const.Roles.ADMIN)
@PutMapping("/{clubId}")
public void updateClub(
@PathVariable UUID clubId,
@RequestBody ClubDto clubDto
) {
ClubEntity club = clubRepository.findById(clubId).orElseThrow(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: optional: line separator before dot

() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No club with id " + clubId)
);

club.setName(clubDto.getName());
club.setDescription(clubDto.getDescription());
club.setLocation(clubDto.getLocation());

clubRepository.save(club);
}

@Secured(Const.Roles.ADMIN)
@DeleteMapping("/{clubId}")
public void deleteClub(
@PathVariable UUID clubId
) {
if (!clubRepository.existsById(clubId)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No club with id " + clubId);
}
vladimirshefer marked this conversation as resolved.
Show resolved Hide resolved

clubRepository.deleteById(clubId);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.chessgrinder.chessgrinder.controller;

import com.chessgrinder.chessgrinder.dto.SubmitMatchResultRequestDto;
import com.chessgrinder.chessgrinder.entities.RoleEntity;
import com.chessgrinder.chessgrinder.service.MatchService;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,7 +16,7 @@ public class MatchController {

private final MatchService matchService;

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping()
public void submitMatchResult(
@PathVariable UUID matchId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.chessgrinder.chessgrinder.dto.ParticipantDto;
import com.chessgrinder.chessgrinder.entities.ParticipantEntity;
import com.chessgrinder.chessgrinder.entities.RoleEntity;
import com.chessgrinder.chessgrinder.entities.UserEntity;
import com.chessgrinder.chessgrinder.mappers.ParticipantMapper;
import com.chessgrinder.chessgrinder.repositories.ParticipantRepository;
import com.chessgrinder.chessgrinder.security.AuthenticatedUserArgumentResolver.AuthenticatedUser;
import com.chessgrinder.chessgrinder.service.ParticipantService;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
Expand All @@ -25,7 +25,7 @@ public class ParticipantController {
private final ParticipantRepository participantRepository;
private final ParticipantMapper participantMapper;

@Secured(RoleEntity.Roles.ADMIN)
@Secured(com.chessgrinder.chessgrinder.utils.Const.Roles.ADMIN)
@PostMapping
public void addParticipantToTournament(
@PathVariable UUID tournamentId,
Expand All @@ -34,7 +34,7 @@ public void addParticipantToTournament(
participantService.addParticipantToTheTournament(tournamentId, participantDto);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(com.chessgrinder.chessgrinder.utils.Const.Roles.ADMIN)
@DeleteMapping("/{participantId}")
public void delete(
@PathVariable UUID tournamentId,
Expand Down Expand Up @@ -63,7 +63,7 @@ public ParticipantDto getMe(
return participantMapper.toDto(participantEntity);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(com.chessgrinder.chessgrinder.utils.Const.Roles.ADMIN)
@PutMapping("/{participantId}")
public void update(
@PathVariable UUID participantId,
Expand All @@ -75,7 +75,7 @@ public void update(
participantRepository.save(participant);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(com.chessgrinder.chessgrinder.utils.Const.Roles.ADMIN)
@PostMapping("/{participantId}/action/miss")
public void miss(
@PathVariable UUID participantId
Expand All @@ -86,7 +86,7 @@ public void miss(
participantRepository.save(participant);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping("/{participantId}/action/unmiss")
public void unmiss(
@PathVariable UUID participantId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.chessgrinder.chessgrinder.controller;

import com.chessgrinder.chessgrinder.entities.RoleEntity;
import com.chessgrinder.chessgrinder.exceptions.RoundNotFoundException;
import com.chessgrinder.chessgrinder.service.RoundService;
import com.chessgrinder.chessgrinder.utils.Const;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,38 +16,38 @@ public class RoundController {

private final RoundService roundService;

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping()
public void createNextRound(@PathVariable UUID tournamentId) {
roundService.createRound(tournamentId);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping("/{roundNumber}/action/finish")
public void finishRound(@PathVariable UUID tournamentId, @PathVariable Integer roundNumber) {
roundService.finishRound(tournamentId, roundNumber);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping("/{roundNumber}/action/reopen")
public void reopenRound(@PathVariable UUID tournamentId, @PathVariable Integer roundNumber) {
roundService.reopenRound(tournamentId, roundNumber);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping("/{roundNumber}/action/matchup")
public void makePairings(@PathVariable UUID tournamentId, @PathVariable Integer roundNumber) {
roundService.makePairings(tournamentId, roundNumber);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping("/{roundNumber}/action/miss")
public void markUserAsMissedInTournament(@PathVariable UUID tournamentId,
@RequestParam UUID userId) {
roundService.markUserAsMissedInTournament(userId, tournamentId);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@DeleteMapping("/{roundNumber}")
public void deleteRound(@PathVariable UUID tournamentId,
@PathVariable Integer roundNumber) throws RoundNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.chessgrinder.chessgrinder.dto.TournamentDto;
import com.chessgrinder.chessgrinder.entities.ParticipantEntity;
import com.chessgrinder.chessgrinder.entities.RoleEntity;
import com.chessgrinder.chessgrinder.entities.TournamentEntity;
import com.chessgrinder.chessgrinder.entities.UserEntity;
import com.chessgrinder.chessgrinder.enums.TournamentStatus;
Expand All @@ -11,6 +10,7 @@
import com.chessgrinder.chessgrinder.repositories.TournamentRepository;
import com.chessgrinder.chessgrinder.repositories.UserRepository;
import com.chessgrinder.chessgrinder.service.TournamentService;
import com.chessgrinder.chessgrinder.utils.Const;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
Expand All @@ -34,21 +34,21 @@ public class TournamentController {
private final ParticipantRepository participantRepository;
private final TournamentMapper tournamentMapper;

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PostMapping
public TournamentDto createTournament() {
return tournamentService.createTournament(LocalDateTime.now());
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@GetMapping("/{tournamentId}/action/start")
public void startTournament(
@PathVariable UUID tournamentId
) {
tournamentService.startTournament(tournamentId);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@GetMapping("/{tournamentId}/action/finish")
public void finishTournament(@PathVariable UUID tournamentId) {
tournamentService.finishTournament(tournamentId);
Expand All @@ -68,7 +68,7 @@ public TournamentDto getTournament(
return tournamentMapper.toDto(tournamentEntity);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@PutMapping("/{tournamentId}")
public void updateTournament(
@PathVariable UUID tournamentId,
Expand All @@ -77,7 +77,7 @@ public void updateTournament(
tournamentService.updateTournament(tournamentId, tournamentDto);
}

@Secured(RoleEntity.Roles.ADMIN)
@Secured(Const.Roles.ADMIN)
@DeleteMapping("/{tournamentId}")
public void deleteTournament(@PathVariable UUID tournamentId) {
tournamentService.deleteTournament(tournamentId);
Expand Down
Loading