Exemplo n.º 1
0
        /// <summary>
        /// Subscribe to group
        /// </summary>
        /// <param name="userID">The active user id</param>
        /// <param name="token">The subscribe token</param>
        /// <returns>true if succesfull subscribed</returns>
        public bool SubscribeToGroup(int userID, string token)
        {
            User user = _unitOfWork.UserRepository.GetUser(userID);

            if (!_tokenHandler.ValidateSubscribeToken(token, user))
            {
                _logger.LogError($"Could not subscribe user {userID} to group: Invalid subscribtion token");
                throw new UnauthorizedAccessException("User has no persion to subscribe to group");
            }

            int groupID = _tokenHandler.GetGroupID(token);

            Subscription subscription = new Subscription()
            {
                DateOfSubscription = DateTime.Now,
                GroupID            = groupID,
                UserID             = user.ID
            };

            try
            {
                _unitOfWork.SubscriptionRepository.SubscribeUser(groupID, userID);
                _unitOfWork.SaveChanges();
            }
            catch (Exception exception)
            {
                _logger.LogError($"Could not add subscription to database");
                throw exception;
            }

            return(true);
        }