Revize 162db5f6
Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)
Backend/Core/Services/TagService/TagServiceEF.cs | ||
---|---|---|
71 | 71 |
throw new InvalidOperationException("Category name or color empty"); |
72 | 72 |
} |
73 | 73 |
|
74 |
if (databaseContext.TagCategories.Any(tc => tc.Name == request.Name)) |
|
75 |
{ |
|
76 |
throw new InvalidOperationException("Category name already used"); |
|
77 |
} |
|
74 |
CategoryNameUnusedOrThrow(request.Name); |
|
78 | 75 |
|
79 | 76 |
databaseContext.TagCategories.Add(new Entities.TagCategory() |
80 | 77 |
{ |
... | ... | |
102 | 99 |
|
103 | 100 |
if (request.Name != null) |
104 | 101 |
{ |
105 |
if (databaseContext.TagCategories.Any(tc => tc.Name == request.Name)) |
|
106 |
{ |
|
107 |
throw new InvalidOperationException("Category name is already used"); |
|
108 |
} |
|
102 |
CategoryNameUnusedOrThrow(request.Name); |
|
109 | 103 |
category.Name = request.Name; |
110 | 104 |
} |
111 | 105 |
|
... | ... | |
126 | 120 |
databaseContext.SaveChanges(); |
127 | 121 |
} |
128 | 122 |
|
123 |
public void CreateTag(CreateTagRequest request) |
|
124 |
{ |
|
125 |
CategoryExistsOrThrow(request.CategoryId); |
|
129 | 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 |
} |
|
130 | 181 |
|
131 | 182 |
|
132 | 183 |
|
... | ... | |
140 | 191 |
throw new InvalidOperationException("Category does not exist"); |
141 | 192 |
} |
142 | 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 |
} |
|
143 | 218 |
} |
144 | 219 |
} |
Také k dispozici: Unified diff
Added endpoints for Tag CRUD