Exemple #1
0
    public async Task <Unit> Handle(RemoveListCardCommand request, CancellationToken cancellationToken)
    {
        ListCards cards = await _context.ListsCards.FirstOrDefaultAsync(x => x.Id == request.Id) ?? throw new NotFoundException("List card with this Id not found");

        _context.ListsCards.Remove(cards);
        await _context.SaveChangesAsync(cancellationToken);

        return(Unit.Value);
    }
    public async Task <Unit> Handle(MoveListCardCommand request, CancellationToken cancellationToken)
    {
        ListCards cards = await _context.ListsCards.FirstOrDefaultAsync(x => x.Id == request.Id) ?? throw new NotFoundException("List card with this Id not found");

        if (request.PrevIndexNumber == null)
        {
            cards.IndexNumber = (int)request.NextIndexNumber - 512;
        }
        else if (request.NextIndexNumber == null)
        {
            cards.IndexNumber = (int)request.PrevIndexNumber + 512;
        }
        else
        {
            cards.IndexNumber = ((int)request.NextIndexNumber + (int)request.PrevIndexNumber) / 2;
        }
        _context.ListsCards.Update(cards);
        if (
            request.PrevIndexNumber != null && Math.Abs(cards.IndexNumber - (int)request.PrevIndexNumber) <= 1
            ||
            request.NextIndexNumber != null && Math.Abs(cards.IndexNumber - (int)request.NextIndexNumber) <= 1
            )
        {
            int indexNumber             = 0;
            List <ListCards> listscards = await _context.ListsCards.Where(x => x.BoardId == cards.BoardId).ToListAsync();

            foreach (ListCards listCard in listscards)
            {
                indexNumber         += 1024;
                listCard.IndexNumber = indexNumber;
            }
            _context.ListsCards.UpdateRange(listscards);
        }
        await _context.SaveChangesAsync(cancellationToken);

        return(Unit.Value);
    }