/// <summary> /// Buy the selected book /// </summary> /// <param name="userId"></param> /// <param name="bookId"></param> public static void BuyBook(int userId, int bookId) { var soldBook = new SoldBook(); var user = GetUserById(userId); var book = GetBookById(bookId); var isLoggedIn = Ping(userId); try { if (isLoggedIn && user.IsActive) { if (book.Amount > 0) { soldBook.Title = book.Title; soldBook.Author = book.Author; soldBook.CategoryId = book.CategoryId; soldBook.Price = book.Price; soldBook.PurchasedDate = DateTime.Now; soldBook.UserId = user.Id; context.Add(soldBook); book.Amount -= 1; user.SessionTimer = DateTime.Now; context.SaveChanges(); return; } } } catch (Exception) { Console.WriteLine("Something went wrong"); } }
public int Add(Order order) { if (order != null) { _context.Add(order); } return(_context.SaveChanges()); }
public Chair AddChair(Chair chair) { foreach (var chairColor in chair.ChairColors) { var color = _ctx.Colors.FirstOrDefault(c => c.Name.Equals(chairColor.Color.Name)); if (color == null) { chairColor.Color = new Color() { Name = chairColor.Color.Name }; } else { chairColor.Color = color; } } foreach (var chairTag in chair.ChairTags) { var tag = _ctx.Tags.FirstOrDefault(t => t.Name.Equals(chairTag.Tag.Name)); if (tag == null) { chairTag.Tag = new Tag() { Name = chairTag.Tag.Name }; } else { chairTag.Tag = tag; } } var designer = _ctx.Designers.FirstOrDefault(d => d.FirstName.ToLower().Equals(chair.Designer.FirstName.ToLower())); if (designer == null) { chair.Designer = new Designer() { FirstName = chair.Designer.FirstName, LastName = chair.Designer.LastName, CountryOfOrigin = chair.Designer.CountryOfOrigin }; } else { chair.Designer = designer; } var chairAdded = _ctx.Add(chair).Entity; _ctx.SaveChanges(); return(chairAdded); }
public User AddUser(User user) { var userSaved = _ctx.Add(user).Entity; var entries = _ctx.ChangeTracker.Entries(); _ctx.SaveChanges(); return(userSaved); }
public async Task <IActionResult> Create([Bind("ID,Name,Description,ImageURL,Price")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("SouvenirName,Price,PhotoPath,Description,CategoryID,SupplierID")] Souvenir souvenir, IList <IFormFile> _files) { var relativeName = ""; var fileName = ""; if (_files.Count < 1) { relativeName = "/images/Souvenir.svg"; } else { foreach (var file in _files) { fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); //Path for localhost relativeName = "/images/SouvenirImages/" + DateTime.Now.ToString("ddMMyyyy-HHmmssffffff") + fileName; using (FileStream fs = System.IO.File.Create(_hostingEnv.WebRootPath + relativeName)) { await file.CopyToAsync(fs); fs.Flush(); } } } souvenir.PhotoPath = relativeName; try { if (ModelState.IsValid) { _context.Add(souvenir); await _context.SaveChangesAsync(); //ViewData["CategoryID"] = new SelectList(_context.Categories, "CategoryID", "CategoryID", souvenir.CategoryID); //ViewData["SupplierID"] = new SelectList(_context.Suppliers, "SupplierID", "SupplierID", souvenir.SupplierID); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(souvenir)); }
public async Task <IActionResult> Create([FromBody] BasketItemInputModel model) { var entity = mapper.Map <BasketItem>(model); entity.AppUserId = UserId; ctx.Add(entity); if (await ctx.SaveChangesAsync() > 0) { var url = Url.Link("BasketItemGet", new { id = entity.Id }); entity = ctx.BasketItems.Where(i => i.Id == entity.Id).Include(i => i.Product).First(); return(Created(url, mapper.Map <BasketItemModel>(entity))); } return(BadRequest()); }
public async Task <IActionResult> Create([Bind("CategoryName,Description")] Category category) { try { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(category)); }
public async Task <IActionResult> Create([Bind("OrderDate,Status,CustomerID,TotalCost")] Order order) { try { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } ViewData["CustomerID"] = new SelectList(_context.Customers, "CustomerID", "CustomerID", order.CustomerID); return(View(order)); }