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.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 ) { // // 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; } } /// /// Update a Webinar ... /// /// /// [HttpPut("Update")] public virtual async Task> 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; } } /// /// 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; } } /// /// Retrieve all Exists Webinar Ids ... /// /// [HttpGet("Ids")] public virtual ActionResult> GetWebinarIds() { // // Do ... try { // var result = WebinarProvider .GetWebinarIds(); // 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 Webinar Ids ... /// /// /// [HttpGet("QueryIds")] public virtual ActionResult> QueryWebinarIds( [FromQuery] XQuery query ) { // // Do ... try { // var result = WebinarProvider .QueryWebinarIds(query); // 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(); 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 ... /// /// 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(); 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; } } /// /// 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(); 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; } } /// /// 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 } }