Projekt

Obecné

Profil

Stáhnout (10.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 Entities.Tag()
168
            {
169
                Name = request.Name,
170
                Color = request.Color,
171
                Description = request.Description,
172
                Category = category
173
            });
174

    
175
            databaseContext.SaveChanges();
176
        }
177

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

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

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

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

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

    
211
            databaseContext.SaveChanges();
212
        }
213

    
214
        public void CreateSubTag(CreateSubTagRequest request)
215
        {
216
            if (request.Name == "")
217
            {
218
                throw new InvalidOperationException("Subtag name empty");
219
            }
220

    
221
            SubTagNameUnusedElseThrow(request.Name);
222

    
223
            TagExistsElseThrow(request.TagId);
224
            var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
225

    
226
            databaseContext.SubTags.Add(new Entities.SubTag()
227
            {
228
                Name = request.Name,
229
                Description = request.Description,
230
                Tag = tag
231
            });
232

    
233
            databaseContext.SaveChanges();
234
        }
235

    
236
        public void DeleteSubTag(Guid subtagId)
237
        {
238
            SubTagExistsElseThrow(subtagId);
239
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
240
            databaseContext.SubTags.Remove(subtag);
241
            databaseContext.SaveChanges();
242
        }
243

    
244
        public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
245
        {
246
            SubTagExistsElseThrow(subtagId);
247
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
248

    
249
            if (request.Name != null && request.Name != subtag.Name)
250
            {
251
                SubTagNameUnusedElseThrow(request.Name);
252
                subtag.Name = request.Name;
253
            }
254

    
255
            if (request.Description != null)
256
            {
257
                subtag.Description = request.Description;
258
            }
259

    
260
            databaseContext.SaveChanges();
261
        }
262

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

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

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

    
291
            occurence.Note = request.Note;
292
            databaseContext.SaveChanges();
293
        }
294

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

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

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

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

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

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