-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCustomSharedDataSourceStorage.cs
105 lines (85 loc) · 3.8 KB
/
CustomSharedDataSourceStorage.cs
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
using SqlDefinitionStorageExample.EFCore;
using SqlDefinitionStorageExample.EFCore.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
using Telerik.WebReportDesigner.Services;
using Telerik.WebReportDesigner.Services.Models;
namespace SqlDefinitionStorageExample
{
public class CustomSharedDataSourceStorage(SqlDefinitionStorageContext context) : CustomResourceStorage(context, "Shared Data Sources"), ISharedDataSourceStorage
{
Task<SharedDataSourceModel> ISharedDataSourceStorage.GetModelAsync(string uri)
{
return Task.FromResult(GetDbResourceModel(uri).ToResourceFileModel() as SharedDataSourceModel);
}
Task<SharedDataSourceModel> ISharedDataSourceStorage.SaveAsync(SaveResourceModel model, byte[] resource)
{
return Task.FromResult(SaveCore(model, resource));
}
Task<SharedDataSourceModel> ISharedDataSourceStorage.RenameAsync(RenameResourceModel model)
{
return Task.FromResult(RenameCore(model));
}
Task<ResourceFileModel> IAssetsStorage.RenameAsync(RenameResourceModel model)
{
return Task.FromResult(RenameCore(model) as ResourceFileModel);
}
public new Task<ResourceFileModel> SaveAsync(SaveResourceModel model, byte[] resource)
{
return Task.FromResult(SaveCore(model, resource) as ResourceFileModel);
}
public new ResourceFileModel Overwrite(OverwriteResourceModel model, byte[] resource)
{
var entity = DbContext.Resources.FirstOrDefault(r => r.Uri == $"{Root}\\{model.Uri}") ?? throw new ResourceNotFoundException();
entity.Bytes = resource;
entity.ModifiedOn = DateTime.Now;
entity.Description = SharedDataSourceDescriptionHelper.Read(resource);
DbContext.SaveChanges();
return entity.ToSharedDataSourceModel();
}
public new byte[] GetByUri(string uri)
{
if (!uri.Contains($"{Root}\\"))
{
uri = $"{Root}\\{uri}";
}
return base.GetByUri(uri);
}
SharedDataSourceModel SaveCore(SaveResourceModel model, byte[] resource)
{
model.ParentUri = string.IsNullOrEmpty(model.ParentUri) ? Root : $"{Root}\\{model.ParentUri}";
model.ParentUri = model.ParentUri.Replace("/", "\\");
if (model.ParentUri.EndsWith('\\'))
{
model.ParentUri = model.ParentUri.Remove(model.ParentUri.Length - 1);
}
var entity = DbContext.Resources.FirstOrDefault(r => r.Uri == FixParentUri(model.ParentUri) + model.Name);
if (entity != null)
{
entity.Bytes = resource;
entity.ModifiedOn = DateTime.Now;
entity.Description = SharedDataSourceDescriptionHelper.Read(resource);
DbContext.SaveChanges();
return entity.ToSharedDataSourceModel();
}
var entityEntry = DbContext.Resources.Add(model.ToDbResourceModel(resource));
DbContext.SaveChanges();
return entityEntry.Entity.ToSharedDataSourceModel();
}
SharedDataSourceModel RenameCore(RenameResourceModel model)
{
string oldName = model.OldUri.Split("\\").Last();
var resource = DbContext.Resources.FirstOrDefault(r => r.Uri == model.OldUri);
if (resource != null)
{
resource.Name = model.Name;
resource.Uri = resource.Uri.Replace(oldName, model.Name);
resource.ModifiedOn = DateTime.Now;
DbContext.SaveChanges();
return resource.ToSharedDataSourceModel();
}
throw new ResourceNotFoundException();
}
}
}