Files
xWebinarService/Controller/XWebinarServiceControllerBase.cs
T

696 lines
20 KiB
C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xCommons.Configurations;
using xCommons.Extensions;
using xCommons.Providers;
using xExceptions.Constants;
using xIdentityService.Controllers;
using xIdentityService.Interfaces;
using xModels.Dtos;
using xWebinarService.Interfaces;
using xWebinarService.Models.Dtos;
namespace xWebinarService.Controller
{
/// <summary>
/// a Controller base Class which implement based Actions to Provides Webinar Provider Access ...
/// </summary>
public abstract class XWebinarServiceControllerBase : XIBaseProviderController, IXWebinarServiceControllerActions
{
//
#region Props ...
public IXWebinarProvider WebinarProvider { get; }
#endregion
//
#region Constructor ...
public XWebinarServiceControllerBase(
ILogger logger,
IXWebinarProvider webinarProvider,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{
//
WebinarProvider = webinarProvider;
}
#endregion
//
#region Actions ...
/// <summary>
/// Create Webinar ...
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
[HttpPost("Create")]
public virtual async Task<ActionResult<XWebinarDto>> CreateWebinar(
[FromBody] XWebinarDto item
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Check Owner ...
if (item.OwnerId.IsNullOrEmpty())
{
item.OwnerId = userId;
}
//
var result = await WebinarProvider
.CreateWebinar(item);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Update a Webinar ...
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
[HttpPut("Update")]
public virtual async Task<ActionResult<XWebinarDto>> UpdateWebinar(
[FromBody] XWebinarDto item
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Check Permission ...
bool has = item.OwnerId == userId || isAdmin;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.UpdateWebinar(item);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Retrieve Specified Webinar ...
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("")]
public virtual async Task<ActionResult<XWebinarDto>> GetWebinar(
[FromQuery] Guid id
)
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.GetWebinar(id);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Retrieve all Exists Webinars ...
/// </summary>
/// <returns></returns>
[HttpGet("All")]
public virtual async Task<ActionResult<IEnumerable<XWebinarDto>>> GetWebinars()
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.GetWebinars();
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Retrieve all Exists Webinar Ids ...
/// </summary>
/// <returns></returns>
[HttpGet("Ids")]
public virtual async Task<ActionResult<IEnumerable<Guid>>> GetWebinarIds()
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.GetWebinarIds();
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Query Webinars ...
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
[HttpGet("Query")]
public virtual async Task<ActionResult<XQueryResult<XWebinarDto>>> QueryWebinars(
[FromQuery] XQuery query
)
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.QueryWebinars(query);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Query Webinar Ids ...
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
[HttpGet("QueryIds")]
public virtual async Task<ActionResult<XQueryResult<Guid>>> QueryWebinarIds(
[FromQuery] XQuery query
)
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.QueryWebinarIds(query);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Remove Specified Webinar ...
/// </summary>
/// <param name="webinarId"></param>
/// <returns></returns>
[HttpDelete("Remove")]
public virtual async Task<ActionResult<bool>> RemoveWebinar(
[FromQuery] Guid webinarId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has = entity.OwnerId == userId || isAdmin;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.RemoveWebinar(webinarId);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
//
#region WebinarSubscriber ...
/// <summary>
/// Subscribe a User to Webinar ...
/// </summary>
/// <param name="webinarId"></param>
/// <param name="subscriberId"></param>
/// <returns></returns>
[HttpPost("Subscribe")]
public virtual async Task<ActionResult<bool>> Subscribe(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
entity.OwnerId == userId ||
entity.Type == Constants.XWebinarType.Public;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.Subscribe(
webinarId: webinarId,
subscriberId: subscriberId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Check Specified User Subscribed on a Webinar or not ...
/// </summary>
/// <param name="webinarId"></param>
/// <param name="subscriberId"></param>
/// <returns></returns>
[HttpGet("IsSubscribed")]
public virtual async Task<ActionResult<bool>> IsSubscribed(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
entity.OwnerId == userId;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.IsSubscribed(
webinarId: webinarId,
subscriberId: subscriberId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Unsubscribe Specified User from a Specified Webinar ...
/// </summary>
/// <param name="webinarId"></param>
/// <param name="subscriberId"></param>
/// <returns></returns>
[HttpDelete("Unsubscribe")]
public virtual async Task<ActionResult<bool>> Unsubscribe(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
subscriberId == userId ||
entity.OwnerId == userId;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.Unsubscribe(
webinarId: webinarId,
subscriberId: subscriberId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Retrieve Specified Subscriber ...
/// </summary>
/// <param name="webinarId"></param>
/// <param name="subscriberId"></param>
/// <returns></returns>
[HttpGet("GetSubscriber")]
public virtual async Task<ActionResult<XWebinarSubscriberDto>> GetSubscriber(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
entity.OwnerId == userId;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.GetSubscriber(
webinarId: webinarId,
subscriberId: subscriberId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Get all Specified Webinars Subscribers ...
/// </summary>
/// <param name="webinarId"></param>
/// <returns></returns>
[HttpGet("GetSubscribers")]
public virtual async Task<ActionResult<IEnumerable<XWebinarSubscriberDto>>> GetSubscribers(
[FromQuery] Guid webinarId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
entity.OwnerId == userId;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.GetSubscribers(webinarId);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Query Specified Webinar's Subscribers ...
/// </summary>
/// <param name="webinarId"></param>
/// <param name="query"></param>
/// <returns></returns>
[HttpGet("QuerySubscribers")]
public virtual async Task<ActionResult<XQueryResult<XWebinarSubscriberDto>>> QuerySubscribers(
[FromQuery] XQuery query,
[FromQuery] Guid webinarId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Retrieve Entity ...
var entity = await WebinarProvider.GetWebinar(webinarId);
bool has = !entity.IsNullOrDefault();
if (!has)
{
XException.NotFound.Throw();
}
//
// Check Permissions ...
has =
isAdmin ||
entity.OwnerId == userId;
if (!has)
{
XException.NotAllowed.Throw();
}
//
var result = await WebinarProvider
.QuerySubscribers(
query: query,
webinarId: webinarId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
}
}