using System; using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Threading; 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 Microsoft.Extensions.Options; using Quadarax.Foundation.Core.Data; using Quadarax.Foundation.Core.Data.Interface; using Quadarax.Foundation.Core.Data.Interface.Entity.Dto; using Quadarax.Foundation.Core.Exceptions; 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, IOptions config, UserRepo repoUser, UserManager userManager, IPrincipal currentPrincipal, ILoggerFactory logger) : base(config, 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 current = GetCurrentUser(); var newWorkspace = _repoWorkspace.CreateWorkspace(workspace.Name, billingPlan, workspace.Apipassword, true, owner,current); _repoWorkspace.Commit(); Log.LogInformation($"Workspace '{newWorkspace.Name}' [API-KEY:{newWorkspace.Apikey}] created and appends to users: {string.Join(",", newWorkspace.UserWorkspaces.Select(x=>x.User.Name))}"); 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()); } public async Task GetWorkspaceIdByAPIAccess(string apiKey, string apiPassword, string currentUserName) { var owner = string.IsNullOrEmpty(currentUserName) ? GetCurrentUser() : _repoUser.GetByName(currentUserName); if (owner == null) { var exc = new EntityNotExistsException(typeof(User), currentUserName, 0); Log.LogWarning(exc.Message); throw exc; } if (!Guid.TryParse(apiKey, out var apiKeyGuid)) apiKeyGuid = Guid.Empty; var workspace = _repoWorkspace.GetAllByOwner(owner, new Paging()) .FirstOrDefault(x => x.Apikey == apiKeyGuid && x.Apipassword == apiPassword); if (workspace == null) { var exc = new CodeException(1400, $"Workspace with APIKey '{apiKey}' not exists or API password is not valid!"); Log.LogWarning(exc.Message + $"\nUser Context:'{owner.Name}' [{owner.Id}]"); throw exc; } return workspace.Id; } #endregion } }