Skip to content

Commit

Permalink
Merge pull request #1024 from bounswe/main
Browse files Browse the repository at this point in the history
Deploy recent changes and developments 17.12.2023 09.45
  • Loading branch information
canuzdrn authored Dec 17, 2023
2 parents add3754 + 9a73ae4 commit 12dda38
Show file tree
Hide file tree
Showing 544 changed files with 145,220 additions and 114,373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.app.gamereview.dto.response.tag.AddGameTagResponseDto;
import com.app.gamereview.dto.response.tag.GetAllTagsOfGameResponseDto;
import com.app.gamereview.model.Game;
import com.app.gamereview.model.User;
import com.app.gamereview.service.GameService;
import com.app.gamereview.util.validation.annotation.AdminRequired;
import com.app.gamereview.util.validation.annotation.AuthorizationRequired;
Expand All @@ -17,6 +18,7 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

@RestController
Expand Down Expand Up @@ -104,4 +106,12 @@ public ResponseEntity<Boolean> deleteGame(@RequestParam String id, @RequestHeade
Boolean isDeleted = gameService.deleteGame(id);
return ResponseEntity.ok(isDeleted);
}

@AuthorizationRequired
@GetMapping("/get-recommendations")
public ResponseEntity<List<Game>> getRecommendedGames(@RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
List<Game> games = gameService.getRecommendedGames(user);
return ResponseEntity.ok(games);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.app.gamereview.dto.response.group.GetGroupResponseDto;
import com.app.gamereview.dto.response.group.GroupApplicationResponseDto;
import com.app.gamereview.dto.response.tag.AddGroupTagResponseDto;
import com.app.gamereview.model.Game;
import com.app.gamereview.model.Group;
import com.app.gamereview.model.User;
import com.app.gamereview.service.GroupService;
Expand Down Expand Up @@ -182,4 +183,12 @@ public ResponseEntity<List<GroupApplicationResponseDto>> listApplications(@Reque
List<GroupApplicationResponseDto> applications = groupService.listApplications(groupId, user);
return ResponseEntity.ok(applications);
}

@AuthorizationRequired
@GetMapping("/get-recommendations")
public ResponseEntity<List<Group>> getRecommendedGroups(@RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
List<Group> groups = groupService.getRecommendedGroups(user);
return ResponseEntity.ok(groups);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.app.gamereview.dto.request.game;

import com.app.gamereview.model.Game;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Getter
@Setter
public class RecommendGameDto implements Comparable<RecommendGameDto>{
private Game game;
private int score = Integer.MAX_VALUE;

@Override
public int compareTo(RecommendGameDto o) {
if(game.getGameName().equals(o.getGame().getGameName())) return 0;
if(score == o.score) return 1;
return Integer.compare(this.score, o.score);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RecommendGameDto given = (RecommendGameDto) o;
return this.game.getGameName().equals(given.getGame().getGameName());
}

@Override
public int hashCode() {
return Objects.hash(game.getGameName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.app.gamereview.dto.request.group;

import com.app.gamereview.model.Group;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Getter
@Setter
public class RecommendGroupDto implements Comparable<RecommendGroupDto>{
private Group group;
private int score = Integer.MAX_VALUE;

@Override
public int compareTo(RecommendGroupDto o) {
if(group.getId().equals(o.getGroup().getId())) return 0;
if(score == o.score) return 1;
return Integer.compare(this.score, o.score);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RecommendGroupDto given = (RecommendGroupDto) o;
return this.group.getId().equals(given.getGroup().getId());
}

@Override
public int hashCode() {
return Objects.hash(group.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.app.gamereview.dto.response.home;

import com.app.gamereview.enums.ForumType;
import com.app.gamereview.model.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
Expand Down Expand Up @@ -35,7 +37,7 @@ public class HomePagePostResponseDto {

private LocalDateTime lastEditedAt;

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

private Boolean inappropriate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

@Getter
@Setter
public abstract class BaseModel {
public class BaseModel {

@Id
private String id;
Expand All @@ -27,4 +28,17 @@ public BaseModel() {
this.isDeleted = false;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseModel given = (BaseModel) o;
return id.equals(given.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

}
111 changes: 103 additions & 8 deletions app/backend/src/main/java/com/app/gamereview/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import com.app.gamereview.enums.TagType;
import com.app.gamereview.exception.BadRequestException;
import com.app.gamereview.exception.ResourceNotFoundException;
import com.app.gamereview.model.Forum;
import com.app.gamereview.model.Game;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.*;
import com.app.gamereview.repository.ForumRepository;
import com.app.gamereview.repository.GameRepository;
import com.app.gamereview.repository.ProfileRepository;
import com.app.gamereview.repository.TagRepository;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
Expand All @@ -23,9 +22,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand All @@ -36,18 +33,21 @@ public class GameService {

private final ForumRepository forumRepository;

private final ProfileRepository profileRepository;

private final MongoTemplate mongoTemplate;
private final ModelMapper modelMapper;

@Autowired
public GameService(
GameRepository gameRepository,
MongoTemplate mongoTemplate,
TagRepository tagRepository, ForumRepository forumRepository, ModelMapper modelMapper) {
MongoTemplate mongoTemplate, TagRepository tagRepository, ForumRepository forumRepository,
ProfileRepository profileRepository, ModelMapper modelMapper) {
this.gameRepository = gameRepository;
this.tagRepository = tagRepository;
this.mongoTemplate = mongoTemplate;
this.forumRepository = forumRepository;
this.profileRepository = profileRepository;
this.modelMapper = modelMapper;

modelMapper.addMappings(new PropertyMap<Game, GameDetailResponseDto>() {
Expand Down Expand Up @@ -356,5 +356,100 @@ public Boolean deleteGame(String id){
return true;
}

public List<Game> getRecommendedGames(User user){
Optional<Profile> findProfile = profileRepository.findByUserIdAndIsDeletedFalse(user.getId());

if(findProfile.isEmpty()){
throw new ResourceNotFoundException("Profile of the user is not found, unexpected error has occurred");
}

Profile profile = findProfile.get();
List<String> followedGameIds = profile.getGames();

TreeSet<RecommendGameDto> recommendedGames = new TreeSet<>(Comparator.reverseOrder());

for(String gameId : followedGameIds){
recommendedGames.addAll(recommendationByGameId(gameId));
}

List<Game> recommendations = new ArrayList<>();

for(RecommendGameDto gameDto : recommendedGames){
recommendations.add(gameDto.getGame());
}

return recommendations;
}

public TreeSet<RecommendGameDto> recommendationByGameId(String gameId){
Optional<Game> findGame = gameRepository.findByIdAndIsDeletedFalse(gameId);

Set<String> idList = new HashSet<>();

if(findGame.isEmpty()){
throw new ResourceNotFoundException("Game is not found");
}

Game game = findGame.get();
idList.add(game.getId());

TreeSet<RecommendGameDto> scoreSet = new TreeSet<>(Comparator.reverseOrder());

String[] words = game.getGameName().split(" ",-2);
for(String word : words){
if(word.length() > 3){
String regexPattern = ".*" + word + ".*";
Query query = new Query();
query.addCriteria(Criteria.where("gameName").regex(regexPattern, "i"));
query.addCriteria(Criteria.where("_id").ne(game.getId()));
List<Game> similarNames = mongoTemplate.find(query, Game.class);
for(Game i : similarNames){
RecommendGameDto dto = new RecommendGameDto();
dto.setGame(i);
scoreSet.add(dto);
idList.add(dto.getGame().getId());
}
}
}

// List<Game> recommendations = new ArrayList<>();

Query allGamesQuery = new Query(); // all games except the base game
allGamesQuery.addCriteria(Criteria.where("isDeleted").is(false));
allGamesQuery.addCriteria(Criteria.where("_id").nin(idList));

List<Game> allGames = mongoTemplate.find(allGamesQuery, Game.class);

for(Game candidateGame : allGames){
int score = calculateSimilarityScore(game, candidateGame);
if(score != 0){
RecommendGameDto dto = new RecommendGameDto();
dto.setGame(candidateGame);
dto.setScore(score);
scoreSet.add(dto);
}
}

return scoreSet;
}

public int calculateSimilarityScore(Game based, Game candidate){
int score = 0;

List<String> baseTags = based.getAllTags();
baseTags.retainAll(candidate.getAllTags());

for(String tagId : baseTags){
Optional<Tag> findTag = tagRepository.findByIdAndIsDeletedFalse(tagId);
if(findTag.isPresent()){
score++;
if(findTag.get().getTagType().equals(TagType.GENRE)){
score++;
}
}
}
return score;
}
}


Loading

0 comments on commit 12dda38

Please sign in to comment.