Projekt

Obecné

Profil

Stáhnout (6.57 KB) Statistiky
| Větev: | Tag: | Revize:
1
using AutoMapper;
2
using Core.Contexts;
3
using Models.Tags;
4
using Serilog;
5
using System;
6
using System.Collections.Generic;
7
using System.Linq;
8
using System.Text;
9
using System.Threading.Tasks;
10

    
11
namespace Core.Services.TagService
12
{
13
    public class TagServiceEF : ITagService
14
    {
15
        private readonly DatabaseContext databaseContext;
16
        private readonly ILogger logger;
17
        private readonly IMapper mapper;
18

    
19
        public TagServiceEF(DatabaseContext databaseContext, ILogger logger, IMapper mapper)
20
        {
21
            this.databaseContext = databaseContext;
22
            this.logger = logger;
23
            this.mapper = mapper;
24
        }
25

    
26
        public TagTreeResponse GetTagTree()
27
        {
28
            // Prefetch from DB
29
            var tagCategories = databaseContext.TagCategories.Select(tc => tc).ToList();
30
            var tags = databaseContext.Tags.Select(tc => tc).ToList();
31
            var subTags = databaseContext.SubTags.Select(tc => tc).ToList();
32

    
33
            var tagCategoriesDTOs = new List<TagCategoryInfo>();
34
            foreach (var tagCategory in tagCategories)
35
            {
36
                var tagCategoryDTO = mapper.Map<TagCategoryInfo>(tagCategory);
37
                var categoryTags = tags.Where(t => t.Category == tagCategory);
38
                var tagDTOs = new List<TagInfo>();
39

    
40
                foreach (var tag in categoryTags)
41
                {
42
                    var tagDTO = mapper.Map<TagInfo>(tag);
43
                    var tagSubTags = subTags.Where(st => st.Tag == tag).ToList();
44
                    var subTagDTOs = new List<SubTagInfo>();
45

    
46
                    if (tagSubTags.Count() != 0)
47
                    {
48
                        foreach (var subTag in tagSubTags)
49
                        {
50
                            subTagDTOs.Add(mapper.Map<SubTagInfo>(subTag));
51
                        }
52
                    }
53
                    tagDTO.SubTags = subTagDTOs;
54
                    tagDTOs.Add(tagDTO);
55
                }
56

    
57
                tagCategoryDTO.Tags = tagDTOs;
58
                tagCategoriesDTOs.Add(tagCategoryDTO);
59
            }
60

    
61
            return new TagTreeResponse()
62
            {
63
                TagCategories = tagCategoriesDTOs
64
            };
65
        }
66

    
67
        public void CreateCategory(CreateCategoryRequest request)
68
        {
69
            if (request.Color == "" || request.Name == "")
70
            {
71
                throw new InvalidOperationException("Category name or color empty");
72
            }
73

    
74
            CategoryNameUnusedOrThrow(request.Name);
75

    
76
            databaseContext.TagCategories.Add(new Entities.TagCategory()
77
            {
78
                Name = request.Name,
79
                Color = request.Color,
80
                Description = request.Description
81
            });
82

    
83
            databaseContext.SaveChanges();
84
        }
85

    
86
        public void DeleteCategory(Guid categoryId)
87
        {
88
            CategoryExistsOrThrow(categoryId);
89

    
90
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
91
            databaseContext.TagCategories.Remove(category);
92
            databaseContext.SaveChanges();
93
        }
94

    
95
        public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId)
96
        {
97
            CategoryExistsOrThrow(categoryId);
98
            var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
99

    
100
            if (request.Name != null)
101
            {
102
                CategoryNameUnusedOrThrow(request.Name);
103
                category.Name = request.Name;
104
            }
105

    
106
            if (request.Description != null)
107
            {
108
                category.Description = request.Description;
109
            }
110

    
111
            if (request.Color != null)
112
            {
113
                if (request.Color == "")
114
                {
115
                    throw new InvalidOperationException("Empty color");
116
                }
117
                category.Color = request.Color;
118
            }
119

    
120
            databaseContext.SaveChanges();
121
        }
122

    
123
        public void CreateTag(CreateTagRequest request)
124
        {
125
            CategoryExistsOrThrow(request.CategoryId);
126

    
127
            TagNameUnusedOrThrow(request.Name);
128

    
129
            if (request.Color == "")
130
            {
131
                throw new InvalidOperationException("Color empty");
132
            }
133

    
134
            var category = databaseContext.TagCategories.First(tc => tc.Id == request.CategoryId);
135

    
136
            databaseContext.Tags.Add(new Entities.Tag()
137
            {
138
                Name = request.Name,
139
                Color = request.Color,
140
                Description = request.Description,
141
                Category = category
142
            });
143

    
144
            databaseContext.SaveChanges();
145
        }
146

    
147
        public void DeleteTag(Guid tagId)
148
        {
149
            TagExistsOrThrow(tagId);
150
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
151
            databaseContext.Tags.Remove(tag);
152
        }
153

    
154
        public void UpdateTag(ModifyTagRequest request, Guid tagId)
155
        {
156
            TagExistsOrThrow(tagId);
157
            var tag = databaseContext.Tags.First(t => t.Id == tagId);
158

    
159
            if (request.Name != null)
160
            {
161
                TagNameUnusedOrThrow(request.Name);
162
                tag.Name = request.Name;
163
            }
164

    
165
            if (request.Color != null)
166
            {
167
                if (request.Color == "")
168
                {
169
                    throw new InvalidOperationException("Color empty");
170
                }
171
                tag.Color = request.Color;
172
            }
173

    
174
            if (request.Description != null)
175
            {
176
                tag.Description = request.Description;
177
            }
178

    
179
            databaseContext.SaveChanges();
180
        }
181

    
182

    
183

    
184

    
185

    
186

    
187
        private void CategoryExistsOrThrow(Guid categoryId)
188
        {
189
            if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
190
            {
191
                throw new InvalidOperationException("Category does not exist");
192
            }
193
        }
194

    
195
        private void CategoryNameUnusedOrThrow(string name)
196
        {
197
            if (databaseContext.TagCategories.Any(tc => tc.Name == name))
198
            {
199
                throw new InvalidOperationException("Category name already used");
200
            }
201
        }
202

    
203
        private void TagExistsOrThrow(Guid tagId)
204
        {
205
            if (!databaseContext.Tags.Any(t => t.Id == tagId))
206
            {
207
                throw new InvalidOperationException("Tag does not exist");
208
            }
209
        }
210

    
211
        private void TagNameUnusedOrThrow(string name)
212
        {
213
            if (databaseContext.Tags.Any(t => t.Name == name))
214
            {
215
                throw new InvalidOperationException("Tag name already used");
216
            }
217
        }
218
    }
219
}
(2-2/2)