Skip to content

Commit

Permalink
Update ControllerWith Generic, TODO mapper with object
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyveth committed Nov 29, 2023
1 parent d05f3f3 commit f623e8c
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 198 deletions.
9 changes: 0 additions & 9 deletions WebApiScrapingData.Domain/Class/TypePok.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,46 @@ namespace WebApiScrapingData.Domain.Class
[DataContract]
public class TypePok : Identity
{
//French Name
[DataMember(Name = DataMember.Name_FR)]
public string? Name_FR { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_FR)]
public string? PathMiniHome_FR { get; set; }

//English Name
[DataMember(Name = DataMember.Name_EN)]
public string? Name_EN { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_EN)]
public string? PathMiniHome_EN { get; set; }

//Spanish Name
[DataMember(Name = DataMember.Name_ES)]
public string? Name_ES { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_ES)]
public string? PathMiniHome_ES { get; set; }

//Italian Name
[DataMember(Name = DataMember.Name_IT)]
public string? Name_IT { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_IT)]
public string? PathMiniHome_IT { get; set; }

//German Name
[DataMember(Name = DataMember.Name_DE)]
public string? Name_DE { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_DE)]
public string? PathMiniHome_DE { get; set; }

//Russian Name
[DataMember(Name = DataMember.Name_RU)]
public string? Name_RU { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_RU)]
public string? PathMiniHome_RU { get; set; }

//Korean Name
[DataMember(Name = DataMember.Name_CO)]
public string? Name_CO { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_CO)]
public string? PathMiniHome_CO { get; set; }

//Chinese Name
[DataMember(Name = DataMember.Name_CN)]
public string? Name_CN { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_CN)]
public string? PathMiniHome_CN { get; set; }

//Japanese Name
[DataMember(Name = DataMember.Name_JP)]
public string? Name_JP { get; set; }
[DataMember(Name = DataMember.UrlMiniHome_JP)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public override async Task<IEnumerable<Attaque>> Find(Expression<Func<Attaque, b
return await this._context.Attaques.Include(m => m.typeAttaque).Include(m => m.typePok).Where(predicate ?? (s => true)).ToListAsync();
}

public override async Task<Attaque> Get(int id)
public override async Task<Attaque?> Get(int id)
{
return await this._context.Attaques.Include(m => m.typeAttaque).Include(m => m.typePok).SingleAsync(x => x.Id.Equals(id));
}

public async Task<Attaque> GetByName(string name)
public async Task<Attaque?> GetByName(string name)
{
return await this._context.Attaques.Include(m => m.typeAttaque).Include(m => m.typePok).SingleAsync(x => x.Name_FR.Equals(name));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using WebApiScrapingData.Infrastructure.Repository.Generic;
using WebApiScrapingData.Infrastructure.Repository.Generic;
using WebApiScrapingData.Domain.Class;
using WebApiScrapingData.Infrastructure.Data;

Expand All @@ -10,14 +9,5 @@ public class GameRepository : Repository<Game>
#region Constructor
public GameRepository(ScrapingContext context) : base(context) { }
#endregion

#region Public Methods
#region Read
public async Task<Game> GetByName(string name)
{
return await this._context.Games.SingleAsync(x => x.Name_FR.Equals(name));
}
#endregion
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,28 @@ public async Task SaveJsonInDb(string json)
#endregion

#region Read
public IEnumerable<Pokemon> Find(Expression<Func<Pokemon, bool>> predicate)
public override async Task<IEnumerable<Pokemon>> Find(Expression<Func<Pokemon, bool>> predicate)
{
return _context.Pokemons
.Include("FR")
.Include("EN")
.Include("ES")
.Include("IT")
.Include("DE")
.Include("RU")
.Include("CO")
.Include("CN")
.Include("JP")
return await _context.Pokemons
.Include(m => m.FR)
.Include(m => m.EN)
.Include(m => m.ES)
.Include(m => m.IT)
.Include(m => m.DE)
.Include(m => m.RU)
.Include(m => m.CO)
.Include(m => m.CN)
.Include(m => m.JP)
.Include(m => m.Pokemon_TypePoks).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Weaknesses).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Talents).ThenInclude(u => u.Talent)
.Where(predicate)
.AsQueryable();
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typePok)
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typeAttaque)
.Include(m => m.Game)
.Where(predicate ?? (s => true)).ToListAsync();
}

