C
C#3y ago
bookuha

❔ Get id from query or DTO

Imagine I have the next two entities class Task{ public int Id {get;set;} public string Desc {get;set;} public int AssignmentId {get;set;} public Assignment Assignment {get;set;} class Assignment{ public int Id {get; set;} public ICollection<Task> Tasks {get;set;} } What would be the best way to POST a task to an existing assignment? How would endpoint and incoming DTO look like? 1) /assignments/3/tasks Receive TaskDTO1{ string Desc } and assignment id to your controller method, call service method that will retrieve assignment by the id and append a task to its task collection, then save 2) /tasks/ Receive TaskDTO2{ string Desc, int AssignmentId } in your controller method, call service method that will create a task entity, retrieve a corresponding assignment by id (AssignmentId from dto2) and fill the Task.Assignment with this assignment, then save Which way looks better to you? Why?
3 Replies
Shirasho
Shirasho3y ago
Assignments can have more than one task, but not the other way around. The former expresses this relationship. There is the assumption you always have the assignment Id on hand.
qqdev
qqdev3y ago
1). Because it is resource-based which is what you usually want
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?