public void DTO2DB_ProductBreakDownDefaultCategory(DTO.ProductBreakDownDefaultCategoryData dtoItem, ref ProductBreakDownDefaultCategory dbItem)
 {
     AutoMapper.Mapper.Map <DTO.ProductBreakDownDefaultCategoryData, ProductBreakDownDefaultCategory>(dtoItem, dbItem);
 }
Beispiel #2
0
        public object UpdateData(int userId, Hashtable filters, out Notification notification)
        {
            DTO.EditFormData data = new DTO.EditFormData();

            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                int typeUpdate = (filters.ContainsKey("typeGet") && filters["typeGet"] != null && !string.IsNullOrEmpty(filters["typeGet"].ToString())) ? Convert.ToInt32(filters["typeGet"].ToString()) : 0;
                int id         = (filters.ContainsKey("id") && filters["id"] != null && !string.IsNullOrEmpty(filters["id"].ToString())) ? Convert.ToInt32(filters["id"].ToString()) : 0;

                switch (typeUpdate)
                {
                case 1:     // Update data ProductBreakDownDefaultCategory
                    DTO.ProductBreakDownDefaultCategoryData dtoItem_1 = ((Newtonsoft.Json.Linq.JObject)filters["dataView"]).ToObject <DTO.ProductBreakDownDefaultCategoryData>();

                    using (var context = CreateContext())
                    {
                        ProductBreakDownDefaultCategory dbItem;

                        if (id == 0)
                        {
                            dbItem = new ProductBreakDownDefaultCategory();
                            context.ProductBreakDownDefaultCategory.Add(dbItem);
                        }
                        else
                        {
                            dbItem = context.ProductBreakDownDefaultCategory.FirstOrDefault(o => o.ProductBreakDownDefaultCategoryID == id);
                        }

                        // Check dbItem is null
                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not find data";

                            return(null);
                        }

                        // Mapping data
                        converter.DTO2DB_ProductBreakDownDefaultCategory(dtoItem_1, ref dbItem);

                        // Update createdBy, createdDate, updatedBy, updatedDate
                        if (id == 0)
                        {
                            dbItem.CreatedBy   = userId;
                            dbItem.CreatedDate = DateTime.Now;
                        }
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // Update display order
                        //int displayOrder = context.ProductBreakDownDefaultCategory.Count();
                        //if (id == 0 && !dtoItem_1.DisplayOrder.HasValue)
                        //{
                        //    dbItem.DisplayOrder = displayOrder + 1;
                        //}
                        //else
                        //{
                        //    dbItem.DisplayOrder = displayOrder;
                        //}

                        context.SaveChanges();

                        filters["id"] = dbItem.ProductBreakDownDefaultCategoryID;
                    }

                    return(GetData(filters, out notification).DefaultData);

                case 2:     // Update data ProductBreakDown
                    DTO.ProductBreakDownData dtoItem_2 = ((Newtonsoft.Json.Linq.JObject)filters["dataView"]).ToObject <DTO.ProductBreakDownData>();

                    using (var context = CreateContext())
                    {
                        ProductBreakDown dbItem;

                        if (id == 0)
                        {
                            dbItem = new ProductBreakDown();
                            context.ProductBreakDown.Add(dbItem);
                        }
                        else
                        {
                            dbItem = context.ProductBreakDown.FirstOrDefault(o => o.ProductBreakDownID == id);
                        }

                        // Check dbItem is null
                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not find data";

                            return(null);
                        }

                        converter.DTO2DB_ProductBreakDown(dtoItem_2, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);

                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        context.ProductBreakDownDetail.Local.Where(o => o.ProductBreakDownCategoryType == null).ToList().ForEach(o => context.ProductBreakDownDetail.Remove(o));
                        context.ProductBreakDownCategoryType.Local.Where(o => o.ProductBreakDownCategory == null).ToList().ForEach(o => context.ProductBreakDownCategoryType.Remove(o));
                        context.ProductBreakDownCategoryImage.Local.Where(o => o.ProductBreakDownCategory == null).ToList().ForEach(o => context.ProductBreakDownCategoryImage.Remove(o));
                        context.ProductBreakDownCategory.Local.Where(o => o.ProductBreakDown == null).ToList().ForEach(o => context.ProductBreakDownCategory.Remove(o));

                        context.SaveChanges();

                        filters["id"] = dbItem.ProductBreakDownID;
                    }

                    return(GetData(filters, out notification).MainData);

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
            }

            return(data);
        }