Share via

Return only one row when not authenticated

Anonymous
2023-05-23T14:52:03.85+00:00

Hi,

I have the below function which I expect to return the following when authenticated

{
    "authenticated": true,
    "member_id": 123,
    "first_name": "my first name",
    "last_name": "my last name"
}

but when authenticated is false then it should only return one row like this:

{
    "authenticated": true,
}

This is my Azure Function:



namespace MyApp;

public class MyFunction
{
    private readonly ILogger _logger;

    public ActivationSeconds(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<MyFunction>();
    }

    [Function("MyFunction")]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "MyFunction")] HttpRequestData req,
        string id,
        string email)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "application/json; charset=utf-8");

        var connectionString = "Server=server11.database.windows.net;Database=db;User Id=myuser;Password=MyPassword;";
        var query = "EXEC get_data @id, @email";

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

            using (var command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@id", id);
                command.Parameters.AddWithValue("@email", email);

                var reader = command.ExecuteReader();
                var dataList = new List<ActivationSecondsData>();
                var authenticated = false;

                while (reader.Read())
                {
                    authenticated = Convert.ToBoolean(reader["authenticated"]);

                    if (authenticated)
                    {
                        var data = new ActivationSecondsData
                        {
                            authenticated = true,
                            member_id = (Int64)reader["member_id"],
                        };

                        string json = JsonSerializer.Serialize(data);
                        response.WriteString(json);
                    }
                    else
                    {
                        var data = new ActivationSecondsData { authenticated = false };
                        string json = JsonSerializer.Serialize(data);
                        response.WriteString(json);
                    }
                }
            }
        }

        return response;
    }
}


It is working when authenticated is true but when authenticated i sfalse I get the following:

{
    "authenticated": false,
    "member_id": 0,
    "first_name": null,
    "last_name": null
}

How can I fix this?

Thanks,

Jassim

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Developer technologies | .NET | .NET Multi-platform App UI
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2023-05-23T15:23:57.61+00:00

    just return what you want:

    var data = new { authenticated = false };
    string json = JsonSerializer.Serialize(data);
    response.WriteString(json);
    

    Was this answer helpful?

    1 person found this answer helpful.

  2. Nisha Pillai 75 Reputation points
    2023-05-24T08:04:40.71+00:00

    Hi @Anonymous ,

    You can fix the issue in couple of different ways:

    1. By updating the underlying model--> Make member_id, first_name, and last_name nullable (int? and string), their default values will be null when authenticated is false and use decorator [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)].
    public class ActivationSecondsData
    {
        public bool authenticated { get; set; }
    
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public int? member_id { get; set; }
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string first_name { get; set; }
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string last_name { get; set; }
    }
    
    1. Don't use the ActivationSecondsData model when authenticated value is false.
    var data = new { authenticated = false }; 
    string json = JsonSerializer.Serialize(data);
    

    Hope this helps.

    Kind Regards,

    Nisha

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.