API Rest only returns the last value

Asked

Viewed 58 times

0

I am using ASP.Net to create a REST API

I’m trying to create a method that returns a list of groups structured this way:

{
  "IdGroup": "1",
  "NameGroup": "Group1",
  "Installations": [
    {
      "IdInstallation": "1",
      "NameInstallation": "Installation 1"
    },
    {
      "IdInstallation": "2",
      "NameInstallation": "Installation 2"
    },
    {
      "IdInstallation": "3",
      "NameInstallation": "Installation 3"
    },
    {
      "IdInstallation": "4",
      "NameInstallation": "Installation 4"
    }
  ]
}

But my method is only returning the last installation of the group (installation 4, in this case).

My method:

[HttpPost]
[Route ("groups")]
public HttpResponseMessage GetGroups() 
{
    try
    {
        List<Groups> listGroups = new List<Groups>();

        using (SqlConnection connection = new SqlConnection(this.connectionString))
        {
            connection.Open();

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "select Groups.IdGroup, Groups.NameGroup, AttributeCloned.IdInstallation, AttributeCloned.NameInstallation " +
                                      "from Groups " +
                                      "join AttributeCloned " +
                                      "on Groups.IdGroup = AttributeCloned.IdGroup "+
                                      "where Groups.IdGroup = 1 "+
                                      "order by IdInstallation asc";

                SqlDataReader reader = command.ExecuteReader();

                Groups group = new Groups();
                Installations inst = new Installations();
                while (reader.Read())
                {
                    group.IdGroup = reader["IdGroup"] == DBNull.Value ? 0 : Convert.ToInt32(reader["IdGroup"]);
                    group.NameGroup = reader["NameGroup"] == DBNull.Value ? string.Empty : reader["NameGroup"].ToString();
                    inst.IdInstallation = reader["IdInstallation"] == DBNull.Value ? 0 : Convert.ToInt32(reader["IdInstallation"]);
                    inst.NameInstallation = reader["NameInstallation"] == DBNull.Value ? string.Empty : reader["NameInstallation"].ToString();
                }

                group.installations.Add(inst);
                listGroups.Add(group);
            }
        }
        return Request.CreateResponse(HttpStatusCode.OK, listGroups.ToArray());
    }
    catch(Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}

What I am missing so that not be returned all the facilities of the respective group?

  • Unrelated to the question, but the method should not be used GET, in place of POST?

  • I added the answer with what I believe is the problem, but I was curious to know why you do not seek to use one ORM.

  • Add your base’s DDL and DML to the question, so you can reproduce the problem.

1 answer

1


There’s only one object inst in your code and every iteration it is updated. Place the lines Installations inst = new Installations(); and group.installations.Add(inst); while and the problem should be solved.

However, there is still another problem, this code is only prepared to return a list of groups with only one entry (in this case it is id =1). The problem is both in the query and in the Binding for your model.

  • Hello, I did what you suggested, put the lines "Installations Inst = new Installations()" and "group.instalations.add(Inst)" but still continues to return only one element in installations.

  • Then add to the question the DDL and DML of your base, so that it is possible to reproduce the problem.

Browser other questions tagged

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