Example #1
0
        public async Task <IActionResult> Vote(int joinRequestId, bool _accept)
        {
            VoteJoinRequestDto dto = new VoteJoinRequestDto {
                Id = joinRequestId, accept = _accept
            };
            //CancelJoinRequestDto dto = new CancelJoinRequestDto {Id = joinRequestId, RequestingStudentId = requestingStudentId, RequestedGroupId = requestedGroupId};
            ServiceResponse <JoinRequestInfoDto> response = await _joinRequestService.Vote(dto);

            if (response.Success)
            {
                return(Ok(response));
            }
            return(NotFound(response));
        }
Example #2
0
        // bugli durum: user oyladi cikti gruptan accepted number yuksek kaldi
        // grupta sifir kisi kaldi gecmis olsun
        public async Task <ServiceResponse <JoinRequestInfoDto> > Vote(VoteJoinRequestDto joinRequestDto)
        {
            ServiceResponse <JoinRequestInfoDto> response = new ServiceResponse <JoinRequestInfoDto>();
            JoinRequest joinRequest = await _context.JoinRequests.Include(jr => jr.RequestedGroup)
                                      .ThenInclude(pg => pg.GroupMembers)
                                      .Include(jr => jr.RequestedGroup)
                                      .ThenInclude(pg => pg.AffiliatedCourse)
                                      .FirstOrDefaultAsync(jr => jr.Id == joinRequestDto.Id);

            User user = await _context.Users
                        .FirstOrDefaultAsync(u => u.Id == GetUserId());

            if (joinRequest == null)
            {
                response.Data    = null;
                response.Message = "There is no such join request";
                response.Success = false;
                return(response);
            }
            if (joinRequest.Resolved)
            {
                response.Data = new JoinRequestInfoDto {
                    Id = joinRequestDto.Id, Accepted = joinRequest.Accepted, Resolved = joinRequest.Resolved, VotedStudents = joinRequest.VotedStudents
                };
                response.Message = "You cannot vote because the join request is resolved already";
                response.Success = false;
                return(response);
            }
            if (!joinRequest.RequestedGroup.GroupMembers.Any(pgu => pgu.UserId == GetUserId()))
            {
                response.Data    = null;
                response.Message = "You cannot vote because you are not in this group";
                response.Success = false;
                return(response);
            }
            if (joinRequest.Accepted)
            {
                response.Data    = null;
                response.Message = "The join request is accepted already";
                response.Success = false;
                return(response);
            }

            if (!IsUserInString(joinRequest.VotedStudents, GetUserId()) && joinRequestDto.accept == false)
            {
                joinRequest.Resolved = true;
                _context.JoinRequests.Update(joinRequest);
                await _context.SaveChangesAsync();

                response.Message = "Join request is cancelled by the negative vote";
                return(response);
            }

            if (IsUserInString(joinRequest.VotedStudents, GetUserId()) && joinRequestDto.accept == false)
            {
                joinRequest.VotedStudents = RemoveUserFromString(joinRequest.VotedStudents, GetUserId());
                joinRequest.AcceptedNumber--;
                _context.JoinRequests.Update(joinRequest);
                await _context.SaveChangesAsync();

                response.Message = "Your vote is taken back successfully.";
                return(response);
            }
            if (IsUserInString(joinRequest.VotedStudents, GetUserId()) && joinRequestDto.accept == true)
            {
                response.Success = false;
                response.Message = "User is already voted accepted for this request";
                return(response);
            }

            int maxSize = (await _context.Courses.FirstOrDefaultAsync(c => c.Id == joinRequest.RequestedGroup.AffiliatedCourseId)).MaxGroupSize;

            joinRequest.AcceptedNumber++;
            joinRequest.VotedStudents = AddUserToString(joinRequest.VotedStudents, GetUserId());

            if (joinRequest.AcceptedNumber >= joinRequest.RequestedGroup.GroupMembers.Count && joinRequest.RequestedGroup.GroupMembers.Count + 1 <= maxSize)
            {
                int newSize = joinRequest.RequestedGroup.GroupMembers.Count + 1;
                joinRequest.Accepted = true;
                _context.JoinRequests.Update(joinRequest);
                await _context.SaveChangesAsync();

                int reqId = joinRequest.RequestedGroupId;

                var tmp = await _projectGroupService.CompleteJoinRequest(joinRequest.Id);

                if (newSize >= maxSize)
                {
                    await DeleteAllJoinRequestsOfGroup(new DeleteAllJoinRequestsGroupDto { projectGroupId = reqId });
                }
                // await DeleteAllJoinRequestsOfUser(new DeleteAllJoinRequestsUserDto { userId = joinRequest.RequestingStudentId });
                // _context.JoinRequests.Remove ( joinRequest );
                await _context.SaveChangesAsync();

                response.Message = "Join request is accepted by all members. The user joined the group successfully";
                return(response);
            }


            response.Data = new JoinRequestInfoDto
            {
                Id             = joinRequestDto.Id,
                Accepted       = joinRequest.Accepted,
                Resolved       = joinRequest.Resolved,
                AcceptedNumber = joinRequest.AcceptedNumber,
                VotedStudents  = joinRequest.VotedStudents
            };

            response.Message = "You succesfully voted";
            response.Success = true;

            _context.JoinRequests.Update(joinRequest);
            await _context.SaveChangesAsync();

            return(response);
        }