Example #1
0
        public async Task <ObjectResult> Handle(CreateGroupRequest request)
        {
            string name          = request.createGroupDTO.Name;
            int    currentUserId = int.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value);


            Group group = new Group
            {
                Name         = name,
                CreatedAt    = DateTime.Now,
                SupervisorId = null
            };

            await _appDBContext.Groups.AddAsync(group);

            await _appDBContext.SaveChangesAsync();

            ParticipantGroup participantGroups = new ParticipantGroup
            {
                IsCreator        = true,
                Status           = Status.Accepted,
                BelongingGroupId = group.Id,
                UserId           = currentUserId
            };

            await _appDBContext.ParticipantGroups.AddAsync(participantGroups);

            await _appDBContext.SaveChangesAsync();

            Discussion discussion = new Discussion
            {
                CreatedAt = DateTime.Now,
                Name      = name,
                GroupId   = group.Id
            };

            await _appDBContext.Discussions.AddAsync(discussion);

            await _appDBContext.SaveChangesAsync();

            UserInDiscussion userInDiscussion = new UserInDiscussion
            {
                JoinedAt      = DateTime.Now,
                DiscussionId  = discussion.Id,
                ParticipantId = currentUserId
            };

            await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);

            await _appDBContext.SaveChangesAsync();

            List <ParticipantGroup> requestsToDelete = await _appDBContext.ParticipantGroups
                                                       .Where(pg => pg.Status == Status.Waiting)
                                                       .ToListAsync();

            _appDBContext.ParticipantGroups.RemoveRange(requestsToDelete);
            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(true));
        }
