Ejemplo n.º 1
0
        //public void DTO2BD(DTO.ProductionItemGroupDTO dtoItem, ref ProductionItemGroup dbItem)
        //{
        //    AutoMapper.Mapper.Map<DTO.ProductionItemGroupDTO, ProductionItemGroup>(dtoItem, dbItem);
        //    dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime();
        //}

        public void DTO2BD(DTO.ProductionItemGroupDTO dtoItem, ref ProductionItemGroup dbItem)
        {
            if (dtoItem.ListProductionItemGroupNotification != null)
            {
                foreach (ProductionItemGroupNotification item in dbItem.ProductionItemGroupNotification.ToArray())
                {
                    if (!dtoItem.ListProductionItemGroupNotification.Select(x => x.ProductionItemGroupNotificationID).Contains(item.ProductionItemGroupNotificationID))
                    {
                        dbItem.ProductionItemGroupNotification.Remove(item);
                    }
                }
                foreach (DTO.ProductionItemGroupNotificationDTO dto in dtoItem.ListProductionItemGroupNotification)
                {
                    ProductionItemGroupNotification item;
                    if (dto.ProductionItemGroupNotificationID <= 0)
                    {
                        item = new ProductionItemGroupNotification();
                        dbItem.ProductionItemGroupNotification.Add(item);
                    }
                    else
                    {
                        item = dbItem.ProductionItemGroupNotification.FirstOrDefault(s => s.ProductionItemGroupNotificationID == dto.ProductionItemGroupNotificationID);
                    }
                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductionItemGroupNotificationDTO, ProductionItemGroupNotification>(dto, item);
                    }
                }

                foreach (ProductionItemGroupStockNotification item in dbItem.ProductionItemGroupStockNotification.ToArray())
                {
                    if (!dtoItem.ListProductionItemGroupStockNotification.Select(x => x.ProductionItemGroupStockNotificationID).Contains(item.ProductionItemGroupStockNotificationID))
                    {
                        dbItem.ProductionItemGroupStockNotification.Remove(item);
                    }
                }
                foreach (DTO.ProductionItemGroupStockNotificationDTO dto in dtoItem.ListProductionItemGroupStockNotification)
                {
                    ProductionItemGroupStockNotification item;
                    if (dto.ProductionItemGroupStockNotificationID <= 0)
                    {
                        item = new ProductionItemGroupStockNotification();
                        dbItem.ProductionItemGroupStockNotification.Add(item);
                    }
                    else
                    {
                        item = dbItem.ProductionItemGroupStockNotification.FirstOrDefault(s => s.ProductionItemGroupStockNotificationID == dto.ProductionItemGroupStockNotificationID);
                    }
                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductionItemGroupStockNotificationDTO, ProductionItemGroupStockNotification>(dto, item);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.ProductionItemGroupDTO, ProductionItemGroup>(dtoItem, dbItem);
            //AutoMapper.Mapper.Map<DTO.ProductionItemGroupDTO, ProductionItemGroup>(dtoItem, dbItem);
            //dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime();
        }
Ejemplo n.º 2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.ProductionItemGroupDTO dtoProductionItemGroup = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductionItemGroupDTO>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int    number;
            string indexName;

            try
            {
                using (ProductionItemGroupEntities context = CreateContext())
                {
                    ProductionItemGroup dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ProductionItemGroup();
                        context.ProductionItemGroup.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.ProductionItemGroup.FirstOrDefault(o => o.ProductionItemGroupID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Production Item Group not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2BD(dtoProductionItemGroup, ref dbItem);

                        dbItem.UpdatedDate = DateTime.Now;
                        dbItem.UpdatedBy   = userId;
                        context.ProductionItemGroupNotification.Local.Where(o => o.ProductionItemGroup == null).ToList().ForEach(o => context.ProductionItemGroupNotification.Remove(o));
                        context.ProductionItemGroupStockNotification.Local.Where(o => o.ProductionItemGroup == null).ToList().ForEach(o => context.ProductionItemGroupStockNotification.Remove(o));
                        context.SaveChanges();
                        dtoItem = GetData(userId, dbItem.ProductionItemGroupID, out notification).Data;

                        return(true);
                    }
                }
            }

            catch (System.Data.DataException dEx)
            {
                notification.Type = Library.DTO.NotificationType.Error;
                Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName);
                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    switch (indexName)
                    {
                    case "IX_ProductionItemGroupUDUnique":
                        notification.Message = "Code must be unique! Please select other code!";
                        break;
                    }
                }
                else
                {
                    notification.Message = dEx.Message;
                }
                return(false);
            }
        }