Jarstat

Форк
0
221 строка · 6.9 Кб
1
using Jarstat.Domain.Errors;
2
using Jarstat.Domain.Primitives;
3
using Jarstat.Domain.Records;
4
using Jarstat.Domain.Shared;
5
using System.Text.Json.Serialization;
6

7
namespace Jarstat.Domain.Entities;
8

9
public class Document : Entity
10
{
11
    private Document() { }
12

13
    private Document(
14
        Guid id,
15
        string displayName,
16
        string fileName,
17
        Folder folder,
18
        string? description,
19
        DateTime dateTimeCreated,
20
        DateTime dateTimeUpdated,
21
        User creator,
22
        User lastUpdater,
23
        File? file,
24
        double sortOrder)
25
    {
26
        Id = id;
27
        DisplayName = displayName;
28
        FileName = fileName;
29
        Folder = folder;
30
        Description = description;
31
        DateTimeCreated = dateTimeCreated;
32
        DateTimeUpdated = dateTimeUpdated;
33
        Creator = creator;
34
        LastUpdater = lastUpdater;
35
        File = file;
36
        SortOrder = sortOrder;
37
    }
38

39
    [JsonConstructor]
40
    public Document(
41
        Guid id,
42
        string displayName,
43
        string fileName,
44
        Folder folder,
45
        string? description,
46
        DateTime dateTimeCreated,
47
        DateTime dateTimeUpdated,
48
        User creator,
49
        User lastUpdater,
50
        Guid? fileId,
51
        double sortOrder)
52
    {
53
        Id = id;
54
        DisplayName = displayName;
55
        FileName = fileName;
56
        Folder = folder;
57
        Description = description;
58
        DateTimeCreated = dateTimeCreated;
59
        DateTimeUpdated = dateTimeUpdated;
60
        Creator = creator;
61
        LastUpdater = lastUpdater;
62
        FileId = fileId;
63
        SortOrder = sortOrder;
64
    }
65

66
    public string DisplayName { get; private set; } = null!; 
67
    public string FileName { get; private set; } = null!;
68
    public Guid FolderId { get; private set; }
69
    public Folder Folder { get; private set; } = null!;
70
    public string? Description { get; private set; }
71
    public Guid? FileId { get; private set; }
72
    public File? File { get; private set; }
73
    public double SortOrder { get; private set; }
74

75
    public static explicit operator Item?(Document? document) => 
76
        document is null ? null : new Item(document.Id, 
77
                                           document.DisplayName, 
78
                                           document.FolderId, 
79
                                           "Document", 
80
                                           document.DateTimeCreated, 
81
                                           document.DateTimeUpdated, 
82
                                           document.SortOrder);
83

84
    //public static Result<Document> Create(
85
    //    string displayName, 
86
    //    string fileName,
87
    //    Folder folder, 
88
    //    string? description, 
89
    //    User creator,
90
    //    File file)
91
    //{
92
    //    if (string.IsNullOrWhiteSpace(displayName) || string.IsNullOrWhiteSpace(fileName))
93
    //        return Result<Document>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue);
94

95
    //    if (folder is null || creator is null || file is null)
96
    //        return Result<Document>.Failure(DomainErrors.ArgumentNullValue);
97

98
    //    var lastUpdater = creator;
99
    //    var document = new Document(
100
    //        Guid.NewGuid(), 
101
    //        displayName, 
102
    //        fileName, 
103
    //        folder, 
104
    //        description, 
105
    //        DateTime.UtcNow, 
106
    //        DateTime.UtcNow, 
107
    //        creator, 
108
    //        lastUpdater, 
109
    //        file);
110

111
    //    return document;
112
    //}
113

114
    public static Result<Document?> Create(
115
        string displayName,
116
        string fileName,
117
        Folder folder,
118
        string? description,
119
        User creator,
120
        Guid? fileId)
121
    {
122
        if (string.IsNullOrWhiteSpace(displayName))
123
            return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
124
                .WithParameters(nameof(displayName), typeof(string).ToString()));
125

126
        if (string.IsNullOrWhiteSpace(fileName))
127
            return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
128
                .WithParameters(nameof(fileName), typeof(string).ToString()));
129

130
        if (folder is null)
131
            return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
132
                .WithParameters(nameof(folder), typeof(Folder).ToString()));
133

134
        if (creator is null)
135
            return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
136
                .WithParameters(nameof(creator), typeof(User).ToString()));
137

138
        var lastUpdater = creator;
139
        var document = new Document(
140
            Guid.NewGuid(),
141
            displayName,
142
            fileName,
143
            folder,
144
            description,
145
            DateTime.UtcNow,
146
            DateTime.UtcNow,
147
            creator,
148
            lastUpdater,
149
            fileId,
150
            long.MaxValue);
151

152
        return document;
153
    }
154

155
    public Result<Document?> Update(
156
        string displayName,
157
        string fileName,
158
        Folder folder,
159
        string? description,
160
        User lastUpdater,
161
        Guid? fileId)
162
    {
163
        if (string.IsNullOrWhiteSpace(displayName))
164
            return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
165
                .WithParameters(nameof(displayName), typeof(string).ToString()));
166

167
        if (string.IsNullOrWhiteSpace(fileName))
168
            return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
169
                .WithParameters(nameof(fileName), typeof(string).ToString()));
170

171
        if (folder is null)
172
            return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
173
                .WithParameters(nameof(folder), typeof(Folder).ToString()));
174

175
        if (lastUpdater is null)
176
            return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
177
                .WithParameters(nameof(lastUpdater), typeof(User).ToString()));
178

179
        DisplayName = displayName;
180
        FileName = fileName;
181
        Folder = folder;
182
        Description = description;
183
        LastUpdater = lastUpdater;
184
        FileId = fileId;
185

186
        return this;
187
    }
188

189
    public Result<Document?> WithFile(File file)
190
    {
191
        if (file is null)
192
            return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
193
                .WithParameters(nameof(file), typeof(File).ToString()));
194

195
        var document = new Document(
196
            this.Id,
197
            this.DisplayName,
198
            this.FileName,
199
            this.Folder,
200
            this.Description,
201
            this.DateTimeCreated,
202
            this.DateTimeUpdated,
203
            this.Creator,
204
            this.LastUpdater,
205
            file,
206
            this.SortOrder);
207

208
        return document;
209
    }
210

211
    public Result<Document?> ChangeSortOrder(double sortOrder)
212
    {
213
        if (sortOrder < 0)
214
            return Result<Document?>.Failure(new Error("Error.ArgumentLessThanZeroValue", "Значение параметра не может быть меньше нуля")
215
                .WithParameters(nameof(sortOrder), typeof(double).ToString()));
216

217
        SortOrder = sortOrder;
218

219
        return this;
220
    }
221
}
222

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.