Revize addbcd33
Přidáno uživatelem Vojtěch Bartička před téměř 3 roky(ů)
Backend/Backend/Controllers/TagController.cs | ||
---|---|---|
40 | 40 | |
41 | 41 |
return Ok(tagService.GetTagTree()); |
42 | 42 |
} |
43 | ||
44 |
[HttpPost("/categories")] |
|
45 |
[Authorize(Models.Enums.ERole.ADMINISTRATOR)] |
|
46 |
[ProducesResponseType((int)HttpStatusCode.OK)] |
|
47 |
[ProducesResponseType((int)HttpStatusCode.Forbidden)] |
|
48 |
public ActionResult CreateCategory([FromBody] CreateCategoryRequest request) |
|
49 |
{ |
|
50 |
try |
|
51 |
{ |
|
52 |
tagService.CreateCategory(request); |
|
53 |
} |
|
54 |
catch (InvalidOperationException e) |
|
55 |
{ |
|
56 |
throw new BadRequestException(e.Message); |
|
57 |
} |
|
58 | ||
59 |
return Ok(); |
|
60 |
} |
|
61 | ||
62 |
[HttpDelete("/category/{categoryId}")] |
|
63 |
[Authorize(Models.Enums.ERole.ADMINISTRATOR)] |
|
64 |
[ProducesResponseType((int)HttpStatusCode.OK)] |
|
65 |
[ProducesResponseType((int)HttpStatusCode.Forbidden)] |
|
66 |
public ActionResult DeleteCategory(Guid categoryId) |
|
67 |
{ |
|
68 |
try |
|
69 |
{ |
|
70 |
tagService.DeleteCategory(categoryId); |
|
71 |
} |
|
72 |
catch (InvalidOperationException e) |
|
73 |
{ |
|
74 |
throw new BadRequestException(e.Message); |
|
75 |
} |
|
76 | ||
77 |
return Ok(); |
|
78 |
} |
|
79 | ||
80 |
[HttpPut("/category/{categoryId}")] |
|
81 |
[Authorize(Models.Enums.ERole.ADMINISTRATOR)] |
|
82 |
[ProducesResponseType((int)HttpStatusCode.OK)] |
|
83 |
[ProducesResponseType((int)HttpStatusCode.Forbidden)] |
|
84 |
public ActionResult UpdateCategory([FromBody] ModifyCategoryRequest request, Guid categoryId) |
|
85 |
{ |
|
86 |
try |
|
87 |
{ |
|
88 |
tagService.UpdateCategory(request, categoryId); |
|
89 |
} |
|
90 |
catch (InvalidOperationException e) |
|
91 |
{ |
|
92 |
throw new BadRequestException(e.Message); |
|
93 |
} |
|
94 | ||
95 |
return Ok(); |
|
96 |
} |
|
43 | 97 |
} |
44 | 98 |
Backend/Core/Services/TagService/ITagService.cs | ||
---|---|---|
10 | 10 |
public interface ITagService |
11 | 11 |
{ |
12 | 12 |
public TagTreeResponse GetTagTree(); |
13 |
public void CreateCategory(CreateCategoryRequest request); |
|
14 |
public void DeleteCategory(Guid categoryId); |
|
15 |
public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId); |
|
16 |
//public void CreateTag(); |
|
17 |
//public void DeleteTag(); |
|
18 |
//public void UpdateTag(); |
|
19 |
//public void CreateSubTag(); |
|
20 |
//public void DeleteSubTag(); |
|
21 |
//public void UpdateSubTag(); |
|
13 | 22 |
} |
14 | 23 |
} |
Backend/Core/Services/TagService/TagServiceEF.cs | ||
---|---|---|
63 | 63 |
TagCategories = tagCategoriesDTOs |
64 | 64 |
}; |
65 | 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 |
if (databaseContext.TagCategories.Any(tc => tc.Name == request.Name)) |
|
75 |
{ |
|
76 |
throw new InvalidOperationException("Category name already used"); |
|
77 |
} |
|
78 | ||
79 |
databaseContext.TagCategories.Add(new Entities.TagCategory() |
|
80 |
{ |
|
81 |
Name = request.Name, |
|
82 |
Color = request.Color, |
|
83 |
Description = request.Description |
|
84 |
}); |
|
85 | ||
86 |
databaseContext.SaveChanges(); |
|
87 |
} |
|
88 | ||
89 |
public void DeleteCategory(Guid categoryId) |
|
90 |
{ |
|
91 |
CategoryExistsOrThrow(categoryId); |
|
92 | ||
93 |
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId); |
|
94 |
databaseContext.TagCategories.Remove(category); |
|
95 |
databaseContext.SaveChanges(); |
|
96 |
} |
|
97 | ||
98 |
public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId) |
|
99 |
{ |
|
100 |
CategoryExistsOrThrow(categoryId); |
|
101 |
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId); |
|
102 | ||
103 |
if (request.Name != null) |
|
104 |
{ |
|
105 |
if (databaseContext.TagCategories.Any(tc => tc.Name == request.Name)) |
|
106 |
{ |
|
107 |
throw new InvalidOperationException("Category name is already used"); |
|
108 |
} |
|
109 |
category.Name = request.Name; |
|
110 |
} |
|
111 | ||
112 |
if (request.Description != null) |
|
113 |
{ |
|
114 |
category.Description = request.Description; |
|
115 |
} |
|
116 | ||
117 |
if (request.Color != null) |
|
118 |
{ |
|
119 |
if (request.Color == "") |
|
120 |
{ |
|
121 |
throw new InvalidOperationException("Empty color"); |
|
122 |
} |
|
123 |
category.Color = request.Color; |
|
124 |
} |
|
125 | ||
126 |
databaseContext.SaveChanges(); |
|
127 |
} |
|
128 | ||
129 | ||
130 | ||
131 | ||
132 | ||
133 | ||
134 | ||
135 | ||
136 |
private void CategoryExistsOrThrow(Guid categoryId) |
|
137 |
{ |
|
138 |
if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId)) |
|
139 |
{ |
|
140 |
throw new InvalidOperationException("Category does not exist"); |
|
141 |
} |
|
142 |
} |
|
66 | 143 |
} |
67 | 144 |
} |
Backend/Models/Tags/CreateCategoryRequest.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 | ||
7 |
namespace Models.Tags |
|
8 |
{ |
|
9 |
public class CreateCategoryRequest |
|
10 |
{ |
|
11 |
public string Name { get; set; } |
|
12 |
public string Description { get; set; } = ""; |
|
13 |
public string Color { get; set; } |
|
14 |
} |
|
15 |
} |
Backend/Models/Tags/ModifyCategoryRequest.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 | ||
7 |
namespace Models.Tags |
|
8 |
{ |
|
9 |
public class ModifyCategoryRequest |
|
10 |
{ |
|
11 |
public string? Name { get; set; } |
|
12 |
public string? Description { get; set; } |
|
13 |
public string? Color { get; set; } |
|
14 |
} |
|
15 |
} |
Také k dispozici: Unified diff
Created endpoints for CRUD on categories