public IHttpActionResult Update(int id, DTO.ImageGalleryMng.ImageGallery 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.ImageGalleryMng.ImageGallery>(dtoItem, out notification)) { return(Ok(new Library.DTO.ReturnData <DTO.ImageGalleryMng.ImageGallery>() { Data = dtoItem, Message = notification })); } // continue processing BLL.ImageGalleryMng bll = new BLL.ImageGalleryMng(Helper.AuthHelper.GetCurrentUserFolder(ControllerContext)); bll.UpdateData(id, ref dtoItem, ControllerContext.GetAuthUserId(), out notification); return(Ok(new Library.DTO.ReturnData <DTO.ImageGalleryMng.ImageGallery>() { Data = dtoItem, Message = notification })); }
public void DTO2DB(DTO.ImageGalleryMng.ImageGallery dtoItem, ref ImageGallery dbItem) { // map fields AutoMapper.Mapper.Map <DTO.ImageGalleryMng.ImageGallery, ImageGallery>(dtoItem, dbItem); dbItem.UpdatedBy = dtoItem.UpdatedBy; dbItem.UpdatedDate = DateTime.Now; DateTime tmpDate; if (DateTime.TryParse(dtoItem.SampleImportDate, new System.Globalization.CultureInfo("vi-VN"), System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.SampleImportDate = tmpDate; } // map client detail if (dtoItem.ImageGalleryClients != null) { // check for child rows deleted foreach (ImageGalleryClient dbClient in dbItem.ImageGalleryClient.ToArray()) { if (!dtoItem.ImageGalleryClients.Select(o => o.ImageGalleryClientID).Contains(dbClient.ImageGalleryClientID)) { dbItem.ImageGalleryClient.Remove(dbClient); } } // map child rows foreach (DTO.ImageGalleryMng.ImageGalleryClient dtoClient in dtoItem.ImageGalleryClients) { ImageGalleryClient dbClient; if (dtoClient.ImageGalleryClientID <= 0) { dbClient = new ImageGalleryClient(); dbItem.ImageGalleryClient.Add(dbClient); } else { dbClient = dbItem.ImageGalleryClient.FirstOrDefault(o => o.ImageGalleryClientID == dtoClient.ImageGalleryClientID); } if (dbClient != null) { AutoMapper.Mapper.Map <DTO.ImageGalleryMng.ImageGalleryClient, ImageGalleryClient>(dtoClient, dbClient); } } } // map version if (dtoItem.ImageGalleryVersions != null) { // check for child rows deleted foreach (ImageGalleryVersion dbVersion in dbItem.ImageGalleryVersion.ToArray()) { if (!dtoItem.ImageGalleryVersions.Select(o => o.ImageGalleryVersionID).Contains(dbVersion.ImageGalleryVersionID)) { dbItem.ImageGalleryVersion.Remove(dbVersion); } } // map child rows foreach (DTO.ImageGalleryMng.ImageGalleryVersion dtoVersion in dtoItem.ImageGalleryVersions) { ImageGalleryVersion dbVersion; if (dtoVersion.ImageGalleryVersionID <= 0) { dbVersion = new ImageGalleryVersion(); dbItem.ImageGalleryVersion.Add(dbVersion); } else { dbVersion = dbItem.ImageGalleryVersion.FirstOrDefault(o => o.ImageGalleryVersionID == dtoVersion.ImageGalleryVersionID); } dbVersion.UpdatedBy = dtoItem.UpdatedBy; dbVersion.UpdatedDate = DateTime.Now; if (dbVersion != null) { AutoMapper.Mapper.Map <DTO.ImageGalleryMng.ImageGalleryVersion, ImageGalleryVersion>(dtoVersion, dbVersion); } } } }