-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b459459
commit 025e759
Showing
24 changed files
with
491 additions
and
4 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/com/chessgrinder/chessgrinder/controller/ClubController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/chessgrinder/chessgrinder/dto/ClubDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/chessgrinder/chessgrinder/dto/SubscriptionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/chessgrinder/chessgrinder/dto/SubscriptionLevelDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/chessgrinder/chessgrinder/entities/ClubEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/chessgrinder/chessgrinder/entities/SubscriptionEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/chessgrinder/chessgrinder/entities/SubscriptionLevelEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/chessgrinder/chessgrinder/mappers/ClubMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.