WorkspaceService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Principal;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using BO.AppServer.Business.Exceptions;
  8. using BO.AppServer.Business.Mapper;
  9. using BO.AppServer.Business.Services.Base;
  10. using BO.AppServer.Data.Entity;
  11. using BO.AppServer.Metadata.Dto;
  12. using BO.AppServer.Metadata.Enums;
  13. using Microsoft.AspNetCore.Identity;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. using Quadarax.Foundation.Core.Data;
  17. using Quadarax.Foundation.Core.Data.Interface;
  18. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  19. using Quadarax.Foundation.Core.Exceptions;
  20. namespace BO.AppServer.Business.Services
  21. {
  22. public class WorkspaceService: UserContextService
  23. {
  24. #region *** Private fields ***
  25. private readonly WorkspaceRepo _repoWorkspace;
  26. private readonly BillingPlanRepo _repoBillingPlan;
  27. #endregion
  28. #region *** Constructor ***
  29. public WorkspaceService(WorkspaceRepo repoWorkspace,
  30. BillingPlanRepo repoBillingPlan,
  31. IOptions<Configuration.Configuration> config,
  32. UserRepo repoUser,
  33. UserManager<IdentityUser> userManager,
  34. IPrincipal currentPrincipal,
  35. ILoggerFactory logger) : base(config, repoUser, userManager, currentPrincipal, logger)
  36. {
  37. _repoWorkspace = repoWorkspace ?? throw new ArgumentNullException(nameof(repoWorkspace));
  38. _repoBillingPlan = repoBillingPlan ?? throw new ArgumentNullException(nameof(repoBillingPlan));
  39. }
  40. #endregion
  41. #region *** Public Operations ***
  42. public async Task<ResultValueDto<WorkspaceRDto>> CreateWorkspaceAsync(WorkspaceCDto workspace)
  43. {
  44. var billingPlan = _repoBillingPlan.GetByCode(workspace.BillingPlanCode);
  45. if (billingPlan == null)
  46. {
  47. var exc = new EntityNotExistsException(typeof(BillingPlan), workspace.BillingPlanCode, 0);
  48. Log.LogWarning(exc.Message);
  49. throw exc;
  50. }
  51. var owner = _repoUser.GetByName(workspace.AssignedUserName);
  52. if (owner == null)
  53. {
  54. var exc = new EntityNotExistsException(typeof(User), workspace.AssignedUserName, 0);
  55. Log.LogWarning(exc.Message);
  56. throw exc;
  57. }
  58. var current = GetCurrentUser();
  59. var newWorkspace = _repoWorkspace.CreateWorkspace(workspace.Name, billingPlan, workspace.Apipassword, true, owner,current);
  60. _repoWorkspace.Commit();
  61. Log.LogInformation($"Workspace '{newWorkspace.Name}' [API-KEY:{newWorkspace.Apikey}] created and appends to users: {string.Join(",", newWorkspace.UserWorkspaces.Select(x=>x.User.Name))}");
  62. return new ResultValueDto<WorkspaceRDto>(newWorkspace.Map<Workspace, WorkspaceRDto>());
  63. }
  64. public async Task<ResultsValueDto<WorkspaceRDto>> GetWorkspacesAsync(WorkspaceScopeEnums scope, PagingDto paging,string ownerUserName)
  65. {
  66. List<Workspace> workspaces = null;
  67. switch (scope)
  68. {
  69. case WorkspaceScopeEnums.All:
  70. workspaces = _repoWorkspace.Query(paging).ToList();
  71. break;
  72. case WorkspaceScopeEnums.Empty:
  73. workspaces = _repoWorkspace.GetAllEmpty(paging).ToList();
  74. break;
  75. case WorkspaceScopeEnums.NotEmpty:
  76. workspaces = _repoWorkspace.GetAllNonEmpty(paging).ToList();
  77. break;
  78. case WorkspaceScopeEnums.My:
  79. var owner = _repoUser.GetByName(ownerUserName);
  80. if (owner == null)
  81. {
  82. var exc = new EntityNotExistsException(typeof(User), ownerUserName, 0);
  83. Log.LogWarning(exc.Message);
  84. throw exc;
  85. }
  86. workspaces = _repoWorkspace.GetAllByOwner(owner, paging).ToList();
  87. break;
  88. }
  89. return new ResultsValueDto<WorkspaceRDto>(workspaces.MapList<Workspace, WorkspaceRDto>());
  90. }
  91. public async Task<long> GetWorkspaceIdByAPIAccess(string apiKey, string apiPassword, string currentUserName)
  92. {
  93. var owner = string.IsNullOrEmpty(currentUserName) ? GetCurrentUser() : _repoUser.GetByName(currentUserName);
  94. if (owner == null)
  95. {
  96. var exc = new EntityNotExistsException(typeof(User), currentUserName, 0);
  97. Log.LogWarning(exc.Message);
  98. throw exc;
  99. }
  100. if (!Guid.TryParse(apiKey, out var apiKeyGuid))
  101. apiKeyGuid = Guid.Empty;
  102. var workspace = _repoWorkspace.GetAllByOwner(owner, new Paging())
  103. .FirstOrDefault(x => x.Apikey == apiKeyGuid && x.Apipassword == apiPassword);
  104. if (workspace == null)
  105. {
  106. var exc = new CodeException(1400, $"Workspace with APIKey '{apiKey}' not exists or API password is not valid!");
  107. Log.LogWarning(exc.Message + $"\nUser Context:'{owner.Name}' [{owner.Id}]");
  108. throw exc;
  109. }
  110. return workspace.Id;
  111. }
  112. #endregion
  113. }
  114. }