Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9190add5

Přidáno uživatelem Vojtěch Bartička před téměř 3 roky(ů)

Added endpoint for adding notes to tags in AnnotationController

Zobrazit rozdíly:

Backend/Backend/Controllers/AnnotationController.cs
7 7
using Serilog;
8 8
using Core.Services.AnnotationService;
9 9
using RestAPI.Exceptions;
10
using Models.Tags;
11
using Core.Services.TagService;
12

  
10 13

  
11 14
namespace RestAPI.Controllers
12 15
{
......
14 17
    {
15 18
        private readonly Serilog.ILogger logger;
16 19
        private readonly IAnnotationService annotationService;
20
        private readonly ITagService tagService;
17 21

  
18
        public AnnotationController(Serilog.ILogger logger, IAnnotationService annotationService)
22
        public AnnotationController(Serilog.ILogger logger, IAnnotationService annotationService, ITagService tagService)
19 23
        {
20 24
            this.logger = logger;
21 25
            this.annotationService = annotationService;
26
            this.tagService = tagService;
22 27
        }
23 28

  
24 29
        [HttpPost("/annotations")]
......
131 136
            }
132 137

  
133 138
        }
139

  
140
        [HttpPost("/annotation/{annotationId}/tag/{occurenceId}/note")]
141
        [ProducesResponseType((int)HttpStatusCode.OK)]
142
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
143
        public ActionResult AddNoteToTagOccurence([FromServices] ClientInfo clientInfo, Guid annotationId, Guid occurenceId,
144
            [FromBody] AddNoteToTagOccurenceRequest request)
145
        {
146
            if (clientInfo.LoggedUser == null)
147
            {
148
                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
149
                return Problem();
150
            }
151

  
152
            // Take care of - non-admin user requesting not-assigned annotation
153
            // non-existent annotation
154
            try
155
            {
156
                tagService.AddAnnotationInstance(annotationId, occurenceId, clientInfo.LoggedUser, request);
157
                return Ok();
158
            }
159
            catch (InvalidOperationException e)
160
            {
161
                throw new BadRequestException("Could not find specified annotation");
162
            }
163
            catch (UnauthorizedAccessException)
164
            {
165
                return Forbid();
166
            }
167

  
168
        }
134 169
    }
135 170
}
Backend/Core/Services/TagService/ITagService.cs
1
using Models.Tags;
1
using Core.Entities;
2
using Models.Enums;
3
using Models.Tags;
4
using Models.Users;
2 5
using System;
3 6
using System.Collections.Generic;
4 7
using System.Linq;
......
19 22
        public void CreateSubTag(CreateSubTagRequest request);
20 23
        public void DeleteSubTag(Guid subtagId);
21 24
        public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId);
25
        public void AddAnnotationInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request);
22 26
    }
23 27
}
Backend/Core/Services/TagService/TagServiceEF.cs
1 1
using AutoMapper;
2 2
using Core.Contexts;
3
using Core.Entities;
4
using Models.Enums;
3 5
using Models.Tags;
6
using Models.Users;
4 7
using Serilog;
5 8
using System;
6 9
using System.Collections.Generic;
7
using System.Linq;
8 10
using System.Text;
9 11
using System.Threading.Tasks;
12
using Microsoft.EntityFrameworkCore;
13
using System.Linq;
10 14

  
11 15
namespace Core.Services.TagService
12 16
{
......
188 192
            }
189 193

  
190 194
            SubTagNameUnusedElseThrow(request.Name);
191
            
195

  
192 196
            TagExistsElseThrow(request.TagId);
193 197
            var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
194 198

  
......
219 223
            {
220 224
                SubTagNameUnusedElseThrow(request.Name);
221 225
                subtag.Name = request.Name;
222
            }     
226
            }
223 227

  
224 228
            if (request.Description != null)
225 229
            {
......
229 233
            databaseContext.SaveChanges();
230 234
        }
231 235

  
236
        public void AddAnnotationInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
237
        {
238
            Annotation annotation = null;
239
            try
240
            {
241
                annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
242
            }
243
            catch (Exception)
244
            {
245
                throw new InvalidOperationException("Annotation not found");
246
            }
232 247

  
248
            if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
249
            {
250
                throw new UnauthorizedAccessException("User does not have access to this annotation");
251
            }
233 252

  
253
            AnnotationTag occurence = null;
254
            try
255
            {
256
                occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
257
                    .First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
258
            }
259
            catch (Exception)
260
            {
261
                throw new InvalidOperationException("The annotation does not have a tag with specified id");
262
            }
234 263

  
264
            occurence.Note = request.Note;
265
            databaseContext.SaveChanges();
266
        }
235 267

  
236 268
        private void CategoryExistsElseThrow(Guid categoryId)
237 269
        {
Backend/Models/Tags/AddNoteToTagOccurenceRequest.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 AddNoteToTagOccurenceRequest
10
    {
11
        public string Note { get; set; }
12
    }
13
}

Také k dispozici: Unified diff