-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathHelpController.cs
More file actions
205 lines (170 loc) · 6.58 KB
/
HelpController.cs
File metadata and controls
205 lines (170 loc) · 6.58 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Signum.API.Filters;
using Signum.Files;
using System.ComponentModel.DataAnnotations;
using System.IO;
namespace Signum.Help;
[ValidateModelFilter]
public class HelpController : ControllerBase
{
[HttpGet("api/help/index")]
public HelpIndexTS Index()
{
HelpPermissions.ViewHelp.AssertAuthorized();
return new HelpIndexTS
{
Culture = HelpLogic.GetCulture().ToCultureInfoEntity(),
Namespaces = HelpLogic.GetNamespaceHelps().Select(s => new NamespaceItemTS
{
Namespace = s.Namespace,
Module = s.Module,
Title = s.Title,
HasEntity = s.DBEntity != null,
AllowedTypes = s.AllowedTypes
}).ToList(),
Appendices = HelpLogic.GetAppendixHelps().Select(s => new AppendiceItemTS
{
UniqueName = s.UniqueName,
Title = s.Title,
}).ToList(),
};
}
[HttpGet("api/help/namespace/{namespace}")]
public NamespaceHelp Namespace(string @namespace)
{
HelpPermissions.ViewHelp.AssertAuthorized();
var help = HelpLogic.GetNamespaceHelp(@namespace.Replace("_", "."));
help.AssertAllowed();
return help;
}
[HttpPost("api/help/saveNamespace")]
public void SaveNamespace([Required][FromBody]NamespaceHelpEntity entity)
{
HelpPermissions.ViewHelp.AssertAuthorized();
if (!entity.Title.HasText() && !entity.Description.HasText())
{
if (!entity.IsNew)
entity.ToLite().DeleteLite(NamespaceHelpOperation.Delete);
}
else
entity.Execute(NamespaceHelpOperation.Save);
}
[HttpGet("api/help/appendix/{uniqueName?}")]
public AppendixHelpEntity Appendix(string? uniqueName)
{
HelpPermissions.ViewHelp.AssertAuthorized();
if (string.IsNullOrWhiteSpace(uniqueName))
return new AppendixHelpEntity
{
Culture = HelpLogic.GetCulture().ToCultureInfoEntity()
};
var help = HelpLogic.GetAppendixHelp(uniqueName);
return help;
}
[HttpPost("api/help/saveAppendix")]
public void SaveNamespace([Required][FromBody] AppendixHelpEntity entity)
{
HelpPermissions.ViewHelp.AssertAuthorized();
entity.Execute(AppendixHelpOperation.Save);
}
[HttpGet("api/help/type/{cleanName}")]
public TypeHelpEntity Type(string cleanName)
{
//HelpPermissions.ViewHelp.AssertAuthorized();
var type = TypeLogic.GetType(cleanName);
var help = HelpLogic.GetTypeHelp(type);
help.AssertAllowed();
return help.GetEntity();
}
[HttpPost("api/help/saveType")]
public void SaveType([Required][FromBody]TypeHelpEntity entity)
{
HelpPermissions.ViewHelp.AssertAuthorized();
using (var tr = new Transaction())
{
foreach (var query in entity.Queries)
{
query.Columns.RemoveAll(a => !a.Description.HasText());
if (query.Columns.IsEmpty() && !query.Description.HasText())
{
if (!query.IsNew)
query.ToLite().DeleteLite(QueryHelpOperation.Delete);
}
else
query.Execute(QueryHelpOperation.Save);
}
var currentProperties = entity.Properties.Select(p => p.Property.ToPropertyRoute()).ToHashSet();
var hiddenProperties = entity.IsNew ? Enumerable.Empty<PropertyRouteHelpEmbedded>() : entity.ToLite().Retrieve().Properties
.Where(p => !currentProperties.Contains(p.Property.ToPropertyRoute()))
.ToList();
entity.Properties.AddRange(hiddenProperties);
entity.Properties.RemoveAll(a => !a.Description.HasText());
var currentOperations = entity.Operations.Select(p => p.Operation).ToHashSet();
var hiddenOperations = entity.IsNew ? Enumerable.Empty<OperationHelpEmbedded>() : entity.ToLite().Retrieve().Operations
.Where(p => !currentOperations.Contains(p.Operation))
.ToList();
entity.Operations.AddRange(hiddenOperations);
entity.Operations.RemoveAll(a => !a.Description.HasText());
if (entity.Properties.IsEmpty() && entity.Operations.IsEmpty() && !entity.Description.HasText())
{
if (!entity.IsNew)
entity.ToLite().DeleteLite(TypeHelpOperation.Delete);
}
else
entity.Execute(TypeHelpOperation.Save);
tr.Commit();
}
}
[HttpPost("api/help/export")]
public FileStreamResult Export([Required, FromBody] Lite<IHelpEntity>[] lites)
{
HelpPermissions.ExportHelp.AssertAuthorized();
var bytes = HelpExportImport.ExportToZipBytes([.. lites.RetrieveList()], "Help");
var typeName = lites.Select(a => a.EntityType).Distinct().SingleEx().ToTypeEntity().CleanName;
var Ids = lites.ToString(a => a.Id.ToString().Truncate(5), "_");
var fileName = $"{typeName}{Ids}.zip";
return MimeMapping.GetFileStreamResult(new MemoryStream(bytes), fileName);
}
[HttpGet("api/help/getImageId")]
public ActionResult<string> GetImageId([Required, FromQuery] Guid guid)
{
Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(365),
};
var id = HelpLogic.GetImageId(guid);
return id is null ? NotFound() : id.Value.ToString();
}
[HttpPost("api/help/importPreview")]
public HelpImportPreviewModel ImportPreview([Required, FromBody] FileUpload file)
{
return HelpExportImport.ImportPreviewFromZip(file.content);
}
[HttpPost("api/help/applyImport")]
public HelpImportReportModel ApplyImport([Required, FromBody] FileUploadWithModel<HelpImportPreviewModel> fileModel)
{
return HelpExportImport.ImportFromZip(fileModel.file.content, fileModel.model);
}
}
public class HelpIndexTS
{
public CultureInfoEntity Culture;
public List<NamespaceItemTS> Namespaces;
public List<AppendiceItemTS> Appendices;
}
public class NamespaceItemTS
{
public string Namespace;
public string? Module;
public string Title;
public bool HasEntity;
public EntityItem[] AllowedTypes;
}
public class AppendiceItemTS
{
public string UniqueName;
public string Title;
}