C
C#8mo ago
Alex

CRUD for complex objects

Hi! I have two tables in my database Sets and Terms. One set has many terms. Set can have any number of terms. I need API routes to do CRUD for both of them. Is it better to create a single route that will accept set object
{
title:string;
description:string;
image:string;
terms:[
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
]
}
{
title:string;
description:string;
image:string;
terms:[
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
{
text:string;
definition:string;
},
]
}
and decide which term update or create. Or make two routes one for Set and other for one Term and call first Set route then Term route for each term one by one? Set also can be imported from file. I have and route that parses file and returns terms that must be stored if user confirms that import is fine.
No description
9 Replies
Pobiega
Pobiega8mo ago
is it a one-to-many relationship, or many to many? ie, can a term belong to more than one set?
Alex
AlexOP8mo ago
one-to-many, One set has many terms
Pobiega
Pobiega8mo ago
and decide which term update or create
how is this a problem then? if a single term only ever belongs to a single set, for an UpdateSet action just take in something that can hold term IDs if the term id is 0, its a new term, if the term id has a value, EF will handle it for you
Alex
AlexOP8mo ago
I can't decide what way is better - create one endpoint for creation, one to update one Term and send request for each edited term one by one or single endpoint where I send many terms both creation and update and decide on server what to do with each term
Pobiega
Pobiega8mo ago
Ah. Given the relationship, I'd personally design this as part of the set endpoints ie, CreateSet can create terms, and UpdateSet can remove/create/update terms
Alex
AlexOP8mo ago
That means I don't need separate endpoints like CreateTerm, UpdateTerm, right?
Pobiega
Pobiega8mo ago
you could, but if you agree with me you can also skip it
Alex
AlexOP8mo ago
sure, that's problem that I'm thinking how can I do it better instead of writing code..
Pobiega
Pobiega8mo ago
wouldn't say thats a problem in itself its a problem if you get hard-stuck because of overthinking but thinking before coding is generally a good thing
Want results from more Discord servers?
Add your server