Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a6675a6d

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

Added GET /annotation for retrieving annotation state

Zobrazit rozdíly:

Backend/Backend/Controllers/AnnotationController.cs
44 44

  
45 45
            return Ok();
46 46
        }
47

  
48
        [HttpGet("/annotation")]
49
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(AnnotationInfo))]
50
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
51
        public ActionResult<AnnotationInfo> GetAnnotation([FromServices] ClientInfo clientInfo, [FromBody] AnnotationGetRequest request)
52
        {
53
            if (clientInfo.LoggedUser == null)
54
            {
55
                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
56
                return Problem();
57
            }
58

  
59
            // Take care of - non-admin user requesting not-assigned annotation
60
            // non-existent annotation
61
            try
62
            {
63
                var res = annotationService.GetAnnotation(request.AnnotationId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role);
64
                return Ok(res);
65
            }
66
            catch (ArgumentException e)
67
            {
68
                throw new BadRequestException(e.Message);
69
            }
70
            catch (UnauthorizedAccessException)
71
            {
72
                return Forbid();
73
            }
74

  
75
        }
47 76
    }
48 77
}
Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
9 9
using System.Text;
10 10
using System.Threading.Tasks;
11 11
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Models.Tags;
12 14

  
13 15
namespace Core.Services.AnnotationService
14 16
{
......
16 18
    {
17 19
        private readonly DatabaseContext context;
18 20
        private readonly ILogger logger;
21
        private readonly IMapper mapper;
19 22

  
20
        public AnnotationServiceEF(DatabaseContext context, ILogger logger)
23
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
21 24
        {
22 25
            this.context = context;
23 26
            this.logger = logger;
27
            this.mapper = mapper;
24 28
        }
25 29

  
26 30
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
......
86 90
                Annotations = infos
87 91
            };
88 92
        }
93

  
94
        public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
95
        {
96
            var a = context.Annotations.Where(a => a.Id == annotationId).Include(a => a.User).Include(a => a.Document).Include(a => a.Document.Content).ToList();
97
            if (a.Count == 0)
98
            {
99
                throw new ArgumentException($"Annotation {annotationId} does not exist");
100
            }
101
            Annotation annotation = a.ElementAt(0);
102

  
103
            if (userRole < ERole.ADMINISTRATOR)
104
            {
105
                if (annotation.User.Id != userId)
106
                {
107
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
108
                }
109
            }
110

  
111
            var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).ToList()[0];
112

  
113
            // We probably cannot use AutoMapper since we are dealing with too many different entities
114
            AnnotationInfo annotationInfo = new()
115
            {
116
                DocumentText = documentContent.Content,
117
                Note = annotation.Note,
118
                State = annotation.State,
119
                Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT
120
            };
121

  
122
            var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId).Include(at => at.Tag).Include(at => at.SubTag).Include(at => at.Tag.Category).ToList();
123
            foreach (var tag in tags)
124
            {
125
                var tagInstance = mapper.Map<TagInstanceInfo>(tag);
126
                annotationInfo.TagInstances.Add(tagInstance);
127
            }
128

  
129
            return annotationInfo;
130
        }
131

  
132
        // TODO temporary
133
        private bool IsHtml(string text)
134
        {
135
            return text.Contains("<!DOCTYPE html>");
136
        }
89 137
    }
90 138
}
Backend/Core/Services/AnnotationService/IAnnotationService.cs
1 1
using Models.Annotations;
2
using Models.Enums;
2 3
using System;
3 4
using System.Collections.Generic;
4 5
using System.Linq;
......
11 12
    {
12 13
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid userId);
13 14
        public AnnotationListResponse GetUserAnnotations(Guid userId);
15
        public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole);
14 16
    }
15 17
}

Také k dispozici: Unified diff