Projekt

Obecné

Profil

Stáhnout (11.7 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
            Annotation annotation = null;
278
            try
279
            {
280
                if (!isFinal)
281
                {
282
                    annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
283
                }
284
                else
285
                {
286
                    annotation = databaseContext.FinalAnnotations.Include(a => a.User).First(a => a.Id == annotationId);
287
                }
288

    
289
            }
290
            catch (Exception)
291
            {
292
                throw new InvalidOperationException("Annotation not found");
293
            }
294

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

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

    
319
            occurence.Note = request.Note;
320
            databaseContext.SaveChanges();
321
        }
322

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

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

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

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

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

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