public IActionResult Get(string set) { var service = new SetService(); var setData = service.Find(set); var cards = _context.CardItems.Where(x => x.SetName == setData.Value.Name); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (userId != null) { var inventoryItems = _context.InventoryItems.Where(x => x.UserId == userId); var data = from c in cards join i in inventoryItems on c.Id equals i.Id into ci from pos in ci.DefaultIfEmpty() select new CardItemViewData { Id = c.Id, Name = c.Name, ManaCost = c.ManaCost, ImageUrl = c.ImageUrl, InInventory = pos != null }; var model = new CardItemViewModel { Items = data }; return(Json(model)); } else { var data = cards.ProjectToType <CardItemViewData>(); var model = new CardItemViewModel { Items = data }; return(Json(model)); } }
public IActionResult Index() { List <Item> cart = HttpContext.Session.GetObjectFromJson <List <Item> >("cart"); if (cart == null || !cart.Any(a => a.Quantity > 0)) { return(View(new List <CardItemViewModel>())); } List <CardItemViewModel> result = new List <CardItemViewModel>(); foreach (Item item in cart.Where(a => a.Quantity > 0)) { Product product = _context.Set <Product>() .Include(a => a.Prices) .ThenInclude(a => a.ProductSize) .Include(a => a.ProductImages) .ThenInclude(a => a.Color) .First(a => a.Id == item.ProductId); CardItemViewModel resultItem = new CardItemViewModel { Product = product, ImageEntity = product.ProductImages.First(a => a.ColorId == item.ColorId), Price = product.Prices.First(a => a.ProductSizeId == item.SizeId), Quantity = item.Quantity, Id = item.Id }; result.Add(resultItem); } ViewBag.cartCount = cart.Count; return(View(result)); }
public async Task <IActionResult> UpdateItem([FromBody] CardItemViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userBook = await _context.BookUsers .Include(bu => bu.User) .Include(bu => bu.Book) .ThenInclude(b => b.Author) .FirstOrDefaultAsync(bu => bu.Id == model.Id.Value); if (!IsUserOwnerOfCard(userBook)) { return(Forbid("You are not allowed to change cards of others")); } if (!userBook.Book.IsVisibleInCatalog) { return(BadRequest("This book is no longer abailable")); } if (model.Amount > userBook.Book.AmInStock) { return(BadRequest($"There are only {userBook.Book.AmInStock} copies available")); } userBook.Amount = model.Amount; await _context.SaveChangesAsync(); model = _mapper.Map <CardItemViewModel>(userBook); return(Ok(model)); }
public async Task <IActionResult> AddItem([FromBody] CardItemViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var book = await _context.Books .Include(b => b.Author) .FirstOrDefaultAsync(b => b.Id == model.BookId); if (book is null) { return(BadRequest($"Database does not contain book with given id!")); } if (model.Amount > book.AmInStock) { return(BadRequest($"There are only {book.AmInStock} copies available to buy")); } if (!book.IsVisibleInCatalog) { return(BadRequest("You can't add this book to shopping card")); } var user = await _userManager.FindByNameAsync(User.Identity.Name); if (user is null) { return(StatusCode(500, "shok")); } var listUserBooks = await _context.BookUsers .Include(bu => bu.Book) .ThenInclude(b => b.Author) .Include(bu => bu.User) .Where(bu => bu.UserId == user.Id) .ToListAsync(); if (listUserBooks.Count >= 10) { return(BadRequest("Shopping card can contain no more than 10 different books!")); } var userBook = listUserBooks.FirstOrDefault(bu => bu.BookId == book.Id && bu.UserId == user.Id); if (userBook != null) { return(BadRequest("You already have this item in your card!")); } userBook = new BookUser { User = user, Book = book, Amount = model.Amount }; _context.BookUsers.Add(userBook); await _context.SaveChangesAsync(); return(Ok(_mapper.Map <CardItemViewModel>(userBook))); }
public ProfilePreviewPage(CardItemViewModel cardItem) { InitializeComponent(); BindingContext = new ProfilePreviewPageViewModel(cardItem); On <Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true); }