aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
blob: 44893073eb5d5848804a1769c7427925443530c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Text.Json;
using Microsoft.Extensions.Options;

namespace CrupestApi.Commons.Crud;

/// <summary>
/// Contains all you need to do with json.
/// </summary>
public class EntityJsonHelper<TEntity> where TEntity : class
{
    private readonly TableInfo _table;
    private readonly IOptionsMonitor<JsonSerializerOptions> _jsonSerializerOptions;

    public EntityJsonHelper(TableInfoFactory tableInfoFactory, IOptionsMonitor<JsonSerializerOptions> jsonSerializerOptions)
    {
        _table = tableInfoFactory.Get(typeof(TEntity));
        _jsonSerializerOptions = jsonSerializerOptions;
    }

    public Dictionary<string, object?> ConvertEntityToDictionary(TEntity entity, bool includeNonColumnProperties = false)
    {
        var result = new Dictionary<string, object?>();

        foreach (var column in _table.PropertyColumns)
        {
            var value = column.PropertyInfo!.GetValue(entity);
            var realValue = column.ColumnType.ConvertToDatabase(value);
            result[column.ColumnName] = realValue;
        }

        if (includeNonColumnProperties)
        {
            foreach (var propertyInfo in _table.NonColumnProperties)
            {
                var value = propertyInfo.GetValue(entity);
                result[propertyInfo.Name] = value;
            }
        }

        return result;
    }

    public string ConvertEntityToJson(TEntity entity, bool includeNonColumnProperties = false)
    {
        var dictionary = ConvertEntityToDictionary(entity, includeNonColumnProperties);
        return JsonSerializer.Serialize(dictionary, _jsonSerializerOptions.CurrentValue);
    }

    public TEntity ConvertJsonToEntityForInsert(JsonElement jsonElement)
    {
        if (jsonElement.ValueKind is not JsonValueKind.Object)
            throw new ArgumentException("The jsonElement must be an object.");

        var result = Activator.CreateInstance<TEntity>();
        foreach (var column in _table.PropertyColumns)
        {
            if (jsonElement.TryGetProperty(column.ColumnName, out var value))
            {
                var realValue = column.ColumnType.ConvertFromDatabase(value);
                column.PropertyInfo!.SetValue(result, realValue);
            }
        }

        return result;
    }

    public TEntity ConvertJsonToEntityForInsert(string json)
    {
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(json, _jsonSerializerOptions.CurrentValue);
        return ConvertJsonToEntityForInsert(jsonElement!);
    }

    public TEntity ConvertJsonToEntityForUpdate(JsonElement jsonElement, out UpdateBehavior updateBehavior)
    {
        if (jsonElement.ValueKind is not JsonValueKind.Object)
            throw new UserException("The jsonElement must be an object.");

        updateBehavior = UpdateBehavior.None;

        if (jsonElement.TryGetProperty("$saveNull", out var saveNullValue))
        {
            if (saveNullValue.ValueKind is JsonValueKind.True)
            {
                updateBehavior |= UpdateBehavior.SaveNull;
            }
            else if (saveNullValue.ValueKind is JsonValueKind.False)
            {

            }
            else
            {
                throw new UserException("The $saveNull must be a boolean.");
            }
        }

        var result = Activator.CreateInstance<TEntity>();
        foreach (var column in _table.PropertyColumns)
        {
            if (jsonElement.TryGetProperty(column.ColumnName, out var value))
            {
                var realValue = column.ColumnType.ConvertFromDatabase(value);
                column.PropertyInfo!.SetValue(result, realValue);
            }
        }

        return result;
    }

    public TEntity ConvertJsonToEntityForUpdate(string json, out UpdateBehavior updateBehavior)
    {
        var jsonElement = JsonSerializer.Deserialize<JsonElement>(json, _jsonSerializerOptions.CurrentValue);
        return ConvertJsonToEntityForUpdate(jsonElement!, out updateBehavior);
    }
}