Esempio n. 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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
            }
        }