public IHttpActionResult PutMovie(int id, Movie movie) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != movie.Id) { return BadRequest(); } db.Entry(movie).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!MovieExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostMovie(Movie movie) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Movies.Add(movie); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = movie.Id }, movie); }
public ActionResult Create(Movie item) { try { if (!ModelState.IsValid) return View(); _db.Movies.InsertOnSubmit(item); _db.SubmitChanges(); return RedirectToRoute(this.GetResourceRouteName(routeName, ActionType.Retrieve), new RouteValueDictionary { { "id", item.Id } }); } catch (Exception exception) { throw new HttpException((int)HttpStatusCode.InternalServerError, "An error has occured; see details:", exception); } }
public ActionResult Edit(int id, Movie item) { try { item.Id = id; _db.Movies.Attach(item, true); _db.SubmitChanges(); return RedirectToRoute(this.GetResourceRouteName(routeName, ActionType.Retrieve), new RouteValueDictionary { { "id", item.Id } }); } catch (Exception exception) { throw new HttpException((int)HttpStatusCode.InternalServerError, "An error has occured; see details:", exception); } }
partial void DeleteMovie(Movie instance);
/// <summary> /// Deprecated Method for adding a new object to the Movies EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToMovies(Movie movie) { base.AddObject("Movies", movie); }
partial void UpdateMovie(Movie instance);
public void CrudMoviesJson() { string director = "Donen"; string title = "Singin' in the Rain"; DateTime dateReleased = new DateTime(1952, 4, 11); List<Movie> originalMovieList; List<Movie> updatedMovieList; using (HttpResponseMessage response = client.Get("Home", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); originalMovieList = response.Content.ReadAsJsonDataContract<List<Movie>>(); } Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("Home/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract<List<Movie>>(); } Movie insertedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(movieToInsert, insertedMovie)); director = "Kelly"; // multiple director credits insertedMovie.Director = director; using (HttpResponseMessage response = client.Put("Home/Edit/" + insertedMovie.Id.ToString(), HttpContentExtensions.CreateJsonDataContract(insertedMovie))) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract<List<Movie>>(); } Movie updatedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(insertedMovie, updatedMovie)); using (HttpResponseMessage response = client.Delete("Home/delete/" + insertedMovie.Id.ToString())) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/json")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsJsonDataContract<List<Movie>>(); } Assert.IsTrue(updatedMovieList.Union(originalMovieList, moviesComparer).Count() == updatedMovieList.Count()); }
public void CrudMoviesXml() { string director = "Nichols"; string title = "The Graduate"; DateTime dateReleased = new DateTime(1967, 12, 21); List<Movie> originalMovieList; List<Movie> updatedMovieList; using (HttpResponseMessage response = client.Get("Home", "application/xml")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); originalMovieList = response.Content.ReadAsDataContract<List<Movie>>(); } Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("Home/create", HttpContentExtensions.CreateDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/xml")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsDataContract<List<Movie>>(); } Movie insertedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(movieToInsert, insertedMovie)); dateReleased = new DateTime(1997, 2, 14); // US re-release date insertedMovie.DateReleased = dateReleased; using (HttpResponseMessage response = client.Put("Home/Edit/" + insertedMovie.Id.ToString(), HttpContentExtensions.CreateDataContract(insertedMovie))) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/xml")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsDataContract<List<Movie>>(); } Movie updatedMovie = updatedMovieList.Except(originalMovieList, moviesComparer).SingleOrDefault(); Assert.IsTrue(moviesComparer.Equals(insertedMovie, updatedMovie)); using (HttpResponseMessage response = client.Delete("Home/delete/" + insertedMovie.Id.ToString())) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); } using (HttpResponseMessage response = client.Get("Home", "application/xml")) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); updatedMovieList = response.Content.ReadAsDataContract<List<Movie>>(); } Assert.IsTrue(updatedMovieList.Union(originalMovieList, moviesComparer).Count() == updatedMovieList.Count()); }
public void PostMoviesWithInvalidAuth() { string director = "Allen"; string title = "Annie Hall"; DateTime dateReleased = new DateTime(1977, 4, 30); client.DefaultHeaders.Add(psuedoAuthHeaderForPhil); Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("AuthHome/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } client.DefaultHeaders.Remove(psuedoAuthHeaderForPhil); }
public void PostMoviesWithoutAuth() { string director = "Wachowski"; string title = "The Matrix"; DateTime dateReleased = new DateTime(1999, 3, 31); Movie movieToInsert = new Movie() { Director = director, DateReleased = dateReleased, Title = title }; using (HttpResponseMessage response = client.Post("AuthHome/create", HttpContentExtensions.CreateJsonDataContract(movieToInsert))) { Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } }
public void PostInvalidMovieFutureXml() { Movie future = new Movie() { Director = "The Director", Title = "The Title", DateReleased = DateTime.Now.Add(TimeSpan.FromDays(300)) }; using (HttpResponseMessage response = client.Post("Movies", HttpContentExtensions.CreateDataContract(future))) { Assert.AreEqual(HttpStatusCode.ExpectationFailed, response.StatusCode); Assert.IsTrue(response.Content.ReadAsString().Contains("Value for Date Released must be between")); } }
public void PostInvalidMovieNoDirectorXml() { Movie noDirector = new Movie() { Title = "The Title", DateReleased = DateTime.Now }; using (HttpResponseMessage response = client.Post("Movies", HttpContentExtensions.CreateDataContract(noDirector))) { Assert.AreEqual(HttpStatusCode.ExpectationFailed, response.StatusCode); Assert.IsTrue(response.Content.ReadAsString().Contains("Please enter the name of the director of this movie.")); } }
public void PostInvalidMovieNoTitleJson() { Movie noTitle = new Movie() { Director = "The Director", DateReleased = DateTime.Now }; using (HttpResponseMessage response = client.Post("Movies", HttpContentExtensions.CreateJsonDataContract(noTitle))) { Assert.AreEqual(HttpStatusCode.ExpectationFailed, response.StatusCode); Assert.IsTrue(response.Content.ReadAsString().Contains("The Title field is required.")); } }
public ActionResult Edit(int id, Movie updatedMovie) { try { var originalMovie = (from m in _db.Movies where m.Id == updatedMovie.Id select m).First(); if(!ModelState.IsValid) return View(originalMovie); _db.ApplyCurrentValues(originalMovie.EntityKey.EntitySetName, updatedMovie); _db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
partial void InsertMovie(Movie instance);
public ActionResult Edit(Movie movieToEdit) { try { _db.Movies.Attach(movieToEdit, true); _db.SubmitChanges(); return RedirectToAction("Details", new RouteValueDictionary { { "id", movieToEdit.Id } }); } catch (Exception exception) { throw new HttpException((int)HttpStatusCode.InternalServerError, "An error has occured; see details:", exception); } }
/// <summary> /// Create a new Movie object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="title">Initial value of the Title property.</param> /// <param name="director">Initial value of the Director property.</param> /// <param name="dateReleased">Initial value of the DateReleased property.</param> public static Movie CreateMovie(global::System.Int32 id, global::System.String title, global::System.String director, global::System.DateTime dateReleased) { Movie movie = new Movie(); movie.Id = id; movie.Title = title; movie.Director = director; movie.DateReleased = dateReleased; return movie; }