簡體   English   中英

使用 Http 補丁上的對象列表的最佳方法

[英]Best way to work with list of objects on Http Patch

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. 如何在我的 api 中處理“標記為啟用”? 我可以在方法 PATCH 上一一更新,但使用列表我不知道如何。

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; }
}

補丁列表的HttpPatch動作與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; }
        }

接下來,我將詳細介紹如何發送Patch Body。

這是原始列表:

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

1.添加到“ /- ”數組的末尾並添加到“ /0 ”數組的第一個

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

結果:

[
    {
        "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.將Order3替換為Order0

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

結果:

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

3.刪除客戶端Client0

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

結果:

[
    {
        "name": "Client1",
        "description": "Description1",
        "enabled": true
    },
    {
        "name": "Client2",
        "description": "Description2",
        "enabled": true
    }
]
  1. 您可以在microsoft doc中找到其他( movecopytest )。

另一種方法是全部replace

    [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
                }
        };
    }
    }

JSONPatch 的主體:

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

這樣,我們就可以更新列表中的每一個 object。 但這與正常方式非常相似(將模型傳遞給行動)。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM