Ejemplo n.º 1
0
        public async Task <IEnumerable <Subscription> > GetSubscriptionsAsync(ObjectId userID)
        {
            try
            {
                await database.Connect();

                List <Subscription>            subscriptions = new List <Subscription>();
                Domain.UserFriends.UserFriends userFriends   = (await database.Get(new EqualityFilter <ObjectId>("userID",
                                                                                                                 userID))).FirstOrDefault();

                if (userFriends == null)
                {
                    throw new ArgumentException("User with such id does not exist");
                }

                foreach (ObjectId subscriptionID in userFriends.SubscriptionsID)
                {
                    UserInfo subUser = await userService.GetAsync(subscriptionID);

                    subscriptions.Add(new Subscription()
                    {
                        BirthDate = subUser.BirthDate,
                        Name      = subUser.Name + " " + subUser.Surname
                    });
                }

                return(subscriptions);
            }
            catch (Exception ex)
            {
                exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration);
                throw new DatabaseException("Error occured while getting the subscriptions");
            }
        }
Ejemplo n.º 2
0
        public async Task AddSubscriberAsync(string token, ObjectId subscriberID)
        {
            try
            {
                ObjectId userID = ObjectId.Parse(tokenService.GetTokenClaim(token, "ID"));

                await database.Connect().ConfigureAwait(false);

                Domain.UserFriends.UserFriends userFriends = (await database.Get(new EqualityFilter <ObjectId>("userID",
                                                                                                               userID))).FirstOrDefault();
                userFriends.SubscribersID.Add(subscriberID);

                await database.Update(userFriends, new[] { "SubscribersID" }).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration);
                throw new DatabaseException("Error occured while adding a new subscriber.");
            }
        }
Ejemplo n.º 3
0
        public async Task CreateUserFriendsRecord(ObjectId userID)
        {
            try
            {
                var userFriends = new Domain.UserFriends.UserFriends()
                {
                    ID              = ObjectId.GenerateNewId(),
                    SubscribersID   = new List <ObjectId>(),
                    SubscriptionsID = new List <ObjectId>(),
                    UserID          = userID
                };

                await database.Connect().ConfigureAwait(false);

                await database.Insert(userFriends).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration);
                throw new DatabaseException("The error occured when creating the user friends object", ex);
            }
        }