Skip to content

Commit

Permalink
FEATURE: Clubs and subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sedub01 authored and vladimirshefer committed Jun 13, 2024
1 parent b459459 commit 025e759
Show file tree
Hide file tree
Showing 24 changed files with 491 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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 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() {
List<ClubDto> allClubs = clubService.getAllClubs();
return ListDto.<ClubDto>builder().values(allClubs).build();
}

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

@Secured(RoleEntity.Roles.ADMIN)
@PostMapping("/createClub")
public void createClub(
@RequestBody ClubDto clubDto
) {
ClubEntity club = ClubEntity.builder()
.name(clubDto.getName())
.description(clubDto.getDescription())
.location(clubDto.getLocation())
.build();

clubRepository.save(club);
}

@Secured(RoleEntity.Roles.ADMIN)
@PutMapping("/{clubId}")
public void updateClub(
@PathVariable UUID clubId,
@RequestBody ClubDto clubDto
) {
ClubEntity club = clubRepository.getById(clubId);

if (clubDto.getName() != null) {
club.setName(clubDto.getName());
}
if (clubDto.getDescription() != null) {
club.setDescription(clubDto.getDescription());
}
if (clubDto.getLocation() != null) {
club.setLocation(clubDto.getLocation());
}

clubRepository.save(club);
}

