public async Task <JsonPatchDocument <DbWorkspace> > MapAsync(JsonPatchDocument <EditWorkspaceRequest> request)
        {
            if (request is null)
            {
                return(null);
            }

            JsonPatchDocument <DbWorkspace> result = new();

            foreach (var item in request.Operations)
            {
                if (item.path.EndsWith(nameof(EditWorkspaceRequest.Image), StringComparison.OrdinalIgnoreCase))
                {
                    ImageConsist image = JsonConvert.DeserializeObject <ImageConsist>(item.value.ToString());
                    (bool _, string resizedContent, string extension) = await _resizeHelper.ResizeAsync(
                        image.Content, image.Extension);

                    result.Operations.Add(new Operation <DbWorkspace>(item.op, nameof(DbWorkspace.ImageContent), item.from, resizedContent));
                    result.Operations.Add(new Operation <DbWorkspace>(item.op, nameof(DbWorkspace.ImageExtension), item.from, extension));

                    continue;
                }

                result.Operations.Add(new Operation <DbWorkspace>(item.op, item.path, item.from, item.value));
            }

            return(result);
        }
Beispiel #2
0
        public async Task <DbChannel> MapAsync(CreateChannelRequest request)
        {
            if (request is null)
            {
                return(null);
            }

            Guid createdBy = _httpContextAccessor.HttpContext.GetUserId();
            Guid channelId = Guid.NewGuid();

            ICollection <DbChannelUser> dbChannelUsers = request.Users?
                                                         .Select(u => _channelUserMapper.Map(channelId, u.UserId, u.IsAdmin, createdBy))
                                                         .ToHashSet();

            dbChannelUsers.Add(_channelUserMapper.Map(channelId, createdBy, true, createdBy));

            (bool _, string resizedContent, string extension) = request.Image is null
        ? (false, null, null)
        : (await _resizeHelper.ResizeAsync(request.Image.Content, request.Image.Extension));

            return(new()
            {
                Id = channelId,
                WorkspaceId = request.WorkspaceId,
                Name = request.Name,
                IsPrivate = request.IsPrivate,
                IsActive = true,
                ImageContent = resizedContent,
                ImageExtension = extension,
                CreatedBy = createdBy,
                CreatedAtUtc = DateTime.UtcNow,
                Users = dbChannelUsers.ToHashSet()
            });
        }
        public async Task <DbWorkspace> MapAsync(CreateWorkspaceRequest request)
        {
            if (request is null)
            {
                return(null);
            }

            Guid workspaceId = Guid.NewGuid();
            Guid createdBy   = _httpContextAccessor.HttpContext.GetUserId();

            List <DbWorkspaceUser> dbWorkspaceUsers = request.Users?
                                                      .Select(u => _workspaceUserMapper.Map(workspaceId, u.UserId, u.IsAdmin, createdBy))
                                                      .ToList();

            dbWorkspaceUsers.Add(_workspaceUserMapper.Map(workspaceId, createdBy, true, createdBy));

            (bool _, string resizedContent, string extension) = request.Image is null
        ? (false, null, null)
        : (await _resizeHelper.ResizeAsync(request.Image.Content, request.Image.Extension));

            return(new DbWorkspace
            {
                Id = workspaceId,
                Name = request.Name,
                Description = request.Description,
                IsActive = true,
                ImageContent = resizedContent,
                ImageExtension = extension,
                CreatedBy = createdBy,
                CreatedAtUtc = DateTime.UtcNow,
                Users = dbWorkspaceUsers,
                Channels = new List <DbChannel>()
                {
                    _channelMapper.Map(workspaceId, dbWorkspaceUsers, createdBy)
                }
            });
        }