Example #2
0
        private void SeedData(ModelBuilder modelBuilder)
        {
            var participant = new Participant {
                Sk = 1, FirstName = "Viktor", LastName = "Plane"
            };
            var activity = new Activity {
                Sk = 1, Name = "Football"
            };
            var activityParticipant = new ActivityParticipant
            {
                ActivitySk = activity.Sk, ParticipantSk = participant.Sk, ResultStatus = "Victory"
            };
            var participantGroup = new ParticipantGroup
            {
                GroupSk       = 0,
                ParticipantSk = participant.Sk,
                GroupCategory = "Company",
                GroupName     = "Simployer"
            };

            modelBuilder.Entity <Participant>().HasData(participant);
            modelBuilder.Entity <Activity>().HasData(activity);
            modelBuilder.Entity <ActivityParticipant>().HasData(activityParticipant);
            modelBuilder.Entity <ParticipantGroup>().HasData(participantGroup);
        }
        public async Task Edit(GroupEdit groupToEdit)
        {
            Group orignalGroup = await this.groupRepository.GetWithRequestUser(groupToEdit.Id);

            List <int> originalMembers = orignalGroup.Members.Where(pg => pg.Status == Status.Accepted).Select(pg => pg.User).Select(user => user.Id).ToList();

            List <int> currentMembers = groupToEdit.MembersId;

            currentMembers.Add(groupToEdit.CreatorId);

            List <int> membersToDelete = originalMembers.Except(currentMembers).ToList();

            membersToDelete.ForEach(
                (userId) =>
            {
                ParticipantGroup requestAccepted = orignalGroup.Members.FirstOrDefault(pg => pg.UserId == userId);
                orignalGroup.Members.Remove(requestAccepted);
            }
                );

            List <int> membersToAdd = currentMembers.Except(originalMembers).ToList();

            foreach (var userId in membersToAdd)
            {
                await RemoveAllRequests(userId);
            }
            membersToAdd.ForEach(
                (userId) =>
            {
                if (userId != groupToEdit.CreatorId)
                {
                    orignalGroup.Members.Add(new ParticipantGroup
                    {
                        IsCreator        = false,
                        Status           = Status.Accepted,
                        BelongingGroup   = orignalGroup,
                        BelongingGroupId = orignalGroup.Id,
                        UserId           = userId
                    });
                }
                else
                {
                    orignalGroup.Members.Add(new ParticipantGroup
                    {
                        IsCreator        = true,
                        Status           = Status.Accepted,
                        BelongingGroup   = orignalGroup,
                        BelongingGroupId = orignalGroup.Id,
                        UserId           = userId
                    });
                }
            }
                );

            orignalGroup.Name = groupToEdit.Name;

            await this.groupRepository.Update(orignalGroup);
        }
        void IDropTarget.DragOver(IDropInfo dropInfo)
        {
            ParticipantGroup source = dropInfo.Data as ParticipantGroup;
            ParticipantGroup target = dropInfo.TargetItem as ParticipantGroup;

            if (source != null && target != null)
            {
                dropInfo.Effects           = DragDropEffects.Move;
                dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
            }
        }
        public async Task <ObjectResult> Handle(AcceptGroupRequestRequest request)
        {
            ParticipantGroup membershipToUpdate = await _appDBContext.ParticipantGroups
                                                  .Where(pg => pg.Id == request.Id && pg.Status == Status.Waiting)
                                                  .FirstOrDefaultAsync();

            if (membershipToUpdate == null)
            {
                return(new NotFoundObjectResult("No corresponding request was found"));
            }

            int maxGroupSize = await _appDBContext.Companies
                               .Where(c => c.Id == 1)
                               .Select(c => c.MaxGroupSize)
                               .FirstOrDefaultAsync();

            int actualGroupSize = await _appDBContext.Groups
                                  .Where(g => g.Id == membershipToUpdate.BelongingGroupId)
                                  .Select(g => g.Members.Where(pg => pg.Status == Status.Accepted).Count())
                                  .FirstOrDefaultAsync();

            if (actualGroupSize < maxGroupSize)
            {
                membershipToUpdate.Status = Status.Accepted;

                await _appDBContext.SaveChangesAsync();

                List <ParticipantGroup> requestsToDelete = await _appDBContext.ParticipantGroups
                                                           .Where(pg => pg.UserId == membershipToUpdate.UserId && pg.Status == Status.Waiting)
                                                           .ToListAsync();

                _appDBContext.ParticipantGroups.RemoveRange(requestsToDelete);
                await _appDBContext.SaveChangesAsync();

                UserInDiscussion userInDiscussion = new UserInDiscussion
                {
                    JoinedAt      = DateTime.Now,
                    DiscussionId  = membershipToUpdate.BelongingGroupId,
                    ParticipantId = membershipToUpdate.UserId
                };

                await _appDBContext.UsersInDiscussion.AddAsync(userInDiscussion);

                await _appDBContext.SaveChangesAsync();

                return(new OkObjectResult(true));
            }
            else
            {
                return(new BadRequestObjectResult("Group has already ready reach max size"));
            }
        }
Example #6
0
        public static ParticipantGroupMinimalDTO ToParticipantGroupMinimalDTO(this ParticipantGroup data)
        {
            if (data == null)
            {
                return(null);
            }

            return(new ParticipantGroupMinimalDTO
            {
                Id = data.Id,
                User = data.User.ToUserMinimalDTO(),
            });
        }
        public async Task<ObjectResult> Handle(DeleteGroupRequestRequest request)
        {
            ParticipantGroup membershipToRemove = await _appDBContext.ParticipantGroups
                .Where(pg => pg.Id == request.Id && pg.Status == Status.Waiting)
                .FirstOrDefaultAsync();

            if (membershipToRemove == null) { return new NotFoundObjectResult("No corresponding request was found"); }

            _appDBContext.ParticipantGroups.Remove(membershipToRemove);

            await _appDBContext.SaveChangesAsync();

            return new OkObjectResult(true);
        }
