| | 1 | | using System; |
| | 2 | | using System.Net; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using ApplicationCore; |
| | 5 | | using Microsoft.AspNetCore.Http; |
| | 6 | | using Microsoft.AspNetCore.Mvc; |
| | 7 | | using Microsoft.Azure.WebJobs; |
| | 8 | | using Microsoft.Azure.WebJobs.Extensions.Http; |
| | 9 | | using Microsoft.Extensions.Logging; |
| | 10 | |
|
| | 11 | | namespace JekyllBlogCommentsAzure |
| | 12 | | { |
| | 13 | | public class PostCommentToPullRequestFunction |
| | 14 | | { |
| | 15 | | private readonly IPostCommentService _postCommentService; |
| | 16 | |
|
| 8 | 17 | | public PostCommentToPullRequestFunction(IPostCommentService postCommentService) |
| 8 | 18 | | { |
| 8 | 19 | | _postCommentService = postCommentService; |
| 8 | 20 | | } |
| | 21 | |
|
| | 22 | | [FunctionName("PostComment")] |
| | 23 | | public async Task<IActionResult> Run( |
| | 24 | | [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest request, |
| | 25 | | ILogger logger) |
| 8 | 26 | | { |
| 8 | 27 | | if (request is null) |
| 1 | 28 | | { |
| 1 | 29 | | logger.LogError(CommentResources.ParameterCannotBeNull, nameof(request)); |
| 1 | 30 | | return new BadRequestResult(); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | try |
| 7 | 34 | | { |
| 7 | 35 | | var form = await request.ReadFormAsync().ConfigureAwait(false); |
| 7 | 36 | | var result = await _postCommentService.PostCommentAsync(form).ConfigureAwait(false); |
| | 37 | |
|
| 6 | 38 | | switch (result.HttpStatusCode) |
| | 39 | | { |
| | 40 | | case HttpStatusCode.OK: |
| 1 | 41 | | return new OkResult(); |
| | 42 | | case HttpStatusCode.Redirect: |
| 1 | 43 | | return new RedirectResult(result.RedirectUrl?.AbsoluteUri); |
| | 44 | | default: |
| 4 | 45 | | logger.LogError(result.Exception, result.Error); |
| 4 | 46 | | return new BadRequestResult(); |
| | 47 | | } |
| | 48 | | } |
| 1 | 49 | | catch (Exception e) |
| 1 | 50 | | { |
| 1 | 51 | | logger.LogError(e, CommentResources.PostCommentExceptionMessage, e.Message); |
| 1 | 52 | | throw; |
| | 53 | | } |
| 7 | 54 | | } |
| | 55 | | } |
| | 56 | | } |