Skip to content

Commit

Permalink
GetAllGenres EndPoint Added
Browse files Browse the repository at this point in the history
  • Loading branch information
AbreuHD committed Sep 9, 2024
1 parent bd9b664 commit 8f79ed0
Show file tree
Hide file tree
Showing 13 changed files with 777 additions and 50 deletions.
13 changes: 13 additions & 0 deletions Core.Application/DTOs/Genres/TmdbGenreApiResponseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Application.DTOs.Genres
{
public class TmdbGenreApiResponseDto
{
public List<TmdbGenreResponseDto>? genres { get; set; }
}
}
14 changes: 14 additions & 0 deletions Core.Application/DTOs/Genres/TmdbGenreResponseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Application.DTOs.Genres
{
public class TmdbGenreResponseDto
{
public int id { get; set; }
public string name { get; set; }
}
}
14 changes: 14 additions & 0 deletions Core.Application/DTOs/Genres/TmdbGenreResponseListDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Application.DTOs.Genres
{
public class TmdbGenreResponseListDto
{
public List<TmdbGenreResponseDto>? Movies { get; set; }
public List<TmdbGenreResponseDto>? Series { get; set; }
}
}
47 changes: 0 additions & 47 deletions Core.Application/Enum/Genres.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Core.Application.DTOs.General;
using Core.Application.Helpers.TMDB;
using Core.Application.Interface.Repositories;
using Core.Domain.Entities.GeneralMovie;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Core.Application.Features.GenreModule.Commands.GetAllGenres
{
public class GetAllGenresCommand : IRequest<GenericApiResponse<string>>
{
}
public class GetAllGenresCommandHandler : IRequestHandler<GetAllGenresCommand, GenericApiResponse<string>>
{
private readonly GetTMDBData _getTMDBData;
private readonly IGenreRepository _genreRepository;

public GetAllGenresCommandHandler(GetTMDBData getTMDBData, IGenreRepository genreRepository)
{
_getTMDBData = getTMDBData;
_genreRepository = genreRepository;
}

public async Task<GenericApiResponse<string>> Handle(GetAllGenresCommand request, CancellationToken cancellationToken)
{
try
{
int i = 0;
var res = await _getTMDBData.GetAllGenres();
foreach (var m in res.Movies)
{
var InDb = await _genreRepository.Exist(m.id);
if (!InDb)
{
i++;
await _genreRepository.AddAsync(new Genre
{
GenreID = m.id,
Name = m.name,
IsMovie = true
});
}
}
foreach (var m in res.Series)
{
var InDb = await _genreRepository.Exist(m.id);
if (!InDb)
{
i++;
await _genreRepository.AddAsync(new Genre
{
GenreID = m.id,
Name = m.name,
IsMovie = false
});
}
}
return new GenericApiResponse<string>
{
Statuscode = 200,
Message = $"{i} Genres Added Successfully",
Payload = HttpStatusCode.OK.ToString(),
Success = true
};
}
catch (Exception e)
{
return new GenericApiResponse<string>
{
Statuscode = 500,
Message = e.Message,
Payload = HttpStatusCode.InternalServerError.ToString(),
Success = false
};
}
}
}
}
19 changes: 17 additions & 2 deletions Core.Application/Helpers/TMDB/GetTMDBData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using AutoMapper;
using Core.Application.DTOs.Genres;
using Core.Application.DTOs.Scraping;
using Core.Application.DTOs.TMDB;
using Core.Application.Enums;
using Core.Domain.Entities.GeneralMovie;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
Expand All @@ -12,11 +14,13 @@ public class GetTMDBData
{
public readonly IConfiguration _configuration;
private readonly IMapper _mapper;
private readonly string TMDBAPIKEY;

public GetTMDBData(IConfiguration configuration, IMapper mapper)
{
_configuration = configuration;
_mapper = mapper;
TMDBAPIKEY = _configuration["TMDBAPIKey"] ?? "false";
}

public async Task<CheckedList> GetTMDBId(List<MovieWebDTO> movies)
Expand All @@ -27,8 +31,7 @@ public async Task<CheckedList> GetTMDBId(List<MovieWebDTO> movies)
{
try
{
var TMDBApiKey = _configuration["TMDBAPIKey"];
string TMDBData = new WebClient().DownloadString($"https://api.themoviedb.org/3/search/movie?api_key={TMDBApiKey}&language=es-MX&query={data.Name}&include_adult=true");
string TMDBData = new WebClient().DownloadString($"https://api.themoviedb.org/3/search/movie?api_key={TMDBAPIKEY}&language=es-MX&query={data.Name}&include_adult=true");
var result = JsonConvert.DeserializeObject<TMDBResponse>(TMDBData);
TMDBResult tmdb = result.results.FirstOrDefault();
Console.WriteLine("Getting TMDB Data");
Expand All @@ -53,5 +56,17 @@ public async Task<CheckedList> GetTMDBId(List<MovieWebDTO> movies)
MovieWebDTO = MovieData
};
}

public async Task<TmdbGenreResponseListDto> GetAllGenres()
{
var movies = new HttpClient().GetStringAsync($"https://api.themoviedb.org/3/genre/movie/list?api_key={TMDBAPIKEY}&language=en-us").Result;
var series = new HttpClient().GetStringAsync($"https://api.themoviedb.org/3/genre/tv/list?api_key={TMDBAPIKEY}&language=en-us").Result;

return new TmdbGenreResponseListDto
{
Movies = JsonConvert.DeserializeObject<TmdbGenreApiResponseDto>(movies).genres,
Series = JsonConvert.DeserializeObject<TmdbGenreApiResponseDto>(series).genres
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Core.Application.Interface.Repositories
{
public interface IGenreRepository : IGenericRepository<Genre>
{

Task<bool> Exist(int TmdbId);
}
}
1 change: 1 addition & 0 deletions Core.Domain/Entities/Movie/Genre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Genre : AuditableBase
{
public required string Name { get; set; }
public required int GenreID { get; set; }
public required bool IsMovie { get; set; } = false;

public ICollection<Genre_Movie> Genre_Movie { get; set; }
}
Expand Down
Loading

0 comments on commit 8f79ed0

Please sign in to comment.