public void UpdateTest() { int affectedRows; MoviesUpdateRequest model = new MoviesUpdateRequest { Id = 1, Title = "Some updated movie", ReleaseYear = 2001 }; affectedRows = service.Update(model); Assert.IsTrue(affectedRows > 0, "The update failed"); }
public HttpResponseMessage Update(int id, MoviesUpdateRequest moviesUpdateRequest) { if (moviesUpdateRequest == null) { ModelState.AddModelError("", "no body data sent."); } else if (id != moviesUpdateRequest.Id) { ModelState.AddModelError("id", "id in the URL does not match the ID in the body"); } if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } movieService.Update(moviesUpdateRequest); return(Request.CreateResponse(HttpStatusCode.OK)); }
public int Update(MoviesUpdateRequest model) { int affectedRows; using (SqlConnection connection = new SqlConnection(connectionString)) { string sqlQuery = "Movies_Update"; using (SqlCommand command = new SqlCommand(sqlQuery, connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@Id", model.Id); command.Parameters.AddWithValue("@Title", model.Title); command.Parameters.AddWithValue("@ReleaseYear", model.ReleaseYear /*?? (object)DBNull.Value at the end of a parameter*/); //If a parameter is nullable, ? in the model and also ?? (object)DBNull.Value at the end of a parameter. connection.Open(); affectedRows = command.ExecuteNonQuery(); connection.Close(); } } return(affectedRows); }