@Secured(RoleEntity.Roles.ADMIN)
@DeleteMapping("/{clubId}")
public void deleteClub(
@PathVariable UUID clubId
) {
if (!clubRepository.existsById(clubId)) {
throw new ResponseStatusException(HttpStatus.OK, "No club with id " + clubId);
}

clubRepository.deleteById(clubId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.chessgrinder.chessgrinder.enums.TournamentStatus;
import com.chessgrinder.chessgrinder.exceptions.UserNotFoundException;
import com.chessgrinder.chessgrinder.mappers.ParticipantMapper;
import com.chessgrinder.chessgrinder.mappers.SubscriptionMapper;
import com.chessgrinder.chessgrinder.mappers.TournamentMapper;
import com.chessgrinder.chessgrinder.mappers.UserMapper;
import com.chessgrinder.chessgrinder.repositories.*;
Expand All @@ -21,6 +22,8 @@
import org.springframework.web.server.ResponseStatusException;

import javax.annotation.Nullable;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
Expand All @@ -34,6 +37,10 @@ public class UserController {

private final UserService userService;
private final UserRepository userRepository;
private final ClubRepository clubRepository;
private final SubscriptionLevelRepository subscriptionLevelRepository;
private final SubscriptionRepository subscriptionRepository;
private final SubscriptionMapper subscriptionMapper;
private final BadgeRepository badgeRepository;
private final UserBadgeRepository userBadgeRepository;
private final TournamentMapper tournamentMapper;
Expand Down Expand Up @@ -211,4 +218,33 @@ public void signUp(
.build()
);
}

@Secured(RoleEntity.Roles.ADMIN)
@PostMapping("/{userId}/subscription")
public void assignSubscription(
@PathVariable UUID userId,
@RequestBody SubscriptionDto data
) {
UserEntity user = userRepository.findById(userId).orElseThrow();
ClubEntity club = clubRepository.getById(UUID.fromString(data.getClub().getId()));
SubscriptionLevelEntity subscriptionLevel = subscriptionLevelRepository.findByName(
data.getSubscriptionLevel().getName()).orElseThrow();
final var subscription = SubscriptionEntity.builder()
.club(club)
.subscriptionLevel(subscriptionLevel)
.startDate(Instant.now(Clock.systemUTC()))
.finishDate(data.getFinishDate())
.user(user)
.build();

subscriptionRepository.save(subscription);
}

@GetMapping("/{userId}/subscription")
public ListDto<SubscriptionDto> getUserSubscriptions(
@PathVariable UUID userId
) {
final var allSubscriptions = subscriptionRepository.findAllByUserId(userId);
return ListDto.<SubscriptionDto>builder().values(subscriptionMapper.toDto(allSubscriptions)).build();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/chessgrinder/chessgrinder/dto/ClubDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.chessgrinder.chessgrinder.dto;

import com.chessgrinder.chessgrinder.utils.Const;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;

import java.time.Instant;

@Data
@Builder
public class ClubDto {
private String id;
private String name;
private String description;
private String location;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.Tournaments.DATETIME_PATTERN)
private Instant registrationDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.chessgrinder.chessgrinder.dto;

import com.chessgrinder.chessgrinder.utils.Const;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;

import java.time.Instant;

@Data
@Builder
public class SubscriptionDto {
private String id;
private UserDto user;
private ClubDto club;
private SubscriptionLevelDto subscriptionLevel;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.Tournaments.DATETIME_PATTERN)
private Instant startDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.Tournaments.DATETIME_PATTERN)
private Instant finishDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.chessgrinder.chessgrinder.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class SubscriptionLevelDto {
private String name;
private String description;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chessgrinder.chessgrinder.dto;

import com.chessgrinder.chessgrinder.enums.TournamentStatus;
import com.chessgrinder.chessgrinder.utils.Const;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.annotation.Nullable;
import lombok.Builder;
Expand All @@ -21,10 +22,12 @@ public class TournamentDto {
@Nullable
private String locationUrl;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.Tournaments.DATETIME_PATTERN)
private LocalDateTime date;

private TournamentStatus status;

private Integer roundsNumber;

private ClubDto clubDto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.chessgrinder.chessgrinder.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.UuidGenerator;

import java.util.UUID;

@Getter
@Setter
@ToString
@Entity
@Table(name = "clubs_table")
@Slf4j
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ClubEntity extends AbstractAuditingEntity {

@Id
@Column(name = "id")
@UuidGenerator
private UUID id;

@Column(name = "name")
private String name;

@Column(name = "description")
private String description;

@Column(name = "location")
private String location;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.chessgrinder.chessgrinder.entities;

import jakarta.persistence.*;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.UuidGenerator;

import java.time.Instant;
import java.util.UUID;

@Getter
@Setter
@ToString
@Entity
@Table(name = "subscriptions_table")
@Slf4j
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubscriptionEntity extends AbstractAuditingEntity {

@Id
@Column(name = "id")
@UuidGenerator
private UUID id;

@OneToOne
@JoinColumn(name = "user_id")
private UserEntity user;

@ManyToOne
@JoinColumn(name = "club_id")
private ClubEntity club;

@ManyToOne
@JoinColumn(name = "subscription_level_id")
private SubscriptionLevelEntity subscriptionLevel;

@Column(name = "start_date")
private Instant startDate;

@Column(name = "finish_date")
private Instant finishDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.chessgrinder.chessgrinder.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.UuidGenerator;

import java.util.UUID;

@Getter
@Setter
@ToString
@Entity
@Table(name = "subscription_levels_table")
@Slf4j
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubscriptionLevelEntity extends AbstractAuditingEntity {

@Id
@Column(name = "id")
@UuidGenerator
private UUID id;

@Column(name = "name")
private String name;

@Column(name = "description")
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public class TournamentEntity extends AbstractAuditingEntity {
@ToString.Exclude
@OneToMany(mappedBy = "tournament")
private List<RoundEntity> rounds;

@ManyToOne
@JoinColumn(name = "club_id")
private ClubEntity club;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.chessgrinder.chessgrinder.mappers;

import com.chessgrinder.chessgrinder.dto.ClubDto;
import com.chessgrinder.chessgrinder.entities.ClubEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ClubMapper {

public ClubDto toDto(ClubEntity club) {

return ClubDto.builder()
.id(club.getId().toString())
.name(club.getName())
.description(club.getDescription())
.location(club.getLocation())
.registrationDate(club.getCreatedAt())
.build();
}
}
Loading

0 comments on commit 025e759

Please sign in to comment.