Skip to content

Commit

Permalink
Merge pull request #826 from bounswe/backend/achievement-model
Browse files Browse the repository at this point in the history
Backend/achievement model
  • Loading branch information
canuzdrn authored Nov 23, 2023
2 parents ab00e1f + 2c6e035 commit e165f98
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 7 deletions.
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);
}
}
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;
}
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;
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public class CreatePostRequestDto {
message = "One of the tags has invalid Id (UUID) format")String> tags;

// TODO annotations
// TODO achievements

@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 achievement;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.app.gamereview.dto.response.post;

import com.app.gamereview.model.Achievement;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.User;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -23,11 +23,12 @@ public class GetPostDetailResponseDto {

private User poster;

@NotNull
private String forum;

// TODO avatar

private Achievement achievement;

private LocalDateTime lastEditedAt;

private List<Tag> tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ public class GetPostListResponseDto {
// TODO reports
// TODO comments
// TODO annotations
// TODO achievements
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.app.gamereview.enums;

public enum AchievementType {
GAME,
META
}
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;
}
3 changes: 2 additions & 1 deletion app/backend/src/main/java/com/app/gamereview/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class Post extends BaseModel {

// TODO reports
// TODO annotations
// TODO achievements

private String achievement;

public void addVote(VoteChoice choice){
voteCount += 1;
Expand Down
6 changes: 5 additions & 1 deletion app/backend/src/main/java/com/app/gamereview/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.List;

@Document(collection = "User")
@TypeAlias("User")
@Getter
Expand All @@ -23,6 +26,8 @@ public class User extends BaseModel {

private Boolean isVerified;

private List<String> achievements = new ArrayList<>();

public String getUsername() {
return username;
}
Expand Down Expand Up @@ -63,5 +68,4 @@ public void setVerified(Boolean verified) {
isVerified = verified;
}


}
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);
}
Loading

0 comments on commit e165f98

Please sign in to comment.