public IHttpActionResult GettblToy(int id) { var toy = db.tblToys.SingleOrDefault(t => t.id == id && t.isActived == true); ToyDTO dto = null; if (toy != null) { dto = new ToyDTO { id = toy.id, name = toy.name, price = toy.price, image = toy.image, description = toy.desciption, quantity = toy.quantity, category = toy.category }; } if (toy == null) { return(NotFound()); } return(Ok(dto)); }
// GET: tblToys/Details/5 public async Task <ActionResult> Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } HttpResponseMessage response = await client.GetAsync(baseURL + "Toys" + "\\" + id); string strDto = response.Content.ReadAsStringAsync().Result; ToyDTO dto = JsonConvert.DeserializeObject <ToyDTO>(strDto); if (dto == null) { return(HttpNotFound()); } response = await client.GetAsync(baseURL + "Toys" + "?id=" + id + "&related=" + dto.category); string strRelated = response.Content.ReadAsStringAsync().Result; IEnumerable <ToyDTO> relatedProduct = JsonConvert.DeserializeObject <IEnumerable <ToyDTO> >(strRelated); ViewBag.RelatedProduct = relatedProduct; response = await client.GetAsync(baseURL + "Feedbacks\\ToyId?toyId=" + id); string strFeedback = response.Content.ReadAsStringAsync().Result; IEnumerable <FeedbackDTO> feedbacks = JsonConvert.DeserializeObject <IEnumerable <FeedbackDTO> >(strFeedback); ViewBag.Feedbacks = feedbacks; ViewBag.NoFeedback = feedbacks.Count(); return(View(dto)); }
private void btnSavePro_Click(object sender, EventArgs e) { try { int id = int.Parse(txtProID.Text); string proName = txtProName.Text.Trim(); float proPrice = float.Parse(txtProPrice.Text.Trim()); int proQuantity = int.Parse(txtProQuantity.Text.Trim()); string proCategory = (string)cbProCategory.SelectedItem; string proDescription = txtProDescription.Text.Trim(); byte[] image = imageToByteArray(pbProImage.Image); ToyDTO dto = new ToyDTO { id = id, name = proName, price = proPrice, quantity = proQuantity, category = proCategory, description = proDescription, image = image }; updateToy(dto); MessageBox.Show("Update toy id" + id + " success!"); btnClearPro_Click(sender, e); loadToys(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private async void updateToy(ToyDTO dto) { HttpResponseMessage response = await client.PutAsJsonAsync(BASE_URL + "Toys/" + dto.id, dto); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public IHttpActionResult PosttblToy(ToyDTO toy) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.tblToys.Add(new tblToy { name = toy.name, price = toy.price, category = toy.category, desciption = toy.description, image = toy.image, createdBy = toy.createdBy, isActived = true, quantity = toy.quantity }); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = toy.id }, toy)); }
public IHttpActionResult PuttblToy(int id, ToyDTO tblToy) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != tblToy.id) { return(BadRequest()); } tblToy toy = db.tblToys.Find(id); toy.name = tblToy.name; toy.price = tblToy.price; toy.quantity = tblToy.quantity; toy.category = tblToy.category; toy.desciption = tblToy.description; toy.image = tblToy.image; db.Entry(toy).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!tblToyExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
private void btnAddPro_Click(object sender, EventArgs e) { string proName, proCategory, proDescription; float proPrice = -1; int proQuantity = -1; byte[] image = null; bool check = true; proName = txtProName.Text.Trim(); proCategory = (string)cbProCategory.SelectedItem; proDescription = txtProDescription.Text.Trim(); if (proName.Length == 0 || proCategory.Length == 0 || proDescription.Length == 0) { check = false; } try { proPrice = float.Parse(txtProPrice.Text.Trim()); if (proPrice <= 0) { check = false; } } catch (Exception) { check = false; } try { proQuantity = int.Parse(txtProQuantity.Text.Trim()); if (proQuantity <= 0) { check = false; } } catch (Exception) { check = false; } if (pbProImage.Image == null) { check = false; } else { image = imageToByteArray(pbProImage.Image); } if (check) { ToyDTO dto = new ToyDTO { name = proName, price = proPrice, quantity = proQuantity, category = proCategory, description = proDescription, image = image }; addToy(dto); MessageBox.Show("Add a toy success!"); btnClearPro_Click(sender, e); loadToys(); } else { MessageBox.Show("Your input is invalid! Please try again!"); } }