Skip to content

Commit

Permalink
Merge pull request #973 from bounswe/backend/character
Browse files Browse the repository at this point in the history
added character entity and related endpoints
  • Loading branch information
halisbal authored Dec 9, 2023
2 parents d4a9d77 + a1d9495 commit 891093a
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.app.gamereview.controller;

import com.app.gamereview.dto.request.character.CreateCharacterRequestDto;
import com.app.gamereview.dto.request.character.UpdateCharacterRequestDto;
import com.app.gamereview.model.Character;
import com.app.gamereview.service.CharacterService;
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 java.util.List;

@RestController
@RequestMapping("/api/character")
@Validated
public class CharacterController {

private final CharacterService characterService;

public CharacterController(CharacterService characterService) {
this.characterService = characterService;
}

@PostMapping("/create")
@AuthorizationRequired
@AdminRequired
public ResponseEntity<Character> createCharacter(@Valid @RequestBody CreateCharacterRequestDto characterDto,
@RequestHeader String Authorization, HttpServletRequest request) {
Character character = characterService.createCharacter(characterDto);
return ResponseEntity.ok(character);
}

@PostMapping("/update")
@AuthorizationRequired
@AdminRequired
public ResponseEntity<Character> updateCharacter(@RequestParam String id,
@Valid @RequestBody UpdateCharacterRequestDto updateCharacterRequestDto,
@RequestHeader String Authorization, HttpServletRequest request) {
Character character = characterService.updateCharacter(id, updateCharacterRequestDto);
return ResponseEntity.ok(character);
}

@DeleteMapping("/delete")
@AuthorizationRequired
@AdminRequired
public ResponseEntity<Character> deleteCharacter(@RequestParam String id, @RequestHeader String Authorization,
HttpServletRequest request) {
Character character = characterService.deleteCharacter(id);
return ResponseEntity.ok(character);
}

@GetMapping("/get-game-characters")
public ResponseEntity<List<Character>> getGameCharacters(@RequestParam String gameId) {
List<Character> gameCharacters = characterService.getGameCharacters(gameId);
return ResponseEntity.ok(gameCharacters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.app.gamereview.dto.request.character;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateCharacterRequestDto {

@NotEmpty(message = "Character name cannot be empty.")
private String name;

@NotEmpty(message = "Character icon cannot be empty.")
private String icon;

@NotEmpty(message = "Game list cannot be empty.")
private List<@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")String> games;

private String type;

private String gender;

private String race;

private String status;

private String occupation;

private String birthDate;

private String voiceActor;

private String height;

private String age;

private Map<String, String> customFields;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.app.gamereview.dto.request.character;

import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UpdateCharacterRequestDto {

private String name;

private String icon;

private List<@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")String> games;

private String type;

private String gender;

private String race;

private String status;

private String occupation;

private String birthDate;

private String voiceActor;

private String height;

private String age;

private Map<String, String> customFields;
}
46 changes: 46 additions & 0 deletions app/backend/src/main/java/com/app/gamereview/model/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.app.gamereview.model;

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;
import org.springframework.data.mongodb.core.mapping.Field;

import java.util.List;
import java.util.Map;

@Document(collection = "Character")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Character extends BaseModel {
private String name;

private String icon;

private List<String> games;

private String type;

private String gender;

private String race;

private String status;

private String occupation;

private String birthDate;

private String voiceActor;

private String height;

private String age;

@Field("customFields")
private Map<String, String> customFields;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.app.gamereview.repository;

import com.app.gamereview.model.Character;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;
import java.util.Optional;

public interface CharacterRepository extends MongoRepository<Character, String> {

Optional<Character> findByIdAndIsDeletedFalse(String id);

List<Character> findByGamesContains(String game);

}
Loading

0 comments on commit 891093a

Please sign in to comment.