public GroupMemberDTO CreateMember(int idGroup, int idUser)
        {
            FriendGroup foundGroup = _friendGroupRepository.FindFullGroup(idGroup);
            User        foundUser  = _userRepository.FindUser(idUser);

            if (foundGroup == null)
            {
                throw new APIException("The group does not exist!", StatusCodes.Status404NotFound);
            }

            if (foundUser == null)
            {
                throw new APIException("The user does not exist!", StatusCodes.Status404NotFound);
            }
            GroupMemberDTO groupMembership = foundGroup.members.Where(x => x.UserId == idUser && x.GroupId == idGroup).FirstOrDefault();

            if (groupMembership == null)
            {
                _friendGroupRepository.AddMember(idGroup, idUser, DateTime.Now);
                return(new GroupMemberDTO());
            }
            else
            {
                throw new APIException("The user is already a member!", StatusCodes.Status400BadRequest);
            }
        }
        public List <GetUserDTO> GetNonMembersOfGroupFriendsWithUser(int userId, int groupId)
        {
            FriendGroup       friendGroup = _friendGroupRepository.FindFullGroup(groupId);
            User              user        = _userRepository.FindFullUser(userId);
            List <GetUserDTO> notMembers  = new List <GetUserDTO>();

            if (user == null)
            {
                throw new APIException("The user does not exist!", StatusCodes.Status404NotFound);
            }


            if (friendGroup == null)
            {
                throw new APIException("The friend group does not exist!", StatusCodes.Status404NotFound);
            }

            foreach (FriendUserDTO fud in user.Friends)
            {
                GroupMemberDTO gmd = friendGroup.members.Where(x => x.UserId == fud.UserId).FirstOrDefault();
                if (gmd == null)
                {
                    notMembers.Add(new GetUserDTO {
                        FirstName   = fud.FirstName,
                        Id          = fud.UserId,
                        LastName    = fud.LastName,
                        PhoneNumber = fud.PhoneNumber
                    });
                }
            }
            return(notMembers);
        }
        public GroupMemberDTO FindMember(int id)
        {
            var            sql         = "SELECT * FROM FriendsGroupsMembers WHERE Id=@Id";
            GroupMemberDTO groupMember = _dbConnection.Query <GroupMemberDTO>(sql, new { Id = id }).FirstOrDefault();

            return(groupMember);
        }
        public GroupMemberDTO DeleteMember(int id)
        {
            GroupMemberDTO groupMember = _friendGroupRepository.FindMember(id);

            if (groupMember == null)
            {
                throw new APIException("The membership does not exist!", StatusCodes.Status404NotFound);
            }
            _friendGroupRepository.DeleteMember(id);
            return(groupMember);
        }
예제 #5
0
        public IActionResult LeaveGroup(Guid Id)
        {
            try
            {
                var CurrentUser     = _context.Users.Find(userManager.GetUserId(User));
                var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id);
                if (existingAccount.AccountStatus.Equals(Status.Suspended))
                {
                    signInManager.SignOutAsync();
                }

                var existingGroup = _GroupStore.GetGroupById(Id);
                if (existingGroup is null)
                {
                    throw new DomainException(ErrorMessages.GroupDoesNotExist);
                }

                var existingGroupMember = _GroupStore.GetGroupMembers(Id)
                                          .FirstOrDefault(x => x.UserId.Equals(CurrentUser.Id));

                if (existingGroupMember is null)
                {
                    throw new DomainException(ErrorMessages.UserDoesNotExist);
                }

                var GroupID = existingGroupMember.GroupId.ToString().Clone();


                var GroupMemberDTO = new GroupMemberDTO()
                {
                    GroupId  = existingGroup.Id,
                    Id       = existingGroupMember.Id,
                    Username = existingGroupMember.Username
                };
                return(View(GroupMemberDTO));
            }
            catch (DomainException ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.NotFound, ControllerName.Accounts));
            }
            catch (ForbiddenException ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.NotAuthorized, ControllerName.Accounts));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts));
            }
        }
예제 #6
0
 public IActionResult AddMemberToGroup(int groupId, int userId)
 {
     try
     {
         GroupMemberDTO newMembership = _friendsGroupsService.CreateMember(groupId, userId);
         return(Ok(newMembership));
     }
     catch (APIException ex)
     {
         ErrorMessage err = new ErrorMessage {
             message = ex.Message
         };
         return(StatusCode(ex.StatusCode, err));
     }
 }
예제 #7
0
 public IActionResult DeleteMembership(int membershipId)
 {
     try
     {
         GroupMemberDTO oldMembership = _friendsGroupsService.DeleteMember(membershipId);
         return(Ok(oldMembership));
     }
     catch (APIException ex)
     {
         ErrorMessage err = new ErrorMessage {
             message = ex.Message
         };
         return(StatusCode(ex.StatusCode, err));
     }
 }
