aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
blob: 6f2f4466b01800ff3c0dff5805a978cc95bd708d (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
using System.Diagnostics;
using System.Text.Json;
using Microsoft.Extensions.Options;

namespace CrupestApi.Commons.Crud;

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

    public EntityJsonHelper(TableInfoFactory tableInfoFactory)
    {
        _table = tableInfoFactory.Get(typeof(TEntity));
        _jsonSerializerOptions = new JsonSerializerOptions();
        _jsonSerializerOptions.AllowTrailingCommas = true;
        _jsonSerializerOptions.PropertyNameCaseInsensitive = true;
        _jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        foreach (var type in _table.Columns.Select(c => c.ColumnType))
        {
            if (type.JsonConverter is not null)
            {
                _jsonSerializerOptions.Converters.Add(type.JsonConverter);
            }
        }
    }

    public virtual JsonDocument ConvertEntityToJson(object? entity)
    {
        Debug.Assert(entity is null || entity is TEntity);
        return JsonSerializer.SerializeToDocument<TEntity?>((TEntity?)entity, _jsonSerializerOptions);
    }

    public virtual TEntity? ConvertJsonToEntity(JsonDocument json)
    {
        var entity = json.Deserialize<TEntity>();
        if (entity is null) return null;

        foreach (var column in _table.Columns)
        {
            var propertyValue = column.PropertyInfo?.GetValue(entity);

            if ((column.IsAutoIncrement || column.IsGenerated) && propertyValue is not null)
            {
                throw new Exception("You can't specify this property because it is auto generated.");
            }
        }

        return entity;
    }
}