コード例 #1
0
        public async Task <IActionResult> PartiallyUpdateMovie(Guid movieId, [FromBody] JsonPatchDocument <Models.MovieForUpdate> patchDoc)
        {
            Movie movieEntity = await this.moviesRepository.GetMovieAsync(movieId);

            if (movieEntity == null)
            {
                return(this.NotFound());
            }

            // the patch is on a DTO, not on the movie entity
            Models.MovieForUpdate movieToPatch = Mapper.Map <Models.MovieForUpdate>(movieEntity);

            patchDoc.ApplyTo(movieToPatch, this.ModelState);

            if (!this.ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(this.ModelState));
            }

            // map back to the entity, and save
            Mapper.Map(movieToPatch, movieEntity);

            // call into UpdateMovie even though in our implementation
            // this doesn't contain code - doing this ensures the code stays
            // reliable when other repository implementations (eg: a mock repository) are used.
            this.moviesRepository.UpdateMovie(movieEntity);
            await this.moviesRepository.SaveChangesAsync();

            // return the updated movie, after mapping it
            return(this.Ok(this.mapper.Map <Models.Movie>(movieEntity)));
        }
コード例 #2
0
        public async Task <IActionResult> UpdateMovie(Guid movieId, [FromBody] Models.MovieForUpdate movieForUpdate)
        {
            // model validation
            if (!this.ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(this.ModelState));
            }

            Movie movieEntity = await this.moviesRepository.GetMovieAsync(movieId);

            if (movieEntity == null)
            {
                return(this.NotFound());
            }

            // map the inputted object into the movie entity
            // this ensures properties will get updated
            this.mapper.Map(movieForUpdate, movieEntity);

            // call into UpdateMovie even though in our implementation
            // this doesn't contain code - doing this ensures the code stays
            // reliable when other repository implementations (e.g.: a mock
            // repository) are used.
            this.moviesRepository.UpdateMovie(movieEntity);
            await this.moviesRepository.SaveChangesAsync();

            // return the updated movie, after mapping it
            return(this.Ok(this.mapper.Map <Models.Movie>(movieEntity)));
        }
コード例 #3
0
        public async Task UpdateResource()
        {
            var movieToUpdate = new Models.MovieForUpdate()
            {
                Title       = "pulp Fiction",
                Description = "The movie with Zed.",
                DirectorId  = Guid.Parse("d28888e9-2ba9-473a-a40f-e38cb54f9b35"),
                ReleaseDate = new DateTime(1992, 9, 2),
                Genre       = "Crime, Drama"
            };

            var serializedMovieToUpdate = JsonSerializer.Serialize(movieToUpdate);

            var request = new HttpRequestMessage(HttpMethod.Put, "api/movies/5b1c2b4d-48c7-402a-80c3-cc796ad49c6b");

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            request.Content = new StringContent(serializedMovieToUpdate);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await httpClient.SendAsync(request);

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            var updatedMovie = JsonSerializer.Deserialize <Models.Movie>(content, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });
        }
コード例 #4
0
        public async Task UpdateResourceShortcut()
        {
            var movieToUpdate = new Models.MovieForUpdate()
            {
                Title       = "pulp Fiction",
                Description = "The movie with Zed.",
                DirectorId  = Guid.Parse("d28888e9-2ba9-473a-a40f-e38cb54f9b35"),
                ReleaseDate = new DateTime(1992, 9, 2),
                Genre       = "Crime, Drama"
            };

            var response = await httpClient.PutAsync("api/movies/5b1c2b4d-48c7-402a-80c3-cc796ad49c6b", new StringContent(JsonSerializer.Serialize(movieToUpdate), Encoding.UTF8, "application/json"));

            response.EnsureSuccessStatusCode();
        }