public virtual async Task <ICollection <TEntity> > FindAll() { try { if (dbc.Connection.State != ConnectionState.Open) { await dbc.Open(); } using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection)) { using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) { ICollection <TEntity> entities = new List <TEntity>(); while (await reader.ReadAsync()) { entities.Add(MapToObject.Execute(reader)); } logger.LogInformation($"FindAll complete for {typeof(TEntity)} entity."); return(entities); } } } catch (SqlException ex) { logger.LogError(ex.Message); return(null); } }
/// <summary> /// This method populate pokemon list by /// generating series of mock models from json data and mapping them to base model /// </summary> public void SetPokemons(int offset, int limit) { var newPokemonList = new PokemonListJson(); var uriList = new List <Uri>(); // Uri of current APi end point of pokemon list var uri = new Uri("https://pokeapi.co/api/v2/pokemon?offset=" + offset + "&limit=" + limit); // Mapping and getting json helper model of PokemonListJson newPokemonList = (PokemonListJson)MapToObject.MapJsonToModel(uri, newPokemonList); // if PokemonListJson is null stop and return (Api or http services not working) if (newPokemonList == null) { return; } // setup total count of pokemon for the model _totalCount = newPokemonList.Count; // loop-through jsonHelper model and get uri of all pokemon foreach (var result in newPokemonList.Results) { uriList.Add(result.Url); } // innit empty SimplifiedPokemon list _pokemonListModel = new List <SimplifiedPokemon>(); // loop-through uri list and generate pokemon models using GeneratePokemon method foreach (var tempUri in uriList) { _pokemonListModel.Add(GeneratePokemon(tempUri)); } }
public virtual async Task <ICollection <TEntity> > FindAllPaged(int offset, int pageSize) { try { if (dbc.Connection.State != ConnectionState.Open) { await dbc.Open(); } using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@p1", offset)); cmd.Parameters.Add(new SqlParameter("@p2", pageSize)); using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) { ICollection <TEntity> entities = new List <TEntity>(); while (await reader.ReadAsync()) { entities.Add(MapToObject.Execute(reader)); } logger.LogInformation($"FindAllPaged complete for {typeof(TEntity)} entity."); return(entities); } } } catch (SqlException ex) { logger.LogError(ex.Message); return(null); } }
public void SetPokemon(int id) { var tempPokemonJson = new PokemonJson(); var uri = new Uri("https://pokeapi.co/api/v2/pokemon/" + id + "/"); tempPokemonJson = (PokemonJson)MapToObject.MapJsonToModel(uri, tempPokemonJson); _pokemonModel = MapModels(tempPokemonJson); }
/// <summary> /// This method generate a base model SimplifiedPokemon from a given api endpoint /// </summary> /// <param name="pokemonUri"> Uri of a pokemon </param> /// <returns> /// A SimplifiedPokemon or null of no data form Api /// </returns> private SimplifiedPokemon GeneratePokemon(Uri pokemonUri) { var tempPokemonJson = new SimplifiedPokemonJson(); // Mapping and getting json helper model of SimplifiedPokemonJson tempPokemonJson = (SimplifiedPokemonJson)MapToObject.MapJsonToModel(pokemonUri, tempPokemonJson); var newPokemon = MapModels(tempPokemonJson); return(newPokemon); }
public virtual async Task <TEntity> FindByPK(IPrimaryKey pk) { int idx = 1; TEntity entity = null; try { if (dbc.Connection.State != ConnectionState.Open) { await dbc.Open(); } using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection)) { if (SqlCommandType == Constants.DBCommandType.SPROC) { cmd.CommandType = CommandType.StoredProcedure; } if (pk.IsComposite) { foreach (int k in ((PrimaryKey)pk).CompositeKey) { cmd.Parameters.Add(new SqlParameter("@pk" + idx.ToString(), k)); idx++; } } else { cmd.Parameters.Add(new SqlParameter("@pk", pk.Key)); } using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) { if (reader.Read()) { entity = MapToObject.Execute(reader); } else { entity = null; } logger.LogInformation($"FindByPK complete for {typeof(TEntity)} entity."); } } } catch (SqlException ex) { logger.LogError(ex.Message); return(null); } return(entity); }
/// <summary> /// This helper function get ability data from given api endpoint /// </summary> /// <param name="abilityUri"> Api endpoint of ability data</param> /// <returns> /// tuple with (ability name, ability description) /// </returns> private Tuple <string, string> GetAbilityTuple(Uri abilityUri) { var tempAbilityJson = new AbilityJson(); // get Json helper model of AbilityJson from uri tempAbilityJson = (AbilityJson)MapToObject.MapJsonToModel(abilityUri, tempAbilityJson); // get values var name = tempAbilityJson.Name; var desc = tempAbilityJson.EffectEntries.First().ShortEffect; return(Tuple.Create(name, desc)); }
public virtual TEntity FindByPK(IPrimaryKey pk) { int idx = 1; TEntity entity = null; try { if (dbc.Connection.State != ConnectionState.Open) { dbc.Open(); } using (SqliteCommand cmd = new SqliteCommand(CMDText, dbc.Connection)) { if (pk.IsComposite) { foreach (int k in ((PrimaryKey)pk).CompositeKey) { cmd.Parameters.Add(new SqliteParameter("@pk" + idx.ToString(), k)); idx++; } } else { cmd.Parameters.Add(new SqliteParameter("@pk", pk.Key)); } using (SqliteDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { entity = MapToObject.Execute(reader); } else { entity = null; } logger.LogInformation($"FindByPK complete for {typeof(TEntity)} entity."); } } } catch (SqliteException ex) { logger.LogError(ex.Message); return(null); } return(entity); }
public virtual async Task <ICollection <TEntity> > FindAll(IList <SqlParameter> parms) { ICollection <TEntity> entities; try { if (dbc.Connection.State != ConnectionState.Open) { await dbc.Open(); } using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection)) { if (SqlCommandType == Constants.DBCommandType.SPROC) { cmd.CommandType = CommandType.StoredProcedure; } foreach (SqlParameter parm in parms) { cmd.Parameters.Add(parm); } using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) { entities = new List <TEntity>(); while (await reader.ReadAsync()) { entities.Add(MapToObject.Execute(reader)); } logger.LogInformation($"FindAll(SqlParameter) complete for {typeof(TEntity)} entity."); return(entities); } } } catch (SqlException ex) { logger.LogError(ex.Message); return(null); } }
public virtual async Task <TEntity> FindByPK(IPrimaryKey pk) { TEntity entity = null; try { if (dbc.Connection.State != ConnectionState.Open) { await dbc.Open(); } using (SqlCommand cmd = new SqlCommand(CMDText, dbc.Connection)) { cmd.Parameters.Add(new SqlParameter("@pk", pk.Key)); using (SqlDataReader reader = await cmd.ExecuteReaderAsync()) { if (reader.Read()) { entity = MapToObject.Execute(reader); } else { entity = null; } logger.LogInformation($"FindByPK complete for {typeof(TEntity)} entity."); } } } catch (SqlException ex) { logger.LogError(ex.Message); return(null); } return(entity); }