Example #1
0
        public async Task <IActionResult> Patch(int id, JsonPatchDocument <UpdateHotelResource> pathPatchDocument)
        {
            //https://docs.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-3.1

            var entity = await this.context.Hotels.FindAsync(id);

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

            var existing = new UpdateHotelResource
            {
                City = entity.City
                       // imagine that we have a lot of props/info here
            };

            pathPatchDocument.ApplyTo(existing, this.ModelState);

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

            entity.UpdateWith(existing);
            this.context.Hotels.Update(entity);
            await this.context.SaveChangesAsync();

            return(this.NoContent());
        }
Example #2
0
        public async Task <IActionResult> Put(long id, UpdateHotelResource model)
        {
            var entity = await this.context.Hotels.FindAsync(id);

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

            entity.UpdateWith(model);
            this.context.Hotels.Update(entity);
            await this.context.SaveChangesAsync();

            return(this.NoContent());
        }
 public static void UpdateWith(this Hotel room, UpdateHotelResource model)
 {
     room.City = model.City;
 }