Permalink
Browse files

Links and Commands

  • Loading branch information...
1 parent 135d274 commit b5e3d7ed57b325ccaa3b05a271d459b2039ce5b0 @andlju committed Apr 25, 2012
@@ -5,8 +5,8 @@ namespace Entile.Server.Commands
{
public class RegisterClientCommand : CommandBase
{
- public Guid ClientId;
- public string NotificationChannel;
+ public Guid ClientId { get; set; }
+ public string NotificationChannel { get; set; }
public RegisterClientCommand()
{
@@ -4,11 +4,11 @@ namespace Entile.Server.Commands
{
public class SendRawNotificationCommand : CommandBase
{
- public Guid ClientId;
- public Guid SubscriptionId;
- public Guid NotificationId;
- public string Content;
- public int NumberOfAttempts;
+ public Guid ClientId { get; set; }
+ public Guid SubscriptionId { get; set; }
+ public Guid NotificationId { get; set; }
+ public string Content { get; set; }
+ public int NumberOfAttempts { get; set; }
public SendRawNotificationCommand()
{
@@ -4,19 +4,19 @@ namespace Entile.Server.Commands
{
public class SendTileNotificationCommand : CommandBase
{
- public Guid ClientId;
- public Guid SubscriptionId;
- public Guid NotificationId;
+ public Guid ClientId { get; set; }
+ public Guid SubscriptionId { get; set; }
+ public Guid NotificationId { get; set; }
- public string Title;
- public int Counter;
- public string BackgroundUri;
+ public string Title { get; set; }
+ public int Counter { get; set; }
+ public string BackgroundUri { get; set; }
- public string BackTitle;
- public string BackContent;
- public string BackBackgroundUri;
+ public string BackTitle { get; set; }
+ public string BackContent { get; set; }
+ public string BackBackgroundUri { get; set; }
- public int NumberOfAttempts;
+ public int NumberOfAttempts { get; set; }
public SendTileNotificationCommand()
{
@@ -1,15 +1,27 @@
using System;
+using System.ComponentModel.DataAnnotations;
namespace Entile.Server.Commands
{
public class SendToastNotificationCommand : CommandBase
{
- public Guid ClientId;
- public Guid SubscriptionId;
- public Guid NotificationId;
- public string Title;
- public string Body;
- public int NumberOfAttempts;
+ [Key]
+ public Guid ClientId { get; set; }
+ [Key]
+ public Guid SubscriptionId { get; set; }
+ [Key]
+ public Guid NotificationId { get; set; }
+
+ [Required]
+ [Display(Description = "Title of the Toast")]
+ public string Title { get; set; }
+
+ [Display(Description = "Body of the Toast")]
+ public string Body { get; set; }
+
+ [Required]
+ [Display(Description = "Number of attempts")]
+ public int NumberOfAttempts { get; set; }
public SendToastNotificationCommand()
{
@@ -1,16 +1,29 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
+using System.ComponentModel.DataAnnotations;
using Entile.Server.Domain;
namespace Entile.Server.Commands
{
public class SubscribeCommand : CommandBase
{
- public Guid ClientId;
- public Guid SubscriptionId;
- public NotificationKind Kind;
- public string Uri;
- public Dictionary<string, string> ExtendedInformation;
+ [Key]
+ public Guid ClientId { get; set; }
+
+ [Key]
+ public Guid SubscriptionId { get; set; }
+
+ [Required]
+ [Display(Description = "Notification kind")]
+ public NotificationKind Kind { get; set; }
+
+ [Required]
+ [Display(Description = "Parameter uri")]
+ public string Uri { get; set; }
+
+ [Display(Description = "Extended information")]
+ public Dictionary<string, string> ExtendedInformation { get; set; }
public SubscribeCommand()
{
@@ -1,10 +1,12 @@
using System;
+using System.ComponentModel.DataAnnotations;
namespace Entile.Server.Commands
{
public class UnregisterClientCommand : CommandBase
{
- public Guid ClientId;
+ [Key]
+ public Guid ClientId { get; set; }
public UnregisterClientCommand()
{
@@ -1,11 +1,14 @@
using System;
+using System.ComponentModel.DataAnnotations;
namespace Entile.Server.Commands
{
public class UnsubscribeCommand : CommandBase
{
- public Guid ClientId;
- public Guid SubscriptionId;
+ [Key]
+ public Guid ClientId { get; set; }
+ [Key]
+ public Guid SubscriptionId { get; set; }
public UnsubscribeCommand()
{
@@ -0,0 +1,37 @@
+using System;
+using System.Net;
+using System.Net.Http;
+using System.Web.Http;
+using Entile.Server;
+using Entile.Server.Commands;
+
+namespace Entile.WebApiHost.Controllers
+{
+ public class ClientSubscriptionsController : ApiController
+ {
+ private readonly IMessageDispatcher _dispatcher;
+ private readonly HyperMediaApplier _applier;
+
+ public ClientSubscriptionsController()
+ {
+ _dispatcher = Bootstrapper.CurrentServer.CommandDispatcher;
+ _applier = new HyperMediaApplier();
+ _applier.RegisterProvider(new SubscriptionHyperMediaProvider());
+ }
+
+ public HttpResponseMessage Post(Guid clientId, SubscriptionResource subscription)
+ {
+ var subscriptionId = Guid.NewGuid();
+ var command = new SubscribeCommand(
+ clientId,
+ subscriptionId,
+ (Server.Domain.NotificationKind)Enum.Parse(typeof (Server.Domain.NotificationKind),
+ subscription.NotificationKind),
+ subscription.ParamUri, subscription.ExtendedInfo);
+
+ _dispatcher.Dispatch(command);
+
+ return new HttpResponseMessage(HttpStatusCode.Created);
+ }
+ }
+}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
@@ -16,110 +17,98 @@ public class ClientResource
public string NotificationUri { get; set; }
}
- public class LinkDefinition
- {
- public string Rel { get; set; }
- public Uri Uri { get; set; }
- }
-
- public class CommandDefinition
+ public class ClientResponse : HyperMediaResponse
{
- public string Name { get; set; }
- public string Description { get; set; }
- public string Method { get; set; }
- public FieldDefinition[] Fields { get; set; }
- }
+ public Guid ClientId { get; set; }
+ public string NotificationUri { get; set; }
- public class FieldDefinition
- {
- public string Name { get; set; }
- public string Description { get; set; }
- public bool Optional { get; set; }
+ public SubscriptionResponse[] Subscriptions { get; set; }
}
- public class HyperMediaResponse
+ public class SubscriptionResource
{
- public HyperMediaResponse()
- {
- Links = new List<LinkDefinition>();
- Commands = new List<CommandDefinition>();
- }
-
- public List<LinkDefinition> Links { get; set; }
- public List<CommandDefinition> Commands { get; set; }
+ public string NotificationKind { get; set; }
+ public string ParamUri { get; set; }
+ public Dictionary<string, string> ExtendedInfo { get; set; }
}
- public class ClientResponse : HyperMediaResponse
+ public class SubscriptionResponse : HyperMediaResponse
{
- public string NotificationUri { get; set; }
+ public Guid ClientId { get; set; }
+ public Guid SubscriptionId { get; set; }
+ public string NotificationKind { get; set; }
+ public string ParamUri { get; set; }
}
-
public class ClientsController : ApiController
{
private readonly IMessageDispatcher _dispatcher;
+ private readonly HyperMediaApplier _applier;
public ClientsController()
{
_dispatcher = Bootstrapper.CurrentServer.CommandDispatcher;
- }
-
- // GET /api/values
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
+ _applier = new HyperMediaApplier();
+ _applier.RegisterProvider(new ClientHyperMediaProvider());
+ _applier.RegisterProvider(new SubscriptionHyperMediaProvider());
}
// GET /api/clients/a42076d0-9728-48d9-ba6b-c6ed722fb49e
- public ClientResponse Get(Guid id)
+ public ClientResponse Get(Guid clientId)
{
var queries = new ClientQueries();
- var client = queries.GetClient(new GetClientQuery() {ClientId = id});
+ var client = queries.GetClient(new GetClientQuery() { ClientId = clientId });
var response = new ClientResponse()
{
- NotificationUri = client.NotificationChannel
+ ClientId = client.ClientId,
+ NotificationUri = client.NotificationChannel,
+ Subscriptions = client.Subscriptions.Select(s => new SubscriptionResponse()
+ {
+ ClientId = client.ClientId,
+ SubscriptionId = s.SubscriptionId,
+ NotificationKind = ((Server.Domain.NotificationKind)s.NotificationKind).ToString(CultureInfo.InvariantCulture),
+ ParamUri = s.ParamUri
+ }).ToArray()
};
+ _applier.Apply(Request, response);
+ foreach(var sub in response.Subscriptions)
+ _applier.Apply(Request, sub);
return response;
}
// POST /api/clients
- public HttpResponseMessage<HyperMediaResponse> Post(ClientResource client)
+ public HttpResponseMessage Post(ClientResource client)
{
var id = Guid.NewGuid();
var command = new RegisterClientCommand() { ClientId = id, NotificationChannel = client.NotificationUri };
_dispatcher.Dispatch(command);
var clientUri = new Uri(Request.RequestUri, "/api/clients/" + id);
-
- var hyperMediaResponse = new HyperMediaResponse();
- hyperMediaResponse.Links.Add(new LinkDefinition() { Rel = "self", Uri = clientUri});
- var response = new HttpResponseMessage<HyperMediaResponse>(hyperMediaResponse, HttpStatusCode.Created);
+ var response = new HttpResponseMessage(HttpStatusCode.Created);
response.Headers.Location = clientUri;
return response;
}
- public HttpResponseMessage<HyperMediaResponse> Post(Guid id, ClientResource client)
+ // POST /api/clients/a42076d0-9728-48d9-ba6b-c6ed722fb49e
+ public HttpResponseMessage Post(Guid clientId, ClientResource client)
{
- var command = new RegisterClientCommand() { ClientId = id, NotificationChannel = client.NotificationUri };
+ var command = new RegisterClientCommand() { ClientId = clientId, NotificationChannel = client.NotificationUri };
_dispatcher.Dispatch(command);
- var clientUri = new Uri(Request.RequestUri, "/api/clients/" + id);
-
- var hyperMediaResponse = new HyperMediaResponse();
- hyperMediaResponse.Links.Add(new LinkDefinition() { Rel = "self", Uri = clientUri });
+ var clientUri = new Uri(Request.RequestUri, "/api/clients/" + clientId);
- var response = new HttpResponseMessage<HyperMediaResponse>(hyperMediaResponse, HttpStatusCode.Accepted);
+ var response = new HttpResponseMessage(HttpStatusCode.Accepted);
response.Headers.Location = clientUri;
return response;
}
// DELETE /api/clients/a42076d0-9728-48d9-ba6b-c6ed722fb49e
- public void Delete(Guid id)
+ public void Delete(Guid clientId)
{
- var command = new UnregisterClientCommand(id);
+ var command = new UnregisterClientCommand(clientId);
_dispatcher.Dispatch(command);
}
}
@@ -0,0 +1,38 @@
+using System.Collections.Generic;
+using System.Net.Http;
+using Entile.Server.Commands;
+
+namespace Entile.WebApiHost.Controllers
+{
+ public class ClientHyperMediaProvider : HyperMediaProviderBase<ClientResponse>
+ {
+ protected override IEnumerable<LinkDefinition> Links(HttpRequestMessage request, ClientResponse response)
+ {
+ yield return BuildLink(request, response, "/api/clients/{clientId}", "self");
+ yield return BuildLink(request, response, "/api/clients/{clientId}/subscriptions", "subscriptions");
+ }
+
+ protected override IEnumerable<CommandDefinition> Commands(HttpRequestMessage request, ClientResponse response)
+ {
+ yield return BuildCommand(request, response, "/api/clients/{clientId}", "unregister", "Unregister client", "DELETE");
+ yield return BuildCommand<SubscribeCommand>(request, response, "/api/clients/{clientId}/subscriptions", "subscribe", "Add subscription");
+ }
+ }
+
+ public class SubscriptionHyperMediaProvider: HyperMediaProviderBase<SubscriptionResponse>
+ {
+ protected override IEnumerable<LinkDefinition> Links(HttpRequestMessage request, SubscriptionResponse response)
+ {
+ yield return BuildLink(request, response, "/api/clients/{clientId}/subscriptions/{subscriptionId}", "self");
+ yield return BuildLink(request, response, "/api/clients/{clientId}", "client");
+ }
+
+ protected override IEnumerable<CommandDefinition> Commands(HttpRequestMessage request, SubscriptionResponse response)
+ {
+ yield return
+ BuildCommand<UnsubscribeCommand>(request, response,
+ "/api/clients/{clientId}/subscriptions/{subscriptionId}", "unsubscribe",
+ "Unsubscribe from notifications", "DELETE");
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit b5e3d7e

Please sign in to comment.