1
|
using AutoMapper;
|
2
|
using Core.Contexts;
|
3
|
using Core.Entities;
|
4
|
using Models.Enums;
|
5
|
using Models.Tags;
|
6
|
using Models.Users;
|
7
|
using Serilog;
|
8
|
using System;
|
9
|
using System.Collections.Generic;
|
10
|
using System.Text;
|
11
|
using System.Threading.Tasks;
|
12
|
using Microsoft.EntityFrameworkCore;
|
13
|
using System.Linq;
|
14
|
|
15
|
namespace Core.Services.TagService
|
16
|
{
|
17
|
public class TagServiceEF : ITagService
|
18
|
{
|
19
|
private readonly DatabaseContext databaseContext;
|
20
|
private readonly ILogger logger;
|
21
|
private readonly IMapper mapper;
|
22
|
|
23
|
public TagServiceEF(DatabaseContext databaseContext, ILogger logger, IMapper mapper)
|
24
|
{
|
25
|
this.databaseContext = databaseContext;
|
26
|
this.logger = logger;
|
27
|
this.mapper = mapper;
|
28
|
}
|
29
|
|
30
|
public TagTreeResponse GetTagTree()
|
31
|
{
|
32
|
// Prefetch from DB
|
33
|
var tagCategories = databaseContext.TagCategories.Select(tc => tc).ToList();
|
34
|
var tags = databaseContext.Tags.Select(tc => tc).ToList();
|
35
|
var subTags = databaseContext.SubTags.Select(tc => tc).ToList();
|
36
|
|
37
|
var tagCategoriesDTOs = new List<TagCategoryInfo>();
|
38
|
foreach (var tagCategory in tagCategories)
|
39
|
{
|
40
|
var tagCategoryDTO = mapper.Map<TagCategoryInfo>(tagCategory);
|
41
|
var categoryTags = tags.Where(t => t.Category == tagCategory);
|
42
|
var tagDTOs = new List<TagInfo>();
|
43
|
|
44
|
foreach (var tag in categoryTags)
|
45
|
{
|
46
|
var tagDTO = mapper.Map<TagInfo>(tag);
|
47
|
var tagSubTags = subTags.Where(st => st.Tag == tag).ToList();
|
48
|
var subTagDTOs = new List<SubTagInfo>();
|
49
|
|
50
|
if (tagSubTags.Count() != 0)
|
51
|
{
|
52
|
foreach (var subTag in tagSubTags)
|
53
|
{
|
54
|
subTagDTOs.Add(mapper.Map<SubTagInfo>(subTag));
|
55
|
}
|
56
|
}
|
57
|
tagDTO.SubTags = subTagDTOs;
|
58
|
tagDTOs.Add(tagDTO);
|
59
|
}
|
60
|
|
61
|
tagCategoryDTO.Tags = tagDTOs;
|
62
|
tagCategoriesDTOs.Add(tagCategoryDTO);
|
63
|
}
|
64
|
|
65
|
return new TagTreeResponse()
|
66
|
{
|
67
|
TagCategories = tagCategoriesDTOs
|
68
|
};
|
69
|
}
|
70
|
|
71
|
public void CreateCategory(CreateCategoryRequest request)
|
72
|
{
|
73
|
if (request.Color == "" || request.Name == "")
|
74
|
{
|
75
|
throw new InvalidOperationException("Category name or color empty");
|
76
|
}
|
77
|
|
78
|
CategoryNameUnusedElseThrow(request.Name);
|
79
|
|
80
|
databaseContext.TagCategories.Add(new Entities.TagCategory()
|
81
|
{
|
82
|
Name = request.Name,
|
83
|
Color = request.Color,
|
84
|
Description = request.Description
|
85
|
});
|
86
|
|
87
|
databaseContext.SaveChanges();
|
88
|
}
|
89
|
|
90
|
public void DeleteCategory(Guid categoryId)
|
91
|
{
|
92
|
CategoryExistsElseThrow(categoryId);
|
93
|
|
94
|
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
|
95
|
databaseContext.TagCategories.Remove(category);
|
96
|
databaseContext.SaveChanges();
|
97
|
}
|
98
|
|
99
|
public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId)
|
100
|
{
|
101
|
CategoryExistsElseThrow(categoryId);
|
102
|
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
|
103
|
|
104
|
if (request.Name != null)
|
105
|
{
|
106
|
CategoryNameUnusedElseThrow(request.Name);
|
107
|
category.Name = request.Name;
|
108
|
}
|
109
|
|
110
|
if (request.Description != null)
|
111
|
{
|
112
|
category.Description = request.Description;
|
113
|
}
|
114
|
|
115
|
if (request.Color != null)
|
116
|
{
|
117
|
if (request.Color == "")
|
118
|
{
|
119
|
throw new InvalidOperationException("Empty color");
|
120
|
}
|
121
|
category.Color = request.Color;
|
122
|
}
|
123
|
|
124
|
databaseContext.SaveChanges();
|
125
|
}
|
126
|
|
127
|
public void CreateTag(CreateTagRequest request)
|
128
|
{
|
129
|
CategoryExistsElseThrow(request.CategoryId);
|
130
|
|
131
|
TagNameUnusedElseThrow(request.Name);
|
132
|
|
133
|
if (request.Color == "")
|
134
|
{
|
135
|
throw new InvalidOperationException("Color empty");
|
136
|
}
|
137
|
|
138
|
var category = databaseContext.TagCategories.First(tc => tc.Id == request.CategoryId);
|
139
|
|
140
|
databaseContext.Tags.Add(new Entities.Tag()
|
141
|
{
|
142
|
Name = request.Name,
|
143
|
Color = request.Color,
|
144
|
Description = request.Description,
|
145
|
Category = category
|
146
|
});
|
147
|
|
148
|
databaseContext.SaveChanges();
|
149
|
}
|
150
|
|
151
|
public void DeleteTag(Guid tagId)
|
152
|
{
|
153
|
TagExistsElseThrow(tagId);
|
154
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
155
|
databaseContext.Tags.Remove(tag);
|
156
|
databaseContext.SaveChanges();
|
157
|
}
|
158
|
|
159
|
public void UpdateTag(ModifyTagRequest request, Guid tagId)
|
160
|
{
|
161
|
TagExistsElseThrow(tagId);
|
162
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
163
|
|
164
|
if (request.Name != null)
|
165
|
{
|
166
|
TagNameUnusedElseThrow(request.Name);
|
167
|
tag.Name = request.Name;
|
168
|
}
|
169
|
|
170
|
if (request.Color != null)
|
171
|
{
|
172
|
if (request.Color == "")
|
173
|
{
|
174
|
throw new InvalidOperationException("Color empty");
|
175
|
}
|
176
|
tag.Color = request.Color;
|
177
|
}
|
178
|
|
179
|
if (request.Description != null)
|
180
|
{
|
181
|
tag.Description = request.Description;
|
182
|
}
|
183
|
|
184
|
databaseContext.SaveChanges();
|
185
|
}
|
186
|
|
187
|
public void CreateSubTag(CreateSubTagRequest request)
|
188
|
{
|
189
|
if (request.Name == "")
|
190
|
{
|
191
|
throw new InvalidOperationException("Subtag name empty");
|
192
|
}
|
193
|
|
194
|
SubTagNameUnusedElseThrow(request.Name);
|
195
|
|
196
|
TagExistsElseThrow(request.TagId);
|
197
|
var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
|
198
|
|
199
|
databaseContext.SubTags.Add(new Entities.SubTag()
|
200
|
{
|
201
|
Name = request.Name,
|
202
|
Description = request.Description,
|
203
|
Tag = tag
|
204
|
});
|
205
|
|
206
|
databaseContext.SaveChanges();
|
207
|
}
|
208
|
|
209
|
public void DeleteSubTag(Guid subtagId)
|
210
|
{
|
211
|
SubTagExistsElseThrow(subtagId);
|
212
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
213
|
databaseContext.SubTags.Remove(subtag);
|
214
|
databaseContext.SaveChanges();
|
215
|
}
|
216
|
|
217
|
public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
|
218
|
{
|
219
|
SubTagExistsElseThrow(subtagId);
|
220
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
221
|
|
222
|
if (request.Name != null)
|
223
|
{
|
224
|
SubTagNameUnusedElseThrow(request.Name);
|
225
|
subtag.Name = request.Name;
|
226
|
}
|
227
|
|
228
|
if (request.Description != null)
|
229
|
{
|
230
|
subtag.Description = request.Description;
|
231
|
}
|
232
|
|
233
|
databaseContext.SaveChanges();
|
234
|
}
|
235
|
|
236
|
public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
|
237
|
{
|
238
|
Annotation annotation = null;
|
239
|
try
|
240
|
{
|
241
|
annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
|
242
|
}
|
243
|
catch (Exception)
|
244
|
{
|
245
|
throw new InvalidOperationException("Annotation not found");
|
246
|
}
|
247
|
|
248
|
if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
|
249
|
{
|
250
|
throw new UnauthorizedAccessException("User does not have access to this annotation");
|
251
|
}
|
252
|
|
253
|
AnnotationTag occurence = null;
|
254
|
try
|
255
|
{
|
256
|
occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
|
257
|
.First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
|
258
|
}
|
259
|
catch (Exception)
|
260
|
{
|
261
|
throw new InvalidOperationException("The annotation does not have a tag with specified id");
|
262
|
}
|
263
|
|
264
|
occurence.Note = request.Note;
|
265
|
databaseContext.SaveChanges();
|
266
|
}
|
267
|
|
268
|
private void CategoryExistsElseThrow(Guid categoryId)
|
269
|
{
|
270
|
if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
|
271
|
{
|
272
|
throw new InvalidOperationException("Category does not exist");
|
273
|
}
|
274
|
}
|
275
|
|
276
|
private void CategoryNameUnusedElseThrow(string name)
|
277
|
{
|
278
|
if (databaseContext.TagCategories.Any(tc => tc.Name == name))
|
279
|
{
|
280
|
throw new InvalidOperationException("Category name already used");
|
281
|
}
|
282
|
}
|
283
|
|
284
|
private void TagExistsElseThrow(Guid tagId)
|
285
|
{
|
286
|
if (!databaseContext.Tags.Any(t => t.Id == tagId))
|
287
|
{
|
288
|
throw new InvalidOperationException("Tag does not exist");
|
289
|
}
|
290
|
}
|
291
|
|
292
|
private void TagNameUnusedElseThrow(string name)
|
293
|
{
|
294
|
if (databaseContext.Tags.Any(t => t.Name == name))
|
295
|
{
|
296
|
throw new InvalidOperationException("Tag name already used");
|
297
|
}
|
298
|
}
|
299
|
|
300
|
private void SubTagExistsElseThrow(Guid subtagId)
|
301
|
{
|
302
|
if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
|
303
|
{
|
304
|
throw new InvalidOperationException("Subtag does not exist");
|
305
|
}
|
306
|
}
|
307
|
|
308
|
private void SubTagNameUnusedElseThrow(string name)
|
309
|
{
|
310
|
if (databaseContext.SubTags.Any(st => st.Name == name))
|
311
|
{
|
312
|
throw new InvalidOperationException("Subtag name already used");
|
313
|
}
|
314
|
}
|
315
|
}
|
316
|
}
|