diff --git a/Controllers/XFileServiceControllerBase.cs b/Controllers/XFileServiceControllerBase.cs new file mode 100644 index 0000000..4686b2f --- /dev/null +++ b/Controllers/XFileServiceControllerBase.cs @@ -0,0 +1,597 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xFileService.Interfaces; +using xFileService.Models.Dtos; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; +using xModels.Dtos; +using XFileDto = xFileService.Models.Dtos.XFileDto; + +namespace xFileService.Controllers +{ + /// + /// a Controller base Class which implement based Actions to Provides File Provider Access ... + /// + public abstract class XFileServiceControllerBase : XIBaseProviderController, IXFileServiceControllerActions + { + // + #region Props ... + public IXFileProvider FileProvider { get; } + #endregion + + // + #region Constructor ... + protected XFileServiceControllerBase( + ILogger logger, + IXFileProvider fileProvider, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider + ) : base( + logger, + appConfiguration, + identityProvider, + validationProvider + ) + { + // + FileProvider = fileProvider; + } + #endregion + + // + #region Tags ... + /// + /// Attach Tag to Specified Model ... + /// + /// + /// + /// + [HttpGet("Tags/Attach")] + public async Task TagAttach( + [FromQuery] string tag, + [FromQuery] 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; + + // + await FileProvider.TagAttach( + id: id, + tag: tag, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Detach Tag From Specified Model ... + /// + /// + /// + /// + [HttpGet("Tags/Detach")] + public async Task TagDetach( + [FromQuery] string tag, + [FromQuery] 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; + + // + await FileProvider.TagDetach( + id: id, + tag: tag, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Attach Tags to Specified Model ... + /// + /// + /// + /// + [HttpGet("Tags/AttachMany")] + public async Task TagsAttach( + [FromQuery] string tags, + [FromQuery] Guid id + ) + { + // + // Do ... + try + { + // + var tagsList = tags.ParseListString(); + + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin"); + var userId = userInfo.UserId; + + // + await FileProvider.TagsAttach( + id: id, + tags: tagsList, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Detach Tags From Specified Model ... + /// + /// + /// + /// + [HttpGet("Tags/DetachMany")] + public async Task TagsDetach( + [FromQuery] string tags, + [FromQuery] Guid id + ) + { + // + // Do ... + try + { + // + var tagsList = tags.ParseListString(); + + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin"); + var userId = userInfo.UserId; + + // + await FileProvider.TagsDetach( + id: id, + tags: tagsList, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Tools ... + /// + /// Stream Specified File ... + /// + /// + /// + [HttpGet("{id}/Stream/ById")] + public async Task Stream( + [FromRoute] Guid id + ) + { + // + // Do ... + try + { + // + var result = await FileProvider.Stream(id); + + // + return result; + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Stream Specified File ... + /// + /// + /// + [HttpGet("{name}/Stream/ByName")] + public async Task> Stream( + [FromRoute] string name + ) + { + // + // Do ... + try + { + // + var result = await FileProvider.Stream(name); + + // + return result; + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Download Specified File ... + /// + /// + /// + [HttpGet("{id}/Download/ById")] + public async Task Download( + [FromRoute] Guid id + ) + { + // + // Do ... + try + { + // + var result = await FileProvider.Download(id); + + // + return result; + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Download Specified File ... + /// + /// + /// + [HttpGet("{name}/Download/ByName")] + public async Task Download( + [FromRoute] string name + ) + { + // + // Do ... + try + { + // + var result = await FileProvider.Download(name); + + // + return result; + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Upload Files ... + /// + /// + /// + [HttpPost("Upload")] + [RequestSizeLimit(966_367_641)] + public async Task>> Upload( + [FromForm] IFormFileCollection files + ) + { + // + // 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; + + // + var result = await FileProvider.Upload( + files: files, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Remove Specified Files ... + /// + /// + /// + /// [HttpDelete("Remove")] + [HttpDelete("Remove")] + public async Task>> Remove( + [FromQuery] string ids + ) { + // + // 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; + + // + var result = await FileProvider.Remove( + ids: ids, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Model ... + /// + /// Get Specified File Model ... + /// + /// + /// + /// + [HttpGet("{id}")] + public async Task> Get( + [FromRoute] Guid id + ) { + // + // Do ... + try + { + // + var result = await FileProvider.Get(id); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Get All Exists File Models ... + /// + /// + /// + [HttpGet("All")] + public async Task>> GetAll() { + // + // Do ... + try + { + // + var result = await FileProvider.GetAll(); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// retrieve Entities based on XQuery Pagination structure ... + /// + /// + /// + [HttpGet("Query")] + public async Task>> Query( + [FromQuery] XQuery query + ) { + // + // Do ... + try + { + // + var result = await FileProvider.Query(query); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// retrieve Owned Entities based on XQuery Pagination structure ... + /// + /// + /// + [HttpGet("Owned/Query")] + public async Task>> QueryOwned( + [FromQuery] XQuery query + ) { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + + // + var result = await FileProvider.QueryOwned( + query: query, + userInfo: userInfo + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// count all exists Entities ... + /// + /// + [HttpGet("Count")] + public async Task> Count() { + // + // Do ... + try + { + // + var result = await FileProvider.Count(); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Check an Entity exists or not ... + /// + /// + /// + [HttpGet("{id}/IsExists")] + public async Task> IsExists( + [FromRoute] Guid id + ) { + // + // Do ... + try + { + // + var result = await FileProvider.IsExists(id); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + } +} \ No newline at end of file diff --git a/Interfaces/IXFileProvider.cs b/Interfaces/IXFileProvider.cs index c5f8757..4d0a869 100644 --- a/Interfaces/IXFileProvider.cs +++ b/Interfaces/IXFileProvider.cs @@ -62,11 +62,13 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task TagAttach( string tag, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -75,11 +77,13 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task TagDetach( string tag, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -88,11 +92,13 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task TagsAttach( IEnumerable tags, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -101,11 +107,13 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task TagsDetach( IEnumerable tags, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -113,10 +121,12 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task TagsDetach( Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); #endregion @@ -155,10 +165,12 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task> Upload( IFormFileCollection files, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -166,10 +178,12 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task> Remove( string ids, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -220,12 +234,14 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task AddReference( string providedFor, TKey forProvidedId, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -235,12 +251,14 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task RemoveReference( string providedFor, TKey forProvidedId, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); #endregion @@ -287,6 +305,17 @@ namespace xFileService.Interfaces XQuery query ); + /// + /// retrieve Owned Entities based on XQuery Pagination structure ... + /// + /// + /// + /// + Task> QueryOwned( + XQuery query, + XUserClaimsInfoDto userInfo = null + ); + /// /// retrieve Entities based on XQuery Pagination structure by providing a Conditional Expression ... /// @@ -298,17 +327,32 @@ namespace xFileService.Interfaces XQuery query ); + /// + /// retrieve Owned Entities based on XQuery Pagination structure by providing a Conditional Expression ... + /// + /// + /// + /// + /// + Task> ConditionalQueryOwned( + Expression> whereClause, + XQuery query, + XUserClaimsInfoDto userInfo = null + ); + /// /// Update an Entity values ... /// /// /// /// + /// /// Task Update( Guid id, XFileDto item, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// @@ -316,10 +360,12 @@ namespace xFileService.Interfaces /// /// /// + /// /// Task Remove( Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ); /// diff --git a/Interfaces/IXFileServiceControllerActions.cs b/Interfaces/IXFileServiceControllerActions.cs new file mode 100644 index 0000000..a56e3b1 --- /dev/null +++ b/Interfaces/IXFileServiceControllerActions.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using xFileService.Models.Dtos; +using xModels.Dtos; +using XFileDto = xFileService.Models.Dtos.XFileDto; + +namespace xFileService.Interfaces +{ + public interface IXFileServiceControllerActions + { + // + #region Tags ... + /// + /// Attach Tag to Specified Model ... + /// + /// + /// + /// + Task TagAttach( + string tag, + Guid id + ); + + /// + /// Detach Tag From Specified Model ... + /// + /// + /// + /// + Task TagDetach( + string tag, + Guid id + ); + + /// + /// Attach Tags to Specified Model ... + /// + /// + /// + /// + Task TagsAttach( + string tags, + Guid id + ); + + /// + /// Detach Tags From Specified Model ... + /// + /// + /// + /// + Task TagsDetach( + string tags, + Guid id + ); + #endregion + + // + #region Tools ... + /// + /// Stream Specified File ... + /// + /// + /// + Task Stream(Guid id); + + /// + /// Stream Specified File ... + /// + /// + /// + Task> Stream(string fileName); + + /// + /// Download Specified File ... + /// + /// + /// + Task Download(Guid id); + + /// + /// Download Specified File ... + /// + /// + /// + Task Download(string fileName); + + /// + /// Upload Files ... + /// + /// + /// + Task>> Upload( + IFormFileCollection files + ); + + /// + /// Remove Specified Files ... + /// + /// + /// + Task>> Remove( + string ids + ); + #endregion + + // + #region Model ... + /// + /// Get Specified File Model ... + /// + /// + /// + /// + Task> Get( + Guid id + ); + + /// + /// Get All Exists File Models ... + /// + /// + /// + Task>> GetAll(); + + /// + /// retrieve Entities based on XQuery Pagination structure ... + /// + /// + /// + Task>> Query( + XQuery query + ); + + /// + /// retrieve Owned Entities based on XQuery Pagination structure ... + /// + /// + /// + Task>> QueryOwned( + XQuery query + ); + + /// + /// count all exists Entities ... + /// + /// + Task> Count(); + + /// + /// Check an Entity exists or not ... + /// + /// + /// + Task> IsExists( + Guid id + ); + #endregion + } +} \ No newline at end of file diff --git a/Providers/XFileProvider.cs b/Providers/XFileProvider.cs index 5b02c6f..c2469e3 100644 --- a/Providers/XFileProvider.cs +++ b/Providers/XFileProvider.cs @@ -24,6 +24,7 @@ using System.IO; using Microsoft.Extensions.FileProviders; using Microsoft.AspNetCore.StaticFiles; using System.Linq; +using xPushService.Constants; namespace xFileService.Providers { @@ -217,11 +218,14 @@ namespace xFileService.Providers /// /// /// + /// + /// /// public async Task TagAttach( string tag, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -246,7 +250,12 @@ namespace xFileService.Providers // try { - await TagProvider.Attach(tag, id); + // + await TagProvider.Attach( + tag, + id, + connectionId + ); } catch { } } @@ -257,11 +266,14 @@ namespace xFileService.Providers /// /// /// + /// + /// /// public async Task TagDetach( string tag, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -285,7 +297,12 @@ namespace xFileService.Providers // try { - await TagProvider.Detach(tag, id); + // + await TagProvider.Detach( + tag, + id, + connectionId + ); } catch { } } @@ -296,11 +313,14 @@ namespace xFileService.Providers /// /// /// + /// + /// /// public async Task TagsAttach( IEnumerable tags, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -328,7 +348,13 @@ namespace xFileService.Providers // foreach (var tag in tags) { - await TagAttach(tag, id); + // + await TagAttach( + tag, + id, + userInfo, + connectionId + ); } } catch { } @@ -340,11 +366,14 @@ namespace xFileService.Providers /// /// /// + /// + /// /// public async Task TagsDetach( IEnumerable tags, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -372,7 +401,13 @@ namespace xFileService.Providers // foreach (var tag in tags) { - await TagDetach(tag, id); + // + await TagDetach( + tag, + id, + userInfo, + connectionId + ); } } catch { } @@ -383,10 +418,13 @@ namespace xFileService.Providers /// Detach all Attached Tags for Specified File ... /// /// + /// + /// /// public async Task TagsDetach( Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -573,10 +611,12 @@ namespace xFileService.Providers /// /// /// + /// /// public async Task> Upload( IFormFileCollection files, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -631,6 +671,14 @@ namespace xFileService.Providers // if (!entity.IsNullOrDefault()) { + // + await SendPush( + action: XBaseEntityHubAction.Add.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + + // entities.Add(entity); } } @@ -649,10 +697,13 @@ namespace xFileService.Providers /// Remove Specified Files ... /// /// + /// + /// /// public async Task> Remove( string ids, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -733,8 +784,15 @@ namespace xFileService.Providers // // Remove File Entty ... - await FileRepository.RemoveAsync(model.Id); + var entity = await FileRepository.RemoveAsync(model.Id); result.Add(model.Id); + + // + await SendPush( + action: XBaseEntityHubAction.Delete.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); } // @@ -744,7 +802,10 @@ namespace xFileService.Providers // foreach (var id in result) { - await TagsDetach(id); + // + // Here we send Null Connection ID for Preventing Update Push ... + // since we Remove Entity ... + await TagsDetach(id, userInfo, null); } } @@ -971,7 +1032,7 @@ namespace xFileService.Providers // var identifier = GetIdentifier( - providedFor: providedFor, + providedFor: providedFor, forProvidedId: forProvidedId ); var result = @@ -990,12 +1051,14 @@ namespace xFileService.Providers /// /// /// + /// /// public async Task AddReference( string providedFor, TKey forProvidedId, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -1015,7 +1078,7 @@ namespace xFileService.Providers // Check Has Reference or not ... isValid = await IsProvidedFor( providedFor: providedFor, - forProvidedId: forProvidedId, + forProvidedId: forProvidedId, id: id ); if (!isValid) @@ -1050,7 +1113,8 @@ namespace xFileService.Providers dto = await Update( id: dto.Id, item: dto, - userInfo: userInfo + userInfo: userInfo, + connectionId: connectionId ); isValid = !dto.IsNullOrDefault(); if (!isValid) @@ -1067,12 +1131,14 @@ namespace xFileService.Providers /// /// /// + /// /// public async Task RemoveReference( string providedFor, TKey forProvidedId, Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -1119,7 +1185,7 @@ namespace xFileService.Providers // var identifier = GetIdentifier( - providedFor: providedFor, + providedFor: providedFor, forProvidedId: forProvidedId ); dto.References = dto.References @@ -1128,7 +1194,8 @@ namespace xFileService.Providers dto = await Update( id: dto.Id, item: dto, - userInfo: userInfo + userInfo: userInfo, + connectionId: connectionId ); isValid = !dto.IsNullOrDefault(); if (!isValid) @@ -1159,14 +1226,14 @@ namespace xFileService.Providers } // - var entitiy = await FileRepository.GetAsync(id); - if (entitiy.IsNullOrDefault()) + var entity = await FileRepository.GetAsync(id); + if (entity.IsNullOrDefault()) { XException.NotFound.Throw(); } // - var result = await ToDto(entitiy); + var result = await ToDto(entity); if (result.IsNullOrDefault()) { XException.ActionFailed.Throw(); @@ -1267,6 +1334,37 @@ namespace xFileService.Providers return result; } + /// + /// retrieve Owned Entities based on XQuery Pagination structure ... + /// + /// + /// + /// + public async Task> QueryOwned( + XQuery query, + XUserClaimsInfoDto userInfo = null + ) + { + // + // Validate ... + var isValid = !userInfo.IsNullOrDefault(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Create Where Clause ... + Expression> whereClause = e => e.OwnerId == userInfo.UserId; + var result = await ConditionalQuery( + query: query, + whereClause: whereClause + ); + + // + return result; + } + /// /// retrieve Entities based on XQuery Pagination structure by providing a Conditional Expression ... /// @@ -1295,17 +1393,60 @@ namespace xFileService.Providers return result; } + /// + /// retrieve Owned Entities based on XQuery Pagination structure by providing a Conditional Expression ... + /// + /// + /// + /// + /// + public async Task> ConditionalQueryOwned( + Expression> whereClause, + XQuery query, + XUserClaimsInfoDto userInfo = null + ) + { + // + // Validate ... + var isValid = !whereClause.IsNull() && + !userInfo.IsNullOrDefault(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Compile Where Clause ... + var whereFunc = whereClause.Compile(); + + // + // Create new Where Clause ... + Expression> condition = + e => whereFunc(e) && e.OwnerId == userInfo.UserId; + + // + var result = await ConditionalQuery( + query: query, + whereClause: condition + ); + + // + return result; + } + /// /// Update an Entity values ... /// /// /// /// + /// /// public async Task Update( Guid id, XFileDto item, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -1320,7 +1461,7 @@ namespace xFileService.Providers } // - // Check Entitiy Exists and Retrieve it ... + // Check entity Exists and Retrieve it ... isValid = await IsExists(id); if (!isValid) { @@ -1375,6 +1516,17 @@ namespace xFileService.Providers // Converts to Dto ... var result = await ToDto(entity); + // + if (!result.IsNullOrDefault()) + { + // + await SendPush( + action: XBaseEntityHubAction.Update.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + } + // return result; } @@ -1384,10 +1536,12 @@ namespace xFileService.Providers /// /// /// + /// /// public async Task Remove( Guid id, - XUserClaimsInfoDto userInfo = null + XUserClaimsInfoDto userInfo = null, + string connectionId = null ) { // @@ -1428,14 +1582,14 @@ namespace xFileService.Providers } // - var entitiy = await FileRepository.RemoveAsync(id); - if (entitiy.IsNullOrDefault()) + var entity = await FileRepository.RemoveAsync(id); + if (entity.IsNullOrDefault()) { XException.ActionFailed.Throw(); } // - result = await ToDto(entitiy); + result = await ToDto(entity); if (result.IsNullOrDefault()) { XException.ActionFailed.Throw(); @@ -1445,6 +1599,13 @@ namespace xFileService.Providers // Removing Tags ... await TagProvider.RemoveTagsFor(result.Id); + // + await SendPush( + action: XBaseEntityHubAction.Delete.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + // return result; } @@ -1470,5 +1631,57 @@ namespace xFileService.Providers return await FileRepository.IsExistsAsync(id); } #endregion + + // + #region Hub Actions ... + /// + /// Send Custom Push Message ... + /// + /// + /// + /// + /// + public async Task SendPush( + string action, + string payLoad, + string connectionId = null + ) + { + // + var actions = new List + { + XBaseEntityHubAction.Add.GetStringValue(), + XBaseEntityHubAction.Update.GetStringValue(), + XBaseEntityHubAction.Delete.GetStringValue(), + XBaseEntityHubAction.AddMany.GetStringValue(), + XBaseEntityHubAction.DeleteMany.GetStringValue(), + XBaseEntityHubAction.UpdateMany.GetStringValue(), + XBaseEntityHubAction.AddOrUpdate.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); + } + try + { + await clients.SendAsync(action, payLoad, connectionId); + } + catch { } + } + #endregion } } \ No newline at end of file