示例#1
0
        public async Task <GenericResponseMessage> Update(User user)
        {
            var response = new GenericResponseMessage();

            try
            {
                var currentUser = await _userRepository.GetById(user.Id);

                if (user.Username == currentUser.Username)
                {
                    await _userRepository.Update(user);

                    response.IsSuccessful = true;
                    response.Message      = "Successfully updated user.";
                }
                else
                {
                    response.IsSuccessful = false;
                    response.Message      = "You can not update the username field.";
                }
            }
            catch (Exception)
            {
                response.IsSuccessful = false;
                response.Message      = $"Could not update user.";
            }

            return(response);
        }
示例#2
0
        public static GenericResponseMessage RejectFriendRequest(int thisUser, int thatUser)
        {
            GenericResponseMessage response = new GenericResponseMessage();

            var friendRequest = GetLatestFriendRequestMadeByThatUserToThisUser(thatUser, thisUser);

            if (friendRequest == null || friendRequest.RequestStatus != (int)FriendRequestStatusType.Requested)
            {
                response.AddError(MessageCodes.ThereIsNoRequestToReject);
            }

            if (response.IsSuccessful)
            {
                UserFriendRequestDO ufDataObj = new UserFriendRequestDO();
                friendRequest.RequestStatus = (int)FriendRequestStatusType.Rejected;
                ObjectMapper.MapObjects <USR_FriendRequest, UserFriendRequestDO>(friendRequest, ufDataObj);
                SaveFriendRequest(ufDataObj, thisUser);

                if (GetThisUsersFriendsIds(thisUser).Contains(thatUser))
                {
                    DeleteFriend(thatUser, thisUser);
                }
            }

            return(response);
        }
示例#3
0
        public async Task <GenericResponseMessage> Delete(Guid id)
        {
            var response = new GenericResponseMessage();

            try
            {
                await _userRepository.Delete(id);

                response.IsSuccessful = true;
                response.Message      = "Successfully deleted user.";
            }
            catch (Exception)
            {
                response.IsSuccessful = false;
                response.Message      = $"Could not delete user.";
            }

            return(response);
        }
示例#4
0
        public async Task <GenericResponseMessage> Update(Assignment assignment)
        {
            var response = new GenericResponseMessage();

            try
            {
                await _assignmentRepository.Update(assignment);

                response.IsSuccessful = true;
                response.Message      = "Successfully updated assignment.";
            }
            catch (Exception)
            {
                response.IsSuccessful = false;
                response.Message      = $"Could not update assignment.";
            }

            return(response);
        }
示例#5
0
        public async Task <GenericResponseMessage> SignUp(User user)
        {
            var response = new GenericResponseMessage();

            try
            {
                var existingUser = await _userRepository.GetByUsername(user.Username);

                if (existingUser != null)
                {
                    response.Message      = "A user with these credentials already exists.";
                    response.IsSuccessful = false;
                    return(response);
                }
                else
                {
                    byte[] salt       = Guid.NewGuid().ToByteArray();
                    var    saltedHash = new Rfc2898DeriveBytes(user.Password, salt, 1000);
                    var    pass       = saltedHash.GetBytes(20);
                    var    passString = Convert.ToBase64String(pass);
                    var    saltString = Convert.ToBase64String(saltedHash.Salt);

                    user.Salt     = saltString;
                    user.Password = passString;
                    user.IsAdmin  = false;

                    await _userRepository.Create(user);

                    response.Message      = "Successfully signed up.";
                    response.IsSuccessful = true;
                    return(response);
                }
            }
            catch (Exception)
            {
                response.IsSuccessful = false;
                response.Message      = "Something went wrong. Please try again.";
                return(response);
            }
        }
示例#6
0
        public static GenericResponseMessage AcceptFriendRequest(int thisUser, int thatUser)
        {
            GenericResponseMessage response = new GenericResponseMessage();

            var request = GetLatestFriendRequestMadeByThatUserToThisUser(thatUser, thisUser);

            if (GetThisUsersFriendsIds(thisUser).Contains(thatUser))
            {
                response.AddError(MessageCodes.TheseUsersAreAlreadyFriends);
            }
            else
            {
                if (request == null)
                {
                    response.AddError(MessageCodes.ThereIsNoRequestToAccept);
                }
                else if (request.RequestStatus != (int)FriendRequestStatusType.Requested)
                {
                    response.AddError(MessageCodes.ThereIsNoRequestToAccept);
                }
            }

            if (response.IsSuccessful)
            {
                UserFriendRequestDO ufDataObj = new UserFriendRequestDO();
                request.RequestStatus = (int)FriendRequestStatusType.Friends;
                ObjectMapper.MapObjects(request, ufDataObj);
                SaveFriendRequest(ufDataObj, thisUser);

                UserFriendDO uDataObj = new UserFriendDO();
                uDataObj.UserID       = thisUser;
                uDataObj.FriendUserID = thatUser;
                SaveFriend(uDataObj, thisUser);
            }

            return(response);
        }