Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6bdb3d95

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

Endpoint /user/annotations for user's annotations

Added endpoint to UserController

Zobrazit rozdíly:

Backend/Backend/Controllers/UserController.cs
1 1
using Core.Services;
2
using Core.Services.AnnotationService;
2 3
using Microsoft.AspNetCore.Mvc;
3 4
using Models.Users;
4 5
using RestAPI.Authentication;
......
13 14
    {
14 15
        private readonly Serilog.ILogger logger;
15 16
        private readonly IUserService userService;
17
        private readonly IAnnotationService annotationService;
16 18

  
17
        public UserController(Serilog.ILogger logger, IUserService userService)
19
        public UserController(Serilog.ILogger logger, IUserService userService, IAnnotationService annotationService)
18 20
        {
19 21
            this.logger = logger;
20 22
            this.userService = userService;
23
            this.annotationService = annotationService;
21 24
        }
22 25

  
23 26
        [HttpGet("/users")]
......
42 45
                throw new InternalErrorException(e.Message);
43 46
            }
44 47

  
45
            
48

  
49
        }
50

  
51
        [HttpGet("/user/annotations")]
52
        [ProducesResponseType((int)HttpStatusCode.OK)]
53
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
54
        public ActionResult GetUserAnnotations([FromServices] ClientInfo clientInfo)
55
        {
56
            if (clientInfo.LoggedUser == null)
57
            {
58
                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
59
                return Problem();
60
            }
61

  
62
            var res = annotationService.GetUserAnnotations(clientInfo.LoggedUser.Id);
63
            return Ok(res);
46 64
        }
47 65
    }
48 66
}
Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
8 8
using System.Linq;
9 9
using System.Text;
10 10
using System.Threading.Tasks;
11
using Microsoft.EntityFrameworkCore;
11 12

  
12 13
namespace Core.Services.AnnotationService
13 14
{
......
34 35
                throw new InvalidOperationException($"{request.DocumentIdList.Count - documents.Count()} of the received documents do not exist");
35 36
            }
36 37

  
37
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList(); 
38
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
38 39
            foreach (var user in users)
39 40
            {
40 41
                var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
......
61 62

  
62 63
            context.SaveChanges();
63 64
        }
65

  
66
        public AnnotationListResponse GetUserAnnotations(Guid userId)
67
        {
68
            var annotations = context.Annotations.Where(a => a.User.Id == userId).Include(a => a.Document).ToList();
69
            var documentIds = annotations.Select(a => a.Document.Id).ToList();
70
            var documents = context.Documents.Where(d => documentIds.Contains(d.Id));
71
            var infos = new List<AnnotationListInfo>();
72

  
73
            var annotationsDocuments = annotations.Zip(documents, (a, d) => new { Annotation = a, Document = d });
74
            foreach (var ad in annotationsDocuments)
75
            {
76
                infos.Add(new AnnotationListInfo()
77
                {
78
                    AnnotationId = ad.Annotation.Id,
79
                    DocumentName = ad.Document.Name,
80
                    State = ad.Annotation.State
81
                });
82
            }
83

  
84
            return new AnnotationListResponse()
85
            {
86
                Annotations = infos
87
            };
88
        }
64 89
    }
65 90
}
Backend/Core/Services/AnnotationService/IAnnotationService.cs
10 10
    public interface IAnnotationService
11 11
    {
12 12
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid userId);
13
        public AnnotationListResponse GetUserAnnotations(Guid userId);
13 14
    }
14 15
}
Backend/Models/Annotations/AnnotationListInfo.cs
1
using Models.Enums;
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7

  
8
namespace Models.Annotations
9
{
10
    public class AnnotationListInfo
11
    {
12
        public string DocumentName { get; set; }
13
        public EState State { get; set; }
14
        public Guid AnnotationId { get; set; }
15
    }
16
}
Backend/Models/Annotations/AnnotationListResponse.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.Annotations
8
{
9
    public class AnnotationListResponse
10
    {
11
        public List<AnnotationListInfo> Annotations { get; set; } = new();
12
    }
13
}

Také k dispozici: Unified diff