public Model.Product Insert(Model.Requests.InsertProductRequest pr) { bool validCinemaId = false; foreach (var cinema in _context.Cinemas.ToList()) { if (pr.CinemaId == cinema.CinemaId) { validCinemaId = true; break; } } if (!validCinemaId) { throw new UserException("Could not find sepcified cinemaId!"); } var product = _mapper.Map <Database.Products>(pr); var inBaseProducts = _context.Products.Where(p => p.CinemaId == pr.CinemaId).ToList(); foreach (var inProduct in inBaseProducts) { if (inProduct.Name.ToLower() == product.Name.ToLower() && inProduct.Price == product.Price && inProduct.CinemaId == product.CinemaId) { return(_mapper.Map <Model.Product>(product)); } } _context.Products.Add(product); _context.SaveChanges(); return(_mapper.Map <Model.Product>(product)); }
public Product Update(int productId, Model.Requests.InsertProductRequest product) { bool validProductId = false; foreach (var inProdict in _context.Products.ToList()) { if (productId == inProdict.ProductId) { validProductId = true; break; } } if (!validProductId) { throw new UserException("Cannot find specified Product with that productId!"); } var baseProduct = _context.Products.Find(productId); bool validCinemaId = false; foreach (var cinema in _context.Cinemas.ToList()) { if (product.CinemaId == cinema.CinemaId) { validCinemaId = true; break; } } if (!validCinemaId) { throw new UserException("Invalid cinemaId inside of hall!"); } var oldPrice = baseProduct.Price; baseProduct.Name = product.Name; baseProduct.Price = product.Price; _context.SaveChanges(); Helper helper = new Helper(_context); helper.ChangeProductNotification(baseProduct, $"Product information changed (price: {baseProduct.Price}, name: {baseProduct.Name})", "Warning", oldPrice, baseProduct.Price); return(_mapper.Map <Model.Product>(baseProduct)); }
private async void saveBtn_Click(object sender, EventArgs e) { var messageBox = new CustomMessageBox(); if (string.IsNullOrWhiteSpace(ProductName.Text) || ProductName.Text.Length < 3) { messageBox.Show("The name field requires 3 letters!", "error"); return; } if (!_helper.ValidateDecimalString(ProductPrice.Text, 1, 150)) { messageBox.Show("Enter a valid price (1-150)!", "error"); return; } Model.Requests.InsertProductRequest product = new Model.Requests.InsertProductRequest() { Name = ProductName.Text, Price = decimal.Parse(ProductPrice.Text), CinemaId = _cinemaId }; if (_productId.HasValue) { await _apiService.Update <Model.Product>(_productId, product); messageBox.Show("Product updated succesfully", "Success"); } else { await _apiService.Insert <Model.Product>(product); messageBox.Show("Product added succesfully", "Success"); } CinemasProductsForm form = new CinemasProductsForm(_menuForm, _cinemaId, _cinemaName); _cinemasProductsForm.Close(); _helper.CloseForm(this, 15); _helper.ShowForm(form, 15); }
public ActionResult <Model.Product> UpdateProduct(int productId, Model.Requests.InsertProductRequest product) { return(_service.Update(productId, product)); }
public ActionResult <Model.Product> AddProductToCinema(Model.Requests.InsertProductRequest product) { return(_service.Insert(product)); }