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 xWebinarService.Interfaces; using xWebinarService.Models.Dtos; using Microsoft.AspNetCore.Http; using XFileDto = xFileService.Models.Dtos.XFileDto; using xModels.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 ... /// /// /// /// /// /// /// Create Webinar ... /// /// /// /// /// [HttpPost("")] public virtual async Task> CreateWebinar( [FromForm] 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("{id}")] public virtual async Task> UpdateWebinar( [FromRoute] Guid id, [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("{id}")] public virtual async Task> GetWebinar( [FromRoute] 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("Query/Owned")] 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("Query/Joinable")] 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("{id}")] public virtual async Task> RemoveWebinar( [FromRoute] Guid id ) { // // 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(id); 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: id, connectionId: connectionId ); // return Ok(result. ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion // #region WebinarSubscriber ... /// /// Subscribe a User to Webinar ... /// /// /// /// [HttpPost("{id}/Subscribe/{subscriberId}")] public virtual async Task> Subscribe( [FromRoute] Guid id, [FromRoute] 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(id); 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: id, 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("{id}/IsSubscribed/{subscriberId}")] public virtual async Task> IsSubscribed( [FromRoute] Guid id, [FromRoute] 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(id); 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: id, subscriberId: subscriberId ); // return Ok(result. ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Unsubscribe Specified User from a Specified Webinar ... /// /// /// /// [HttpDelete("{id}/Unsubscribe/{subscriberId}")] public virtual async Task> Unsubscribe( [FromRoute] Guid id, [FromRoute] 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(id); 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: id, subscriberId: subscriberId, connectionId: connectionId ); // return Ok(result. ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Retrieve Specified Subscriber ... /// /// /// /// [HttpGet("{id}/Subscriber/{subscriberId}")] public virtual async Task> GetSubscriber( [FromRoute] Guid id, [FromRoute] 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(id); 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: id, subscriberId: subscriberId ); // return Ok(result. ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Get all Specified Webinars Subscribers ... /// /// /// [HttpGet("{id}/Subscribers")] public virtual async Task>> GetSubscribers( [FromRoute] Guid id ) { // // 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(id); 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(id); // return Ok(result. ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Query Specified Webinar's Subscribers ... /// /// /// /// [HttpGet("{id}/Subscribers/Query")] public virtual async Task>> QuerySubscribers( [FromRoute] Guid id, [FromQuery] XQuery query ) { // // 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(id); 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: id ); // 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>> UploadFiles( [FromRoute] Guid id, [FromForm] IFormFileCollection files ) { // // Do ... try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await WebinarProvider .UploadFiles( 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>> RemoveFiles( [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; } } /// /// Stream Specified File of Specified Webinar ... /// /// /// /// [HttpGet("{id}/Files/{fileId}/Stream")] public virtual async Task StreamFile( [FromRoute] Guid id, [FromRoute] Guid fileId ) { // // Do ... try { // var result = await WebinarProvider .StreamFile( id: id, fileId: fileId ); // return result; } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Download Specified File of Specified Webinar ... /// /// /// /// [HttpGet("{id}/Files/{fileId}/Download")] public virtual async Task DownloadFile( [FromRoute] Guid id, [FromRoute] Guid fileId ) { // // Do ... try { // var result = await WebinarProvider .DownloadFile( id: id, fileId: fileId ); // return result; } 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 AttachTags( [FromRoute] Guid id, [FromQuery] string tags ) { // // Do ... try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var tagsList = tags.ParseListString(); await WebinarProvider .AttachTags( 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 DetachTags( [FromRoute] Guid id, [FromQuery] string tags ) { // // Do ... try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var tagsList = tags.ParseListString(); await WebinarProvider .DetachTags( id: id, tags: tagsList, userInfo: userInfo, connectionId: connectionId ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Retrieve Specified Webinars Tags ... /// /// /// [HttpGet("{id}/Tags")] public virtual async Task>> GetTags( [FromRoute] Guid id ) { // // Do ... try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); // await WebinarProvider .GetTags( id: id, userInfo: userInfo ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion } }