public async Task <IActionResult> PUT([FromRoute] int id, [FromBody] ShopReview shopReview) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE ShopReviews SET Reviews = @reviews, DateCreated = @dateCreated, ShopName = @shopName, ShopLocation = @shopLocation, UserId = @userId WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@reviews", shopReview.Reviews)); cmd.Parameters.Add(new SqlParameter("@dateCreated", shopReview.DateCreated)); cmd.Parameters.Add(new SqlParameter("@shopName", shopReview.ShopName)); cmd.Parameters.Add(new SqlParameter("@shopLocation", shopReview.ShopLocation)); cmd.Parameters.Add(new SqlParameter("@userId", shopReview.UserId)); cmd.Parameters.Add(new SqlParameter("@id", id)); int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { return(new StatusCodeResult(StatusCodes.Status204NoContent)); } throw new Exception("No rows affected"); } } } catch (Exception) { if (!ShopReviewExist(id)) { return(NotFound()); } else { throw; } } }
public async Task <IActionResult> Create([Bind("Id,ShopId,Rating,UserId,DetailReview")] ShopReview shopReview) { ModelState.Remove("UserId"); ModelState.Remove("User"); if (ModelState.IsValid) { var currentUser = await GetCurrentUserAsync(); shopReview.UserId = currentUser.Id; _context.Add(shopReview); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Shops", new { id = shopReview.ShopId })); } ViewData["ShopId"] = new SelectList(_context.Shops, "Id", "Id", shopReview.ShopId); return(RedirectToAction("Index", "Shops")); }
public async Task <IActionResult> GET([FromRoute] int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT Id, Reviews, DateCreated, ShopName, ShopLocation, UserId FROM ShopReviews WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); ShopReview shopReview = null; if (reader.Read()) { shopReview = new ShopReview { Id = reader.GetInt32(reader.GetOrdinal("Id")), Reviews = reader.GetString(reader.GetOrdinal("Reviews")), DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated")), ShopName = reader.GetString(reader.GetOrdinal("ShopName")), ShopLocation = reader.GetString(reader.GetOrdinal("ShopLocation")), UserId = reader.GetInt32(reader.GetOrdinal("UserId")) }; reader.Close(); return(Ok(shopReview)); } else { return(NotFound()); } } } }
public async Task <IActionResult> GET() { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT Id, Reviews, DateCreated, ShopName, ShopLocation, UserId FROM ShopReviews " ; SqlDataReader reader = cmd.ExecuteReader(); var shopReviews = new List <ShopReview>(); while (reader.Read()) { var shopReview = new ShopReview { Id = reader.GetInt32(reader.GetOrdinal("Id")), Reviews = reader.GetString(reader.GetOrdinal("Reviews")), DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated")), ShopName = reader.GetString(reader.GetOrdinal("ShopName")), ShopLocation = reader.GetString(reader.GetOrdinal("ShopLocation")), UserId = reader.GetInt32(reader.GetOrdinal("UserId")) }; shopReviews.Add(shopReview); } reader.Close(); return(Ok(shopReviews)); } } }
public async Task <IActionResult> POST([FromBody] ShopReview shopReview) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" INSERT INTO ShopReviews (Reviews, DateCreated, ShopName, ShopLocation, UserId) OUTPUT INSERTED.Id VALUES (@reviews, @dateCreated, @shopName, @shopLocation, @userId)"; cmd.Parameters.Add(new SqlParameter("@reviews", shopReview.Reviews)); cmd.Parameters.Add(new SqlParameter("@dateCreated", shopReview.DateCreated)); cmd.Parameters.Add(new SqlParameter("@shopName", shopReview.ShopName)); cmd.Parameters.Add(new SqlParameter("@shopLocation", shopReview.ShopLocation)); cmd.Parameters.Add(new SqlParameter("@userId", shopReview.UserId)); int newId = (int)cmd.ExecuteScalar(); shopReview.Id = newId; return(CreatedAtRoute("GetShopReviews", new { id = newId }, shopReview)); } } }
public async Task <IActionResult> Edit(int id, [Bind("Id,ShopId,Rating,UserId,DetailReview")] ShopReview shopReview) { if (id != shopReview.Id) { return(NotFound()); } ModelState.Remove("UserId"); ModelState.Remove("User"); if (ModelState.IsValid) { try { var currentUser = await GetCurrentUserAsync(); shopReview.UserId = currentUser.Id; _context.Update(shopReview); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShopReviewExists(shopReview.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Details", "Shops", new { id = shopReview.ShopId })); } ViewData["ShopId"] = new SelectList(_context.Shops, "Id", "Id", shopReview.ShopId); ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", shopReview.UserId); return(View(shopReview)); }
public void TestConstructor() { // Properties used delcared ShopReview shopReview; int shopReviewId; User shopReviewUser; Shop shopReviewShop; DateTime shopReviewDateTime; int shopReviewSGeneral; string shopReviewEGeneral; int shopReviewSDelivery; string shopReviewEDelivery; int shopReviewSCustomerService; string shopReviewECustomerService; for (int i = 0; i < 2; i++) { if (i == 0) { // Arrange User user = new User(1, "test", "test"); Shop shop = new Shop(1, "4Launch"); DateTime time = DateTime.Now; shopReview = new ShopReview(1, user, shop, time, 3, "test", 3, "test", 3, "test"); // Act shopReviewId = shopReview.ID; shopReviewUser = shopReview.User; shopReviewShop = shopReview.Shop; shopReviewDateTime = shopReview.Date; shopReviewSGeneral = shopReview.SGeneral; shopReviewEGeneral = shopReview.EGeneral; shopReviewSDelivery = shopReview.SDelivery; shopReviewEDelivery = shopReview.EDelivery; shopReviewSCustomerService = shopReview.SCustomerService; shopReviewECustomerService = shopReview.ECustomerService; // Assert Assert.AreEqual(1, shopReviewId); Assert.AreEqual(user, shopReviewUser); Assert.AreEqual(shop, shopReviewShop); Assert.AreEqual(time, shopReviewDateTime); Assert.AreEqual(3, shopReviewSGeneral); Assert.AreEqual("test", shopReviewEGeneral); Assert.AreEqual(3, shopReviewSDelivery); Assert.AreEqual("test", shopReviewEDelivery); Assert.AreEqual(3, shopReviewSCustomerService); Assert.AreEqual("test", shopReviewECustomerService); } if (i == 1) { // Arrange User user = new User(1, "test", "test"); Shop shop = new Shop(1, "4Launch"); DateTime time = DateTime.Now; shopReview = new ShopReview(user, shop, time, 3, "test", 3, "test", 3, "test"); // Act shopReviewUser = shopReview.User; shopReviewShop = shopReview.Shop; shopReviewDateTime = shopReview.Date; shopReviewSGeneral = shopReview.SGeneral; shopReviewEGeneral = shopReview.EGeneral; shopReviewSDelivery = shopReview.SDelivery; shopReviewEDelivery = shopReview.EDelivery; shopReviewSCustomerService = shopReview.SCustomerService; shopReviewECustomerService = shopReview.ECustomerService; // Assert Assert.AreEqual(user, shopReviewUser); Assert.AreEqual(shop, shopReviewShop); Assert.AreEqual(time, shopReviewDateTime); Assert.AreEqual(3, shopReviewSGeneral); Assert.AreEqual("test", shopReviewEGeneral); Assert.AreEqual(3, shopReviewSDelivery); Assert.AreEqual("test", shopReviewEDelivery); Assert.AreEqual(3, shopReviewSCustomerService); Assert.AreEqual("test", shopReviewECustomerService); } } }