예제 #8
0
        public IActionResult RevokeGroupAdmin(Guid Id)
        {
            try
            {
                var CurrentUser     = _context.Users.Find(userManager.GetUserId(User));
                var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id);
                if (existingAccount.AccountStatus.Equals(Status.Suspended))
                {
                    signInManager.SignOutAsync();
                }


                var existingGroupUser = _GroupStore.GetGroupMemberById(Id);

                var existingGroup = _GroupStore.GetGroupById(existingGroupUser.GroupId);
                if (CurrentUser.Id.Equals(existingGroup.UserId) is false)
                {
                    throw new ForbiddenException(ErrorMessages.ForbiddenAccess);
                }

                var GroupMemberDTO = new GroupMemberDTO()
                {
                    Id       = existingGroupUser.Id,
                    Username = existingGroupUser.Username,
                    Type     = existingGroupUser.Type,
                    GroupId  = existingGroupUser.GroupId,
                    UserId   = existingGroupUser.UserId
                };
                return(View(GroupMemberDTO));
            }
            catch (DomainException ex)
            {
                _logger.LogError(ex.Message);
                ModelState.AddModelError("Error", ex.Message);
                return(View());
            }
            catch (ForbiddenException ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.NotAuthorized, ControllerName.Accounts));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts));
            }
        }
