I'm building a OData v4 service and I need help on my model and what can be done and cannot be done with OData. I'm using .NET Core 2.2 and Entity Framework Core and ASP.NET Core also. This is my first application with .NET Core.
I have, at this moment this POST request
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey, [FromBody] Booking booking)
{
// ...
}
And my POCO (EF entity)
public class Booking
{
[Key]
public Guid Id { get; set; }
[Required]
public Event Event { get; set; }
[Required]
public User Student { get; set; }
[Required]
public int Position { get; set; } // Position in the registration queue
[Required]
public DateTime ReservationTime { get; set; }
public DateTime? CancelTime { get; set; } // null by default
}
In this POCO Booking only CancelTime is not required. During PostBooking() the system will setup all required properties:
POST https://www.example.com/odata/Events(84a5c788-4f57-4983-b074-4a03a401484a)/Bookings
BODY
{
// In fact my body is empty because Position and ReservationTime I given by system. (now)
// Id is simply a new guid
// Event is in my oData link (84a5c788-4f57-4983-b074-4a03a401484a). I need to check it can be found
// Position is the number of booking register for this event + 1.
// UserId will be in my header (in a session token I will implement later with all security)
}
I see 3 solutions
1) Usage of OData Action? 2) I don't need t booking object coming from body in JSON. Can I write a POST method like this:
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey)
{
// ...
}
3) Or like this
[HttpPost]
[ODataRoute("Events({eventKey})/Bookings")]
public async Task<IActionResult> PostBooking([FromODataUri] Guid eventKey, [FromBody] BookingPostActionDto booking)
{
// ...
}
where
public class BookingPostActionDto() // Is a data tranfert object that I use only for API, not save in database
{
[Required]
public Guid RegistrationUserId { get; set; }
}
According to the OData standard what are the working solutions here? I'm not asking for the best one but for the valid one according to standard. For example I even don't know if OData allow me to work with Dto system like in my solution 3. My solution 2 is working if I never validate my model state because I I validate my model state it will miss all required data. Can I create an action like in my solution 1 when I can do a POST and a POST seems more appropriate?