#StackBounty: #odata #asp.net-core-webapi Create, Update, Delete a $ref with ODate

Bounty: 50

After reading several tutorial I have a good idea how to implement a Rest API with OData. I can create, update and delete an entity and I can link existing entities to a collection of an entity. But what if my entity just contains another entity.

Example, let’s say I have a Course

public class Course
{
    [Key]
    public Guid Id { get; set; }
    public ICollection<Student> Students { get; set; }
    public Teacher Teacher { get; set; }
    public Location Location { get; set; }
}

public class Teacher
{
    [Key]
    public Guid Id { get; set; }
    public ICollection<Location> Location { get; set; }
}

How should I implement the Course controller to link to a Teacher or Location. Location must be a Location of this Teacher.

I just want to know what are the Route I need to create

    [HttpPost]
    [ODataRoute("Course({key})/Location/$ref")]

    [HttpPut]
    [ODataRoute("Course({key})/Location({relatedKey})/$ref")]

    [HttpDelete]
    [ODataRoute("Course({key})/Location({relatedKey})/$ref")]

This is more about the HttpPut and HttpDelete I have some doubt because I don’t have a collection of location. So the related key has no sense here. Should I write my route like this:

    [HttpPut]
    [ODataRoute("Course({key})/Location/$ref")]

    [HttpDelete]
    [ODataRoute("Course({key})/Location/$ref")]

Is it correct ?

EDIT

At this moment I have this in my code

    [HttpPut]
    [ODataRoute("Course({key})/Location/$ref")]
    public async Task<IActionResult> UpdateLinkToLocation([FromODataUri] Guid key, [FromBody] Uri link)
    {
        throw new NotImplementedException();
    }

    [HttpDelete]
    [ODataRoute("Course({key})/Location/$ref")]
    public async Task<IActionResult> DeleteLinkToLocation([FromODataUri] Guid key)
    {
        throw new NotImplementedException();
    }

I find that strange to be able to delete without having to give the key of my location. But why not actually, I have only one location, if I want to remove it, let’s do so. I just need some confirmation on this.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.