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;
using Microsoft.AspNetCore.Http;
namespace xWebinarService.Controllers
{
///
/// a Controller base Class which implement based Actions to Provides Webinar Provider Access ...
///
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 ...
///
/// Create Webinar ...
///
///
///
///
///
[HttpPost("Create")]
public virtual async Task> CreateWebinar(
[FromBody] XWebinarDto item,
[FromQuery] string tags = null,
[FromForm] IFormFileCollection files = null
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
// Check Owner ...
if (item.OwnerId.IsNullOrEmpty())
{
item.OwnerId = userId;
}
//
var tagList = tags.ParseListString();
var result = await WebinarProvider
.CreateWebinar(
item: item,
files: files,
tags: tagList,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Update a Webinar ...
///
///
///
[HttpPut("Update")]
public virtual async Task> UpdateWebinar(
[FromBody] XWebinarDto item
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
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: item,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Retrieve Specified Webinar ...
///
///
///
[HttpGet("")]
public virtual async Task> 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;
}
}
///
/// Retrieve all Exists Webinars ...
///
///
[HttpGet("All")]
public virtual async Task>> GetWebinars()
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.GetWebinars();
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Query Webinars ...
///
///
///
[HttpGet("Query")]
public virtual async Task>> 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;
}
}
///
/// Query Owned Webinars ...
///
///
///
[HttpGet("QueryOwned")]
public virtual async Task>> QueryOwnedWebinars(
[FromQuery] XQuery query
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
//
var result = await WebinarProvider
.QueryOwnedWebinars(
query: query,
userInfo: userInfo
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Query Joinable Webinars ...
///
///
///
[HttpGet("QueryJoinable")]
public virtual async Task>> QueryJoinableWebinars(
[FromQuery] XQuery query
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
//
var result = await WebinarProvider
.QueryJoinableWebinars(
query: query,
userInfo: userInfo
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Remove Specified Webinar ...
///
///
///
[HttpDelete("Remove")]
public virtual async Task> RemoveWebinar(
[FromQuery] Guid webinarId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
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: webinarId,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
//
#region WebinarSubscriber ...
///
/// Subscribe a User to Webinar ...
///
///
///
///
[HttpPost("Subscribe")]
public virtual async Task> Subscribe(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
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,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Check Specified User Subscribed on a Webinar or not ...
///
///
///
///
[HttpGet("IsSubscribed")]
public virtual async Task> 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;
}
}
///
/// Unsubscribe Specified User from a Specified Webinar ...
///
///
///
///
[HttpDelete("Unsubscribe")]
public virtual async Task> Unsubscribe(
[FromQuery] Guid webinarId,
[FromQuery] string subscriberId
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
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,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Retrieve Specified Subscriber ...
///
///
///
///
[HttpGet("GetSubscriber")]
public virtual async Task> 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;
}
}
///
/// Get all Specified Webinars Subscribers ...
///
///
///
[HttpGet("GetSubscribers")]
public virtual async Task>> 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;
}
}
///
/// Query Specified Webinar's Subscribers ...
///
///
///
///
[HttpGet("QuerySubscribers")]
public virtual async Task>> 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
//
#region File Actions ...
///
/// Upload Specified Files for Specified Webinar ...
///
///
///
///
[HttpPost("{id}/Files")]
public virtual async Task>> Upload(
[FromRoute] Guid id,
[FromForm] IFormFileCollection files
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await WebinarProvider
.Upload(
id: id,
files: files,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Remove Specified (All) Files of Specified Webinar ...
///
///
///
///
[HttpDelete("{id}/Files")]
public virtual async Task>> Remove(
[FromRoute] Guid id,
[FromQuery] string ids = null
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await WebinarProvider
.RemoveFiles(
id: id,
ids: ids,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Retrieve Specified Webinar's Files ...
///
///
///
[HttpGet("{id}/Files")]
public virtual async Task>> GetFiles(
[FromRoute] Guid id
)
{
//
// Do ...
try
{
//
var result = await WebinarProvider
.GetFiles(id);
//
return Ok(result.
ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
//
#region Tags Actions ...
///
/// Attach Specified Tags To Specified Webinar ...
///
///
///
///
[HttpPost("{id}/Tags")]
public virtual async Task TagsAttach(
[FromRoute] Guid id,
[FromQuery] string tags
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var tagsList = tags.ParseListString();
await WebinarProvider
.TagsAttach(
id: id,
tags: tagsList,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Detach Specified Tags From Specified Webinar ...
///
///
///
///
[HttpDelete("{id}/Tags")]
public virtual async Task TagsDetach(
[FromRoute] Guid id,
[FromQuery] string tags
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var tagsList = tags.ParseListString();
await WebinarProvider
.TagsDetach(
id: id,
tags: tagsList,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
}
}