Example #8
0
        public static ParticipantGroupDTO ToDTO(this ParticipantGroup data)
        {
            if (data == null)
            {
                return(null);
            }

            return(new ParticipantGroupDTO
            {
                RequestId = data.Id,
                UserId = data.User.Id,
                Username = data.User.Username,
                IsCreator = data.IsCreator
            });
        }
        /// <summary>
        /// Accept a user to join a group
        /// </summary>
        public async Task AcceptRequest(ParticipantGroup participantGroup)
        {
            participantGroup.Status = Status.Accepted;
            await this.participantGroupRepository.Update(participantGroup);

            List <ParticipantGroup> allRequests = await this.participantGroupRepository.GetByUser(participantGroup.UserId);

            allRequests.Remove(participantGroup);

            foreach (var request in allRequests)
            {
                this.participantGroupRepository.Remove(request);
            }

            await this.participantGroupRepository.SaveChanges();
        }
        public async Task <bool> ExistRequest(ParticipantGroup data)
        {
            ParticipantGroup participantGroup = await this.participantGroupRepository.Get(data.BelongingGroupId, data.UserId);

            if (participantGroup == null)
            {
                return(false);
            }

            if (data.Status == participantGroup.Status)
            {
                return(true);
            }

            return(false);
        }
        void IDropTarget.Drop(IDropInfo dropInfo)
        {
            ParticipantGroup source = dropInfo.Data as ParticipantGroup;
            ParticipantGroup target = dropInfo.TargetItem as ParticipantGroup;

            if (source != null && target != null)
            {
                var iSource = _viewModel.Items.IndexOf(source);
                var iTarget = _viewModel.Items.IndexOf(target);

                if (iSource != iTarget)
                {
                    _viewModel.Items.Move(iSource, iTarget);
                }
            }
        }
