Esempio n. 1
0
        public async Task <ActionResult <Participant> > DeleteAsync(int participantId, CancellationToken cancellationToken)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                var participant = await _context.Participants.FirstOrDefaultAsync(Participant.GetById(participantId), cancellationToken);

                if (participant == null)
                {
                    return(null);
                }

                Transaction.Current.EnlistVolatile(new TransactionEnlistmentNotification(() => _voteHub.ParticipantLeft(participant, cancellationToken)), EnlistmentOptions.None);
                _context.Participants.Remove(participant);
                await _context.SaveChangesAsync(cancellationToken);


                var room = await _context.Rooms.Include(x => x.Participants).FirstOrDefaultAsync(Room.GetById(participant.RoomId), cancellationToken);

                if (room == null)
                {
                    return(participant);
                }

                if (!room.Participants.Any())
                {
                    Transaction.Current.EnlistVolatile(new TransactionEnlistmentNotification(() => _voteHub.RoomDeleted(room, cancellationToken)), EnlistmentOptions.None);
                    _context.Rooms.Remove(room);
                    await _context.SaveChangesAsync(cancellationToken);
                }
                scope.Complete();
                return(participant);
            }
        }