public static BoosterPack ToModel(this BoosterPackEntity entity) { var boosterpack = new BoosterPack { Name = entity.Name, Url = entity.Url, OcgEcists = entity.OcgExists, TcgExists = entity.TcgExists, Dates = entity.Dates.Select(dateEntity => dateEntity.ToModel()).ToList(), Cards = entity.Cards.Select(cardEntity => cardEntity.ToModel()).ToList() }; return(boosterpack); }
public async Task <BoosterPackEntity> GetBoosterPackAsync(string input) { await using var connection = _config.GetYuGiOhDbConnection(); await connection.OpenAsync().ConfigureAwait(false); BoosterPackEntity entity = null; var dateEntityDict = new Dictionary <string, BoosterPackDateEntity>(); var cardEntityDict = new Dictionary <string, BoosterPackCardEntity>(); await connection.QueryAsync <BoosterPackEntity, BoosterPackDateEntity, BoosterPackCardEntity, string, BoosterPackEntity>( "select * from joined_boosterpacks where name ilike @input", ( boosterpackEntity, dateEntity, cardEntity, rarity ) => { if (entity is null) { entity = boosterpackEntity; entity.Cards = new List <BoosterPackCardEntity>(); entity.Dates = new List <BoosterPackDateEntity>(); } if (!dateEntityDict.TryGetValue(dateEntity.Name, out var dateBpEntity)) { dateBpEntity = dateEntity; dateEntityDict[dateEntity.Name] = dateBpEntity; } if (!cardEntityDict.TryGetValue(cardEntity.Name, out var cardBpEntity)) { cardBpEntity = cardEntity; cardBpEntity.Rarities = new List <string>(); cardEntityDict[cardBpEntity.Name] = cardBpEntity; } if (!entity.Dates.Contains(dateBpEntity)) { entity.Dates.Add(dateBpEntity); } if (!entity.Cards.Contains(cardBpEntity)) { entity.Cards.Add(cardBpEntity); } if (!cardBpEntity.Rarities.Contains(rarity)) { cardBpEntity.Rarities.Add(rarity); } return(entity); }, new { input }, splitOn : "boosterpackdatename,boosterpackcardname,boosterpackrarity" ) .ConfigureAwait(false); return(entity); }
public async Task InsertBoosterPack(BoosterPackEntity boosterPack) { await using var connection = _config.GetYuGiOhDbConnection(); await connection.OpenAsync().ConfigureAwait(false); var doesExistBit = await connection.ExecuteScalarAsync <int>("select count(1) from boosterpacks where id = @Id", new { boosterPack.Id }).ConfigureAwait(false); if (doesExistBit == 1) { await connection.UpdateAsync(boosterPack).ConfigureAwait(false); } else { await connection.InsertAsync(boosterPack).ConfigureAwait(false); } var(datesId, cardsId) = await connection.QuerySingleAsync <(int, int)>("select dates, cards from boosterpacks where id = @id", new { id = boosterPack.Id }).ConfigureAwait(false); // var datesId = await connection.QuerySingleAsync<int>("select dates from boosterpacks where id = @id", new { id = boosterPack.Id }).ConfigureAwait(false); // var cardsId = await connection.QuerySingleAsync<int>("select cards from boosterpacks where id = @id", new { id = boosterPack.Id }).ConfigureAwait(false); foreach (var date in boosterPack.Dates) { date.BoosterPackDatesId = datesId; await connection.ExecuteAsync( "upsert_boosterpack_date", new { boosterpackdatesid = date.BoosterPackDatesId, name = date.Name, date = date.Date }, commandType : CommandType.StoredProcedure) .ConfigureAwait(false); } foreach (var card in boosterPack.Cards) { card.BoosterPackCardsId = cardsId; card.Id = await connection.QuerySingleAsync <int>( "insert_or_get_boosterpack_card", new { _boosterpackcardsid = card.BoosterPackCardsId, _name = card.Name }, commandType : CommandType.StoredProcedure) .ConfigureAwait(false); var raritiesId = await connection.QuerySingleAsync <int>("select rarities from boosterpack_cards where id = @id", new { id = card.Id }).ConfigureAwait(false); foreach (var rarity in card.Rarities) { await connection.ExecuteAsync( "insert into boosterpack_rarities(boosterpackraritiesid, name) values(@boosterpackraritiesid, @name) on conflict on constraint boosterpack_rarities_boosterpackraritiesid_name_unique_pair do nothing", new { boosterpackraritiesid = raritiesId, name = rarity }) .ConfigureAwait(false); } } }