public StatisticsServiceTests() { mockProjectRepository = new Mock <IProjectRepository>(); mockUserRepository = new Mock <IUserRepository>(); mockTaskRepository = new Mock <ITaskRepository>(); mockPersonInProjectRepository = new Mock <IPersonInProjectRepository>(); mapper = new MapperConfiguration(x => x.AddProfile <MappingProfile>()).CreateMapper(); mockPerson = new Core.Entities.Person { PersonId = Guid.NewGuid(), CreatedDate = DateTime.Now, FirstName = "NewPerson", LastName = "NewPerson", IsActive = true }; mockProject = new Core.Entities.Project { ProjectId = mockProjectId, AuthorId = Guid.NewGuid(), CreatedDate = DateTime.Now, StartDate = DateTime.Now, ConclusionDate = DateTime.Now.AddDays(15), Description = "NewProject", Title = "NewProject" }; }
public async Task <IActionResult> PlatformUrl([FromBody] UpdatePlatformUrlRequest request) { //var errors = Util.UriErrors(new Dictionary<string, string> //{ { "platform-url", request.Url }, //}, _logger); //if (errors != null && errors.Any()) //{ // return BadRequest(new { message = "All urls have to be valid.", errors = errors }); //} try { // Who's logged in? using var session = _documentStore.OpenAsyncSession(); var user = await _platformAdminUserManager.GetByUniqueIdentifierAsync(User.Identity.Name, session); var testMode = !ProjectId.IsValidIdentity(request.ProjectId); // Which project are we working on? Core.Entities.Project project = (!request.TestMode && ProjectId.IsValidIdentity(request.ProjectId)) ? await _projectManager.Get((ProjectId)request.ProjectId, session) : // if (request.TestMode && TestProjectId.IsValidIdentity(request.ProjectId)) await _projectManager.GetTest(request.ProjectId, session); if (project == null) { throw new ApiException("Project not found.", (int)System.Net.HttpStatusCode.NotFound); } // 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); } Core.Entities.Platform platform; if (project.Platforms == null || !project.Platforms.Any()) { _logger.LogCritical("Platform not found."); throw new ApiException("Platform not found. Please contact your admin or create a new project.", (int)System.Net.HttpStatusCode.NotFound); } else if (project.Platforms.FirstOrDefault().ExportDataUri == request.Url) { // No change. Return. return(Ok(project)); } else { if (!testMode) { var existingPlatform = await _platformAdminHttpClient.GetPlatform(project.Platforms.FirstOrDefault().Id); platform = new Core.Entities.Platform { Id = existingPlatform.PlatformId, PlatformToken = project.Platforms.FirstOrDefault().PlatformToken, ExportDataUri = request.Url, LastUpdate = DateTime.UtcNow, Published = !existingPlatform.IsInactive }; } else { platform = project.Platforms.FirstOrDefault(); platform.ExportDataUri = request.Url; } } // One platform per project, so just replace. Create() above also sets the LastUpdated date. project.Platforms = new List <Core.Entities.Platform> { platform }; // Save project = await _projectManager.Update(project, session); // TODO: If saving fails, revert back by deleting the platform that was registered with the GigDataService await session.SaveChangesAsync(); return(Ok(project)); } catch (ApiException ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }