Skip to content

Commit

Permalink
refactor: Refactor TMDB data handling to use SearchResultData class
Browse files Browse the repository at this point in the history
Refactored TmdbDataTransformer to encapsulate result details into a new SearchResultData class. This change simplifies the method signatures and improves code readability by reducing parameter count. Added SearchResultData.cs to define the new data structure.
  • Loading branch information
DineshSolanki committed Jul 27, 2024
1 parent 95d2a19 commit 82584c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
12 changes: 12 additions & 0 deletions FoliCon/Models/Data/SearchResultData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace FoliCon.Models.Data;

public class SearchResultData
{
public dynamic Result { get; set; }
public string ResultType { get; set; }
public string FullFolderPath { get; set; }
public string Rating { get; set; }
public bool IsPickedById { get; set; }
public string FolderName { get; set; }
public string LocalPosterPath { get; set; }
}
80 changes: 41 additions & 39 deletions FoliCon/Modules/TMDB/TmdbDataTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ public void ResultPicked(dynamic result, string resultType, string fullFolderPat
var localPosterPath = Path.Combine(fullFolderPath, $"{IconUtils.GetImageName()}.png");
var posterUrl = GetPosterUrl(result.PosterPath, PosterSize.Original, client);

string type = PickResult(result, resultType, fullFolderPath, rating, isPickedById,out int id, folderName, localPosterPath);
var resultDetails = new SearchResultData
{
Result = result,
ResultType = resultType,
FullFolderPath = fullFolderPath,
Rating = rating,
IsPickedById = isPickedById,
FolderName = folderName,
LocalPosterPath = localPosterPath,
};
var type = PickResult(resultDetails, out var id);

if (!isPickedById && id != 0)
{
Expand Down Expand Up @@ -219,61 +229,53 @@ private static string PrepareRating(string resultType, string rating, dynamic re
return rating;
}

private string PickResult(dynamic result,
string resultType,
string fullFolderPath,
string rating,
bool isPickedById,
out int id,
string folderName,
string localPosterPath)
private string PickResult(SearchResultData details, out int id)
{
id = 0;
var mediaType = resultType;
switch (resultType)
var mediaType = details.ResultType;
switch (mediaType)
{
case MediaTypes.Tv:
{
var pickedResult = CastResult(isPickedById ? typeof(TvShow) : typeof(SearchTv), result);
var year = pickedResult.FirstAirDate?.Year.ToString(CultureInfo.InvariantCulture) ?? "";
AddToPickedList(pickedResult.Name, year, rating, fullFolderPath, folderName, localPosterPath);
id = pickedResult.Id;
break;
}
{
var pickedResult = CastResult(details.IsPickedById ? typeof(TvShow) : typeof(SearchTv), details.Result);
var year = pickedResult.FirstAirDate?.Year.ToString(CultureInfo.InvariantCulture) ?? "";
AddToPickedList(pickedResult.Name, year, details.Rating, details.FullFolderPath, details.FolderName, details.LocalPosterPath);
id = pickedResult.Id;
break;
}
case MediaTypes.Movie:
{
var pickedResult = CastResult(isPickedById ? typeof(Movie) : typeof(SearchMovie), result);
var year = pickedResult.ReleaseDate?.Year.ToString(CultureInfo.InvariantCulture) ?? "";
AddToPickedList(pickedResult.Title, year, rating, fullFolderPath, folderName, localPosterPath);
id = pickedResult.Id;
break;
}
{
var pickedResult = CastResult(details.IsPickedById ? typeof(Movie) : typeof(SearchMovie), details.Result);
var year = pickedResult.ReleaseDate?.Year.ToString(CultureInfo.InvariantCulture) ?? "";
AddToPickedList(pickedResult.Title, year, details.Rating, details.FullFolderPath, details.FolderName, details.LocalPosterPath);
id = pickedResult.Id;
break;
}
case MediaTypes.Collection:
{
var pickedResult = CastResult(isPickedById ? typeof(Collection) : typeof(SearchCollection), result);
AddToPickedList(pickedResult.Name, null, rating, fullFolderPath, folderName, localPosterPath);
id = pickedResult.Id;
break;
}
{
var pickedResult = CastResult(details.IsPickedById ? typeof(Collection) : typeof(SearchCollection), details.Result);
AddToPickedList(pickedResult.Name, null, details.Rating, details.FullFolderPath, details.FolderName, details.LocalPosterPath);
id = pickedResult.Id;
break;
}
case MediaTypes.Mtv:
mediaType = SelectMtvType(result, fullFolderPath, rating, isPickedById, out id, folderName, localPosterPath);
mediaType = SelectMtvType(details, out id);
break;
default: throw new InvalidDataException($"Invalid Result Type: {resultType}");
default: throw new InvalidDataException($"Invalid Result Type: {mediaType}");
}

return mediaType;
}

private string SelectMtvType(dynamic result, string fullFolderPath, string rating, bool isPickedById, out int id, string folderName, string localPosterPath)
private string SelectMtvType(SearchResultData details, out int id)
{
var mediaType = result.MediaType;
var mediaType = details.Result.MediaType;
id = 0;
details.ResultType = mediaType.ToString();
return mediaType switch
{
MediaType.Tv => PickResult(result, MediaTypes.Tv, fullFolderPath, rating, isPickedById, out id, folderName,
localPosterPath),
MediaType.Movie => PickResult(result, MediaTypes.Movie, fullFolderPath, rating, isPickedById, out id,
folderName, localPosterPath),
MediaType.Tv => PickResult(details, out id),
MediaType.Movie => PickResult(details, out id),
_ => mediaType
};
}
Expand Down

0 comments on commit 82584c8

Please sign in to comment.