C#C
C#4y 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?
Was this page helpful?