public IActionResult Upsert(Project project) { string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\project"); var extension = Path.GetExtension(files[0].FileName); if (project.Image != null && !project.Image.Contains("basic")) { var imagePath = Path.Combine(webRootPath, project.Image.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create)) { files[0].CopyTo(filesStreams); } project.Image = @"\images\project\" + fileName + extension; } else { project.Image = @"\images\project\basic\" + "room" + ".png"; } if (project.CustomerUserId == null) { project.CustomerUserId = _userManager.GetUserId(User); } if (project.Id == null) { if (_unitOfWork.Project.GetAll().Count() == 0) { project.Id = "Pr1"; } else { int maxId = _unitOfWork.Project.GetAll() .Select(x => Convert.ToInt32(x.Id.Replace("Pr", ""))).Max(); project.Id = "Pr" + (maxId + 1).ToString(); } _unitOfWork.Project.Add(project); } else { _unitOfWork.Project.Update(project); } _unitOfWork.Save(); ProjectHomeVM projectHomeVM = new ProjectHomeVM() { Projects = _unitOfWork.Project.GetAll(x => x.ApplicationUserId == _userManager.GetUserId(User) , includeProperties: "ApplicationUser,CustomerUser") }; return(RedirectToAction("Index", projectHomeVM)); }
public IActionResult Index(ProjectHomeVM projectHomeVM) { projectHomeVM.Projects = _unitOfWork.Project.GetAll(x => x.ApplicationUserId == _userManager.GetUserId(User) , includeProperties: "ApplicationUser,CustomerUser"); return(View(projectHomeVM)); }