Created
June 30, 2020 06:10
-
-
Save dbarkol/a8882394a03446b780f6bc80bfcfad4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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