public async Task <IActionResult> Create([FromBody] ApplicationCreationRequest request) { try { // Who's logged in? using var session = _documentStore.OpenAsyncSession(); var user = await _platformAdminUserManager.GetByUniqueIdentifierAsync(User.Identity.Name, session); // Which project are we working on? var testMode = TestProjectId.IsValidIdentity(request.ProjectId) && !ProjectId.IsValidIdentity(request.ProjectId); var project = testMode ? await _projectManager.GetTest((TestProjectId)request.ProjectId, session) : await _projectManager.Get((ProjectId)request.ProjectId, session); // Does the user have access to the project? if (!project.AdminIds.Contains(user.Id) && project.OwnerAdminId != user.Id) { throw new ApiException("Seems you are not an admin on this project.", (int)System.Net.HttpStatusCode.Unauthorized); } var registeredApplication = await _applicationHttpClient.CreateApplication(new CreateApplicationModel { Name = project.Name, AuthCallbackUrl = request.AuthCallbackUrl ?? "", //EmailVerificationNotificationEndpointUrl = request.EmailVerificationUrl ?? "", DataUpdateCallbackUrl = request.DataUpdateCallbackUrl ?? "" }); if (string.IsNullOrEmpty(registeredApplication.ApplicationId)) { _logger.LogError("Create application failed. {@request} {@registeredApplication}", request, registeredApplication); throw new ApiException("Creating the application failed."); } var application = request.CreateApplication(registeredApplication); // One application per project, so just replace project.Applications = new List <Core.Entities.Application> { application }; // Save project = await _projectManager.Update(project, session); await session.SaveChangesAsync(); return(Ok(project)); } catch (ApiException ex) { _logger.LogError(ex, "Unable to create application. {@request}", request); // return error message if there was an exception // return BadRequest(new { message = ex.Message }); throw; } }
public async Task <IActionResult> Create([FromBody] CreateProjectRequest request) { var requestName = request.Name.Trim(); if (string.IsNullOrEmpty(requestName) || string.IsNullOrWhiteSpace(requestName)) { return(BadRequest(new { message = "You have to enter a project name." })); } try { _logger.LogInformation("Creating project"); using var session = _documentStore.OpenAsyncSession(); var user = await _platformAdminUserManager.GetByUniqueIdentifierAsync(User.Identity.Name, session); // TODO: Do not create platform on LIVE project when creating the entity. Instead do it after. var platform = await _platformAdminHttpClient.CreatePlatform(new CreatePlatformModel { AuthMechanism = PlatformAuthenticationMechanism.Email, Name = requestName, MaxRating = 5, MinRating = 1, RatingSuccessLimit = 3 }); var application = await _applicationHttpClient.CreateApplication(new CreateApplicationModel { Name = requestName }); var entityToCreate = request.WithOwner(user.Id).ToEntity( Core.Entities.Platform.Create(platform.PlatformId), new Core.Entities.Application { Id = application.ApplicationId, SecretKey = application.SecretKey } ); var project = await _projectManager.Create(entityToCreate, session); await session.SaveChangesAsync(); try { var testEntityToCreate = project.ToTestEntity(); var testProject = await _projectManager.Create(testEntityToCreate, session); await session.SaveChangesAsync(); if (request.TestMode) { return(Ok(testProject)); } } catch (Exception ex) { _logger.LogError(ex, "Ye testproject creation failed. Project {@project} Request {@request}", project, request); } // TODO: Get project and testproject to verify creation return(Ok(project)); } catch (ApiException ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }