public Package BindingModelToModel(PackageBindingModel packageBindingModel) { return(new Package { Id = packageBindingModel.Id, Name = packageBindingModel.Name, Description = packageBindingModel.Description, Products = string.Join(',', packageBindingModel.Products.Distinct()), }); }
public async Task <IActionResult> CreatePackage(PackageBindingModel packageBindingModel, string currency = "USD") { var package = await PreparePackage(packageBindingModel, currency); package.Id = 0; DbContext.Add(package); await DbContext.SaveChangesAsync(); return(new AcceptedResult()); }
public async Task <ActionResult> UpdatePackage(int id, PackageBindingModel packageBindingModel, string currency = "USD") { if (!DbContext.Package.Any(p => p.Id == id)) { return(NotFound()); } var package = await PreparePackage(packageBindingModel, currency); package.Id = id; DbContext.Entry(package).State = EntityState.Modified; await DbContext.SaveChangesAsync(); return(new NoContentResult()); }
public async Task <IActionResult> Create(PackageBindingModel package) { if (ModelState.IsValid) { var currentUser = this._context.Users.FirstOrDefault(x => x.PhoneNumber == package.Sender.PhoneNumber); if (!_context.Recipients.Any(x => x.PhoneNumber == package.Recipient.PhoneNumber)) { _context.Recipients.Add(package.Recipient); // await _context.SaveChangesAsync(); } if (currentUser != null) { Sender sender = new Sender() { Address = currentUser.Address, Age = currentUser.Age, FirstName = currentUser.FirstName, LastName = currentUser.LastName, PhoneNumber = currentUser.PhoneNumber, UserName = currentUser.UserName }; Package pack = new Package() { Description = package.Description, IsPackageDelivered = false, IsPackagePaid = false, Recipient = package.Recipient, Sender = sender, User = currentUser, Weight = package.Weight }; _context.Senders.Add(sender); _context.Packages.Add(pack); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(package)); }
private async Task <Package> PreparePackage(PackageBindingModel packageBindingModel, string currency) { var areValidProductIds = await _productService.AreValidProductIds(packageBindingModel.Products); if (!areValidProductIds) { throw new InvalidProductsException(); } var products = (await _productService.GetAllProducts()) .Where(p => packageBindingModel.Products.Contains(p.Id)); var package = _packageMapper.BindingModelToModel(packageBindingModel); package.UsdPrice = await _pricingService.ConvertCurrency(currency, "USD", products.Sum(p => p.UsdPrice)); return(package); }
public AddPackageViewModel() { StoreOptions = new SelectList(new List <SelectListItem>()); Package = new PackageBindingModel(); Products = new List <PackageProductViewModel>(); }
public async Task <ActionResult> Index(PackageBindingModel Package, List <PackageProductViewModel> Products) { try { AddPackageViewModel model = new AddPackageViewModel(); model.Package = Package; model.Products = Products; if (model.Products == null) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Please select products to add")); } var package_Products = model.Products.Where(x => x.IsChecked == true).ToList(); model.Package.Description = model.Package.Description ?? ""; if (package_Products.Count == 0) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Please select products to add")); } else if (package_Products.Count == 1 && package_Products.First().Qty == 1) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Quantity should be greater than 1 for single product in package")); } MultipartFormDataContent content; bool FileAttached = (Request.RequestContext.HttpContext.Session["AddPackageImage"] != null); bool ImageDeletedOnEdit = false; var imgDeleteSessionValue = Request.RequestContext.HttpContext.Session["ImageDeletedOnEdit"]; if (imgDeleteSessionValue != null) { ImageDeletedOnEdit = Convert.ToBoolean(imgDeleteSessionValue); } byte[] fileData = null; var ImageFile = (HttpPostedFileWrapper)Request.RequestContext.HttpContext.Session["AddPackageImage"]; if (FileAttached) { using (var binaryReader = new BinaryReader(ImageFile.InputStream)) { fileData = binaryReader.ReadBytes(ImageFile.ContentLength); } } ByteArrayContent fileContent; JObject response; bool firstCall = true; callAgain : content = new MultipartFormDataContent(); if (FileAttached) { fileContent = new ByteArrayContent(fileData); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ImageFile.FileName }; content.Add(fileContent); } if (model.Package.Id > 0) { content.Add(new StringContent(model.Package.Id.ToString()), "Id"); } content.Add(new StringContent(model.Package.Name), "Name"); content.Add(new StringContent(model.Package.Price.ToString()), "Price"); content.Add(new StringContent(model.Package.Store_Id.ToString()), "Store_Id"); content.Add(new StringContent(model.Package.Description), "Description"); content.Add(new StringContent(Convert.ToString(ImageDeletedOnEdit)), "ImageDeletedOnEdit"); var packageProducts = JsonConvert.SerializeObject(package_Products); var buffer = System.Text.Encoding.UTF8.GetBytes(packageProducts); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); content.Add(byteContent, "package_products"); response = await ApiCall.CallApi("api/Admin/AddPackage", User, isMultipart : true, multipartContent : content); if (firstCall && response.ToString().Contains("UnAuthorized")) { firstCall = false; goto callAgain; } else if (response.ToString().Contains("UnAuthorized")) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "UnAuthorized Error")); } if (response is Error) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, (response as Error).ErrorMessage)); } else { if (model.Package.Id > 0) { TempData["SuccessMessage"] = "The package has been updated successfully."; } else { TempData["SuccessMessage"] = "The package has been added successfully."; } //return RedirectToAction("ManageProducts"); return(Json(new { success = true, responseText = "Success" }, JsonRequestBehavior.AllowGet)); //return RedirectToAction("Index"); } } catch (Exception ex) { return(new HttpStatusCodeResult(Utility.LogError(ex))); } }