WorkspaceService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Data.Repository;
  12. using BO.AppServer.Metadata.Dto;
  13. using BO.AppServer.Metadata.Enums;
  14. using Microsoft.AspNetCore.Identity;
  15. using Microsoft.Extensions.Logging;
  16. using Microsoft.Extensions.Options;
  17. using Quadarax.Foundation.Core.Data;
  18. using Quadarax.Foundation.Core.Data.Interface;
  19. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  20. using Quadarax.Foundation.Core.Exceptions;
  21. namespace BO.AppServer.Business.Services
  22. {
  23. public class WorkspaceService: UserContextService
  24. {
  25. #region *** Private fields ***
  26. private readonly WorkspaceRepo _repoWorkspace;
  27. private readonly BillingPlanRepo _repoBillingPlan;
  28. private readonly WorkspaceBillingRepo _repoWorkspaceBilling;
  29. #endregion
  30. #region *** Constructor ***
  31. public WorkspaceService(WorkspaceRepo repoWorkspace,
  32. BillingPlanRepo repoBillingPlan,
  33. WorkspaceBillingRepo repoWorkspaceBilling,
  34. IOptions<Configuration.Configuration> config,
  35. UserRepo repoUser,
  36. UserManager<IdentityUser> userManager,
  37. IPrincipal currentPrincipal,
  38. ILoggerFactory logger) : base(config, repoUser, userManager, currentPrincipal, logger)
  39. {
  40. _repoWorkspace = repoWorkspace ?? throw new ArgumentNullException(nameof(repoWorkspace));
  41. _repoBillingPlan = repoBillingPlan ?? throw new ArgumentNullException(nameof(repoBillingPlan));
  42. _repoWorkspaceBilling = repoWorkspaceBilling ?? throw new ArgumentNullException(nameof(repoWorkspaceBilling));
  43. }
  44. #endregion
  45. #region *** Public Operations ***
  46. public async Task<ResultValueDto<WorkspaceRDto>> CreateWorkspaceAsync(WorkspaceCDto workspace)
  47. {
  48. var billingPlan = _repoBillingPlan.GetByCode(workspace.BillingPlanCode);
  49. if (billingPlan == null)
  50. {
  51. var exc = new EntityNotExistsException(typeof(BillingPlan), workspace.BillingPlanCode, 0);
  52. Log.LogWarning(exc.Message);
  53. throw exc;
  54. }
  55. var owner = _repoUser.GetByName(workspace.AssignedUserName);
  56. if (owner == null)
  57. {
  58. var exc = new EntityNotExistsException(typeof(User), workspace.AssignedUserName, 0);
  59. Log.LogWarning(exc.Message);
  60. throw exc;
  61. }
  62. var current = GetCurrentUser();
  63. var newWorkspace = _repoWorkspace.CreateWorkspace(workspace.Name, billingPlan, workspace.Apipassword, true, owner,current);
  64. _repoWorkspace.Commit();
  65. Log.LogInformation($"Workspace '{newWorkspace.Name}' [API-KEY:{newWorkspace.Apikey}] created and appends to users: {string.Join(",", newWorkspace.UserWorkspaces.Select(x=>x.User.Name))}");
  66. return new ResultValueDto<WorkspaceRDto>(newWorkspace.Map<Workspace, WorkspaceRDto>());
  67. }
  68. public async Task<ResultsValueDto<WorkspaceRDto>> GetWorkspacesAsync(WorkspaceScopeEnums scope, PagingDto paging,string ownerUserName)
  69. {
  70. List<Workspace> workspaces = null;
  71. switch (scope)
  72. {
  73. case WorkspaceScopeEnums.All:
  74. workspaces = _repoWorkspace.Query(paging).ToList();
  75. break;
  76. case WorkspaceScopeEnums.Empty:
  77. workspaces = _repoWorkspace.GetAllEmpty(paging).ToList();
  78. break;
  79. case WorkspaceScopeEnums.NotEmpty:
  80. workspaces = _repoWorkspace.GetAllNonEmpty(paging).ToList();
  81. break;
  82. case WorkspaceScopeEnums.My:
  83. var owner = _repoUser.GetByName(ownerUserName);
  84. if (owner == null)
  85. {
  86. var exc = new EntityNotExistsException(typeof(User), ownerUserName, 0);
  87. Log.LogWarning(exc.Message);
  88. throw exc;
  89. }
  90. workspaces = _repoWorkspace.GetAllByOwner(owner, paging).ToList();
  91. break;
  92. }
  93. return new ResultsValueDto<WorkspaceRDto>(workspaces.MapList<Workspace, WorkspaceRDto>());
  94. }
  95. public async Task<long> GetWorkspaceIdByAPIAccess(string apiKey, string apiPassword, string currentUserName)
  96. {
  97. var owner = string.IsNullOrEmpty(currentUserName) ? GetCurrentUser() : _repoUser.GetByName(currentUserName);
  98. if (owner == null)
  99. {
  100. var exc = new EntityNotExistsException(typeof(User), currentUserName, 0);
  101. Log.LogWarning(exc.Message);
  102. throw exc;
  103. }
  104. if (!Guid.TryParse(apiKey, out var apiKeyGuid))
  105. apiKeyGuid = Guid.Empty;
  106. var workspace = _repoWorkspace.GetAllByOwner(owner, new Paging())
  107. .FirstOrDefault(x => x.Apikey == apiKeyGuid && x.Apipassword == apiPassword);
  108. if (workspace == null)
  109. {
  110. var exc = new CodeException(1400, $"Workspace with APIKey '{apiKey}' not exists or API password is not valid!");
  111. Log.LogWarning(exc.Message + $"\nUser Context:'{owner.Name}' [{owner.Id}]");
  112. throw exc;
  113. }
  114. return workspace.Id;
  115. }
  116. public async Task<ResultValueDto<WorkspaceRDto>> GetWorkspace(IdentificationTypeEnum identificationType, string identificationValue)
  117. {
  118. var workspace = identificationType == IdentificationTypeEnum.Id ?
  119. _repoWorkspace.Get(ArgumentAsLong(nameof(identificationValue), identificationValue))
  120. : _repoWorkspace.GetByName(identificationValue);
  121. if (workspace == null)
  122. throw new NoDataException();
  123. var currentBillingPlan = GetCurrentWorkspaceBillingOrCreate(workspace.Id);
  124. // gather rest of data
  125. var result = new ResultValueDto<WorkspaceRDto>(workspace.Map<Workspace, WorkspaceRDto>());
  126. result.Value.CreditAmount = currentBillingPlan.ValueNumber;
  127. result.Value.BillingPlanCode = currentBillingPlan.BillingPlan.Code;
  128. return result;
  129. }
  130. #endregion
  131. #region *** Private Operations ***
  132. private WorkspaceBilling GetCurrentWorkspaceBillingOrCreate(long workspaceId)
  133. {
  134. var currentBillingPlan = _repoWorkspaceBilling.GetByCurrentForWorkspace(workspaceId);
  135. if (currentBillingPlan == null)
  136. {
  137. currentBillingPlan = _repoWorkspaceBilling.CreateForWorkspace(workspaceId,
  138. _repoBillingPlan.GetByCode(_configuration.Business.DefaultBillingPlanCode));
  139. _repoWorkspaceBilling.Commit();
  140. Log.LogWarning($"Default billing plan was not set for workspace [{workspaceId}]. Set default billing plan '{currentBillingPlan.BillingPlan.Code}'.");
  141. }
  142. return currentBillingPlan;
  143. }
  144. #endregion
  145. }
  146. }