//[Authorize("TravelProvider")] public async Task <IActionResult> Add(PackageAddViewModel vm, IFormFile file) { if (ModelState.IsValid) { //get the user who already logged in IdentityUser user = await _userManagerService.FindByNameAsync(User.Identity.Name); //get the profile for this user //call the service to check if Package name already exist Package packExist = _packageDataService.GetSingle(p => p.PackName.ToUpper().Trim() == vm.PackName.ToUpper().Trim()); if (packExist == null) // Not found, then ok to add { Package package = new Package { UserId = user.Id, PackName = vm.PackName, PackLocation = vm.PackLocation, PackDescription = vm.PackDescription, PackPrice = vm.PackPrice, PackageStatus = vm.PackageStatus }; if (file != null) { //upload server path var uploadPath = Path.Combine(_environment.WebRootPath, "images"); //create subfolder Directory.CreateDirectory(Path.Combine(uploadPath, User.Identity.Name)); //get the file name string fileName = FileNameHelper.GetNameFormated(Path.GetFileName(file.FileName)); // stream the file to the srever using (var fileStream = new FileStream(Path.Combine(uploadPath, User.Identity.Name, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } //add the file url to category package.PackImage = User.Identity.Name + "/" + fileName; } _packageDataService.Create(package); //go to home page return(RedirectToAction("Index", "Package")); } else { ModelState.AddModelError("", "Something is wrong"); } } return(View(vm)); }
//the two below action method used for create Package public virtual JsonResult CalculatedUltimated(PackageAddViewModel model) { var package = new Package { DisCountPrice = model.DisCountPrice, StartDate = model.StartDate, EndDate = model.EndDate, Explain = model.Explain, Name = model.Name, OriginalPrice = model.OriginalPrice, Percent = model.Percent }; _packageService.Create(package); if (_unitOfWork.SaveAllChanges() > 0) { int Id = package.Id; return(Json(new { Id = Id }, JsonRequestBehavior.AllowGet)); } return(Json(new { Id = -1 }, JsonRequestBehavior.AllowGet)); }