public async Task <IEnumerable <AllianceJoinRequestDto> > Handle(GetAllianceJoinRequestsQuery request, CancellationToken cancellationToken)
        {
            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

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

            var requests = await _context.UserRequests
                           .Include(x => x.User)
                           .Where(x => x.AllianceId == userAlliance.Id).ToListAsync(cancellationToken);

            return(_mapper.Map <IEnumerable <AllianceJoinRequestDto> >(requests));
        }
Esempio n. 2
0
        public async Task <Unit> Handle(LeaveAllianceCommand request, CancellationToken cancellationToken)
        {
            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

            var userId = _currentUserService.UserId;

            var user = userAlliance.Members.First(x => x.Id == userId);

            userAlliance.Members.Remove(user);

            _context.Alliances.Update(userAlliance);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <ChallengeDto> Handle(CreateChallengeCommand request, CancellationToken cancellationToken)
        {
            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

            var entity = new Challenge
            {
                Title       = request.Title,
                StartDate   = request.StartDate,
                FinishDate  = request.FinishDate,
                Status      = ChallengeStatus.Scheduled,
                Competitors = { userAlliance }
            };

            foreach (var taskId in request.Exercises)
            {
                var exercise = await _context.Exercises.FindAsync(new object[] { taskId }, cancellationToken);

                if (exercise != null)
                {
                    entity.Exercises.Add(exercise);
                }
            }

            await _context.Challenges.AddAsync(entity, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);


            foreach (var allianceId in request.Alliances)
            {
                var challengeRequest = new ChallengeRequest {
                    AllianceId = allianceId, ChallengeId = entity.Id
                };
                await _context.ChallengeRequests.AddAsync(challengeRequest, cancellationToken);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <ChallengeDto>(entity));
        }
        public async Task <Unit> Handle(RespondChallengeRequestCommand request, CancellationToken cancellationToken)
        {
            var currentUserAlliance = await _currentUserService.GetCurrentUserAlliance();

            var challengeRequest = await _context.ChallengeRequests.FirstOrDefaultAsync(
                x => x.AllianceId == currentUserAlliance.Id && x.ChallengeId == request.ChallengeId,
                cancellationToken);

            _context.ChallengeRequests.Remove(challengeRequest);
            if (request.Accepted)
            {
                var challenge = await _context.Challenges.FirstOrDefaultAsync(x => x.Id == request.ChallengeId, cancellationToken);

                challenge.Competitors.Add(currentUserAlliance);

                _context.Challenges.Update(challenge);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Esempio n. 5
0
        public async Task <AllianceDetailsDto> Handle(GetAllianceByIdQuery request, CancellationToken cancellationToken)
        {
            var alliance = await _context.Alliances
                           .Include(x => x.Leader)
                           .Include(x => x.Ranking)
                           .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

            var res = _mapper.Map <AllianceDetailsDto>(alliance);

            res.UserType = userAlliance == null ? AllianceUserType.VisitorWithoutAlliance : AllianceUserType.VisitorWithAlliance;

            if (userAlliance?.Id == alliance.Id)
            {
                res.UserType = alliance.LeaderId == _currentUserService.UserId
                    ? AllianceUserType.Manager
                    : AllianceUserType.Member;
            }

            return(res);
        }
        public async Task <Unit> Handle(RespondJoinRequestCommand request, CancellationToken cancellationToken)
        {
            var currentUserAlliance = await _currentUserService.GetCurrentUserAlliance();

            var joinRequest = await _context.UserRequests.FirstOrDefaultAsync(
                x => x.AllianceId == currentUserAlliance.Id && x.UserId == request.UserId,
                cancellationToken);

            _context.UserRequests.Remove(joinRequest);
            if (request.Accepted)
            {
                var user = await _context.AppUsers.FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken);

                currentUserAlliance.Members.Add(user);

                _context.Alliances.Update(currentUserAlliance);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        private async Task <bool> BeAllianceLeader(bool accepted, CancellationToken cancellationToken)
        {
            var currentUserAlliance = await _currentUserService.GetCurrentUserAlliance();

            return(currentUserAlliance?.LeaderId == _currentUserService.UserId);
        }
        private async Task <bool> BeWithoutAlliance(int _, CancellationToken cancellationToken)
        {
            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

            return(userAlliance == null);
        }
        private async Task <bool> BeAllianceMember(int allianceId, CancellationToken cancellationToken)
        {
            var userAlliance = await _currentUserService.GetCurrentUserAlliance();

            return(userAlliance?.Id == allianceId);
        }