Skip to content

Commit

Permalink
Land Subscriptions Web API adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcota committed Feb 22, 2019
1 parent 6f08b50 commit 5ed52eb
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Land.WebAPI/SearchServices/SubscriptionModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Land Messaging services Component : Web Api *
* Assembly : Empiria.Land.WebApi.dll Pattern : Response methods *
* Type : SubscriptionModels License : Please read LICENSE.txt file *
* *
* Summary : Models for Subscription responses. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

using Empiria.Land.Messaging;

namespace Empiria.Land.WebApi {

/// <summary>Response models for SignEvent entities.</summary>
static internal class SubscriptionModels {

#region Response models

static internal object ToResponse(this Subscription subscription) {
return new {
uid = subscription.UID,
type = subscription.SubscriptionType,
subscribedObjectUID = subscription.SubscribedObjectUID,
sendTo = new {
name = subscription.SendTo.Name,
address = subscription.SendTo.Address,
},
status = subscription.Status
};
}

#endregion Response models

} // class SubscriptionModels

} // namespace Empiria.OnePoint.WebApi

105 changes: 105 additions & 0 deletions Land.WebAPI/SearchServices/SubscriptionServicesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Subscription services Component : Web Api *
* Assembly : Empiria.Land.WebApi.dll Pattern : Controller *
* Type : SubscriptionServicesController License : Please read LICENSE.txt file *
* *
* Summary : Subscription services used to follow changes on resources and documents. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;
using System.Web.Http;

using Empiria.Json;
using Empiria.WebApi;

using Empiria.Land.Messaging;

namespace Empiria.Land.WebApi {

/// <summary>Subscription services used to follow changes on resources and documents.</summary>
public class SubscriptionServicesController : WebApiController {

#region Public APIs


[HttpPost, AllowAnonymous]
[Route("v1/online-services/subscriptions/{subscriptionUID}/confirm")]
public SingleObjectModel ConfirmSubscription([FromUri] string subscriptionUID,
[FromBody] object body) {
try {
SubscriptionRequest subscriptionRequest =
this.BuildSubscriptionRequestFromBody(SubscriptionRequestCommand.ConfirmSubscription, body);

Subscription subscription = SubscriptionServices.ConfirmSubscription(subscriptionRequest);

return new SingleObjectModel(this.Request, subscription.ToResponse(),
typeof(Subscription).ToString());

} catch (Exception e) {
throw base.CreateHttpException(e);
}
}


[HttpPost, AllowAnonymous]
[Route("v1/online-services/subscriptions")]
public SingleObjectModel Subscribe([FromBody] object body) {
try {
SubscriptionRequest subscriptionRequest =
this.BuildSubscriptionRequestFromBody(SubscriptionRequestCommand.Subscribe, body);

Subscription subscription = SubscriptionServices.Subscribe(subscriptionRequest);

return new SingleObjectModel(this.Request, subscription.ToResponse(),
typeof(Subscription).ToString());

} catch (Exception e) {
throw base.CreateHttpException(e);
}
}


[HttpPost, AllowAnonymous]
[Route("v1/online-services/subscriptions/{subscriptionUID}/unsubscribe")]
public SingleObjectModel Unsubscribe([FromUri] string subscriptionUID,
[FromBody] object body) {
try {
base.RequireResource(subscriptionUID, "subscriptionUID");

SubscriptionRequest subscriptionRequest =
this.BuildSubscriptionRequestFromBody(SubscriptionRequestCommand.Unsubscribe, body);

Subscription subscription = SubscriptionServices.Unsubscribe(subscriptionUID, subscriptionRequest);

return new SingleObjectModel(this.Request, subscription.ToResponse(),
typeof(Subscription).ToString());

} catch (Exception e) {
throw base.CreateHttpException(e);
}
}


#endregion Public APIs

#region Private methods


private SubscriptionRequest BuildSubscriptionRequestFromBody(SubscriptionRequestCommand command,
object body) {
base.RequireBody(body);

var bodyAsJson = JsonObject.Parse(body);

bodyAsJson.Add("command", command.ToString());

return SubscriptionRequest.Parse(bodyAsJson);
}


#endregion Private methods

} // class SubscriptionServicesController

} // namespace Empiria.Land.WebApi

0 comments on commit 5ed52eb

Please sign in to comment.