-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateToDoTrigger.cs
More file actions
55 lines (53 loc) · 1.93 KB
/
CreateToDoTrigger.cs
File metadata and controls
55 lines (53 loc) · 1.93 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
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Claims;
using azure_functions.Domain;
using azure_functions.Infrastructure;
namespace azure_functions
{
public class CreateToDoTrigger
{
private readonly NoteDbContext _dbContext;
public CreateToDoTrigger(NoteDbContext dbContext)
{
_dbContext = dbContext;
}
[FunctionName("CreateToDoTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
ClaimsPrincipal identities = req.HttpContext.User;
var userName = identities.Identity?.Name ?? req.Headers["X-MS-CLIENT-PRINCIPAL-NAME"].ToString();
if (string.IsNullOrEmpty(userName))
{
return new StatusCodeResult(403);
}
string requestBody = string.Empty;
using (StreamReader streamReader = new(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
dynamic data = JsonConvert.DeserializeObject(requestBody);
var title = data?.title;
var message = data?.message;
var newNote = new Note
{
Title = title,
Message = message,
CreatedBy = userName,
};
log.LogInformation($"Insert new note. User: ${userName}");
await _dbContext.AddAsync(newNote);
await _dbContext.SaveChangesAsync();
log.LogInformation($"Created Note: ${newNote.Id}");
return new OkObjectResult(newNote);
}
}
}