【问题标题】:REST API - appropriate method and route to have child change what its parent resource isREST API - 让子更改其父资源的适当方法和路线
【发布时间】:2020-04-26 16:28:14
【问题描述】:

假设我有 2 个模型,LocationItem,它们对应于数据库中的 2 个表,用下面的 c# 表示。类似于PATCH 请求/items/{itemId} 是否适合更改项目的位置ID 以引用不同的位置资源?这是我第一次遇到想要更改资源父级的情况,因此我不确定适当的方法和路线。我正在使用 DTO(数据传输对象),所以理想情况下我想做的事情是可以接受的,所以我可以只使用一个 DTO 补丁类来更新 Item 模型。

public class Location {
    public long Id { get; set; }
    public ushort RoomNumber { get; set; }
    public string Description { get; set; }

    public List<Item> Items { get; set; }
}
public class Item {
    public long Id { get; set; }
    public string AssetNumber { get; set; }
    public string SerialNumber { get; set; }

    public long LocationId { get; set; }
    public Location Location { get; set; }
}

【问题讨论】:

    标签: rest routes http-method


    【解决方案1】:

    鉴于您的 url 是 /items/{itemId} 而不是 /location/{locationId}/items/{itemId},我认为从 HTTP/Rest 角度更改 locationId 与任何其他属性相比没有什么特别之处。

    PATCH 是对资源进行部分更改的好方法。

    【讨论】:

    • 我想我会实现这样一个端点,但不会经常使用它。那么问题是否会是 api 的使用者可能存储标识符(例如,/location/5/items/3 的 5 和 3),但是更改项目位置 ID 会使该标识符无效?如果是,那你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多