public async Task CreateResource() { var movieToCreate = new Models.MovieForCreation() { Title = "Resevoir Dogs", Description = "After a simple jewelry heist goes terribly wrong, the surviving criminals begin to suspect that one of them is a police informant.", DirectorId = Guid.Parse("d28888e9-2ba9-473a-a40f-e38cb54f9b35"), ReleaseDate = new DateTime(1992, 9, 2), Genre = "Crime, Drama" }; var serializedMovieToCreate = JsonSerializer.Serialize(movieToCreate); var request = new HttpRequestMessage(HttpMethod.Post, "api/movies"); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Content = new StringContent(serializedMovieToCreate); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var createdMovie = JsonSerializer.Deserialize <Models.Movie>(content, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); }
public async Task <IActionResult> CreateMovie([FromBody] Models.MovieForCreation movieForCreation) { // model validation if (movieForCreation == null) { return(BadRequest()); } if (!ModelState.IsValid) { // return 422 - Unprocessable Entity when validation fails return(new UnprocessableEntityObjectResult(ModelState)); } var movieEntity = _mapper.Map <Movie>(movieForCreation); _moviesRepository.AddMovie(movieEntity); // save the changes await _moviesRepository.SaveChangesAsync(); // Fetch the movie from the data store so the director is included await _moviesRepository.GetMovieAsync(movieEntity.Id); return(CreatedAtRoute("GetMovie", new { movieId = movieEntity.Id }, _mapper.Map <Models.Movie>(movieEntity))); }
public async Task CreateResourceShortcut() { var movieToCreate = new Models.MovieForCreation() { Title = "Resevoir Dogs", Description = "After a simple jewelry heist goes terribly wrong, the surviving criminals begin to suspect that one of them is a police informant.", DirectorId = Guid.Parse("d28888e9-2ba9-473a-a40f-e38cb54f9b35"), ReleaseDate = new DateTime(1992, 9, 2), Genre = "Crime, Drama" }; var response = await httpClient.PostAsync("api/movie", new StringContent(JsonSerializer.Serialize(movieToCreate), Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); }