Projekt

Obecné

Profil

Stáhnout (10.4 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 && request.Name != category.Name)
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
            {
153
                Name = request.Name,
154
                Color = request.Color,
155
                Description = request.Description,
156
                Category = category
157
            });
158

    
159
            databaseContext.SaveChanges();
160
        }
161

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

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

    
175
            if (request.Name != null && request.Name != tag.Name)
176
            {
177
                TagNameUnusedElseThrow(request.Name);
178
                tag.Name = request.Name;
179
            }
180

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

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

    
195
            databaseContext.SaveChanges();
196
        }
197

    
198
        public void CreateSubTag(CreateSubTagRequest request)
199
        {
200
            if (request.Name == "")
201
            {
202
                throw new InvalidOperationException("Subtag name empty");
203
            }
204

    
205
            SubTagNameUnusedElseThrow(request.Name);
206

    
207
            TagExistsElseThrow(request.TagId);
208
            var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
209

    
210
            databaseContext.SubTags.Add(new Entities.SubTag()
211
            {
212
                Name = request.Name,
213
                Description = request.Description,
214
                Tag = tag
215
            });
216

    
217
            databaseContext.SaveChanges();
218
        }
219

    
220
        public void DeleteSubTag(Guid subtagId)
221
        {
222
            SubTagExistsElseThrow(subtagId);
223
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
224
            databaseContext.SubTags.Remove(subtag);
225
            databaseContext.SaveChanges();
226
        }
227

    
228
        public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
229
        {
230
            SubTagExistsElseThrow(subtagId);
231
            var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
232

    
233
            if (request.Name != null && request.Name != subtag.Name)
234
            {
235
                SubTagNameUnusedElseThrow(request.Name);
236
                subtag.Name = request.Name;
237
            }
238

    
239
            if (request.Description != null)
240
            {
241
                subtag.Description = request.Description;
242
            }
243

    
244
            databaseContext.SaveChanges();
245
        }
246

    
247
        public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
248
        {
249
            Annotation annotation = null;
250
            try
251
            {
252
                annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
253
            }
254
            catch (Exception)
255
            {
256
                throw new InvalidOperationException("Annotation not found");
257
            }
258

    
259
            if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
260
            {
261
                throw new UnauthorizedAccessException("User does not have access to this annotation");
262
            }
263

    
264
            AnnotationTag occurence = null;
265
            try
266
            {
267
                occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
268
                    .First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
269
            }
270
            catch (Exception)
271
            {
272
                throw new InvalidOperationException("The annotation does not have a tag with specified id");
273
            }
274

    
275
            occurence.Note = request.Note;
276
            databaseContext.SaveChanges();
277
        }
278

    
279
        private void CategoryExistsElseThrow(Guid categoryId)
280
        {
281
            if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
282
            {
283
                throw new InvalidOperationException("Category does not exist");
284
            }
285
        }
286

    
287
        private void CategoryNameUnusedElseThrow(string name)
288
        {
289
            if (databaseContext.TagCategories.Any(tc => tc.Name == name))
290
            {
291
                throw new InvalidOperationException("Category name already used");
292
            }
293
        }
294

    
295
        private void TagExistsElseThrow(Guid tagId)
296
        {
297
            if (!databaseContext.Tags.Any(t => t.Id == tagId))
298
            {
299
                throw new InvalidOperationException("Tag does not exist");
300
            }
301
        }
302

    
303
        private void TagNameUnusedElseThrow(string name)
304
        {
305
            if (databaseContext.Tags.Any(t => t.Name == name))
306
            {
307
                throw new InvalidOperationException("Tag name already used");
308
            }
309
        }
310

    
311
        private void SubTagExistsElseThrow(Guid subtagId)
312
        {
313
            if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
314
            {
315
                throw new InvalidOperationException("Subtag does not exist");
316
            }
317
        }
318

    
319
        private void SubTagNameUnusedElseThrow(string name)
320
        {
321
            if (databaseContext.SubTags.Any(st => st.Name == name))
322
            {
323
                throw new InvalidOperationException("Subtag name already used");
324
            }
325
        }
326
    }
327
}
(2-2/2)