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
|
CategoryNameUnusedElseThrow(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
|
CategoryExistsElseThrow(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
|
CategoryExistsElseThrow(categoryId);
|
98
|
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
|
99
|
|
100
|
if (request.Name != null)
|
101
|
{
|
102
|
CategoryNameUnusedElseThrow(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
|
CategoryExistsElseThrow(request.CategoryId);
|
126
|
|
127
|
TagNameUnusedElseThrow(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
|
TagExistsElseThrow(tagId);
|
150
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
151
|
databaseContext.Tags.Remove(tag);
|
152
|
databaseContext.SaveChanges();
|
153
|
}
|
154
|
|
155
|
public void UpdateTag(ModifyTagRequest request, Guid tagId)
|
156
|
{
|
157
|
TagExistsElseThrow(tagId);
|
158
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
159
|
|
160
|
if (request.Name != null)
|
161
|
{
|
162
|
TagNameUnusedElseThrow(request.Name);
|
163
|
tag.Name = request.Name;
|
164
|
}
|
165
|
|
166
|
if (request.Color != null)
|
167
|
{
|
168
|
if (request.Color == "")
|
169
|
{
|
170
|
throw new InvalidOperationException("Color empty");
|
171
|
}
|
172
|
tag.Color = request.Color;
|
173
|
}
|
174
|
|
175
|
if (request.Description != null)
|
176
|
{
|
177
|
tag.Description = request.Description;
|
178
|
}
|
179
|
|
180
|
databaseContext.SaveChanges();
|
181
|
}
|
182
|
|
183
|
public void CreateSubTag(CreateSubTagRequest request)
|
184
|
{
|
185
|
if (request.Name == "")
|
186
|
{
|
187
|
throw new InvalidOperationException("Subtag name empty");
|
188
|
}
|
189
|
|
190
|
SubTagNameUnusedElseThrow(request.Name);
|
191
|
|
192
|
TagExistsElseThrow(request.TagId);
|
193
|
var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
|
194
|
|
195
|
databaseContext.SubTags.Add(new Entities.SubTag()
|
196
|
{
|
197
|
Name = request.Name,
|
198
|
Description = request.Description,
|
199
|
Tag = tag
|
200
|
});
|
201
|
|
202
|
databaseContext.SaveChanges();
|
203
|
}
|
204
|
|
205
|
public void DeleteSubTag(Guid subtagId)
|
206
|
{
|
207
|
SubTagExistsElseThrow(subtagId);
|
208
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
209
|
databaseContext.SubTags.Remove(subtag);
|
210
|
databaseContext.SaveChanges();
|
211
|
}
|
212
|
|
213
|
public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
|
214
|
{
|
215
|
SubTagExistsElseThrow(subtagId);
|
216
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
217
|
|
218
|
if (request.Name != null)
|
219
|
{
|
220
|
SubTagNameUnusedElseThrow(request.Name);
|
221
|
subtag.Name = request.Name;
|
222
|
}
|
223
|
|
224
|
if (request.Description != null)
|
225
|
{
|
226
|
subtag.Description = request.Description;
|
227
|
}
|
228
|
|
229
|
databaseContext.SaveChanges();
|
230
|
}
|
231
|
|
232
|
|
233
|
|
234
|
|
235
|
|
236
|
private void CategoryExistsElseThrow(Guid categoryId)
|
237
|
{
|
238
|
if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
|
239
|
{
|
240
|
throw new InvalidOperationException("Category does not exist");
|
241
|
}
|
242
|
}
|
243
|
|
244
|
private void CategoryNameUnusedElseThrow(string name)
|
245
|
{
|
246
|
if (databaseContext.TagCategories.Any(tc => tc.Name == name))
|
247
|
{
|
248
|
throw new InvalidOperationException("Category name already used");
|
249
|
}
|
250
|
}
|
251
|
|
252
|
private void TagExistsElseThrow(Guid tagId)
|
253
|
{
|
254
|
if (!databaseContext.Tags.Any(t => t.Id == tagId))
|
255
|
{
|
256
|
throw new InvalidOperationException("Tag does not exist");
|
257
|
}
|
258
|
}
|
259
|
|
260
|
private void TagNameUnusedElseThrow(string name)
|
261
|
{
|
262
|
if (databaseContext.Tags.Any(t => t.Name == name))
|
263
|
{
|
264
|
throw new InvalidOperationException("Tag name already used");
|
265
|
}
|
266
|
}
|
267
|
|
268
|
private void SubTagExistsElseThrow(Guid subtagId)
|
269
|
{
|
270
|
if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
|
271
|
{
|
272
|
throw new InvalidOperationException("Subtag does not exist");
|
273
|
}
|
274
|
}
|
275
|
|
276
|
private void SubTagNameUnusedElseThrow(string name)
|
277
|
{
|
278
|
if (databaseContext.SubTags.Any(st => st.Name == name))
|
279
|
{
|
280
|
throw new InvalidOperationException("Subtag name already used");
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|