Navigation: Helper Classes >

JsonUpdateResponse

 

 

 

 

JsonUpdateResponse

 

The JsonUpdateResponse class is used to send a response from the custom end-point used to apply the changes sent via the onJsonUpdate event using an external API. The class structures the response in a format that the client-side DbNetEditCore class expects and can process.

 

public class JsonUpdateResponse

{

public bool Success { get; set; } // Indicates if the request was successful

public string Message { get; set; } // Success or error message displayed to the user

public object DataSet { get; set; } // Updated JSON dataset to refresh the UI

}

 

Below is an example of usage of the JsonUpdateResponse class in an end-point that takes the information passed to it and builds the relevant sql statements to apply the changes.

 

public async Task<IActionResult> OnPost([FromBody] JsonUpdateRequest jsonUpdateRequest)

{

    string message = "";

    bool success = true;

 

    using (var conn = new SqliteConnection(_connectionString))

    {

        string sql = string.Empty;

        var paramValues = new DynamicParameters();

        switch (jsonUpdateRequest.EditMode)

        {

            case EditMode.Update:

                string set = string.Join(",", jsonUpdateRequest.Changes.Keys.Select(k => $"{k} = @{k}"));

                paramValues.Add($"@{jsonUpdateRequest.PrimaryKeyName}", jsonUpdateRequest.PrimaryKeyValue);

                foreach (string key in jsonUpdateRequest.Changes.Keys)

                {

                    paramValues.Add($"@{key}", jsonUpdateRequest.Changes[key]);

                }

                sql = $"update products set {set} where {jsonUpdateRequest.PrimaryKeyName} = @{jsonUpdateRequest.PrimaryKeyName}";

                message = "Product updated";

                break;

            case EditMode.Insert:

                string columns = string.Join(",", jsonUpdateRequest.Changes.Keys.Select(k => $"{k}"));

                string values = string.Join(",", jsonUpdateRequest.Changes.Keys.Select(k => $"@{k}"));

 

                foreach (string key in jsonUpdateRequest.Changes.Keys)

                {

                    paramValues.Add($"@{key}", jsonUpdateRequest.Changes[key]);

                }

                sql = $"insert into products({columns}) values({values})";

                message = "Product added";

                break;

            case EditMode.Delete:

                paramValues.Add($"@{jsonUpdateRequest.PrimaryKeyName}", jsonUpdateRequest.PrimaryKeyValue);

                sql = $"delete from products where {jsonUpdateRequest.PrimaryKeyName} = @{jsonUpdateRequest.PrimaryKeyName}";

                message = "Product deleted";

                break;

        }

 

        try

        {

            conn.Execute(sql, paramValues);

        }

        catch (Exception ex)

        {

            message = ex.Message;

            success = false;

        }

 

        var products = await conn.QueryAsync<Product>("select * from products;");

        return new JsonResult(new JsonUpdateResponse { Success = success, Message = message, DataSet = products });

    }

 

 

 

 

 

Copyright © 2023 DbNetLink