Jarstat
221 строка · 6.9 Кб
1using Jarstat.Domain.Errors;
2using Jarstat.Domain.Primitives;
3using Jarstat.Domain.Records;
4using Jarstat.Domain.Shared;
5using System.Text.Json.Serialization;
6
7namespace Jarstat.Domain.Entities;
8
9public class Document : Entity
10{
11private Document() { }
12
13private Document(
14Guid id,
15string displayName,
16string fileName,
17Folder folder,
18string? description,
19DateTime dateTimeCreated,
20DateTime dateTimeUpdated,
21User creator,
22User lastUpdater,
23File? file,
24double sortOrder)
25{
26Id = id;
27DisplayName = displayName;
28FileName = fileName;
29Folder = folder;
30Description = description;
31DateTimeCreated = dateTimeCreated;
32DateTimeUpdated = dateTimeUpdated;
33Creator = creator;
34LastUpdater = lastUpdater;
35File = file;
36SortOrder = sortOrder;
37}
38
39[JsonConstructor]
40public Document(
41Guid id,
42string displayName,
43string fileName,
44Folder folder,
45string? description,
46DateTime dateTimeCreated,
47DateTime dateTimeUpdated,
48User creator,
49User lastUpdater,
50Guid? fileId,
51double sortOrder)
52{
53Id = id;
54DisplayName = displayName;
55FileName = fileName;
56Folder = folder;
57Description = description;
58DateTimeCreated = dateTimeCreated;
59DateTimeUpdated = dateTimeUpdated;
60Creator = creator;
61LastUpdater = lastUpdater;
62FileId = fileId;
63SortOrder = sortOrder;
64}
65
66public string DisplayName { get; private set; } = null!;
67public string FileName { get; private set; } = null!;
68public Guid FolderId { get; private set; }
69public Folder Folder { get; private set; } = null!;
70public string? Description { get; private set; }
71public Guid? FileId { get; private set; }
72public File? File { get; private set; }
73public double SortOrder { get; private set; }
74
75public static explicit operator Item?(Document? document) =>
76document is null ? null : new Item(document.Id,
77document.DisplayName,
78document.FolderId,
79"Document",
80document.DateTimeCreated,
81document.DateTimeUpdated,
82document.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
114public static Result<Document?> Create(
115string displayName,
116string fileName,
117Folder folder,
118string? description,
119User creator,
120Guid? fileId)
121{
122if (string.IsNullOrWhiteSpace(displayName))
123return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
124.WithParameters(nameof(displayName), typeof(string).ToString()));
125
126if (string.IsNullOrWhiteSpace(fileName))
127return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
128.WithParameters(nameof(fileName), typeof(string).ToString()));
129
130if (folder is null)
131return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
132.WithParameters(nameof(folder), typeof(Folder).ToString()));
133
134if (creator is null)
135return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
136.WithParameters(nameof(creator), typeof(User).ToString()));
137
138var lastUpdater = creator;
139var document = new Document(
140Guid.NewGuid(),
141displayName,
142fileName,
143folder,
144description,
145DateTime.UtcNow,
146DateTime.UtcNow,
147creator,
148lastUpdater,
149fileId,
150long.MaxValue);
151
152return document;
153}
154
155public Result<Document?> Update(
156string displayName,
157string fileName,
158Folder folder,
159string? description,
160User lastUpdater,
161Guid? fileId)
162{
163if (string.IsNullOrWhiteSpace(displayName))
164return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
165.WithParameters(nameof(displayName), typeof(string).ToString()));
166
167if (string.IsNullOrWhiteSpace(fileName))
168return Result<Document?>.Failure(DomainErrors.ArgumentNullOrWhiteSpaceValue
169.WithParameters(nameof(fileName), typeof(string).ToString()));
170
171if (folder is null)
172return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
173.WithParameters(nameof(folder), typeof(Folder).ToString()));
174
175if (lastUpdater is null)
176return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
177.WithParameters(nameof(lastUpdater), typeof(User).ToString()));
178
179DisplayName = displayName;
180FileName = fileName;
181Folder = folder;
182Description = description;
183LastUpdater = lastUpdater;
184FileId = fileId;
185
186return this;
187}
188
189public Result<Document?> WithFile(File file)
190{
191if (file is null)
192return Result<Document?>.Failure(DomainErrors.ArgumentNullValue
193.WithParameters(nameof(file), typeof(File).ToString()));
194
195var document = new Document(
196this.Id,
197this.DisplayName,
198this.FileName,
199this.Folder,
200this.Description,
201this.DateTimeCreated,
202this.DateTimeUpdated,
203this.Creator,
204this.LastUpdater,
205file,
206this.SortOrder);
207
208return document;
209}
210
211public Result<Document?> ChangeSortOrder(double sortOrder)
212{
213if (sortOrder < 0)
214return Result<Document?>.Failure(new Error("Error.ArgumentLessThanZeroValue", "Значение параметра не может быть меньше нуля")
215.WithParameters(nameof(sortOrder), typeof(double).ToString()));
216
217SortOrder = sortOrder;
218
219return this;
220}
221}
222