using System; using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Threading.Tasks; using BO.AppServer.Business.Exceptions; using BO.AppServer.Business.Mapper; using BO.AppServer.Business.Services.Base; using BO.AppServer.Data.Entity; using BO.AppServer.Metadata.Dto; using BO.AppServer.Metadata.Enums; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using Quadarax.Foundation.Core.Data.Interface; using Quadarax.Foundation.Core.Data.Interface.Entity.Dto; namespace BO.AppServer.Business.Services { public class WorkspaceService: UserContextService { #region *** Private fields *** private readonly WorkspaceRepo _repoWorkspace; private readonly BillingPlanRepo _repoBillingPlan; #endregion #region *** Constructor *** public WorkspaceService(WorkspaceRepo repoWorkspace, BillingPlanRepo repoBillingPlan, UserRepo repoUser, UserManager userManager, IPrincipal currentPrincipal, ILoggerFactory logger) : base(repoUser, userManager, currentPrincipal, logger) { _repoWorkspace = repoWorkspace ?? throw new ArgumentNullException(nameof(repoWorkspace)); _repoBillingPlan = repoBillingPlan ?? throw new ArgumentNullException(nameof(repoBillingPlan)); } #endregion #region *** Public Operations *** public async Task> CreateWorkspaceAsync(WorkspaceCDto workspace) { var billingPlan = _repoBillingPlan.GetByCode(workspace.BillingPlanCode); if (billingPlan == null) { var exc = new EntityNotExistsException(typeof(BillingPlan), workspace.BillingPlanCode, 0); Log.LogWarning(exc.Message); throw exc; } var owner = _repoUser.GetByName(workspace.AssignedUserName); if (owner == null) { var exc = new EntityNotExistsException(typeof(User), workspace.AssignedUserName, 0); Log.LogWarning(exc.Message); throw exc; } var newWorkspace = _repoWorkspace.NewWorkspace(workspace.Name, billingPlan,true, owner,GetCurrentUser()); _repoWorkspace.Commit(); return new ResultValueDto(newWorkspace.Map()); } public async Task> GetWorkspacesAsync(WorkspaceScopeEnums scope, PagingDto paging,string ownerUserName) { List workspaces = null; switch (scope) { case WorkspaceScopeEnums.All: workspaces = _repoWorkspace.Query(paging).ToList(); break; case WorkspaceScopeEnums.Empty: workspaces = _repoWorkspace.GetAllEmpty(paging).ToList(); break; case WorkspaceScopeEnums.NotEmpty: workspaces = _repoWorkspace.GetAllNonEmpty(paging).ToList(); break; case WorkspaceScopeEnums.My: var owner = _repoUser.GetByName(ownerUserName); if (owner == null) { var exc = new EntityNotExistsException(typeof(User), ownerUserName, 0); Log.LogWarning(exc.Message); throw exc; } workspaces = _repoWorkspace.GetAllByOwner(owner, paging).ToList(); break; } return new ResultsValueDto(workspaces.MapList()); } #endregion } }