public async Task <IActionResult> ToFavourite([FromBody] FavouriteModel model) { if (model.Value == "1") { User user = await _context.Users.Include(x => x.UserChosenTextiles).FirstOrDefaultAsync(x => x.UserName == User.Identity.Name); user.UserChosenTextiles.Add(new UserChosenTextile() { TextileId = Guid.Parse(model.TextileId), UserId = user.Id }); try { await _userManager.UpdateAsync(user); await _context.SaveChangesAsync(); } catch (Exception ex) { return(BadRequest(ex)); } return(Ok()); } else { User user = await _context.Users.Include(x => x.UserChosenTextiles) .FirstOrDefaultAsync(x => x.UserName == User.Identity.Name); if (user.UserChosenTextiles.Contains(user.UserChosenTextiles.FirstOrDefault(x => x.TextileId == Guid.Parse(model.TextileId)))) { user.UserChosenTextiles.Remove( user.UserChosenTextiles.FirstOrDefault(x => x.TextileId == Guid.Parse(model.TextileId))); try { await _userManager.UpdateAsync(user); await _context.SaveChangesAsync(); } catch (Exception ex) { return(BadRequest(ex)); } return(Ok()); } else { return(BadRequest()); } } }
public async Task <IActionResult> DeleteFavourite(string TextileId) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } string Url = Request.Headers["Referer"].ToString(); User user = await _context.Users.Include(x => x.UserChosenTextiles) .FirstOrDefaultAsync(x => x.UserName == User.Identity.Name); if (user.UserChosenTextiles.Contains( user.UserChosenTextiles.FirstOrDefault(x => x.TextileId == Guid.Parse(TextileId)))) { user.UserChosenTextiles.Remove( user.UserChosenTextiles.FirstOrDefault(x => x.TextileId == Guid.Parse(TextileId))); await _userManager.UpdateAsync(user); await _context.SaveChangesAsync(); } if (Url == "") { Url = this.Url.Action("Index", "Home"); } return(Redirect(Url)); }
public async Task <IActionResult> CreateItem(TextileAddModel model) { Guid Id = new Guid(); double Discount = (Convert.ToDouble(Math.Round(((model.Price - model.Discount) / model.Price), 3))); Textile textile = new Textile(); if (model.ChildCategoryId == "Нет") { textile = new Textile() { TextileId = Id, WarehouseAmount = model.WarehouseAmount, Name = model.Name, Price = model.Price, Description = model.Description, Discount = Discount, DateWhenAdded = DateTime.Now, IsOnDiscount = model.IsOnDiscount, CategoryId = Guid.Parse(model.CategoryId), PriceWithDiscount = model.Discount, CostWithWholeCost = model.CostWithWholeCost }; } else { textile = new Textile() { TextileId = Id, WarehouseAmount = model.WarehouseAmount, Name = model.Name, Price = model.Price, Description = model.Description, Discount = Discount, DateWhenAdded = DateTime.Now, IsOnDiscount = model.IsOnDiscount, CategoryId = Guid.Parse(model.CategoryId), ChildCategoryId = Guid.Parse(model.ChildCategoryId), PriceWithDiscount = model.Discount, CostWithWholeCost = model.CostWithWholeCost }; } if (model.MainFile != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(model.MainFile.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)model.MainFile.Length); } Image image = new Image() { Name = model.MainFile.FileName, MainTextileId = Id, ByteImg = imageData, ImageId = Guid.NewGuid() }; textile.MainImage = image; } ICollection <Image> Image = new List <Image>(); for (int i = 0; i < 3; i++) { try { if (model.Files[i] != null) { byte[] imageData1 = null; using (var binaryReader = new BinaryReader(model.Files[i].OpenReadStream())) { imageData1 = binaryReader.ReadBytes((int)model.Files[i].Length); } Image image2 = new Image() { Name = model.Files[i].FileName, TextileId = Id, ByteImg = imageData1, ImageId = Guid.NewGuid() }; Image.Add(image2); } } catch { } } textile.Images = Image; ICollection <CharachteristicValues> values = new List <CharachteristicValues>(); for (int i = 0; i < model.CharacsNames.Length; i++) { values.Add(new CharachteristicValues() { CharachteristicValuesId = Guid.NewGuid(), Value = model.CharacsValues[i], Name = model.CharacsNames[i], TextileId = Id }); } textile.Charachteristics = values; await _context.Textiles.AddAsync(textile); await _context.SaveChangesAsync(); return(RedirectToAction("Main", "Admin")); }
public async Task <IActionResult> AddToCart(string TextileId, string Api = "none") { bool flag = false; if (User.Identity.IsAuthenticated) { Cart cart = await _context.Carts.Include(x => x.Items).ThenInclude(x => x.Textile).ThenInclude(x => x.Charachteristics).Include(x => x.User) .FirstOrDefaultAsync(x => x.User.UserName == User.Identity.Name); foreach (var x in cart.Items) { if (x.Textile.TextileId == Guid.Parse(TextileId)) { flag = true; break; } } if (!flag) { if (_context.Textiles.Find(Guid.Parse(TextileId)).WarehouseAmount >= 1) { await _context.Items.AddAsync(new Item() { ItemId = Guid.NewGuid(), CartId = cart.CartId, TextileId = Guid.Parse(TextileId), ItemsAmount = 1 }); await _context.SaveChangesAsync(); } } else { if (_context.Textiles.Find(Guid.Parse(TextileId)).WarehouseAmount > cart.Items .FirstOrDefault(x => x.Textile.TextileId == Guid.Parse(TextileId)).ItemsAmount) { cart.Items.FirstOrDefault(x => x.Textile.TextileId == Guid.Parse(TextileId)).ItemsAmount++; _context.Update(cart); await _context.SaveChangesAsync(); } } } else { if (!HttpContext.Request.Cookies.ContainsKey("Cart")) { bool flag1; Guid Id = Guid.NewGuid(); Guid CartId = Guid.NewGuid(); Cart Cart = new Cart() { CartId = CartId, NonAuthorizedId = Id }; await _context.Carts.AddAsync(Cart); if (_context.Textiles.Find(Guid.Parse(TextileId)).WarehouseAmount >= 1) { await _context.Items.AddAsync(new Item() { ItemId = Guid.NewGuid(), CartId = CartId, TextileId = Guid.Parse(TextileId), ItemsAmount = 1 }); } await _context.SaveChangesAsync(); HttpContext.Response.Cookies.Append("Cart", Id.ToString(), new Microsoft.AspNetCore.Http.CookieOptions() { Expires = DateTime.Now.Add(TimeSpan.FromDays(90)), IsEssential = true }); } else { bool flag2 = false; Guid Id = Guid.Parse(HttpContext.Request.Cookies["Cart"]); Cart Cart = await _context.Carts.Include(x => x.Items).ThenInclude(x => x.Textile).ThenInclude(x => x.Charachteristics) .FirstOrDefaultAsync(x => x.NonAuthorizedId == Id); foreach (var x in Cart.Items) { if (x.Textile.TextileId == Guid.Parse(TextileId)) { flag2 = true; break; } } if (!flag2) { if (_context.Textiles.Find(Guid.Parse(TextileId)).WarehouseAmount >= 1) { _context.Items.Add(new Item() { CartId = Cart.CartId, ItemId = Guid.NewGuid(), ItemsAmount = 1, TextileId = Guid.Parse(TextileId) }); _context.Update(Cart); await _context.SaveChangesAsync(); } } else { if (_context.Textiles.Find(Guid.Parse(TextileId)).WarehouseAmount > Cart.Items .FirstOrDefault(x => x.Textile.TextileId == Guid.Parse(TextileId)).ItemsAmount) { Cart.Items.FirstOrDefault(x => x.Textile.TextileId == Guid.Parse(TextileId)).ItemsAmount++; _context.Update(Cart); await _context.SaveChangesAsync(); } } } } string Url = Request.Headers["Referer"].ToString(); if (Api == "none") { return(Redirect(Url)); } else { return(Ok()); } }