Revize eff8ec56
Přidáno uživatelem Vojtěch Bartička před asi 3 roky(ů)
Backend/Backend/Controllers/AnnotationController.cs | ||
---|---|---|
1 |
using Microsoft.AspNetCore.Mvc; |
|
2 |
using Models.Annotations; |
|
3 |
using RestAPI.Authentication; |
|
4 |
using RestAPI.Controllers.Common; |
|
5 |
using RestAPI.Utils; |
|
6 |
using System.Net; |
|
7 |
using Serilog; |
|
8 |
using Core.Services.AnnotationService; |
|
9 |
using RestAPI.Exceptions; |
|
10 | ||
11 |
namespace RestAPI.Controllers |
|
12 |
{ |
|
13 |
public class AnnotationController : CommonControllerBase |
|
14 |
{ |
|
15 |
private readonly Serilog.ILogger logger; |
|
16 |
private readonly IAnnotationService annotationService; |
|
17 | ||
18 |
public AnnotationController(Serilog.ILogger logger, IAnnotationService annotationService) |
|
19 |
{ |
|
20 |
this.logger = logger; |
|
21 |
this.annotationService = annotationService; |
|
22 |
} |
|
23 | ||
24 |
[HttpPost("/annotations")] |
|
25 |
[Authorize(Models.Enums.ERole.ADMINISTRATOR)] |
|
26 |
[ProducesResponseType((int)HttpStatusCode.OK)] |
|
27 |
[ProducesResponseType((int)HttpStatusCode.Forbidden)] |
|
28 |
public ActionResult AddDocumentAnnotations([FromServices] ClientInfo clientInfo, [FromBody] AnnotationsAddRequest request) |
|
29 |
{ |
|
30 |
if (clientInfo.LoggedUser == null) |
|
31 |
{ |
|
32 |
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations"); |
|
33 |
return Problem(); |
|
34 |
} |
|
35 | ||
36 |
try |
|
37 |
{ |
|
38 |
annotationService.CreateDocumentAnnotations(request, clientInfo.LoggedUser.Id); |
|
39 |
} |
|
40 |
catch (InvalidOperationException e) |
|
41 |
{ |
|
42 |
throw new BadRequestException(e.Message); |
|
43 |
} |
|
44 | ||
45 |
return Ok(); |
|
46 |
} |
|
47 |
} |
|
48 |
} |
Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs | ||
---|---|---|
1 |
using Core.Contexts; |
|
2 |
using Core.Entities; |
|
3 |
using Models.Annotations; |
|
4 |
using Models.Enums; |
|
5 |
using Serilog; |
|
6 |
using System; |
|
7 |
using System.Collections.Generic; |
|
8 |
using System.Linq; |
|
9 |
using System.Text; |
|
10 |
using System.Threading.Tasks; |
|
11 | ||
12 |
namespace Core.Services.AnnotationService |
|
13 |
{ |
|
14 |
public class AnnotationServiceEF : IAnnotationService |
|
15 |
{ |
|
16 |
private readonly DatabaseContext context; |
|
17 |
private readonly ILogger logger; |
|
18 | ||
19 |
public AnnotationServiceEF(DatabaseContext context, ILogger logger) |
|
20 |
{ |
|
21 |
this.context = context; |
|
22 |
this.logger = logger; |
|
23 |
} |
|
24 | ||
25 |
public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId) |
|
26 |
{ |
|
27 |
User addingUser = context.Users.Single(u => u.Id == clientUserId); |
|
28 | ||
29 |
// Check the documents exist |
|
30 |
foreach (Guid docId in request.DocumentIdList) |
|
31 |
{ |
|
32 |
if (!context.Documents.Any(d => d.Id == docId)) |
|
33 |
{ |
|
34 |
logger.Information($"Received a non-existent Document ID when assigning documents to users"); |
|
35 |
throw new InvalidOperationException($"Document with ID {docId} does not exist"); |
|
36 |
} |
|
37 |
} |
|
38 | ||
39 |
foreach (Guid userId in request.UserIdList) |
|
40 |
{ |
|
41 |
var user = context.Users.Single(u => u.Id == userId); |
|
42 |
var userAnnotatedDocuments = context.Annotations.Where(a => a.User.Id == userId).Select(a => a.Document.Id); |
|
43 | ||
44 |
foreach (Guid docId in request.DocumentIdList) |
|
45 |
{ |
|
46 |
if (userAnnotatedDocuments.Contains(docId)) |
|
47 |
{ |
|
48 |
logger.Information($"User {user.Username} has already been assigned the document {docId}"); |
|
49 |
throw new InvalidOperationException($"User {user.Username} has already been assigned the document {docId}"); |
|
50 |
} |
|
51 | ||
52 |
var document = context.Documents.Single(d => d.Id == docId); |
|
53 | ||
54 |
context.Annotations.Add(new Annotation() |
|
55 |
{ |
|
56 |
User = user, |
|
57 |
UserAssigned = addingUser, |
|
58 |
DateAssigned = DateTime.Now, |
|
59 |
DateLastChanged = DateTime.Now, |
|
60 |
Document = document, |
|
61 |
State = EState.NEW, |
|
62 |
Note = "" |
|
63 |
}); |
|
64 |
} |
|
65 |
} |
|
66 | ||
67 |
context.SaveChanges(); |
|
68 |
} |
|
69 |
} |
|
70 |
} |
Backend/Core/Services/AnnotationService/IAnnotationService.cs | ||
---|---|---|
1 |
using Models.Annotations; |
|
2 |
using System; |
|
3 |
using System.Collections.Generic; |
|
4 |
using System.Linq; |
|
5 |
using System.Text; |
|
6 |
using System.Threading.Tasks; |
|
7 | ||
8 |
namespace Core.Services.AnnotationService |
|
9 |
{ |
|
10 |
public interface IAnnotationService |
|
11 |
{ |
|
12 |
public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid userId); |
|
13 |
} |
|
14 |
} |
Backend/Core/Services/Registration.cs | ||
---|---|---|
1 | 1 |
using Core.Authentication; |
2 |
using Core.Services.AnnotationService; |
|
2 | 3 |
using Core.Services.DocumentService; |
3 | 4 |
using Microsoft.AspNetCore.Builder; |
4 | 5 |
using Microsoft.Extensions.DependencyInjection; |
... | ... | |
12 | 13 |
builder.Services.AddScoped<IUserService, UserServiceEF>(); |
13 | 14 |
builder.Services.AddScoped<IDocumentService, DocumentServiceEF>(); |
14 | 15 |
builder.Services.AddScoped<IAuthService, AuthService>(); |
16 |
builder.Services.AddScoped<IAnnotationService, AnnotationServiceEF>(); |
|
15 | 17 |
|
16 | 18 |
builder.Services.AddScoped<IJwtUtils, JwtUtils>(); |
17 | 19 |
} |
Backend/Models/Annotations/AnnotationsAddRequest.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 AnnotationsAddRequest |
|
10 |
{ |
|
11 |
public List<Guid> UserIdList { get; set; } |
|
12 |
public List<Guid> DocumentIdList { get; set; } |
|
13 |
} |
|
14 |
Také k dispozici: Unified diff
Added AnnotationController and Service
Documents can now be assigned to Users