-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLearningPath.cs
More file actions
57 lines (49 loc) · 1.94 KB
/
Copy pathGetLearningPath.cs
File metadata and controls
57 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using HoTeach.Infrastructure.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using HoTeach.Entities;
using HoTeach.Requests;
using Newtonsoft.Json;
namespace HoTeach
{
public class GetLearningPath
{
private readonly ILogger<GetLearningPath> _logger;
private readonly IRepository<HoTeach.Entities.Path> pathRepository;
public GetLearningPath(ILogger<GetLearningPath> logger, IRepository<HoTeach.Entities.Path> pathRepository)
{
_logger = logger;
this.pathRepository = pathRepository;
}
[Function("GetLearningPath")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req)
{
try
{
var principal = AuthHelper.ValidateToken(req);
if (!AuthHelper.HasScope(principal, "hoteach:default"))
{
return new UnauthorizedResult();
}
_logger.LogInformation($"Authenticated user: {principal.Identity.Name}");
}
catch (Exception ex)
{
_logger.LogWarning(ex.Message);
return new UnauthorizedResult();
}
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if (string.IsNullOrEmpty(requestBody))
{
return new BadRequestObjectResult("Request body is empty.");
}
var data = JsonConvert.DeserializeObject<GetPathRequest>(requestBody);
if (data == null || string.IsNullOrEmpty(data.UserId))
return new BadRequestObjectResult("UserId is required.");
var path = await pathRepository.GetFirstOrDefaultAsync(x => x.UserId == data.UserId) ?? null;
return new OkObjectResult(path );
}
}
}