using System.Collections.Generic;
using System.Linq;
using xCommons.Extensions;
using xExceptions.Constants;
using xPushService.Base;
using xPushService.Models;
namespace xPushService.Helpers {
public class XPushServiceHelper {
///
/// a collection of Hubs for Registring ...
///
///
private List hubs = new List ();
///
/// add specified hub to provided hubs for registration ...
///
///
///
public void AddHub (string route) where TXHub : XBaseHub {
//
#region Validate Args ...
//
// check route not null ...
if (
route.IsNullOrEmpty () ||
route.Trim ().IsNullOrEmpty ()
) {
throw XException.InvalidArgs.ToException ();
}
//
// Normalize Route ...
route = route.ToNormalString ();
//
// check route and hub must be unique ...
var isRouteExists = hubs.Any (hd => hd.Route == route);
var isHubExists = hubs.Any (hd => hd.Hub == typeof (TXHub));
if (
isHubExists ||
isRouteExists
) {
throw XException.Duplicate.ToException ();
}
#endregion
//
// Add Provided Hub to List ...
hubs.Add (new XHubDescriptor {
Route = route,
Hub = typeof (TXHub)
});
}
///
/// remove an specific hub from provided list ...
///
///
public void RemoveHub () where TXHub : XBaseHub {
//
var existsHubDescriptor = hubs.SingleOrDefault (hd => hd.Hub == typeof (TXHub));
if (!existsHubDescriptor.IsNull ()) {
hubs.Remove (existsHubDescriptor);
}
}
///
/// retrieve provided hubs count ...
///
///
public int Count () {
return hubs.Count;
}
///
/// retrieve all provided hubs for registration ...
///
///
public List GetHubs () {
return hubs;
}
}
}