We should refactor the function GetLayoutNameAndID(int userID) In LayoutService #762

Closed
opened 2026-05-06 06:48:42 +00:00 by a22erigr · 1 comment
Collaborator

Description

The current implementation is fragile and needs to be refactors. It manually creates a json array. If it is possible implement a more robust way to convert json strings into a array

public async Task<LayoutResponse> GetLayoutNameAndID(int userID)
  {
    try
    {
      string responseString = "[";
      List<LayoutNameAndIDResponse> results = await DatabaseQueries.GetLayoutNameAndID(userID);
      foreach (LayoutNameAndIDResponse result in results)
      {
        var data = new { result.Name, result.ID };
        string jsonString = JsonSerializer.Serialize(data);
        responseString += jsonString;
        responseString += ",";
      }
      responseString = responseString[..^1]; // Removes last "," 
      responseString += "]";
      // This converts the object into the string: "{"Name":null,"ID":null}"
      return responseString == "]" ? new LayoutResponse("Could not find any layouts") : new LayoutResponse(null, responseString);// NOTE should be moved to service 
    }
    catch (Exception exception)
    {
      Console.WriteLine(exception.Message);
      return new LayoutResponse(exception.Message);
    }
  }

What should be done

  • Refactor the code so it no longer "manually" builds a json array
## Description The current implementation is fragile and needs to be refactors. It manually creates a json array. If it is possible implement a more robust way to convert json strings into a array ```c# public async Task<LayoutResponse> GetLayoutNameAndID(int userID) { try { string responseString = "["; List<LayoutNameAndIDResponse> results = await DatabaseQueries.GetLayoutNameAndID(userID); foreach (LayoutNameAndIDResponse result in results) { var data = new { result.Name, result.ID }; string jsonString = JsonSerializer.Serialize(data); responseString += jsonString; responseString += ","; } responseString = responseString[..^1]; // Removes last "," responseString += "]"; // This converts the object into the string: "{"Name":null,"ID":null}" return responseString == "]" ? new LayoutResponse("Could not find any layouts") : new LayoutResponse(null, responseString);// NOTE should be moved to service } catch (Exception exception) { Console.WriteLine(exception.Message); return new LayoutResponse(exception.Message); } } ``` ## What should be done + Refactor the code so it no longer "manually" builds a json array
a22erigr changed title from We should refactor the fuction GetLayoutNameAndID(int userID) In LayoutService to We should refactor the function GetLayoutNameAndID(int userID) In LayoutService 2026-05-06 06:49:06 +00:00
Author
Collaborator

Has been merged #783

Has been merged #783
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Andras/BoundlessFlowCampus2K#762
No description provided.