Projekt

Obecné

Profil

Stáhnout (11.8 KB) Statistiky
| Větev: | Tag: | Revize:
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(ERole userRole)
31
        {
32
            // Prefetch from DB
33
            var tagCategories = databaseContext.TagCategories
34
                .Select(tc => tc)
35
                .OrderBy(tc => tc.Name)
36
                .ToList();
37

    
38
            var tags = databaseContext.Tags
39
                .Select(tc => tc)
40
                .ToList();
41

    
42
            var subTags = databaseContext.SubTags
43
                .Select(tc => tc)
44
                .ToList();
45

    
46
            var tagCategoriesDTOs = new List<TagCategoryInfo>();
47
            foreach (var tagCategory in tagCategories)
48
            {
49
                if (tagCategory.DisabledForAnnotators && userRole == ERole.ANNOTATOR)
50
                {
51
                    continue;
52
                }
53

    
54
                var tagCategoryDTO = mapper.Map<TagCategoryInfo>(tagCategory);
55
                var categoryTags = tags
56
                    .Where(t => t.Category == tagCategory)
57
                    .OrderBy(t => t.Name);
58

    
59
                var tagDTOs = new List<TagInfo>();
60

    
61
                foreach (var tag in categoryTags)
62
                {
63
                    var tagDTO = mapper.Map<TagInfo>(tag);
64
                    var tagSubTags = subTags
65
                        .Where(st => st.Tag == tag)
66
                        .OrderBy(st => st.Name)
67
                        .ToList();
68

    
69
                    var subTagDTOs = new List<SubTagInfo>();
70

    
71
                    if (tagSubTags.Count() != 0)
72
                    {
73
                        foreach (var subTag in tagSubTags)
74
                        {
75
                            subTagDTOs.Add(mapper.Map<SubTagInfo>(subTag));
76
                        }
77
                    }
78
                    tagDTO.SubTags = subTagDTOs;
79
                    tagDTOs.Add(tagDTO);
80
                }
81

    
82
                tagCategoryDTO.Tags = tagDTOs;
83
                tagCategoriesDTOs.Add(tagCategoryDTO);
84
            }
85

    
86
            return new TagTreeResponse()
87
            {
88
                TagCategories = tagCategoriesDTOs
89
            };
90
        }
91

    
92
        public void CreateCategory(CreateCategoryRequest request)
93
        {
94
            if (request.Color == "" || request.Name == "")
95
            {
96
                throw new InvalidOperationException("Category name or color empty");
97
            }
98

    
99
            CategoryNameUnusedElseThrow(request.Name);
100

    
101
            databaseContext.TagCategories.Add(new TagCategory()
102
            {
103
                Name = request.Name,
104
                Color = request.Color,
105
                Description = request.Description,
106
                DisabledForAnnotators = request.DisabledForAnnotators
107
            });
108

    
109
            databaseContext.SaveChanges();
110
        }
111

    
112
        public void DeleteCategory(Guid categoryId)
113
        {
114
            CategoryExistsElseThrow(categoryId);
115

    
116
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
117
            databaseContext.TagCategories.Remove(category);
118
            databaseContext.SaveChanges();
119
        }
120

    
121
        public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId)
122
        {
123
            CategoryExistsElseThrow(categoryId);
124
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
125

    
126
            if (request.Name != null && request.Name != category.Name)
127
            {
128
                CategoryNameUnusedElseThrow(request.Name);
129
                category.Name = request.Name;
130
            }
131

    
132
            if (request.Description != null)
133
            {
134
                category.Description = request.Description;
135
            }
136

    
137
            if (request.Color != null)
138
            {
139
                if (request.Color == "")
140
                {
141
                    throw new InvalidOperationException("Empty color");
142
                }
143
                category.Color = request.Color;
144
            }
145

    
146
            if (request.DisabledForAnnotators != null)
147
            {
148
                category.DisabledForAnnotators = (bool)request.DisabledForAnnotators;
149
            }
150

    
151
            databaseContext.SaveChanges();
152
        }
153

    
154
        public void CreateTag(CreateTagRequest request)
155
        {
156
            CategoryExistsElseThrow(request.CategoryId);
157

    
158
            TagNameUnusedElseThrow(request.Name);
159

    
160
            if (request.Color == "")
161
            {
162
                throw new InvalidOperationException("Color empty");
163
            }
164

    
165
            var category = databaseContext.TagCategories.First(tc => tc.Id == request.CategoryId);
166

    
167
            databaseContext.Tags.Add(new Tag()
168
            {
169
                Name = request.Name,
170
                Color = request.Color,
171
                Description = request.Description,
172
                Category = category,
173
                SentimentEnabled = request.SentimentEnabled
174
            });
175

    
176
            databaseContext.SaveChanges();
177
        }
178

    
179
        public void DeleteTag(Guid tagId)
180
        {
181
            TagExistsElseThrow(tagId);
182
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
183
            databaseContext.Tags.Remove(tag);
184
            databaseContext.SaveChanges();
185
        }
186

    
187
        public void UpdateTag(ModifyTagRequest request, Guid tagId)