public async Task<Pokemon> Get(int id)
public override async Task<Pokemon?> Get(int id)
{
return await _context.Pokemons
.Include(m => m.FR)
Expand All @@ -87,30 +89,36 @@ public async Task<Pokemon> Get(int id)
.Include(m => m.CO)
.Include(m => m.CN)
.Include(m => m.JP)
//.Include(m => m.Pokemon_TypePoks).ThenInclude(u => u.TypePok)
//.Include(m => m.Pokemon_Weaknesses).ThenInclude(u => u.TypePok)
//.Include(m => m.Pokemon_Talents).ThenInclude(u => u.Talent)
//.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typePok)
//.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typeAttaque)
.Include(m => m.Pokemon_TypePoks).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Weaknesses).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Talents).ThenInclude(u => u.Talent)
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typePok)
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typeAttaque)
.SingleAsync(x => x.Id.Equals(id));
}

public IQueryable<Pokemon> Query()
public override IQueryable<Pokemon> Query()
{
return _context.Pokemons
.Include("FR")
.Include("EN")
.Include("ES")
.Include("IT")
.Include("DE")
.Include("RU")
.Include("CO")
.Include("CN")
.Include("JP")
.Include(m => m.FR)
.Include(m => m.EN)
.Include(m => m.ES)
.Include(m => m.IT)
.Include(m => m.DE)
.Include(m => m.RU)
.Include(m => m.CO)
.Include(m => m.CN)
.Include(m => m.JP)
.Include(m => m.Pokemon_TypePoks).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Weaknesses).ThenInclude(u => u.TypePok)
.Include(m => m.Pokemon_Talents).ThenInclude(u => u.Talent)
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typePok)
.Include(m => m.Pokemon_Attaques).ThenInclude(u => u.Attaque).ThenInclude(u => u.typeAttaque)
.Include(m => m.Game)
.AsQueryable();
}

public async Task<IEnumerable<Pokemon>> GetAll()
public override async Task<IEnumerable<Pokemon>> GetAll()
{
return await _context.Pokemons
.Include(m => m.FR)
Expand Down Expand Up @@ -146,20 +154,20 @@ public async Task<IEnumerable<Pokemon>> GetAllLight()
.Include(m => m.Pokemon_TypePoks).ThenInclude(u => u.TypePok)
.ToListAsync();
}

public Task<List<Pokemon>> GetFamilyWithoutVariantAsync(string family)
public async Task<List<Pokemon>> GetFamilyWithoutVariantAsync(string family)
{
string[] vs = family.Split(',');
List<Pokemon> result = new List<Pokemon>();

foreach (var item in vs)
{
Pokemon pokemon = Find(m => m.FR.Name.Equals(item)).FirstOrDefault();
Pokemon pokemon = Find(m => m.FR.Name.Equals(item)).Result.FirstOrDefault();
if (pokemon != null)
result.Add(pokemon);
}

return Task.FromResult(result);
return await Task.FromResult(result);
}

public async Task<IEnumerable<Pokemon>> GetAllVariantAsync(string number)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq.Expressions;
using WebApiScrapingData.Core.Repositories;
using WebApiScrapingData.Domain.Interface;
Expand All @@ -14,10 +13,12 @@ public class Repository<T> : IRepository<T> where T : class, ITIdentity
protected readonly ScrapingContext _context;
#endregion

#region Constructor
public Repository(ScrapingContext context)
{
_context = context;
}
#endregion

#region Public Methods
#region Read
Expand Down Expand Up @@ -95,11 +96,13 @@ public bool RemoveRange(IEnumerable<T> entities)
}
#endregion

