public async Task <IActionResult> PutPerson(int id, Person person) { if (id != person.personId) { return(BadRequest()); } _context.Entry(person).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <int> Add(product entity) { if (db != null) { await db.product.AddAsync(entity); await db.SaveChangesAsync(); return(entity.idproduct); } return(0); }
public async Task <IActionResult> OnPostAsync(IFormFile picture) { try { ValidationUniqueEmail vue = new ValidationUniqueEmail(); if (!vue.CheckEmail(ModelState["Customer.Email"].RawValue.ToString())) { ModelState.AddModelError("email", "E-mail already exists."); } if (picture == null) { ModelState.AddModelError("picture", "Picture is empty."); } if (!ModelState.IsValid) { return(Page()); } pictureConvert = new PictureConvert(); Customer.Picture = await pictureConvert.IFormFileToByte(picture); _dbContext.Customer.Add(Customer); await _dbContext.SaveChangesAsync(); alert.ShowMessage("Successfully created."); } catch (Exception) { alert.ShowMessage("Error to creating.", 4); return(Page()); } return(RedirectToPage("/Customers/Index")); }
public async void SetData() { await _dbContext.Customer.AddRangeAsync( ); await _dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Index(ProductModel pm) { if (ModelState.IsValid) { Product product = new Product { Name = pm.Name, Description = pm.Description, Price = pm.Price }; await _db.Products.AddAsync(product); await _db.SaveChangesAsync(); } List <Product> products = await _db.Products .Take(5) .ToListAsync(); foreach (Product product in products) { var client = new RestClient("https://google-translate1.p.rapidapi.com/language/translate/v2"); var request = new RestRequest(Method.POST); request.AddHeader("x-rapidapi-host", "google-translate1.p.rapidapi.com"); request.AddHeader("x-rapidapi-key", "e219260d74mshc9842dc6a717bd6p187982jsnddeece884433"); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddParameter("application/x-www-form-urlencoded", $"source=az&q={product.Description}target=en", ParameterType.RequestBody); IRestResponse response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { JObject json = JObject.Parse(response.Content); JToken data = json["data"]; JToken translations = data["translations"]; JToken firstTranslations = translations.First; product.DescEng = firstTranslations["translatedText"].ToString(); } else { product.DescEng = ""; } } pm.Products = products; return(View(pm)); }
public async Task <IActionResult> OnPostAsync(IFormFile picture) { try { if (_email != ModelState["Customer.Email"].RawValue.ToString()) { ValidationUniqueEmail vue = new ValidationUniqueEmail(); if (!vue.CheckEmail(ModelState["Customer.Email"].RawValue.ToString())) { ModelState.AddModelError("email", "E-mail already exists."); } } if (!ModelState.IsValid) { ViewData["picture"] = _picture; return(Page()); } if (picture != null) { pictureConvert = new PictureConvert(); Customer.Picture = await pictureConvert.IFormFileToByte(picture); } else { Customer.Picture = _picture; } _dbContext.Customer.Add(Customer).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await _dbContext.SaveChangesAsync(); alert.ShowMessage("Successfully edited."); } catch (Exception) { alert.ShowMessage("Error to editing.", 4); return(Page()); } return(RedirectToPage("/Customers/Index")); }