public async Task <string> Handle(CreateEmptyListRequest request, CancellationToken cancellationToken)
        {
            // check for existing contributions
            // if this is equal to limit already, throw 403
            var contributionsForUser = _context.ListContributors.Where(lc => lc.UserIdent == request.UserIdent);
            var listsLimit           = _configuration.GetValue <int>("Limits:Lists");

            if (contributionsForUser.Count() >= listsLimit)
            {
                throw new RequestFailedException($"List limit reached ({listsLimit})", System.Net.HttpStatusCode.Forbidden);
            }

            var list = new List
            {
                Id      = _randomisedWordProvider.CreateRandomId(),
                Name    = _randomisedWordProvider.CreateRandomName(),
                Created = _nowProvider.Now,
                Updated = _nowProvider.Now,
            };

            var contribution = new ListContributor
            {
                ListId    = list.Id,
                UserIdent = request.UserIdent
            };

            await _context.Lists.AddAsync(list, cancellationToken);

            await _context.ListContributors.AddAsync(contribution, cancellationToken);

            await _context.SaveChangesAsync();

            return(list.Id);
        }
示例#2
0
        public async Task <Unit> Handle(UpdateListRequest request, CancellationToken cancellationToken)
        {
            var itemLimit = _configuration.GetValue <int>("Limits:ListItems");

            if (request.DTO.Items.Count >= itemLimit)
            {
                throw new RequestFailedException($"List Item limit reached ({itemLimit})", System.Net.HttpStatusCode.Forbidden);
            }

            var existing = await _context.Lists
                           .Include(l => l.Items)
                           .FirstOrDefaultAsync(l => l.Id == request.DTO.Id);

            if (existing != null)
            {
                existing.Name    = request.DTO.Name?.Trim();
                existing.Updated = _nowProvider.Now;
                // remove the old items as we're going to overwrite them
                foreach (var removedItem in existing.Items)
                {
                    _context.ListItems.Remove(removedItem);
                }

                existing.Items = request.DTO.Items?.Select((i, index) => new ListItem
                {
                    Id         = i.Id.Trim(),
                    Order      = index,
                    Value      = i.Value?.Trim(),
                    Created    = _nowProvider.Now,
                    Notes      = i.Notes?.Trim(),
                    Completed  = i.Completed,
                    ParentList = existing
                }).ToList();

                // find all existing contributions
                // if there is none for this list and user add a new one
                if (!await _context.ListContributors.AnyAsync(lc =>
                                                              lc.ListId == existing.Id && lc.UserIdent == request.UserIdent))
                {
                    var newContributor = new ListContributor
                    {
                        ListId    = existing.Id,
                        UserIdent = request.UserIdent
                    };

                    await _context.ListContributors.AddAsync(newContributor);
                }

                await _context.SaveChangesAsync(cancellationToken);
            }

            return(Unit.Value);
        }