#region Import/Export
public Task SaveJsonInDb(string json)
{
throw new NotImplementedException();
}
#endregion
#endregion

#region Private Methods
private void UpdateInfo(T entity, bool edit = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class GenericController<T, D, R> : ControllerBase
#endregion

#region Constructor
public GenericController(ILogger<T> logger, GenericMapper<T,D> mapper, R repository)
protected GenericController(ILogger<T> logger, GenericMapper<T,D> mapper, R repository)
{
_logger = logger;
_mapper = mapper;
Expand Down
7 changes: 4 additions & 3 deletions WepApiScrapingData/Controllers/AttaqueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using WebApiScrapingData.Core;
using WebApiScrapingData.Domain.Class;
using WebApiScrapingData.Infrastructure.Repository.Class;
using WebApiScrapingData.Infrastructure.Repository.Generic;
using WepApiScrapingData.Controllers.Abstract;
using WepApiScrapingData.DTOs.Concrete;
using WepApiScrapingData.ExtensionMethods;
Expand All @@ -21,11 +20,13 @@ public AttaqueController(ILogger<Attaque> logger, GenericMapper<Attaque, Attaque
}
#endregion

#region Public Methods
[HttpGet]
[Route("FindByName/{name}")]
public async Task<IEnumerable<Attaque>> GetFindByName(string name)
public async Task<Attaque> GetFindByName(string name)
{
return await _repository.Find(m => m.Name_FR.Equals(name));
return await _repository.GetByName(name);
}
#endregion
}
}
32 changes: 8 additions & 24 deletions WepApiScrapingData/Controllers/PokemonController.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
using HtmlAgilityPack;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using WebApiScrapingData.Core;
using WebApiScrapingData.Domain.Body;
using WebApiScrapingData.Domain.Class;
using WebApiScrapingData.Infrastructure.Repository.Class;
using WepApiScrapingData.Controllers.Abstract;
using WepApiScrapingData.DTOs.Concrete;
using WepApiScrapingData.ExtensionMethods;

namespace WepApiScrapingData.Controllers
{
[ApiController]
[Route("api/v1.0/[controller]")]
[EnableCors(SecurityMethods.DEFAULT_POLICY)]
public class PokemonController : ControllerBase
public class PokemonController : GenericController<Pokemon, PokemonDto, PokemonRepository>
{
#region Fields
private readonly PokemonRepository _repository;
#endregion

#region Constructors
public PokemonController(PokemonRepository repository)
public PokemonController(ILogger<Pokemon> logger, GenericMapper<Pokemon, PokemonDto> mapper, PokemonRepository repository) : base(logger, mapper, repository)
{
_repository = repository;
}
#endregion

#region Public Methods
[HttpGet]
public async Task<IEnumerable<Pokemon>> GetAll()
{
return await _repository.GetAll();
}

[HttpGet]
[Route("{limit}/{max}")]
public async Task<IEnumerable<Pokemon>> GetAllinDB(int max = 20, bool limit = true)
Expand All @@ -53,23 +44,16 @@ public async Task<IEnumerable<Pokemon>> GetAllLight(int max = 20, bool limit = t
return pokemons;
}

[HttpGet]
[Route("{id}")]
public async Task<Pokemon> GetSingle(int id)
{
return await _repository.Get(id);
}

[HttpGet]
[Route("FindByName/{name}")]
public IEnumerable<Pokemon> GetFindByName(string name)
public Task<IEnumerable<Pokemon>> GetFindByName(string name)
{
return _repository.Find(m => m.FR.Name.Equals(name));
}

[HttpGet]
[Route("FindByNumber/{number}")]
public IEnumerable<Pokemon> GetFindByNumber(string number)
public Task<IEnumerable<Pokemon>> GetFindByNumber(string number)
{
return _repository.Find(m => m.Number.Equals(number));
}
Expand Down
Loading

0 comments on commit f623e8c

Please sign in to comment.