-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #826 from bounswe/backend/achievement-model
Backend/achievement model
- Loading branch information
Showing
16 changed files
with
427 additions
and
7 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
app/backend/src/main/java/com/app/gamereview/controller/AchievementController.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,72 @@ | ||
package com.app.gamereview.controller; | ||
|
||
import com.app.gamereview.dto.request.achievement.CreateAchievementRequestDto; | ||
import com.app.gamereview.dto.request.achievement.GrantAchievementRequestDto; | ||
import com.app.gamereview.dto.request.achievement.UpdateAchievementRequestDto; | ||
import com.app.gamereview.service.AchievementService; | ||
import com.app.gamereview.util.validation.annotation.AdminRequired; | ||
import com.app.gamereview.util.validation.annotation.AuthorizationRequired; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import com.app.gamereview.model.Achievement; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/achievement") | ||
@Validated | ||
public class AchievementController { | ||
|
||
private final AchievementService achievementService; | ||
|
||
public AchievementController(AchievementService achievementService) { | ||
this.achievementService = achievementService; | ||
} | ||
|
||
@PostMapping("/create") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Achievement> createAchievement(@Valid @RequestBody CreateAchievementRequestDto achievementDto, | ||
@RequestHeader String Authorization, HttpServletRequest request) { | ||
Achievement achievement = achievementService.createAchievement(achievementDto); | ||
return ResponseEntity.ok(achievement); | ||
} | ||
|
||
@PostMapping("/update") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Achievement> updateAchievement(@RequestParam String id, | ||
@Valid @RequestBody UpdateAchievementRequestDto updateAchievementRequestDto, | ||
@RequestHeader String Authorization, HttpServletRequest request) { | ||
Achievement achievement = achievementService.updateAchievement(id, updateAchievementRequestDto); | ||
return ResponseEntity.ok(achievement); | ||
} | ||
|
||
@DeleteMapping("/delete") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<Achievement> deleteAchievement(@RequestParam String id, @RequestHeader String Authorization, | ||
HttpServletRequest request) { | ||
Achievement achievement = achievementService.deleteAchievement(id); | ||
return ResponseEntity.ok(achievement); | ||
} | ||
|
||
@GetMapping("/get-game-achievements") | ||
public ResponseEntity<List<Achievement>> getGameAchievements(@RequestParam String gameId) { | ||
List<Achievement> gameAchievements = achievementService.getGameAchievements(gameId); | ||
return ResponseEntity.ok(gameAchievements); | ||
} | ||
|
||
@PostMapping("/grant-achievement") | ||
@AuthorizationRequired | ||
@AdminRequired | ||
public ResponseEntity<List<String>> grantAchievement(@Valid @RequestBody GrantAchievementRequestDto achievementDto, | ||
@RequestHeader String Authorization, HttpServletRequest request) { | ||
List<String> userAchievements = achievementService.grantAchievement(achievementDto); | ||
return ResponseEntity.ok(userAchievements); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...src/main/java/com/app/gamereview/dto/request/achievement/CreateAchievementRequestDto.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,33 @@ | ||
package com.app.gamereview.dto.request.achievement; | ||
|
||
import com.app.gamereview.enums.AchievementType; | ||
import com.app.gamereview.util.validation.annotation.ValidAchievementType; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateAchievementRequestDto { | ||
|
||
@NotEmpty(message = "Achievement title cannot be empty.") | ||
private String title; | ||
|
||
@NotEmpty(message = "Achievement description cannot be empty.") | ||
private String description; | ||
|
||
@NotEmpty(message = "Achievement icon cannot be empty.") | ||
private String icon; | ||
|
||
@ValidAchievementType(allowedValues = {AchievementType.GAME, AchievementType.META}) | ||
@NotNull(message = "Achievement type cannot be empty.") | ||
private String type; | ||
|
||
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Game has invalid Id (UUID) format") | ||
private String game; | ||
} |
22 changes: 22 additions & 0 deletions
22
.../src/main/java/com/app/gamereview/dto/request/achievement/GrantAchievementRequestDto.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.app.gamereview.dto.request.achievement; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class GrantAchievementRequestDto { | ||
@NotEmpty(message = "User id cannot be empty.") | ||
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "User has invalid Id (UUID) format") | ||
private String userId; | ||
|
||
@NotEmpty(message = "Achievement id cannot be empty.") | ||
@Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", | ||
message = "Achievement has invalid Id (UUID) format") | ||
private String achievementId; | ||
} |
17 changes: 17 additions & 0 deletions
17
...src/main/java/com/app/gamereview/dto/request/achievement/UpdateAchievementRequestDto.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,17 @@ | ||
package com.app.gamereview.dto.request.achievement; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UpdateAchievementRequestDto { | ||
|
||
private String title; | ||
|
||
private String description; | ||
|
||
private String icon; | ||
} |
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
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
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
6 changes: 6 additions & 0 deletions
6
app/backend/src/main/java/com/app/gamereview/enums/AchievementType.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,6 @@ | ||
package com.app.gamereview.enums; | ||
|
||
public enum AchievementType { | ||
GAME, | ||
META | ||
} |
27 changes: 27 additions & 0 deletions
27
app/backend/src/main/java/com/app/gamereview/model/Achievement.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,27 @@ | ||
package com.app.gamereview.model; | ||
|
||
import com.app.gamereview.enums.AchievementType; | ||
import com.app.gamereview.model.common.BaseModel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Document(collection = "Achievement") | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Achievement extends BaseModel { | ||
|
||
private String title; | ||
|
||
private String description; | ||
|
||
private String icon; | ||
|
||
private AchievementType type; | ||
|
||
private String game; | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
app/backend/src/main/java/com/app/gamereview/repository/AchievementRepository.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,18 @@ | ||
package com.app.gamereview.repository; | ||
|
||
import com.app.gamereview.model.Achievement; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface AchievementRepository extends MongoRepository<Achievement, String> { | ||
|
||
List<Achievement> findByTitleAndIsDeletedFalse(String title); | ||
|
||
Optional<Achievement> findByIdAndIsDeletedFalse(String id); | ||
|
||
List<Achievement> findByGameAndIsDeletedFalse(String game); | ||
|
||
int countByGame(String game); | ||
} |
Oops, something went wrong.