Skip to content

Commit

Permalink
Merge pull request #670 from bounswe/mobile/enhancement/recommendatio…
Browse files Browse the repository at this point in the history
…n-implementation

[Mobile] Recommendation implementation
  • Loading branch information
umutdmr authored Dec 25, 2023
2 parents c3ed5c0 + 6cbd348 commit a42d3a8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
22 changes: 18 additions & 4 deletions app/mobile/lib/data/models/game_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Game {
final int gameId;
final String title;
final String description;

String? developers;
List<String>? genres;
List<String>? platforms;
Expand All @@ -21,6 +21,7 @@ class Game {
String? creationDate;
final String gamePicture;

List<int> similarGameIds;
List<Game> similarGameList;
List<Post> relatedPosts;
String? status;
Expand All @@ -42,6 +43,7 @@ class Game {
this.creationDate,
required this.gamePicture,
this.developers,
this.similarGameIds = const [],
this.similarGameList = const [],
this.relatedPosts = const [],
this.status,
Expand All @@ -53,8 +55,12 @@ class Game {
gameId: json['gameId'],
title: json['title'],
description: json['description'],
genres: json["genres"] != null ? List<String>.from(json["genres"].map((x) => x)) : [],
platforms: json["platforms"] != null ? List<String>.from(json["platforms"].map((x) => x)) : [],
genres: json["genres"] != null
? List<String>.from(json["genres"].map((x) => x))
: [],
platforms: json["platforms"] != null
? List<String>.from(json["platforms"].map((x) => x))
: [],
playerNumber: json['playerNumber'],
releaseYear: json['releaseYear'],
universe: json['universe'],
Expand All @@ -66,7 +72,13 @@ class Game {
creationDate: json['creationDate'],
gamePicture: json['gamePicture'],
status: json['status'],
characters: json['characters'] != null ? List<Character>.from(json["characters"].map((x) => Character.fromJson(x))) : []
characters: json['characters'] != null
? List<Character>.from(
json["characters"].map((x) => Character.fromJson(x)))
: [],
similarGameIds: json['similarGames'] != null
? List<int>.from(json["similarGames"].map((x) => x))
: [],
);
}

Expand All @@ -78,6 +90,7 @@ class Game {
'genres': genres,
'platforms': platforms,
'playerNumber': playerNumber,
'releaseYear': releaseYear,
'universe': universe,
'mechanics': mechanics,
'playtime': playtime,
Expand All @@ -88,6 +101,7 @@ class Game {
'gamePicture': gamePicture,
'status': status,
'characters': characters,
'similarGames': similarGameIds,
};
}
}
24 changes: 24 additions & 0 deletions app/mobile/lib/data/services/game_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class GameService {
final BaseNetworkService service = BaseNetworkService();

static const String _getGames = "/game";
static const String _getRecommendedGames = "/game/recommended";

static List<Game> gameList = [
Game(
gameId: 1,
Expand Down Expand Up @@ -167,6 +169,28 @@ Celeste has left a lasting impact on the indie gaming scene, inspiring other dev
}
}

Future<List<Game>> getRecommendedGames() async {
ServiceResponse<MultipleGameAsDTO> response =
await service.sendRequestSafe<EmptyResponse, MultipleGameAsDTO>(
_getRecommendedGames,
null,
MultipleGameAsDTO(),
'GET',
);

if (response.success) {
List<Game> games = response.responseConverted!.games!.map((e) => e.game!)
.where((game) => game.status == "APPROVED")
.toList();
if (games.isEmpty) {
games = await getGames();
}
return games;
} else {
throw Exception('Failed to load games');
}
}

Future<Game> getGame(int gameid) async {
if (NetworkConstants.useMockGameData) {
return getGameDataList()[gameid - 1];
Expand Down
24 changes: 24 additions & 0 deletions app/mobile/lib/data/services/post_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PostService {
static PostService get instance => _instance;

static const String _getPosts = "/forum/posts";
static const String _getRecommendedPosts = "/forum/posts/recommended";
static const String _getPost = "/forum/posts";
static const String _createPost = "/forum/posts";
static const String _updatePost = "/forum/posts";
Expand Down Expand Up @@ -162,6 +163,29 @@ class PostService {
}
}

Future<List<Post>> getRecommendedPosts() async {
ServiceResponse<MultipleContentAsDTO> response =
await service.sendRequestSafe<EmptyResponse, MultipleContentAsDTO>(
_getRecommendedPosts,
null,
MultipleContentAsDTO(),
'GET',
);
if (response.success) {
List<Post> posts = response.responseConverted!.posts!
.map((e) => e.content! as Post)
.toList();

if (posts.isEmpty) {
posts = await getPosts();
}

return posts;
} else {
throw Exception('Failed to load recommended post');
}
}

Future<Post> getPost(int postId) async {
if (NetworkConstants.useMockData) {
List<Post> posts = await getPosts();
Expand Down
13 changes: 10 additions & 3 deletions app/mobile/lib/presentation/pages/game_wiki_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,18 @@ class _GameWikiPageState extends State<GameWikiPage>

Future<Game> loadGame(int gameId) async {
Game game = await gameService.getGame(gameId);
List<Post> postList = await postService.getPosts();
List<Game> similarGames = await gameService.getGames();
List<Post> postList = await postService.getPostsByGame(gameId);
if (game.similarGameIds.isEmpty) {
game.similarGameList = await gameService.getRecommendedGames();
} else {
List<Game> allGames = await gameService.getGames();
List<Game> similarGames = allGames
.where((element) => game.similarGameIds.contains(element.gameId))
.toList();
game.similarGameList = similarGames;
}

game.relatedPosts = postList;
game.similarGameList = similarGames;

return game;
}
Expand Down
4 changes: 2 additions & 2 deletions app/mobile/lib/presentation/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class _HomePageState extends State<HomePage> {

Future<List<Post>> loadPosts() async {

List<Post> postList = await postService.getPosts();
List<Post> postList = await postService.getRecommendedPosts();

return postList;
}

Future<List<Game>> loadGames() async {

List<Game> gameList = await gameService.getGames();
List<Game> gameList = await gameService.getRecommendedGames();

return gameList;
}
Expand Down

0 comments on commit a42d3a8

Please sign in to comment.