Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 162db5f6

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

Added endpoints for Tag CRUD

Zobrazit rozdíly:

Backend/Backend/Controllers/TagController.cs
94 94

  
95 95
        return Ok();
96 96
    }
97

  
98
    [HttpPost("/tags")]
99
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
100
    [ProducesResponseType((int)HttpStatusCode.OK)]
101
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
102
    public ActionResult CreateTag([FromBody] CreateTagRequest request)
103
    {
104
        try
105
        {
106
            tagService.CreateTag(request);
107
        }
108
        catch (InvalidOperationException e)
109
        {
110
            throw new BadRequestException(e.Message);
111
        }
112

  
113
        return Ok();
114
    }
115

  
116
    [HttpDelete("/tag/{tagId}")]
117
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
118
    [ProducesResponseType((int)HttpStatusCode.OK)]
119
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
120
    public ActionResult DeleteTag(Guid tagId)
121
    {
122
        try
123
        {
124
            tagService.DeleteTag(tagId);
125
        }
126
        catch (InvalidOperationException e)
127
        {
128
            throw new BadRequestException(e.Message);
129
        }
130

  
131
        return Ok();
132
    }
133

  
134
    [HttpPut("/tag/{tagId}")]
135
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
136
    [ProducesResponseType((int)HttpStatusCode.OK)]
137
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
138
    public ActionResult UpdateTag([FromBody] ModifyTagRequest request, Guid tagId)
139
    {
140
        try
141
        {
142
            tagService.UpdateTag(request, tagId);
143
        }
144
        catch (InvalidOperationException e)
145
        {
146
            throw new BadRequestException(e.Message);
147
        }
148

  
149
        return Ok();
150
    }
97 151
}
98 152

  
Backend/Core/Services/TagService/ITagService.cs
13 13
        public void CreateCategory(CreateCategoryRequest request);
14 14
        public void DeleteCategory(Guid categoryId);
15 15
        public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId);
16
        //public void CreateTag();
17
        //public void DeleteTag();
18
        //public void UpdateTag();
16
        public void CreateTag(CreateTagRequest request);
17
        public void DeleteTag(Guid tagId);
18
        public void UpdateTag(ModifyTagRequest request, Guid tagId);
19 19
        //public void CreateSubTag();
20 20
        //public void DeleteSubTag();
21 21
        //public void UpdateSubTag();
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
}
Backend/Models/Tags/CreateTagRequest.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 CreateTagRequest
10
    {
11
        public Guid CategoryId { get; set; }
12
        public string Name { get; set; }
13
        public string Description { get; set; } = "";
14
        public string Color { get; set; }
15
    }
16
}
Backend/Models/Tags/ModifyTagRequest.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 ModifyTagRequest
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