Creating/changing entities in batch

Asked

Viewed 33 times

0

I’m reading a book from Angular 5 + Asp.NET Core 2.0 (although I’m using Angular 7 and Asp.NET Core 2.1), and I’m trying to do the system that this book teaches me to do. It is a quiz system, where you can create your quizzes, having several questions, each question having several answers.

I have already created the web api for basic CRUD for each type of entity: Quiz, Question and Answer.

Remember that each quiz has questions, and each question has answers.

Following the book, to create a question, the quiz must already exist in the database. The same goes for the answers: to create one, your question must already be created.

I would like to make an environment where the user can enter ALL the information of the quiz, the questions and the answers before sending the data to the server. That is, I wish there was the possibility to create the quiz, the questions and the answers on the client side and, when the user is ready, save everything you have to save at once.

Does it make sense to do it that way? Is it viable? If so, should I create loops to send the questions and answers to the server? Or should I send everything in a single object and handle the data on the server side?

I tried to loop Angular, but I think javascript doesn’t do very well with asynchronous order callbacks within loops...

1 answer

0


You can send everything in one object.

There’s a lot of discussion about it when it’s being created Apis Restful. Because it is a convention that each object must have its own Controller and that the POST methods receive only one object.

But this always goes according to the needs of the programmer/ software.

If you send everything at once, you can mount and send JSON this way for example:

var Quiz = {
   nome: "Quiz 01",
   perguntas: [
      {
         nome: "Pergunta01",
         respostas: [
            {
               nome: "resposta A"
            },
            {
               nome: "resposta B"
            }
          ]
      },
      {
         nome: "Pergunta 02",
         respostas: [
            {
               nome: "resposta A"
            },
            {
               nome: "resposta B"
            }
          ]
      }
    ]
  }

I hope I’ve helped.

  • I get it. In this case then the quiz controller would receive a quiz object containing all the questions and answers and, after including the quiz in the BD, would call the quiz controller (which would therefore call the answer controller)?

  • In this case I recommend you treat each persistence separately. It will depend on the ORM you are working on as well... Entity, Nhibernate, Dapper. No Entity if I’m not mistaken you could save everything at once, just by adding the Quiz object to the context and saving the changes.

  • I am using the Entity Framework, but I will treat it separately even. In future projects, I will keep your answer in mind.

Browser other questions tagged

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