Add Support for Hub Actions Implementation ...
This commit is contained in:
@@ -2,6 +2,9 @@ using xExceptions.Attributes;
|
||||
|
||||
namespace xWebinarService.Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Webinar Connection Hub Actions ...
|
||||
/// </summary>
|
||||
public enum XWebinarHubAction
|
||||
{
|
||||
/// <summary>
|
||||
@@ -21,17 +24,28 @@ namespace xWebinarService.Constants
|
||||
/// </summary>
|
||||
[StringValue("ViewerJoined")]
|
||||
ViewerJoined,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// When a Webinar Subscriber Leaved a Webinar ...
|
||||
/// </summary>
|
||||
[StringValue("ViewerLeaved")]
|
||||
ViewerLeaved,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Send a Message to Webinar ...
|
||||
/// </summary>
|
||||
[StringValue("Message")]
|
||||
Message,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entity Hub Actions ...
|
||||
/// </summary>
|
||||
public enum XWebinarEntityHubAction
|
||||
{
|
||||
[StringValue("Subscribed")]
|
||||
Subscribed,
|
||||
[StringValue("Unsubscribe")]
|
||||
Unsubscribe
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xPushService.Base;
|
||||
using xWebinarService.Models.Entities;
|
||||
|
||||
namespace xWebinarService.Hubs
|
||||
{
|
||||
public class XWebinarEntityHub : XBaseEntityHub<XWebinar, Guid>
|
||||
{
|
||||
//
|
||||
#region Constructor ...
|
||||
public XWebinarEntityHub(ILogger<XBaseHub> logger) : base(logger)
|
||||
{ }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using xDataService.Configuration;
|
||||
using xIdentityService.Interfaces;
|
||||
using xModels.Dtos;
|
||||
using xWebinarService.Hubs;
|
||||
using xWebinarService.Interfaces.Entities;
|
||||
using xWebinarService.Models.Dtos;
|
||||
using xWebinarService.Models.Entities;
|
||||
@@ -14,6 +16,7 @@ namespace xWebinarService.Interfaces
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
IHubContext<XWebinarEntityHub> Hub { get; }
|
||||
IXIdentityProvider IdentityProvider { get; }
|
||||
IXWebinarRepository WebinarRepository { get; }
|
||||
XDataServiceConfiguration DataSercviceConfiguration { get; }
|
||||
@@ -46,15 +49,23 @@ namespace xWebinarService.Interfaces
|
||||
/// Create Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<XWebinarDto> CreateWebinar(XWebinarDto item);
|
||||
Task<XWebinarDto> CreateWebinar(
|
||||
XWebinarDto item,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Update a Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<XWebinarDto> UpdateWebinar(XWebinarDto item);
|
||||
Task<XWebinarDto> UpdateWebinar(
|
||||
XWebinarDto item,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Webinar ...
|
||||
@@ -93,8 +104,12 @@ namespace xWebinarService.Interfaces
|
||||
/// Remove Specified Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> RemoveWebinar(Guid webinarId);
|
||||
Task<bool> RemoveWebinar(
|
||||
Guid webinarId,
|
||||
string connectionId = null
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -104,10 +119,12 @@ namespace xWebinarService.Interfaces
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="subscriberId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> Subscribe(
|
||||
Guid webinarId,
|
||||
string subscriberId
|
||||
string subscriberId,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
@@ -126,10 +143,12 @@ namespace xWebinarService.Interfaces
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="subscriberId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> Unsubscribe(
|
||||
Guid webinarId,
|
||||
string subscriberId
|
||||
string subscriberId,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Extensions;
|
||||
@@ -10,7 +11,9 @@ using xIdentityModels.Dtos;
|
||||
using xIdentityService.Extensions;
|
||||
using xIdentityService.Interfaces;
|
||||
using xModels.Dtos;
|
||||
using xPushService.Constants;
|
||||
using xWebinarService.Constants;
|
||||
using xWebinarService.Hubs;
|
||||
using xWebinarService.Interfaces;
|
||||
using xWebinarService.Interfaces.Entities;
|
||||
using xWebinarService.Models.Dtos;
|
||||
@@ -25,6 +28,7 @@ namespace xWebinarService.Providers
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
public IHubContext<XWebinarEntityHub> Hub { get; }
|
||||
public IXIdentityProvider IdentityProvider { get; }
|
||||
public IXWebinarRepository WebinarRepository { get; }
|
||||
public XDataServiceConfiguration DataSercviceConfiguration { get; }
|
||||
@@ -41,7 +45,8 @@ namespace xWebinarService.Providers
|
||||
XDataServiceConfiguration dataSercviceConfiguration,
|
||||
IXWebinarTitleResourceProvider titleResourceProvider,
|
||||
IXWebinarSubscriberRepository webinarSubscriberRepository,
|
||||
IXWebinarDescriptionResourceProvider descriptionResourceProvider
|
||||
IXWebinarDescriptionResourceProvider descriptionResourceProvider,
|
||||
IHubContext<XWebinarEntityHub> hub = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -51,6 +56,7 @@ namespace xWebinarService.Providers
|
||||
TitleResourceProvider = titleResourceProvider;
|
||||
WebinarSubscriberRepository = webinarSubscriberRepository;
|
||||
DescriptionResourceProvider = descriptionResourceProvider;
|
||||
Hub = hub;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -163,8 +169,12 @@ namespace xWebinarService.Providers
|
||||
/// Create Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XWebinarDto> CreateWebinar(XWebinarDto item)
|
||||
public async Task<XWebinarDto> CreateWebinar(
|
||||
XWebinarDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -276,6 +286,13 @@ namespace xWebinarService.Providers
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Add.GetStringValue(),
|
||||
payLoad: entity.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
// Convert Entity to Dto ...
|
||||
var result = await ToXWebinarDto(entity);
|
||||
@@ -286,8 +303,12 @@ namespace xWebinarService.Providers
|
||||
/// Update a Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XWebinarDto> UpdateWebinar(XWebinarDto item)
|
||||
public async Task<XWebinarDto> UpdateWebinar(
|
||||
XWebinarDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -413,8 +434,20 @@ namespace xWebinarService.Providers
|
||||
);
|
||||
|
||||
//
|
||||
entity = await WebinarRepository.GetAsync(entity.Id);
|
||||
var result = await ToXWebinarDto(entity);
|
||||
|
||||
//
|
||||
if (!result.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Update.GetStringValue(),
|
||||
payLoad: entity.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
@@ -615,9 +648,11 @@ namespace xWebinarService.Providers
|
||||
/// Remove Specified Webinar ...
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> RemoveWebinar(
|
||||
Guid webinarId
|
||||
Guid webinarId,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -668,6 +703,15 @@ namespace xWebinarService.Providers
|
||||
//
|
||||
// Check Entity Removed ...
|
||||
result = !await WebinarRepository.IsExistsAsync(webinarId);
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
payLoad: entity.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -681,10 +725,12 @@ namespace xWebinarService.Providers
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="subscriberId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Subscribe(
|
||||
Guid webinarId,
|
||||
string subscriberId
|
||||
string subscriberId,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -750,6 +796,15 @@ namespace xWebinarService.Providers
|
||||
};
|
||||
entity = await WebinarSubscriberRepository.AddAsync(entity);
|
||||
result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XWebinarEntityHubAction.Subscribed.GetStringValue(),
|
||||
payLoad: entity.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -822,10 +877,12 @@ namespace xWebinarService.Providers
|
||||
/// </summary>
|
||||
/// <param name="webinarId"></param>
|
||||
/// <param name="subscriberId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Unsubscribe(
|
||||
Guid webinarId,
|
||||
string subscriberId
|
||||
string subscriberId,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -865,6 +922,15 @@ namespace xWebinarService.Providers
|
||||
);
|
||||
entity = await WebinarSubscriberRepository.RemoveAsync(entity);
|
||||
result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XWebinarEntityHubAction.Unsubscribe.GetStringValue(),
|
||||
payLoad: entity.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -1054,5 +1120,55 @@ namespace xWebinarService.Providers
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Hub Actions ...
|
||||
/// <summary>
|
||||
/// Send Custom Push Message ...
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="payLoad"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendPush(
|
||||
string action,
|
||||
string payLoad,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var actions = new List<string>
|
||||
{
|
||||
XBaseEntityHubAction.Add.GetStringValue(),
|
||||
XBaseEntityHubAction.Update.GetStringValue(),
|
||||
XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
XBaseEntityHubAction.AddMany.GetStringValue(),
|
||||
XBaseEntityHubAction.DeleteMany.GetStringValue(),
|
||||
XBaseEntityHubAction.UpdateMany.GetStringValue(),
|
||||
XBaseEntityHubAction.AddOrUpdate.GetStringValue(),
|
||||
XWebinarEntityHubAction.Subscribed.GetStringValue(),
|
||||
XWebinarEntityHubAction.Unsubscribe.GetStringValue(),
|
||||
};
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!Hub.IsNull() &&
|
||||
!action.IsNullOrEmpty() &&
|
||||
actions.Contains(action);
|
||||
if (!isValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var clients = Hub.Clients.All;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
clients = Hub.Clients.AllExcept(connectionId);
|
||||
}
|
||||
await clients.SendAsync(action, payLoad, connectionId);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user