188
        {
189
            TagExistsElseThrow(tagId);
190
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
191

    
192
            if (request.Name != null && request.Name != tag.Name)
193
            {
194
                TagNameUnusedElseThrow(request.Name);
195
                tag.Name = request.Name;
196
            }
197

    
198
            if (request.Color != null)
199
            {
200
                if (request.Color == "")
201
                {
202
                    throw new InvalidOperationException("Color empty");
203
                }
204
                tag.Color = request.Color;
205
            }
206

    
207
            if (request.Description != null)
208
            {
209
                tag.Description = request.Description;
210
            }
211

    
212
            if (request.SentimentEnabled != null)
213
            {
214
                tag.SentimentEnabled = (bool)request.SentimentEnabled;
215
            }
216

    
217
            databaseContext.SaveChanges();
218
        }
219

    
220
        public void CreateSubTag(CreateSubTagRequest request)
221
        {
222
            if (request.Name == "")
223
            {
224
                throw new InvalidOperationException("Subtag name empty");
225
            }
226

    
227
            SubTagNameUnusedElseThrow(request.Name);
228

    
229
            TagExistsElseThrow(request.TagId);
230
            var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
231

    
232
            databaseContext.SubTags.Add(new Entities.SubTag()
233
            {
234
                Name = request.Name,
235
                Description = request.Description,
236
                Tag = tag,
237
                SentimentEnabled = request.SentimentEnabled
238
            });
239

    
240
            databaseContext.SaveChanges();
241
        }
242

    
243
        public void DeleteSubTag(Guid subtagId)
244
        {
245
            SubTagExistsElseThrow(subtagId);
246
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
247
            databaseContext.SubTags.Remove(subtag);
248
            databaseContext.SaveChanges();
249
        }
250

    
251
        public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
252
        {
253
            SubTagExistsElseThrow(subtagId);
254
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
255

    
256
            if (request.Name != null && request.Name != subtag.Name)
257
            {
258
                SubTagNameUnusedElseThrow(request.Name);
259
                subtag.Name = request.Name;
260
            }
261

    
262
            if (request.Description != null)
263
            {
264
                subtag.Description = request.Description;
265
            }
266

    
267
            if (request.SentimentEnabled != null)
268
            {
269
                subtag.SentimentEnabled = (bool)request.SentimentEnabled;
270
            }
271

    
272
            databaseContext.SaveChanges();
273
        }
274

    
275
        public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request, bool isFinal)
276
        {
277
            // We need to differentiate whether or not the annotation is final
278
            Annotation annotation = null;
279
            try
280
            {
281
                if (!isFinal)
282
                {
283
                    annotation = databaseContext.Annotations
284
                        .Where(a => !(a is FinalAnnotation))
285
                        .Include(a => a.User).First(a => a.Id == annotationId);
286
                }
287
                else
288
                {
289
                    annotation = databaseContext.FinalAnnotations.Include(a => a.User).First(a => a.Id == annotationId);
290
                }
291

    
292
            }
293
            catch (Exception)
294
            {
295
                throw new InvalidOperationException("Annotation not found");
296
            }
297

    
298
            if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
299
            {
300
                throw new UnauthorizedAccessException("User does not have access to this annotation");
301
            }
302

    
303
            AnnotationTagGeneric occurence = null;
304
            try
305
            {
306
                if (!isFinal)
307
                {
308
                    occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
309
                    .First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
310
                }
311
                else
312
                {
313
                    occurence = databaseContext.FinalAnnotationTags.Include(at => at.Annotation)
314
                    .First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
315
                }
316
            }
317
            catch (Exception)
318
            {
319
                throw new InvalidOperationException("The annotation does not have a tag with specified id");
320
            }
321

    
322
            occurence.Note = request.Note;
323
            databaseContext.SaveChanges();
324
        }
325

    
326
        private void CategoryExistsElseThrow(Guid categoryId)
327
        {
328
            if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
329
            {
330
                throw new InvalidOperationException("Category does not exist");
331
            }
332
        }
333

    
334
        private void CategoryNameUnusedElseThrow(string name)
335
        {
336
            if (databaseContext.TagCategories.Any(tc => tc.Name == name))
337
            {
338
                throw new InvalidOperationException("Category name already used");
339
            }
340
        }
341

    
342
        private void TagExistsElseThrow(Guid tagId)
343
        {
344
            if (!databaseContext.Tags.Any(t => t.Id == tagId))
345
            {
346
                throw new InvalidOperationException("Tag does not exist");
347
            }
348
        }
349

    
350
        private void TagNameUnusedElseThrow(string name)
351
        {
352
            if (databaseContext.Tags.Any(t => t.Name == name))
353
            {
354
                throw new InvalidOperationException("Tag name already used");
355
            }
356
        }
357

    
358
        private void SubTagExistsElseThrow(Guid subtagId)
359
        {
360
            if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
361
            {
362
                throw new InvalidOperationException("Subtag does not exist");
363
            }
364
        }
365

    
366
        private void SubTagNameUnusedElseThrow(string name)
367
        {
368
            if (databaseContext.SubTags.Any(st => st.Name == name))
369
            {
370
                throw new InvalidOperationException("Subtag name already used");
371
            }
372
        }
373
    }
374
}
(2-2/2)