WorkspaceService.cs 4.9 KB

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