Save entities with one-to-many relationship using WebAPI

2

I'm starting a WebApi project and I came across a situation that I did not know how to set my Controller class. 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 POST calls to API to save each content individually, with the danger of a problem occurring and having the chance of child content not being saved or using a single call to the % with parent% and the same save the contents of the child record?

Following modeling details

  • Initially it is good to point out that I simplified the entities to not fill with code.

  • When creating an article, you will create it in one language and this article can be translated into multiple languages. A content record will exist for each language.

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

asked by anonymous 29.04.2014 / 19:29

1 answer

2

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

As I understand it, in your case I think the best would be two separate POSTs, allowing the article to be supplemented with a new language in the future.

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

    
30.06.2014 / 20:27