public void UpdateProject(ProjectVM source) { using (var db = DbFactory.GetNotTrackingInstance()) { var target = db.ProjectRepository.Get(x => x.Id == source.Id, "Images"); if (target == null) //Create new Project { var entityProject = source.ToEntity(); foreach (var img in source.Images) { entityProject.Images.Add(new ProjectImage { Url = ImageHelper.SaveImage(img.Image, IMAGE_FULL_URL) }); } db.ProjectRepository.Add(entityProject); } else //Update old Project { target.Name = source.Name; target.Description = source.Description; foreach (var sourceImg in source.Images) { var dbImage = db.ProjectImageRepository.Get(sourceImg.Id); if (dbImage == null) { //create new image db.ProjectImageRepository.Add(new ProjectImage { ProjectId = target.Id, Url = ImageHelper.SaveImage(sourceImg.Image, IMAGE_FULL_URL) }); } } //delete images foreach (var targetImage in target.Images) { if (!source.Images.Any(x => x.Id == targetImage.Id)) { ImageHelper.DeleteImage(targetImage.Url, IMAGE_FULL_URL); } } db.ProjectImageRepository.RemoveRange(target.Images.Where(x => source.Images.All(y => y.Id != x.Id))); db.ProjectRepository.Update(target); } db.Save(); } }
public async Task <IActionResult> Create(List <IFormFile> files, ProjectVM projectVm) { if (!adamUnit.ProjectRepository.Exists(x => x.Name == projectVm.Name)) { // Current login User Information var CurrentUserEmial = User.Identity.Name; var CurrentUserId = adamUnit.AppUserRepository.SingleOrDefault(x => x.Email == CurrentUserEmial).Id; var CurrentName = adamUnit.AppUserRepository.SingleOrDefault(x => x.Email == CurrentUserEmial).FirstName; if (!ModelState.IsValid) { return(View(projectVm)); } var project = new Models.Project(); project = projectVm.ToEntity(); await adamUnit.ProjectRepository.InserAsync(project); var ProjId = project.Id; //save UserInProjUser string[] array = Regex.Split(projectVm.EmpIds, ","); foreach (var item in array) { var ProjUser = new ProjectUser(); ProjUser.ProjectId = ProjId; ProjUser.UserId = Convert.ToInt64(item); await adamUnit.ProjectUserRepository.InserAsync(ProjUser); } string webRootPath = _hostingEnvironment.WebRootPath; var state = adamUnit.AttachmentRepository.OnPostUpload(files, ProjId.ToString(), AttachmentOwner.Projects, AttachmentType.Photo, webRootPath); //send Notification To Users foreach (var item in array) { var UserId = adamUnit.EmployeeRepository.SingleOrDefault(x => x.Id == Convert.ToInt16(item)).UserId; var UserName = adamUnit.AppUserRepository.SingleOrDefault(x => x.Id == UserId).UserName; var attachment = adamUnit.AttachmentRepository.FirstOrDefault(x => x.OwnerId == item).FileName; var notify = new Notfication(); notify.OwnerUserId = CurrentUserId; notify.ToUserId = UserId; notify.TableName = TableName.Project; notify.TablePkId = ProjId; notify.Context = "AddyouToProject"; var Url = "/Admin/Project/Details?id=" + ProjId; await adamUnit.NotificationRepository.InserAsync(notify); if (adamUnit.TokenFireBaseRepository.Exists(x => x.UserId == UserId)) { var tokens = adamUnit.TokenFireBaseRepository.GetAll(x => x.UserId == UserId).Select(x => x.token); foreach (var token in tokens) { NotificationController.SendPushNotification(token, notify.Context, CurrentName, Url, attachment); } } } TempData["SuccessMessage"] = "Created Successfully"; return(RedirectToAction(nameof(Index))); } ViewBag.Name = "Name is Exist Already"; ViewBag.Customer = adamUnit.CustomerRepository.GetAll().ToList(); ViewBag.Employee = adamUnit.EmployeeRepository.GetAllInclude().ToList(); return(View(projectVm)); }