Ejemplo n.º 1
0
        public async Task <bool> DeleteAsync(object id)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    var item = await db.CurrencyExchangeItems.FindAsync((Guid)id);

                    if (item != null)
                    {
                        var tracking = db.CurrencyExchangeItems.Remove(item);
                        await db.SaveChangesAsync();

                        return(tracking.State == EntityState.Deleted);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> UpdateAsync(CurrencyExchangeItem entity)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    var tracking = db.CurrencyExchangeItems.Update(entity);
                    await db.SaveChangesAsync();

                    return(tracking.State == EntityState.Modified);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task <bool> UpdateRangeAsync(IEnumerable <CurrencyExchangeItem> entities)
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    db.CurrencyExchangeItems.UpdateRange(entities);
                    await db.SaveChangesAsync();

                    return(true);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
                return(false);
            }
        }
Ejemplo n.º 4
0
        public async Task InitializeDatabaseAsync()
        {
            try
            {
                using (var db = new CurrencyDataContext())
                {
                    db.Database.EnsureCreated();
                    // Add some default currencies for the first time.
                    var count = await db.CurrencyExchangeItems.CountAsync();

                    if (count == 0)
                    {
                        var targetCodes = new List <string> {
                            "USD", "CNY", "NZD", "AUD"
                        };
                        var targets = new List <CurrencyExchangeItem>();
                        targetCodes.ForEach(x =>
                        {
                            var item = new CurrencyExchangeItem(new CurrencyItem {
                                Code = "USD"
                            }, new CurrencyItem {
                                Code = x
                            }, ++count);
                            targets.Add(item);
                        });
                        await db.CurrencyExchangeItems.AddRangeAsync(targets);

                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(e);
#endif
            }
        }