Skip to content

Commit

Permalink
Merge pull request #992 from bounswe/main
Browse files Browse the repository at this point in the history
deploying backend changes
  • Loading branch information
halisbal authored Dec 12, 2023
2 parents 6f79891 + 964eef7 commit add3754
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class CreateCharacterRequestDto {
@NotEmpty(message = "Character icon cannot be empty.")
private String icon;

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

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class UpdateCharacterRequestDto {

private String icon;

private String description;

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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public class CreatePostRequestDto {
@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;

@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 = "Character has invalid Id (UUID) format")
private String character;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.app.gamereview.enums.VoteChoice;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.User;
import com.app.gamereview.model.Character;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class GetPostDetailResponseDto {

private Achievement achievement;

private Character character;

private LocalDateTime lastEditedAt;

private List<Tag> tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.app.gamereview.enums.VoteChoice;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.User;
import com.app.gamereview.model.Character;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -30,6 +31,8 @@ public class GetPostListResponseDto {

private Achievement achievement;

private Character character;

// TODO avatar

private LocalDateTime lastEditedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Character extends BaseModel {

private String icon;

private String description;

private List<String> games;

private String type;
Expand Down
2 changes: 2 additions & 0 deletions app/backend/src/main/java/com/app/gamereview/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class Post extends BaseModel {

private String achievement;

private String character;

public void addVote(VoteChoice choice){
voteCount += 1;
if(choice.name().equals("UPVOTE")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public Character updateCharacter(String id, UpdateCharacterRequestDto requestDto
characterToUpdate.setIcon(requestDto.getIcon());
}

if (requestDto.getDescription() != null) {
if (requestDto.getDescription().isBlank()) {
throw new BadRequestException("Character description cannot be blank.");
}
characterToUpdate.setDescription(requestDto.getDescription());
}

if (requestDto.getGames() != null) {

if (requestDto.getGames().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.app.gamereview.enums.*;
import com.app.gamereview.exception.BadRequestException;
import com.app.gamereview.model.*;
import com.app.gamereview.model.Character;
import com.app.gamereview.repository.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class PostService {

private final AchievementRepository achievementRepository;

private final CharacterRepository characterRepository;

private final MongoTemplate mongoTemplate;
private final ModelMapper modelMapper;
private final NotificationService notificationService;
Expand All @@ -58,7 +61,7 @@ public class PostService {
public PostService(PostRepository postRepository, ForumRepository forumRepository, UserRepository userRepository,
ProfileRepository profileRepository, TagRepository tagRepository,
CommentRepository commentRepository, VoteRepository voteRepository,
AchievementRepository achievementRepository, GameRepository gameRepository, MongoTemplate mongoTemplate,
AchievementRepository achievementRepository, GameRepository gameRepository, CharacterRepository characterRepository, MongoTemplate mongoTemplate,
NotificationService notificationService, GroupRepository groupRepository,
ModelMapper modelMapper) {

Expand All @@ -71,6 +74,7 @@ public PostService(PostRepository postRepository, ForumRepository forumRepositor
this.commentRepository = commentRepository;
this.voteRepository = voteRepository;
this.achievementRepository = achievementRepository;
this.characterRepository = characterRepository;
this.mongoTemplate = mongoTemplate;
this.modelMapper = modelMapper;
this.notificationService = notificationService;
Expand Down Expand Up @@ -133,14 +137,17 @@ private GetPostListResponseDto mapToGetPostListResponseDto(Post post, String log
Optional<Achievement> postAchievementOptional = achievementRepository.findByIdAndIsDeletedFalse(post.getAchievement());
Achievement postAchievement = postAchievementOptional.orElse(null);

Optional<Character> postCharacterOptional = characterRepository.findByIdAndIsDeletedFalse(post.getCharacter());
Character postCharacter = postCharacterOptional.orElse(null);

// Fetch tags individually
for (String tagId : post.getTags()) {
Optional<Tag> tag = tagRepository.findById(tagId);
tag.ifPresent(tags::add);
}

return new GetPostListResponseDto(post.getId(), post.getTitle(), post.getPostContent(),
posterObject, userVoteChoice, post.getPostImage(), postAchievement, post.getLastEditedAt(), post.getCreatedAt(), isEdited,
posterObject, userVoteChoice, post.getPostImage(), postAchievement, postCharacter, post.getLastEditedAt(), post.getCreatedAt(), isEdited,
tags, post.getInappropriate(), post.getOverallVote(), post.getVoteCount(), commentCount);
}

Expand Down Expand Up @@ -175,6 +182,10 @@ public GetPostDetailResponseDto getPostById(String id, String email) {

postAchievement.ifPresent(postDto::setAchievement);

Optional<Character> postCharacter = characterRepository.findByIdAndIsDeletedFalse(post.get().getCharacter());

postCharacter.ifPresent(postDto::setCharacter);

List<Tag> tags = new ArrayList<>();

// Fetch tags individually
Expand Down Expand Up @@ -273,6 +284,14 @@ public Post createPost(CreatePostRequestDto request, User user) {
}
}

if(request.getCharacter() != null){ // if character is assigned
Optional<Character> characterOptional = characterRepository.findById(request.getCharacter());

if (characterOptional.isEmpty()) {
throw new ResourceNotFoundException("Character is not found.");
}
}

if (request.getTags() != null) {
for (String tagId : request.getTags()) {
Optional<Tag> tag = tagRepository.findById(tagId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class PostServiceTest {
@Mock
private AchievementRepository achievementRepository;

@Mock
private CharacterRepository characterRepository;

@Mock
private MongoTemplate mongoTemplate;

Expand Down Expand Up @@ -104,12 +107,14 @@ void testGetPostById_Success() {
tagList.add("tag");
post.setTags(tagList);
post.setAchievement("");
post.setCharacter("");

when(postRepository.findById(anyString())).thenReturn(Optional.of(post));
when(userRepository.findByEmailAndIsDeletedFalse(anyString())).thenReturn(Optional.of(loggedInUser));
when(forumRepository.findById(anyString())).thenReturn(Optional.of(new Forum()));
when(voteRepository.findByTypeIdAndVotedBy(anyString(), anyString())).thenReturn(Optional.empty());
when(achievementRepository.findByIdAndIsDeletedFalse(anyString())).thenReturn(Optional.empty());
when(characterRepository.findByIdAndIsDeletedFalse(anyString())).thenReturn(Optional.empty());
when(tagRepository.findById(anyString())).thenReturn(Optional.empty());
when(modelMapper.map(any(Optional.class), eq(GetPostDetailResponseDto.class))).thenReturn(new GetPostDetailResponseDto());

Expand All @@ -120,6 +125,7 @@ void testGetPostById_Success() {
Assertions.assertNotNull(result);
verify(voteRepository, times(1)).findByTypeIdAndVotedBy(anyString(), anyString());
verify(achievementRepository, times(1)).findByIdAndIsDeletedFalse(anyString());
verify(characterRepository, times(1)).findByIdAndIsDeletedFalse(anyString());
verify(tagRepository, times(1)).findById(anyString());
}

Expand Down

0 comments on commit add3754

Please sign in to comment.