Example #12
0
        public static MembershipRequestDTO ToMembershipRequestDTO(this ParticipantGroup data)
        {
            if (data == null)
            {
                return(null);
            }

            return(new MembershipRequestDTO
            {
                RequestId = data.Id,
                UserId = data.User.Id,
                Username = data.User.Username,
                Status = data.Status,
                CreatedAt = data.CreatedAt
            });
        }
        /// <summary>
        /// Create a request to join a group
        /// </summary>
        public async Task <ValidationResult> MembershipRequest(int groupId, User connectedUser)
        {
            Group group = await this.groupRepository.Get(groupId);

            var membershipRequest = new ParticipantGroup
            {
                IsCreator        = false,
                Status           = Status.Waiting,
                BelongingGroup   = group,
                User             = connectedUser,
                BelongingGroupId = group.Id,
                UserId           = connectedUser.Id
            };

            await this.participantGroupRepository.Insert(membershipRequest);

            return(null);
        }
        public async Task DeleteRequest(ParticipantGroup participantGroup)
        {
            Group group = null;

            if (participantGroup.IsCreator)
            {
                group = await this.groupRepository.GetWithRequestUserWithChallengeGiven(participantGroup.BelongingGroupId);

                group.Members.Clear();
                group.ChallengesGiven.Clear();

                await this.groupRepository.Delete(group);
            }
            else
            {
                await this.participantGroupRepository.Delete(participantGroup);
            }
        }
        public async Task <IActionResult> DeclineRequest(int requestId)
        {
            ParticipantGroup participantGroup = await this._groupService.GetRequest(requestId);

            if (participantGroup == null)
            {
                return(BadRequest());
            }

            if (participantGroup.Status == Status.Accepted)
            {
                return(BadRequest("Can't decline someone who is already in the group"));
            }

            await this._groupService.DeleteRequest(participantGroup);

            return(Ok(true));
        }
        public async Task <ObjectResult> Handle(LeaveGroupRequest request)
        {
            int currentUserId = int.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value);

            ParticipantGroup membershipToRemove = await _appDBContext.ParticipantGroups
                                                  .Where(pg => pg.BelongingGroupId == request.Id && pg.UserId == currentUserId && pg.Status == Status.Accepted)
                                                  .FirstOrDefaultAsync();

            if (membershipToRemove == null)
            {
                return(new NotFoundObjectResult("No corresponding members was found"));
            }

            _appDBContext.ParticipantGroups.Remove(membershipToRemove);

            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(true));
        }
        public async Task <ObjectResult> Handle(CreateGroupRequestRequest request)
        {
            int currentUserId = int.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value);

            Group group = await _appDBContext.Groups
                          .Where(g => g.Id == request.GroupId)
                          .Include(g => g.Members)
                          .FirstOrDefaultAsync();

            if (group == null)
            {
                return(new NotFoundObjectResult("No corresponding group was found"));
            }

            int maxGroupSize = await _appDBContext.Companies
                               .Where(c => c.Id == 1)
                               .Select(c => c.MaxGroupSize)
                               .FirstOrDefaultAsync();

            if (maxGroupSize == group.Members.Count)
            {
                return(new BadRequestObjectResult("Group is full"));
            }

            ParticipantGroup participantGroup = new ParticipantGroup
            {
                IsCreator        = false,
                Status           = Status.Waiting,
                CreatedAt        = DateTime.Now,
                BelongingGroupId = request.GroupId,
                UserId           = currentUserId
            };

            await _appDBContext.ParticipantGroups.AddAsync(participantGroup);

            await _appDBContext.SaveChangesAsync();

            return(new OkObjectResult(true));
        }
        public async Task <IActionResult> AcceptRequest([FromBody] int requestId)
        {
            ParticipantGroup participantGroup = await this._groupService.GetRequest(requestId);

            if (participantGroup == null)
            {
                return(BadRequest("The request id is invalid"));
            }

            int connectedUserId = int.Parse(User.Identity.Name);

            User creator = await this._groupService.GetCreator(participantGroup.BelongingGroupId);

            if (creator.Id != connectedUserId)
            {
                return(BadRequest("You have to be the creator of the group !"));
            }

            await this._groupService.AcceptRequest(participantGroup);

            return(Ok(true));
        }
        /* ************************************************** */

        private async Task CreateAcceptedMembership(bool isCreator, Group group, int userId)
        {
            User user = await this.userRepository.GetWithRequests(userId);

            foreach (var request in user.ParticipantGroups)
            {
                this.participantGroupRepository.Remove(request);
            }

            await this.participantGroupRepository.SaveChanges();

            var membershipCreator = new ParticipantGroup
            {
                IsCreator        = isCreator,
                Status           = Status.Accepted,
                BelongingGroup   = group,
                User             = user,
                BelongingGroupId = group.Id,
                UserId           = user.Id
            };

            await this.participantGroupRepository.Insert(membershipCreator);
        }
        public async Task <IActionResult> LeaveGroup(int requestId)
        {
            ParticipantGroup participantGroup = await this._groupService.GetRequest(requestId);

            if (participantGroup == null)
            {
                return(BadRequest());
            }
            if (participantGroup.Status != Status.Accepted)
            {
                return(BadRequest("Must be an accepted request !"));
            }

            int connectedUserId = int.Parse(User.Identity.Name);

            if (participantGroup.UserId != connectedUserId)
            {
                return(BadRequest("Current user is different from request user"));
            }

            await this._groupService.DeleteRequest(participantGroup);

            return(Ok(requestId));
        }
        public async Task <bool> IsAccepted(int userId)
        {
            ParticipantGroup participantGroup = await this.participantGroupRepository.IsAccepted(userId);

            return(participantGroup != null);
        }
 public void RemoveGroup(ParticipantGroup g)
 {
 }
 public void CreateOrUpdateGroup(ParticipantGroup g)
 {
 }
        public async Task <User> GetCreator(int groupId)
        {
            ParticipantGroup request = await this.participantGroupRepository.GetRequestFromCreator(groupId);

            return(request.User);
        }
        public async Task <bool> IsAcceptedEdit(int groupId, int userId)
        {
            ParticipantGroup participantGroup = await this.participantGroupRepository.IsAccepted(userId);

            return(participantGroup != null && participantGroup.BelongingGroupId != groupId);
        }