public async Task <IActionResult> Create([FromBody] CreateProductViewModel model) { var brand = await _db.Brands.FirstOrDefaultAsync(x => x.Name == model.Brand); if (brand == null) { brand = new Brand { Name = model.Brand } } ; var os = await _db.OS.FirstOrDefaultAsync(x => x.Name == model.OS); if (os == null) { os = new OS { Name = model.OS } } ; var product = new Product { Name = model.Name, Slug = model.Name.GenerateSlug(), ShortDescription = model.ShortDescription, Description = model.Description, TalkTime = model.TalkTime, StandbyTime = model.StandbyTime, ScreenSize = model.ScreenSize, Brand = brand, OS = os, Thumbnail = "/assets/images/thumbnail.jpeg", Images = new List <Image> { new Image { Url = "/assets/images/gallery1.jpeg" }, new Image { Url = "/assets/images/gallery2.jpeg" }, new Image { Url = "/assets/images/gallery3.jpeg" }, new Image { Url = "/assets/images/gallery4.jpeg" }, new Image { Url = "/assets/images/gallery5.jpeg" }, new Image { Url = "/assets/images/gallery6.jpeg" } } }; foreach (var feature in model.Features) { var feat = await _db.Features.SingleAsync(x => x.Name == feature); product.ProductFeatures.Add(new ProductFeature { Feature = feat }); } foreach (var variant in model.Variants) { var colour = await _db.Colours.FirstOrDefaultAsync(x => x.Name == variant.Colour); if (colour == null) { colour = new Colour { Name = variant.Colour } } ; var storage = await _db.Storage.FirstOrDefaultAsync(x => x.Capacity == variant.Storage); if (storage == null) { storage = new Storage { Capacity = variant.Storage } } ; product.ProductVariants.Add(new ProductVariant { Colour = colour, Storage = storage, Price = variant.Price }); } _db.Products.Add(product); await _db.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> Create([FromBody] CreateOrderViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _db.Users.SingleAsync(x => x.UserName == HttpContext.User.Identity.Name); var order = new eCommerceStu.Data.Entities.Order { DeliveryAddress = new eCommerceStu.Data.Entities.Address { FirstName = model.FirstName, LastName = model.LastName, Address1 = model.Address1, Address2 = model.Address2, TownCity = model.TownCity, County = model.County, Postcode = model.Postcode }, Items = model.Items.Select(x => new eCommerceStu.Data.Entities.OrderItem { ProductId = x.ProductId, ColourId = x.ColourId, StorageId = x.StorageId, Quantity = x.Quantity }).ToList() }; user.Orders.Add(order); await _db.SaveChangesAsync(); var total = await _db.Orders .Where(x => x.Id == order.Id) .Select(x => Convert.ToInt32(x.Items.Sum(i => i.ProductVariant.Price * i.Quantity) * 100)) .SingleAsync(); var charges = new StripeChargeService(); var charge = await charges.CreateAsync(new StripeChargeCreateOptions { Amount = total, Description = $"Order {order.Id} payment", Currency = "GBP", SourceTokenOrExistingSourceId = model.StripeToken }); if (string.IsNullOrEmpty(charge.FailureCode)) { order.PaymentStatus = PaymentStatus.Paid; } else { order.PaymentStatus = PaymentStatus.Declined; } await _db.SaveChangesAsync(); return(Ok(new CreateOrderResponseViewModel(order.Id, order.PaymentStatus))); }