Best way to work with list of objects on Http Patch

Viewed 2156

in my scenario I have a web client and an rest api (.NET Core) in web client has a page that list all Clients, in this list the user can select many Clients and mark them as Enabled. How can I treat that "Mark as Enable" in my api? I can update one by one on method PATCH, but with list I don't know how.

public IActionResult Patch( long id, [FromBody] JsonPatchDocument<Client> patch )
{
    var client = base.DataContext.Clients.Find( id );

    if( client != null )
        patch.ApplyTo( client, ModelState );

    return Ok();
}

public class Client
{
    public string Name { get; set; }

    public string Description { get; set; }

    public bool Enabled { get; set; }
}
2 Answers

The HttpPatch action of patching List is same as Object.

        [HttpPatch]
        public IActionResult JsonPatchForClients([FromBody] JsonPatchDocument<List<Client>> patchDoc)
        {
            var clients = CreateClients();

            patchDoc.ApplyTo(clients);

            return new ObjectResult(clients);
        }

        private List<Client> CreateClients()
        {
            return  new List<Client>()
            {
                    new Client
                    {
                        Name = "Client0",
                        Description = "Description0",
                        Enabled = true
                    },
                    new Client
                    {
                        Name = "Client1",
                        Description = "Description1",
                        Enabled = true
                    },
                    new Client
                    {
                        Name = "Client2",
                        Description = "Description2",
                        Enabled = true
                    }
            };
        }

        public class Client
        {
            public string Name { get; set; }

            public string Description { get; set; }

            public bool Enabled { get; set; }
        }

Next, I will detail how to send Patch Body.

Here is the raw list:

[
    {
        "name": "Client0",
        "description": "Description0",
        "enabled": true
    },
    {
        "name": "Client1",
        "description": "Description1",
        "enabled": true
    },
    {
        "name": "Client2",
        "description": "Description2",
        "enabled": true
    }
]

1.Add to the end of an array of "/-" and add to the first of an array of "/0"

    [
      {
        "op": "add",
        "path": "/-",
        "value": {
          "name": "Order3",
          "description": "Description3",
          "enabled": true
        }
      }
    ]

Result:

[
    {
        "name": "Client0",
        "description": "Description0",
        "enabled": true
    },
    {
        "name": "Client1",
        "description": "Description1",
        "enabled": true
    },
    {
        "name": "Client2",
        "description": "Description2",
        "enabled": true
    },
    {
        "name": "Order3",
        "description": "Description3",
        "enabled": true
    }
]

2. Replace Order3 to Order0

[
  {
    "op": "replace",
    "path": "/0",
    "value": {
      "name": "Order3",
      "description": "Description3",
      "enabled": true
    }
  }
]

Result:

[
    {
        "name": "Order3",
        "description": "Description3",
        "enabled": true
    },
    {
        "name": "Client1",
        "description": "Description1",
        "enabled": true
    },
    {
        "name": "Client2",
        "description": "Description2",
        "enabled": true
    }
]

3.Remve Client0

[
  {
    "op": "remove",
    "path": "/0"
  }
]

Result:

[
    {
        "name": "Client1",
        "description": "Description1",
        "enabled": true
    },
    {
        "name": "Client2",
        "description": "Description2",
        "enabled": true
    }
]
  1. The others(move,copy,test) you can find in the microsoft doc.

The another way is replace all.

    [HttpPatch]
    public IActionResult JsonPatchForClients([FromBody] JsonPatchDocument  patchDoc)
    {
        var clients = new Clients();
        patchDoc.ApplyTo(clients);

        return new ObjectResult(clients);
    }

    public class Clients
    {
        public List<Client> clients { get; set; }
        public Clients()
        {
           clients = new List<Client>()
            {
                new Client
                {
                    Name = "Client0",
                    Description = "Description0",
                    Enabled = true
                },
                new Client
                {
                    Name = "Client1",
                    Description = "Description1",
                    Enabled = true
                },
                new Client
                {
                    Name = "Client2",
                    Description = "Description2",
                    Enabled = true
                }
        };
    }
    }

The body of JSONPatch:

 [
      {
        "op": "replace",
        "path": "/clients",
        "value": [
          {
          "name": "clients1",
          "description": "Desc1",
          "enabled": false
        },{
          "name": "clients2",
          "description": "Desc2",
          "enabled": false
        }
        ]
      }
    ]

In this way, we can update each object of list. But it's very similar as normal way(passing models to action). enter image description here

Related