Save entities with one relationship to many using Webapi

Asked

Viewed 214 times

2

I am starting a project in Webapi and came across a situation that did not know how to define my class of Controller. I have the following structure

public class Artigo {
    public int ArtigoId { get; set; }
    public int AutorId { get; set; }
    ...
    public IQueryable<ConteudoArtigo> ConteudosArtigo  { get; set; }
}

public class ConteudoArtigo {
    public int ConteudoArtigoId { get; set; }
    public int ArtigoId  { get; set; }
    public int IdiomaId { get; set; }
    public string Titulo { get; set; }
    public string Texto { get; set; }
}

My question is whether I should use two calls POST to the API to save each content individually, with the danger of a problem occurring and the chance of the child content not being saved or using a single call to the Controller father and the same save the content of the child record?

Follow details of modeling

  • Initially it is worth noting that I simplified the entities not to fill code.

  • When creating an article, you will create it in one language and this same article can be translated into multiple languages. For each language there will be a record of content.

  • The idea of having the article as a parent entity was that the comments and evaluations will be of the article, regardless of the language it is

  • You know that strange model you passed, sincerity did not understand !!!

  • @Fccdias Initially it is worth noting that I simplified the entities to not fill with code. When creating an article, you will create it in one language and this same article can be translated into multiple languages. For each language there will be a record of content. The idea of having the article as a parent entity was that the comments and evaluations will be of the article, regardless of the language it is

  • You are using ApiController to save the code?

  • Hello @Ciganomorrisonmendez, I left this project aside for now, but was using Apicontroller to save. I intend to resume it within 15 days

1 answer

2


The decision of one or two POST will almost always depend on the business.

From what I understand, in your case I believe that the best would be two separate POST, allowing article to be supplemented with a new language in the future.

Regarding the problem of not having a child saved, I recommend that the client who uses this Webapi check each child if the request was successfully made, but try a second time or alert the user to decide whether or not to try to save that specific language again. This can only be done by checking the return HTTP code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.