public async Task <GroupSite> CreateGroupSiteDataAsync(Guid userId, string slug, CancellationToken cancellationToken) { if (Guid.Empty == userId) { throw new ArgumentOutOfRangeException(nameof(userId)); } if (string.IsNullOrWhiteSpace(slug)) { throw new ArgumentOutOfRangeException(nameof(slug)); } var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(userId, slug, GroupEditRole, cancellationToken); if (!userCanPerformAction) { _logger.LogError("Error: CreateGroupSiteDataAsync - User: {User} does not have permission to create missing group site for group: {GroupSlug}", userId, slug); throw new NotFoundException($"Site data for group slug: {slug} is missing"); } var now = _systemClock.UtcNow.UtcDateTime; var group = await _groupCommand.GetGroupAsync(slug, cancellationToken); var createContentResponse = await _contentService.CreatePageAsync(userId, group.Id, null, cancellationToken); var createdContentGuid = Guid.Parse(createContentResponse.Data); var groupSiteDto = new GroupSiteDto() { GroupId = group.Id, CreatedAtUTC = now, CreatedBy = userId, ContentRootId = createdContentGuid }; await _groupCommand.CreateGroupSiteAsync(groupSiteDto, cancellationToken); return(await _groupDataProvider.GetGroupSiteDataAsync(slug, cancellationToken)); }
public async Task <IActionResult> CreatePageAsync(Guid userId, Guid groupId, [FromBody] GeneralWebPageCreateRequest createRequest, CancellationToken cancellationToken) { var pageGuid = await _contentService.CreatePageAsync(userId, groupId, createRequest, cancellationToken); return(new JsonResult(pageGuid)); }
public async Task CreateGroupAsync(Guid userId, Stream requestBody, string?contentType, CancellationToken cancellationToken) { var now = _systemClock.UtcNow.UtcDateTime; var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(userId, AddGroupRole, cancellationToken); if (userCanPerformAction is not true) { _logger.LogError($"Error: CreateGroupAsync - User:{0} does not have access to add group:{1}", userId); throw new SecurityException($"Error: User does not have access"); } var(group, image) = await AdminUploadGroupImageMultipartContent(userId, requestBody, contentType, cancellationToken); if (image is not null) { try { var imageId = await _imageService.CreateImageAsync(image); group = group with { ImageId = imageId }; } catch (DBConcurrencyException ex) { _logger.LogError(ex, $"Error: CreateImageAsync - Error creating image {0}", null); if (image is not null) { await _blobStorageProvider.DeleteFileAsync(image.FileName); await _imageService.DeleteImageAsync(image.Id); } throw new DBConcurrencyException("Error: User request to create an image was not successful"); } } var groupValidator = new GroupValidator(); var groupValidationResult = await groupValidator.ValidateAsync(group, cancellationToken); if (groupValidationResult.Errors.Count > 0) { throw new ValidationException(groupValidationResult); } var groupSiteDto = new GroupSiteDto() { CreatedAtUTC = now, CreatedBy = userId, }; try { groupSiteDto.GroupId = await _groupCommand.CreateGroupAsync(userId, group, cancellationToken); } catch (DBConcurrencyException ex) { if (image is not null) { await _blobStorageProvider.DeleteFileAsync(image.FileName); await _imageService.DeleteImageAsync(image.Id); } _logger.LogError(ex, $"Error: CreateGroupAsync - Error creating group {0}", null); throw new DBConcurrencyException("Error: User request to create a group was not successful"); } // Create the group homepage content in Umbraco var createContentResponse = await _contentService.CreatePageAsync(userId, groupSiteDto.GroupId, null, cancellationToken); // If the create content request fails, delete the associated group if (createContentResponse is null || !createContentResponse.Succeeded) { //TODO: rollback/delete group (feature: 75323) _logger.LogError($"Error: CreatePageAsync - Error creating group homepage content {groupSiteDto.GroupId}"); throw new HttpRequestException($"Error: CreatePageAsync - Error creating group homepage content {groupSiteDto.GroupId}"); } try { groupSiteDto.ContentRootId = Guid.Parse(createContentResponse.Data); await _groupCommand.CreateGroupSiteAsync(groupSiteDto, cancellationToken); } catch (DBConcurrencyException ex) { _logger.LogError(ex, $"Error: CreateGroupSiteAsync - Error creating group site {0}"); throw new DBConcurrencyException("Error: User request to create a group site was not successful"); } }