Skip to content

Instantly share code, notes, and snippets.

@dbarkol
Created June 30, 2020 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarkol/a8882394a03446b780f6bc80bfcfad4f to your computer and use it in GitHub Desktop.
Save dbarkol/a8882394a03446b780f6bc80bfcfad4f to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents;
using SongRequests.Models;
using Newtonsoft.Json;
namespace SongRequests.Controllers
{
[ApiController]
[Route("[controller]")]
public class RequestController : ControllerBase
{
private readonly ILogger<RequestController> _logger;
public RequestController(ILogger<RequestController> logger)
{
_logger = logger;
}
[HttpGet("/dapr/subscribe")]
public ActionResult<IEnumerable<string>> Get()
{
// Initialize an array of topic subscriptions. Each subscription
// contains the name of the topic and the route.
var topics = new [] {new { topic = "songs", route = "playlist"}};
return new OkObjectResult(topics);
}
[HttpPost("/playlist")]
public async Task<IActionResult> NewSong(CloudEvent cloudEvent)
{
// The message is wrapped in a cloud event envelope. Which means that
// the domain-specific information (the song) is in the Data object.
var song = JsonConvert.DeserializeObject<Song>(cloudEvent.Data.ToString());
_logger.LogInformation($"New song request: {song.Artist} - {song.Name}");
return new OkResult();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment