Projekt

Obecné

Profil

Stáhnout (10.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.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
                if (tagCategory.DisabledForAnnotators && userRole == ERole.ANNOTATOR)
41
                {
42
                    continue;
43
                }
44

    
45
                var tagCategoryDTO = mapper.Map<TagCategoryInfo>(tagCategory);
46
                var categoryTags = tags.Where(t => t.Category == tagCategory);
47
                var tagDTOs = new List<TagInfo>();
48

    
49
                foreach (var tag in categoryTags)
50
                {
51
                    var tagDTO = mapper.Map<TagInfo>(tag);
52
                    var tagSubTags = subTags.Where(st => st.Tag == tag).ToList();
53
                    var subTagDTOs = new List<SubTagInfo>();
54

    
55
                    if (tagSubTags.Count() != 0)
56
                    {
57
                        foreach (var subTag in tagSubTags)
58
                        {
59
                            subTagDTOs.Add(mapper.Map<SubTagInfo>(subTag));
60
                        }
61
                    }
62
                    tagDTO.SubTags = subTagDTOs;
63
                    tagDTOs.Add(tagDTO);
64
                }
65

    
66
                tagCategoryDTO.Tags = tagDTOs;
67
                tagCategoriesDTOs.Add(tagCategoryDTO);
68
            }
69

    
70
            return new TagTreeResponse()
71
            {
72
                TagCategories = tagCategoriesDTOs
73
            };
74
        }
75

    
76
        public void CreateCategory(CreateCategoryRequest request)
77
        {
78
            if (request.Color == "" || request.Name == "")
79
            {
80
                throw new InvalidOperationException("Category name or color empty");
81
            }
82

    
83
            CategoryNameUnusedElseThrow(request.Name);
84

    
85
            databaseContext.TagCategories.Add(new TagCategory()
86
            {
87
                Name = request.Name,
88
                Color = request.Color,
89
                Description = request.Description,
90
                DisabledForAnnotators = request.DisabledForAnnotators
91
            });
92

    
93
            databaseContext.SaveChanges();
94
        }
95

    
96
        public void DeleteCategory(Guid categoryId)
97
        {
98
            CategoryExistsElseThrow(categoryId);
99

    
100
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
101
            databaseContext.TagCategories.Remove(category);
102
            databaseContext.SaveChanges();
103
        }
104

    
105
        public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId)
106
        {
107
            CategoryExistsElseThrow(categoryId);
108
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
109

    
110
            if (request.Name != null)
111
            {
112
                CategoryNameUnusedElseThrow(request.Name);
113
                category.Name = request.Name;
114
            }
115

    
116
            if (request.Description != null)
117
            {
118
                category.Description = request.Description;
119
            }
120

    
121
            if (request.Color != null)
122
            {
123
                if (request.Color == "")
124
                {
125
                    throw new InvalidOperationException("Empty color");
126
                }
127
                category.Color = request.Color;
128
            }
129

    
130
            if (request.DisabledForAnnotators != null)
131
            {
132
                category.DisabledForAnnotators = (bool)request.DisabledForAnnotators;
133
            }
134

    
135
            databaseContext.SaveChanges();
136
        }
137

    
138
        public void CreateTag(CreateTagRequest request)
139
        {
140
            CategoryExistsElseThrow(request.CategoryId);
141

    
142
            TagNameUnusedElseThrow(request.Name);
143

    
144
            if (request.Color == "")
145
            {
146
                throw new InvalidOperationException("Color empty");
147
            }
148

    
149
            var category = databaseContext.TagCategories.First(tc => tc.Id == request.CategoryId);
150

    
151
            databaseContext.Tags.Add(new Entities.Tag()
152
            databaseContext.Tags.Add(new Tag()
153
            {
154
                Name = request.Name,
155
                Color = request.Color,
156
                Description = request.Description,
157
                Category = category,
158
                SentimentEnabled = request.SentimentEnabled
159
            });
160

    
161
            databaseContext.SaveChanges();
162
        }
163

    
164
        public void DeleteTag(Guid tagId)
165
        {
166
            TagExistsElseThrow(tagId);
167
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
168
            databaseContext.Tags.Remove(tag);
169
            databaseContext.SaveChanges();
170
        }
171

    
172
        public void UpdateTag(ModifyTagRequest request, Guid tagId)
173
        {
174
            TagExistsElseThrow(tagId);
175
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
176

    
177
            if (request.Name != null)
178
            {
179
                TagNameUnusedElseThrow(request.Name);
180
                tag.Name = request.Name;
181
            }
182

    
183
            if (request.Color != null)
184
            {
185
                if (request.Color == "")
186
                {
187
                    throw new InvalidOperationException("Color empty");
188
                }
189
                tag.Color = request.Color;
190
            }
191

    
192
            if (request.Description != null)
193
            {
194
                tag.Description = request.Description;
195
            }
196

    
197
            if (request.SentimentEnabled != null)
198
            {
199
                tag.SentimentEnabled = (bool)request.SentimentEnabled;
200
            }
201

    
202
            databaseContext.SaveChanges();
203
        }
204

    
205
        public void CreateSubTag(CreateSubTagRequest request)
206
        {
207
            if (request.Name == "")
208
            {
209
                throw new InvalidOperationException("Subtag name empty");
210
            }
211

    
212
            SubTagNameUnusedElseThrow(request.Name);
213

    
214
            TagExistsElseThrow(request.TagId);
215
            var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
216

    
217
            databaseContext.SubTags.Add(new Entities.SubTag()
218
            {
219
                Name = request.Name,
220
                Description = request.Description,
221
                Tag = tag,
222
                SentimentEnabled = request.SentimentEnabled
223
            });
224

    
225
            databaseContext.SaveChanges();
226
        }
227

    
228
        public void DeleteSubTag(Guid subtagId)
229
        {
230
            SubTagExistsElseThrow(subtagId);
231
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
232
            databaseContext.SubTags.Remove(subtag);
233
            databaseContext.SaveChanges();
234
        }
235

    
236
        public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
237
        {
238
            SubTagExistsElseThrow(subtagId);
239
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
240

    
241
            if (request.Name != null)
242
            {
243
                SubTagNameUnusedElseThrow(request.Name);
244
                subtag.Name = request.Name;
245
            }
246

    
247
            if (request.Description != null)
248
            {
249
                subtag.Description = request.Description;
250
            }
251

    
252
            if (request.SentimentEnabled != null)
253
            {
254
                subtag.SentimentEnabled = (bool) request.SentimentEnabled;
255
            }
256

    
257
            databaseContext.SaveChanges();
258
        }
259

    
260
        public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
261
        {
262
            Annotation annotation = null;
263
            try
264
            {
265
                annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
266
            }
267
            catch (Exception)
268
            {
269
                throw new InvalidOperationException("Annotation not found");
270
            }
271

    
272
            if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
273
            {
274
                throw new UnauthorizedAccessException("User does not have access to this annotation");
275
            }
276

    
277
            AnnotationTag occurence = null;
278
            try
279
            {
280
                occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
281
                    .First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
282
            }
283
            catch (Exception)
284
            {
285
                throw new InvalidOperationException("The annotation does not have a tag with specified id");
286
            }
287

    
288
            occurence.Note = request.Note;
289
            databaseContext.SaveChanges();
290
        }
291

    
292
        private void CategoryExistsElseThrow(Guid categoryId)
293
        {
294
            if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
295
            {
296
                throw new InvalidOperationException("Category does not exist");
297
            }
298
        }
299

    
300
        private void CategoryNameUnusedElseThrow(string name)
301
        {
302
            if (databaseContext.TagCategories.Any(tc => tc.Name == name))
303
            {
304
                throw new InvalidOperationException("Category name already used");
305
            }
306
        }
307

    
308
        private void TagExistsElseThrow(Guid tagId)
309
        {
310
            if (!databaseContext.Tags.Any(t => t.Id == tagId))
311
            {
312
                throw new InvalidOperationException("Tag does not exist");
313
            }
314
        }
315

    
316
        private void TagNameUnusedElseThrow(string name)
317
        {
318
            if (databaseContext.Tags.Any(t => t.Name == name))
319
            {
320
                throw new InvalidOperationException("Tag name already used");
321
            }
322
        }
323

    
324
        private void SubTagExistsElseThrow(Guid subtagId)
325
        {
326
            if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
327
            {
328
                throw new InvalidOperationException("Subtag does not exist");
329
            }
330
        }
331

    
332
        private void SubTagNameUnusedElseThrow(string name)
333
        {
334
            if (databaseContext.SubTags.Any(st => st.Name == name))
335
            {
336
                throw new InvalidOperationException("Subtag name already used");
337
            }
338
        }
339
    }
340
}
(2-2/2)