예제 #9
0
        public ActionResult SubscribeToGroup(int groupId = 0)
        {
            this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name);
            try {
                if (groupId > 0)
                {
                    var _userId      = Session["UserId"];
                    var _sessionUser = Convert.ToInt32(_userId);

                    //create dto for the new member
                    GroupMemberDTO dto = new GroupMemberDTO();
                    dto.GroupId  = groupId;
                    dto.MemberId = _sessionUser;

                    using (var client = new HttpClient()) {
                        client.BaseAddress = new Uri(this.apiMethodsUrl);
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json")
                            );
                        HttpResponseMessage response = client.PostAsJsonAsync("api/groups/AddGroupMember/?groupMember=", dto).Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new CustomException("Could not complete the operation!");
                        }
                    }
                    return(RedirectToAction("DisplayAssociatedGroups"));
                }
                else
                {
                    this.logger.Trace("Operation could not be completed! Try again. --> Username: "******"Operation could not be completed! Try again.";
                    return(View("Error"));
                }
            }
            catch (CustomException ce) {
                this.logger.Trace(ce, "Username: "******"Operation could not be completed! Try again.";
                return(View("Error"));
            }
            catch (Exception ex) {
                this.logger.Trace(ex, "Username: "******"Operation could not be completed!";
                return(View("Error"));
            }
        }
예제 #10
0
        public HttpResponseMessage AddGroupMember(GroupMemberDTO dto)
        {
            try {
                GroupMembers groupMember = new GroupMembers {
                    GroupId       = dto.GroupId,
                    GroupMemberId = dto.GroupMemberId,
                    MemberId      = dto.MemberId
                };

                this._groupsManagement.AddGroupMember(groupMember);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception) {
                // Log exception code goes here
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured while executing method."));
            }
        }
예제 #11
0
        public IActionResult RemoveUser(GroupMemberDTO GroupMemberDTO)
        {
            try
            {
                var existingGroupMember = _GroupStore.GetGroupMemberById(GroupMemberDTO.Id);
                var existingUser        = _userStore.GetByIdentityUserId(existingGroupMember.UserId);
                var GroupID             = new Guid(existingGroupMember.GroupId.ToString());
                var existingGroup       = _GroupStore.GetGroupById(GroupID);


                _GroupStore.DeleteGroupUser(existingGroupMember);
                var Message = $"You have been removed from group '{existingGroup.Name}'";

                Notify(existingUser, existingGroup, Message);
                return(RedirectToAction("GroupDetails", "Groups", new { id = GroupID }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts));
            }
        }
예제 #12
0
        public ActionResult CreateGroup(GroupsViewModel groupViewModel)
        {
            this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name);
            try {
                var groupType = Request["groupType"];
                if (groupType != null && !groupType.Equals(""))
                {
                    var _userId      = Session["UserId"];
                    var _sessionUser = Convert.ToInt32(_userId);

                    Groups group = new Groups();
                    #region create new group

                    //create the dto for the new group
                    GroupDTO dto = new GroupDTO();
                    dto.GroupDescription = groupViewModel.GroupDescription;
                    dto.GroupName        = groupViewModel.GroupName;
                    dto.GroupType        = groupViewModel.GroupType;
                    dto.OwnerId          = _sessionUser;

                    using (var client = new HttpClient()) {
                        client.BaseAddress = new Uri(this.apiMethodsUrl);
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json")
                            );
                        HttpResponseMessage response = client.PostAsJsonAsync("api/groups/AddGroup/?group=", dto).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var g = response.Content.ReadAsAsync <Groups>().Result;
                            if (g != null)
                            {
                                group.GroupDescription = g.GroupDescription;
                                group.GroupId          = g.GroupId;
                                group.GroupName        = g.GroupName;
                                group.GroupType        = g.GroupType;
                                group.OwnerId          = g.OwnerId;
                            }
                            else
                            {
                                throw new CustomException("Could not complete the operation!");
                            }
                        }
                        else
                        {
                            throw new CustomException("Could not complete the operation!");
                        }
                    }

                    #endregion

                    #region subscribe to created group

                    //create dto for the new member
                    GroupMemberDTO dto1 = new GroupMemberDTO();
                    dto1.GroupId  = group.GroupId;
                    dto1.MemberId = _sessionUser;

                    using (var client = new HttpClient()) {
                        client.BaseAddress = new Uri(this.apiMethodsUrl);
                        client.DefaultRequestHeaders.Accept.Add(
                            new MediaTypeWithQualityHeaderValue("application/json")
                            );
                        HttpResponseMessage response = client.PostAsJsonAsync("api/groups/AddGroupMember/?groupMember=", dto1).Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new CustomException("Could not complete the operation!");
                        }
                    }

                    #endregion

                    return(RedirectToAction("DisplayAssociatedGroups"));
                }
                else
                {
                    this.logger.Trace("Username: "******"Please choose a type for the group!";
                    return(View("Error"));
                }
            }
            catch (CustomException ce) {
                this.logger.Trace(ce, "Username: "******"Operation could not be completed! Try again.";
                return(View("Error"));
            }
            catch (Exception ex) {
                this.logger.Trace(ex, "Username: "******"Operation could not be completed!";
                return(View("Error"));
            }
        }
예제 #13
0
        public IActionResult GroupDetails(Guid Id)
        {
            try
            {
                var CurrentUser = _context.Users.Find(userManager.GetUserId(User));
                if (CurrentUser is null)
                {
                    throw new DomainException(ErrorMessages.NotSignedIn);
                }
                var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id);
                if (existingAccount.AccountStatus.Equals(Status.Suspended))
                {
                    signInManager.SignOutAsync();
                }

                var existingGroup = _GroupStore.GetGroupById(Id);
                if (existingGroup is null)
                {
                    throw new DomainException(ErrorMessages.GroupDoesNotExist);
                }


                var groupMembers = _GroupStore.GetGroupMembers(existingGroup.Id);
                var Owner        = _GroupStore.GetGroupOwner(existingGroup.Id);
                var Requests     = _GroupRequestStore.GetGroupRequestsOfGroup(existingGroup.Id);
                var RequestDTOs  = new List <GroupRequestDTO>();

                foreach (var request in Requests)
                {
                    var requestUser     = _GroupRequestStore.GetGroupRequestUser(request.RequestUserId);
                    var GroupRequestDTO = new GroupRequestDTO()
                    {
                        Username      = requestUser.Username,
                        Id            = request.Id,
                        GroupID       = request.GroupId,
                        RequestDate   = request.RequestDate,
                        RequestUserId = request.RequestUserId
                    };
                    RequestDTOs.Add(GroupRequestDTO);
                }
                var CurrentUserAsRequestUser = RequestDTOs.FirstOrDefault(x => x.Username == CurrentUser.UserName);
                var CurrentUserAsMember      = groupMembers.FirstOrDefault(x => x.Username == CurrentUser.UserName);

                var GroupAdmin = _GroupStore.GetGroupMembers(Id)
                                 .FirstOrDefault(x => x.Type.Equals(GroupUserType.Admin) &&
                                                 x.UserId.Equals(CurrentUser.Id));

                var GroupDetailsDTO = new GroupDetailsDTO()
                {
                    Id              = existingGroup.Id,
                    Name            = existingGroup.Name,
                    Description     = existingGroup.Description,
                    CreationDate    = existingGroup.CreationDate,
                    GroupOwner      = Owner.Username,
                    CurrentUsername = CurrentUser.UserName,
                    GroupRequests   = RequestDTOs,
                    PendingRequest  = (CurrentUserAsRequestUser != null) ? true : false,
                    ExistingMember  = (CurrentUserAsMember != null) ? true : false,
                    Admin           = (GroupAdmin != null) ? true : false
                };
                foreach (var member in groupMembers)
                {
                    var GroupMemberDTO = new GroupMemberDTO()
                    {
                        Id       = member.Id,
                        Username = member.Username,
                        UserId   = member.UserId,
                        Type     = member.Type
                    };
                    GroupDetailsDTO.GroupMembers.Add(GroupMemberDTO);
                }
                return(View(GroupDetailsDTO));
            }
            catch (DomainException ex)
            {
                _logger.LogError(ex.Message);
                if (ex.Message.Equals(ErrorMessages.NotSignedIn))
                {
                    return(RedirectToAction(ActionName.Login, ControllerName.Accounts));
                }

                return(RedirectToAction(ActionName.NotFound, ControllerName.Accounts));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts));
            }
        }