public void DTO2DB(DTO.WareHouseMng.WareHouse dtoItem, ref WareHouse dbItem)
        {
            // map child collection
            // delete case
            List <int> toBeDeletedAreaIDs = new List <int>();

            foreach (WareHouseArea toBeDeletedArea in dbItem.WareHouseArea)
            {
                if (!dtoItem.Areas.Select(o => o.WareHouseAreaID).Contains(toBeDeletedArea.WareHouseAreaID))
                {
                    toBeDeletedAreaIDs.Add(toBeDeletedArea.WareHouseAreaID);
                }
            }
            foreach (int toBeDeletedAreaID in toBeDeletedAreaIDs)
            {
                dbItem.WareHouseArea.Remove(dbItem.WareHouseArea.FirstOrDefault(o => o.WareHouseAreaID == toBeDeletedAreaID));
            }

            foreach (DTO.WareHouseMng.WareHouseArea dtoArea in dtoItem.Areas)
            {
                // add new case
                WareHouseArea dbArea = null;
                if (dtoArea.WareHouseAreaID <= 0)
                {
                    dbArea           = new WareHouseArea();
                    dbArea.WareHouse = dbItem;
                    dbItem.WareHouseArea.Add(dbArea);
                }
                else
                {
                    dbArea = dbItem.WareHouseArea.FirstOrDefault(o => o.WareHouseAreaID == dtoArea.WareHouseAreaID);
                }

                if (dbArea != null)
                {
                    AutoMapper.Mapper.Map <DTO.WareHouseMng.WareHouseArea, WareHouseArea>(dtoArea, dbArea);
                }
            }

            // map fields
            AutoMapper.Mapper.Map <DTO.WareHouseMng.WareHouse, WareHouse>(dtoItem, dbItem);
        }
Beispiel #2
0
        public IHttpActionResult Update(int id, DTO.WareHouseMng.WareHouse dtoItem)
        {
            Library.DTO.Notification notification;

            // authentication
            Module.Framework.BLL fwBll = new Module.Framework.BLL();
            if (id > 0 && !fwBll.CanPerformAction(ControllerContext.GetAuthUserId(), moduleCode, Library.DTO.ModuleAction.CanUpdate))
            {
                // edit case
                return(InternalServerError(new Exception(Properties.Resources.NOT_AUTHORIZED)));
            }
            else if (id == 0 && !fwBll.CanPerformAction(ControllerContext.GetAuthUserId(), moduleCode, Library.DTO.ModuleAction.CanCreate))
            {
                // create new case
                return(InternalServerError(new Exception(Properties.Resources.NOT_AUTHORIZED)));
            }

            // validation
            if (!Helper.CommonHelper.ValidateDTO <DTO.WareHouseMng.WareHouse>(dtoItem, out notification))
            {
                return(Ok(new Library.DTO.ReturnData <DTO.WareHouseMng.WareHouse>()
                {
                    Data = dtoItem, Message = notification
                }));
            }

            // continue processing
            BLL.WareHouseMng bll = new BLL.WareHouseMng();
            bll.UpdateData(id, ref dtoItem, ControllerContext.GetAuthUserId(), out notification);
            if (notification.Type == Library.DTO.NotificationType.Error)
            {
                return(InternalServerError(new Exception(notification.Message)));
            }
            return(Ok(new Library.DTO.ReturnData <DTO.WareHouseMng.WareHouse>()
            {
                Data = dtoItem, Message = notification
            }));
        }