WorkspaceService.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Principal;
  5. using System.Threading.Tasks;
  6. using BO.AppServer.Business.Exceptions;
  7. using BO.AppServer.Business.Mapper;
  8. using BO.AppServer.Business.Services.Base;
  9. using BO.AppServer.Data.Entity;
  10. using BO.AppServer.Metadata.Dto;
  11. using BO.AppServer.Metadata.Enums;
  12. using Microsoft.AspNetCore.Identity;
  13. using Microsoft.Extensions.Logging;
  14. using Quadarax.Foundation.Core.Data.Interface;
  15. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  16. namespace BO.AppServer.Business.Services
  17. {
  18. public class WorkspaceService: UserContextService
  19. {
  20. #region *** Private fields ***
  21. private readonly WorkspaceRepo _repoWorkspace;
  22. private readonly BillingPlanRepo _repoBillingPlan;
  23. #endregion
  24. #region *** Constructor ***
  25. public WorkspaceService(WorkspaceRepo repoWorkspace,
  26. BillingPlanRepo repoBillingPlan,
  27. UserRepo repoUser,
  28. UserManager<IdentityUser> userManager,
  29. IPrincipal currentPrincipal,
  30. ILoggerFactory logger) : base(repoUser, userManager, currentPrincipal, logger)
  31. {
  32. _repoWorkspace = repoWorkspace ?? throw new ArgumentNullException(nameof(repoWorkspace));
  33. _repoBillingPlan = repoBillingPlan ?? throw new ArgumentNullException(nameof(repoBillingPlan));
  34. }
  35. #endregion
  36. #region *** Public Operations ***
  37. public async Task<ResultValueDto<WorkspaceRDto>> CreateWorkspaceAsync(WorkspaceCDto workspace)
  38. {
  39. var billingPlan = _repoBillingPlan.GetByCode(workspace.BillingPlanCode);
  40. if (billingPlan == null)
  41. {
  42. var exc = new EntityNotExistsException(typeof(BillingPlan), workspace.BillingPlanCode, 0);
  43. Log.LogWarning(exc.Message);
  44. throw exc;
  45. }
  46. var owner = _repoUser.GetByName(workspace.AssignedUserName);
  47. if (owner == null)
  48. {
  49. var exc = new EntityNotExistsException(typeof(User), workspace.AssignedUserName, 0);
  50. Log.LogWarning(exc.Message);
  51. throw exc;
  52. }
  53. var newWorkspace = _repoWorkspace.NewWorkspace(workspace.Name, billingPlan,true, owner,GetCurrentUser());
  54. _repoWorkspace.Commit();
  55. return new ResultValueDto<WorkspaceRDto>(newWorkspace.Map<Workspace, WorkspaceRDto>());
  56. }
  57. public async Task<ResultsValueDto<WorkspaceRDto>> GetWorkspacesAsync(WorkspaceScopeEnums scope, PagingDto paging,string ownerUserName)
  58. {
  59. List<Workspace> workspaces = null;
  60. switch (scope)
  61. {
  62. case WorkspaceScopeEnums.All:
  63. workspaces = _repoWorkspace.Query(paging).ToList();
  64. break;
  65. case WorkspaceScopeEnums.Empty:
  66. workspaces = _repoWorkspace.GetAllEmpty(paging).ToList();
  67. break;
  68. case WorkspaceScopeEnums.NotEmpty:
  69. workspaces = _repoWorkspace.GetAllNonEmpty(paging).ToList();
  70. break;
  71. case WorkspaceScopeEnums.My:
  72. var owner = _repoUser.GetByName(ownerUserName);
  73. if (owner == null)
  74. {
  75. var exc = new EntityNotExistsException(typeof(User), ownerUserName, 0);
  76. Log.LogWarning(exc.Message);
  77. throw exc;
  78. }
  79. workspaces = _repoWorkspace.GetAllByOwner(owner, paging).ToList();
  80. break;
  81. }
  82. return new ResultsValueDto<WorkspaceRDto>(workspaces.MapList<Workspace, WorkspaceRDto>());
  83. }
  84. #endregion
  85. }
  86. }