Exemple #1
0
        //public async Task<IEnumerable<DistributionDept>> GetByNameAsync(string name)
        //{
        //    using (var db = new DistyModelContainer())
        //    {
        //        return await QueryAsync(u => u.Name == name, db);
        //    }
        //}

        public override async Task <int> SaveAsync(DistributionDept item)
        {
            try
            {
                if (item == null)
                {
                    throw new ArgumentNullException("item");
                }

                using (var db = new DistyModelContainer())
                {
                    var dbItem = Mapper.Map <DistributionDept, Dept>(item);
                    if (item.Id == 0)
                    {
                        db.Set <Dept>().Add(dbItem);
                    }
                    else
                    {
                        db.Set <Dept>().Attach(dbItem);
                        db.Entry(item).State = EntityState.Modified;
                    }

                    await db.SaveChangesAsync();

                    return(dbItem.Id);
                }
            }
            catch (DbEntityValidationException e)
            {
                var msg = HandleValidationError(e);
                _log.Error(string.Format("Error saving Disty list:\r\n  {0}", msg, e));
                throw new LoggedException(msg, e);
            }
        }
Exemple #2
0
 public virtual async Task <EmailAddress> GetAsync(int id)
 {
     using (var db = new DistyModelContainer())
     {
         return(await Task.FromResult <EmailAddress>(
                    Mapper.Map <Email, EmailAddress>(db.Set <Email>()
                                                     .Find(id))
                    ));
     }
 }
Exemple #3
0
 public virtual async Task <TEntity> GetAsync(int id)
 {
     using (var db = new DistyModelContainer())
     {
         return(await Task.FromResult(
                    Mapper.Map <TSetEntity, TEntity>(db.Set <TSetEntity>()
                                                     .Find(id))
                    ));
     }
 }
Exemple #4
0
 public virtual async Task <IEnumerable <TEntity> > GetAsync()
 {
     using (var db = new DistyModelContainer())
     {
         return
             (await
              Task.FromResult <IEnumerable <TEntity> >(
                  Enumerable.ToList(db.Set <TSetEntity>()
                                    .AsEnumerable()
                                    .Select(Mapper.Map <TSetEntity, TEntity>))));
     }
 }
Exemple #5
0
 public virtual async Task <IEnumerable <EmailAddress> > GetByListAsync(int listId)
 {
     using (var db = new DistyModelContainer())
     {
         return
             (await
              Task.FromResult <IEnumerable <EmailAddress> >(
                  db.Set <Email>()
                  .Include("List")
                  .AsEnumerable()
                  .Where(e => e.List.Id == listId)
                  .Select(Mapper.Map <Email, EmailAddress>)
                  .ToList()));
     }
 }
Exemple #6
0
        public virtual async void DeleteAsync(int id)
        {
            using (var db = new DistyModelContainer())
            {
                var obj = await db.Set <TSetEntity>().FindAsync(id);

                if (obj == null)
                {
                    return;
                }

                db.Set <TSetEntity>().Remove(obj);
                await db.SaveChangesAsync();
            }
        }
Exemple #7
0
        public override async Task <int> SaveAsync(DistributionList item)
        {
            try
            {
                // TODO Rework this to pull it up into the service.
                // TODO Also need to validate that the user has rights to save to this dept.
                using (var db = new DistyModelContainer())
                {
                    var dept = db.Depts.Find(item.DeptId);
                    if (dept == null)
                    {
                        var deptId = await _deptRepository.SaveAsync(new DistributionDept()
                        {
                            Name = item.Name
                        });

                        dept = db.Depts.Find(deptId);
                    }

                    var dbItem = Mapper.Map <DistributionList, List>(item);
                    dbItem.Dept = dept;
                    if (item.Id == 0)
                    {
                        db.Set <List>().Add(dbItem);
                    }
                    else
                    {
                        db.Set <List>().Attach(dbItem);
                        db.Entry(item).State = EntityState.Modified;
                    }

                    await db.SaveChangesAsync();

                    return(dbItem.Id);
                }
            }
            catch (DbEntityValidationException e)
            {
                var msg = HandleValidationError(e);
                _log.Error(string.Format("Error saving Disty list:\r\n  {0}", msg), e);
                throw new LoggedException(msg, e);
            }
        }
Exemple #8
0
        public override async Task <int> SaveAsync(EmailAddress item)
        {
            try
            {
                if (item == null)
                {
                    throw new ArgumentNullException("item");
                }

                using (var db = new DistyModelContainer())
                {
                    var dbItem = Mapper.Map <EmailAddress, Email>(item);
                    if (item.Id == 0)
                    {
                        var list = await db.Set <List>().FindAsync(item.ListId);

                        if (list == null)
                        {
                            throw new InvalidOperationException("Unable to find associated list.");
                        }

                        list.Emails.Add(dbItem);
                    }
                    else
                    {
                        db.Set <Email>().Attach(dbItem);
                        db.Entry(item).State = EntityState.Modified;
                    }

                    await db.SaveChangesAsync();

                    return(dbItem.Id);
                }
            }
            catch (DbEntityValidationException e)
            {
                var msg = HandleValidationError(e);
                _log.Error(string.Format("Error saving Disty list:\r\n  {0}", msg), e);
                throw new LoggedException(msg, e);
            }
        }