aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/ColorResource.cs
blob: 289597d79a456c4144c4dfb630aa3024d2579fd8 (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
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Text.Json.Serialization;

namespace ColorPalette
{
    public sealed class ColorResourceFactory
    {
        public ColorResource Create() => new ColorResource();
    }

    public sealed class ColorResource : ResourceData, IResourcePlugin
    {
        [JsonPropertyName("id")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public int ColorId { get; set; }

        [JsonPropertyName("name")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public string Name { get; set; }

        [JsonPropertyName("year")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public int Year { get; set; }

        [JsonPropertyName("color")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public string Color { get; set; }

        [JsonPropertyName("pantone_value")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public string Pantone { get; set; }

        public static List<string> Roles =>
            [nameof(ColorId), nameof(Name), nameof(Year), nameof(Color), nameof(Pantone)];

        public override object this[string role] => role switch
        {
            nameof(ColorId) => ColorId,
            nameof(Name) => Name,
            nameof(Year) => Year,
            nameof(Color) => Color,
            nameof(Pantone) => Pantone,
            _ => null
        };

        public override void Add(PaginatedResource resPg)
        {
            resPg.Add(this);
        }

        public override void Update(PaginatedResource resPg)
        {
            resPg.Update(ColorId, new ColorResource()
            {
                Name = Name, Year = Year, Color = Color, Pantone = Pantone
            });
        }
    }
}