Пример #1
0
        public object UpdatePerson(int userID, int id, System.Collections.Hashtable filters, out Library.DTO.Notification notification)
        {
            notification      = new Library.DTO.Notification();
            notification.Type = Library.DTO.NotificationType.Success;

            DTO.BifaPerson dtoItem = ((Newtonsoft.Json.Linq.JObject)filters["view"]).ToObject <DTO.BifaPerson>();

            try
            {
                using (var context = CreateContext())
                {
                    BifaPerson dbItem;
                    if (id == 0)
                    {
                        dbItem = new BifaPerson();
                        context.BifaPerson.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.BifaPerson.FirstOrDefault(o => o.BifaPersonID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Type    = Library.DTO.NotificationType.Error;
                        notification.Message = "Can not found data";
                        return(null);
                    }

                    converter.DTO2DB_BifaPerson(userID, dtoItem, ref dbItem);

                    context.BifaEmailAddress.Local.Where(o => o.BifaPerson == null).ToList().ForEach(o => context.BifaEmailAddress.Remove(o));
                    context.BifaTelephone.Local.Where(o => o.BifaPerson == null).ToList().ForEach(o => context.BifaTelephone.Remove(o));

                    context.SaveChanges();

                    dtoItem = AutoMapper.Mapper.Map <BifaCompanyMng_BifaPerson_View, DTO.BifaPerson>(context.BifaCompanyMng_BifaPerson_View.FirstOrDefault(o => o.BifaPersonID == dbItem.BifaPersonID));
                }
            }
            catch (System.Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Helper.GetInnerException(ex).Message;
            }

            return(dtoItem);
        }
Пример #2
0
        public void DTO2DB_BifaPerson(int userID, DTO.BifaPerson dtoItem, ref BifaPerson dbItem)
        {
            AutoMapper.Mapper.Map <DTO.BifaPerson, BifaPerson>(dtoItem, dbItem);
            dbItem.DateOfBirth   = dtoItem.DateOfBirth.ConvertStringToDateTime();
            dbItem.UpdatedBy     = userID;
            dbItem.UpdatedDate   = System.DateTime.Now;
            dbItem.BifaCompanyID = dtoItem.BifaCompanyID;

            if (dtoItem.BifaEmailAddresses != null)
            {
                foreach (var item in dbItem.BifaEmailAddress.ToArray())
                {
                    if (!dtoItem.BifaEmailAddresses.Select(s => s.BifaEmailAddressID).Contains(item.BifaEmailAddressID))
                    {
                        dbItem.BifaEmailAddress.Remove(item);
                    }
                }

                foreach (var dtoEmailAddress in dtoItem.BifaEmailAddresses.ToArray())
                {
                    BifaEmailAddress dbEmailAddress;

                    if (dtoEmailAddress.BifaEmailAddressID < 0)
                    {
                        dbEmailAddress = new BifaEmailAddress();
                        dbItem.BifaEmailAddress.Add(dbEmailAddress);
                    }
                    else
                    {
                        dbEmailAddress = dbItem.BifaEmailAddress.FirstOrDefault(s => s.BifaEmailAddressID == dtoEmailAddress.BifaEmailAddressID);
                    }

                    if (dbEmailAddress != null)
                    {
                        AutoMapper.Mapper.Map <DTO.BifaEmailAddress, BifaEmailAddress>(dtoEmailAddress, dbEmailAddress);
                        dbEmailAddress.UpdatedBy   = userID;
                        dbEmailAddress.UpdatedDate = System.DateTime.Now;
                    }
                }
            }

            if (dtoItem.BifaTelephones != null)
            {
                foreach (var item in dbItem.BifaTelephone.ToArray())
                {
                    if (!dtoItem.BifaTelephones.Select(s => s.BifaTelephoneID).Contains(item.BifaTelephoneID))
                    {
                        dbItem.BifaTelephone.Remove(item);
                    }
                }

                foreach (var dtoTelephone in dtoItem.BifaTelephones.ToArray())
                {
                    BifaTelephone dbTelephone;

                    if (dtoTelephone.BifaTelephoneID < 0)
                    {
                        dbTelephone = new BifaTelephone();
                        dbItem.BifaTelephone.Add(dbTelephone);
                    }
                    else
                    {
                        dbTelephone = dbItem.BifaTelephone.FirstOrDefault(s => s.BifaTelephoneID == dtoTelephone.BifaTelephoneID);
                    }

                    if (dbTelephone != null)
                    {
                        AutoMapper.Mapper.Map <DTO.BifaTelephone, BifaTelephone>(dtoTelephone, dbTelephone);
                        dbTelephone.UpdatedBy   = userID;
                        dbTelephone.UpdatedDate = System.DateTime.Now;
                    }
                }
            }
        }