Skip to content

Instantly share code, notes, and snippets.

@screamingworld
Last active March 1, 2021 22:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save screamingworld/9272585d35f1e7377d7b3e9a6ed2e6f4 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Notifier.WebApi.Models;
using Notifier.WebApi.Services;
using System.Threading;
using System.Threading.Tasks;
namespace Notifier.WebApi.Controllers
{
[Route("api/notifications")]
public class NotificationController : ControllerBase
{
private readonly INotificationService _notificationService;
private readonly ILogger<NotificationController> _logger;
public NotificationController(INotificationService notificationService, ILogger<NotificationController> logger)
{
_notificationService = notificationService ?? throw new System.ArgumentNullException(nameof(notificationService));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
}
[Route("ping")]
[HttpGet]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public async Task<IActionResult> GetPingAsync(string message, CancellationToken cancellationToken)
{
return Ok("PING");
}
[HttpPost]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public async Task<IActionResult> PostAsync(string message, CancellationToken cancellationToken)
{
_logger.LogInformation($"Route: {HttpContext.Request.Path} has been called.");
await _notificationService.CreateAndSendAsync(message, cancellationToken);
return Ok();
}
[HttpGet]
[ProducesResponseType(typeof(NotificationModel), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> GetAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Route: {HttpContext.Request.Path} has been called.");
var notifications = await _notificationService.GetAsync(cancellationToken);
return Ok(notifications);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment