Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add controller for handling generic events #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/Controllers/Events/AppController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Altinn.Platform.Events.Models;
using Altinn.Platform.Events.Repository;

using Microsoft.AspNetCore.Mvc;

namespace Altinn.Platform.Events.Controllers
{
/// <summary>
/// Provides operations for handling app events
/// </summary>
[Route("events/api/v1/app")]
[ApiController]
public class AppController : ControllerBase
{
private readonly IEventsRepository _repository;
private readonly ILogger _logger;

/// <summary>
/// Initializes a new instance of the <see cref="AppController"/> class
/// </summary>
/// <param name="repository">the events repository handler</param>
/// <param name="logger">dependency injection of logger</param>
public AppController(IEventsRepository repository, ILogger<EventsController> logger)
{
_repository = repository;
_logger = logger;
}

/// <summary>
/// Inserts a new event.
/// </summary>
/// <param name="cloudEvent">The event to store.</param>
/// <returns>The application metadata object.</returns>
[HttpPost]
[Consumes("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Produces("application/json")]
public async Task<ActionResult<string>> Post([FromBody] CloudEvent cloudEvent)
{
if (string.IsNullOrEmpty(cloudEvent.Source.OriginalString) || string.IsNullOrEmpty(cloudEvent.Specversion) ||
string.IsNullOrEmpty(cloudEvent.Type) || string.IsNullOrEmpty(cloudEvent.Subject))
{
return BadRequest("Missing parameter values: source, subject, type, id or time cannot be null");
}

try
{
// Force cosmos to create id
cloudEvent.Id = null;

string cloudEventId = await _repository.Create(cloudEvent);

_logger.LogInformation("Cloud Event successfully stored with id: {0}", cloudEventId);

return Created(cloudEvent.Subject, cloudEventId);
}
catch (Exception e)
{
_logger.LogError($"Unable to store cloud event in database. {e}");
return StatusCode(500, $"Unable to store cloud event in database. {e}");
}
}
}
}
49 changes: 27 additions & 22 deletions src/Controllers/Events/EventsController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;
using System.Threading.Tasks;

using Altinn.Platform.Events.Models;
using Altinn.Platform.Events.Repository;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Altinn.Platform.Events.Controllers
{
/// <summary>
/// Provides operations for handling events
/// Provides operations for handling generic events
/// </summary>
[Route("events/api/v1/app")]
[Route("events/api/v1/events")]
[ApiController]
public class EventsController : ControllerBase
{
Expand All @@ -40,31 +35,41 @@ public EventsController(IEventsRepository repository, ILogger<EventsController>
[Consumes("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Produces("application/json")]
[Produces("application/cloudevents+json")]
public async Task<ActionResult<string>> Post([FromBody] CloudEvent cloudEvent)
{
if (string.IsNullOrEmpty(cloudEvent.Source.OriginalString) || string.IsNullOrEmpty(cloudEvent.Specversion) ||
string.IsNullOrEmpty(cloudEvent.Type) || string.IsNullOrEmpty(cloudEvent.Subject))
var (isValid, errorMessages) = ValidateCloudEvent(cloudEvent);
if (!isValid)
{
return BadRequest("Missing parameter values: source, subject, type, id or time cannot be null");
return Problem(errorMessages, null, 400);
}

try
{
// Force cosmos to create id
cloudEvent.Id = null;

string cloudEventId = await _repository.Create(cloudEvent);

var cloudEventId = await _repository.Create(cloudEvent);
_logger.LogInformation("Cloud Event successfully stored with id: {0}", cloudEventId);

return Created(cloudEvent.Subject, cloudEventId);
return Ok();
}
catch (Exception e)
catch (Exception exception)
{
_logger.LogError($"Unable to store cloud event in database. {e}");
return StatusCode(500, $"Unable to store cloud event in database. {e}");
_logger.LogError(exception, "Unable to register cloud event.");
return StatusCode(500, $"Unable to register cloud event.");
}
}

private static (bool IsValid, string ErrorMessage) ValidateCloudEvent(CloudEvent cloudEvent)
{
if (string.IsNullOrEmpty(cloudEvent.Resource))
{
return (false, "A 'resource' property must be defined.");
}

if (!Uri.IsWellFormedUriString(cloudEvent.Resource, UriKind.Absolute))
{
return (false, "'Resource' must be a valid urn.");
}

return (true, null);
}
}
}
7 changes: 6 additions & 1 deletion src/Models/Events/CloudEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Newtonsoft.Json;

namespace Altinn.Platform.Events.Models
Expand Down Expand Up @@ -50,5 +49,11 @@ public class CloudEvent
/// </summary>
[JsonProperty(PropertyName = "alternativesubject")]
public string Alternativesubject { get; set; }

/// <summary>
/// Gets or sets the resource of the event
/// </summary>
[JsonProperty(PropertyName = "resource")]
public string Resource { get; set; }
}
}