Beispiel #1
0
        public async Task <PhotoContract> PutAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("Auth: PhotoController PutAsync invoked");

                var registrationReference = await ValidateAndReturnCurrentUserId();

                if (!await _photoValidation.IsUserPhotoOwner(registrationReference, photo.Id))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                return(await _repository.UpdatePhoto(photo));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #2
0
        public async Task <UserContract> GetUser()
        {
            _telemetryClient.TrackEvent("UserController GetUser invoked");

            var registrationReference = await ValidateAndReturnCurrentUserId();

            try
            {
                var currentUser = await _repository.GetUser(null, registrationReference);

                // We need to add a record for the current authenticated user
                if (currentUser.RegistrationReference == null)
                {
                    _telemetryClient.TrackEvent("No user found, creating new user");

                    currentUser = await _repository.CreateUser(registrationReference);
                }

                return(currentUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #3
0
        public async Task <LeaderboardContract> GetAsync([FromUri] int mostGoldCategoriesCount,
                                                         [FromUri] int mostGoldPhotosCount,
                                                         [FromUri] int mostGoldUsersCount,
                                                         [FromUri] int mostGivingUsersCount)
        {
            try
            {
                _telemetryClient.TrackEvent("LeaderboardController GetAsync invoked");

                var leaderboardContract =
                    await
                    _repository.GetLeaderboard(mostGoldCategoriesCount, mostGoldPhotosCount, mostGoldUsersCount,
                                               mostGivingUsersCount);

                return(leaderboardContract);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #4
0
        public async Task <UserContract> UpdateUserProfile(UserContract user)
        {
            try
            {
                _telemetryClient.TrackEvent("UserController UpdateUserProfile invoked");

                var currentUserId = await ValidateAndReturnCurrentUserId();

                if (!currentUserId.Equals(user.RegistrationReference))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                var existingUser = await _repository.UpdateUser(user);

                return(existingUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <CategoryContract> PostAsync(string name)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                var sanitizedName = name.TrimAndReplaceMultipleWhitespaces().ToTitleCase();

                return(await _repository.CreateCategory(sanitizedName));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }
                if (ex.Error == DataLayerError.DuplicateKeyInsert)
                {
                    throw ServiceExceptions.DuplicateCategoryException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #6
0
        public async Task <PagedResponse <PhotoContract> > GetPagedAsync([FromUri] string continuationToken)
        {
            try
            {
                _telemetryClient.TrackEvent("UserPhotoController GetPagedAsync invoked");

                var registrationReference = await ValidateAndReturnCurrentUserId();

                var user = await _repository.GetUser(null, registrationReference);

                var stream = await _repository.GetUserPhotoStream(user.UserId, continuationToken);

                return(stream);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <ReportContract> PostAsync(ReportContract report)
        {
            try
            {
                var registrationReference = await ValidateAndReturnCurrentUserId();

                var reportContract = await _repository.InsertReport(report, registrationReference);

                var photoContract = await _repository.GetPhoto(reportContract.ContentId);

                if (photoContract.Status == PhotoStatus.UnderReview)
                {
                    await _notificationHandler.SendPushAsync(PushNotificationPlatform.Windows,
                                                             "user:"******"Your photo has been placed under review.",
                                                             photoContract.ThumbnailUrl, photoContract.Id);
                }

                return(reportContract);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #8
0
        public async Task <AnnotationContract> PostAsync(AnnotationContract annotation)
        {
            try
            {
                _telemetryClient.TrackEvent("AnnotationController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                // Get the Gold gifting user.
                var fromUser = await _repository.GetUser(annotation.From.UserId);

                // Check to see if the gifting user has enough of a balance to support gift.
                if ((fromUser.GoldBalance - annotation.GoldCount) < 0)
                {
                    throw ServiceExceptions.UserBalanceTooLow();
                }

                var insertedAnnotation = await _repository.InsertAnnotation(annotation);

                var photoContract = await _repository.GetPhoto(annotation.PhotoId);

                var receiver = await _repository.GetUser(photoContract.User.UserId);

                var goldBalance = receiver.GoldBalance;

                try
                {
                    _telemetryClient.TrackEvent("Gold received Push notification invoked.");

                    // Send push notification to the user receiving Gold
                    await
                    _notificationHandler.SendPushAsync(PushNotificationPlatform.Windows,
                                                       "user:"******"You have received GOLD!",
                                                       photoContract.ThumbnailUrl, annotation.PhotoId, goldBalance);
                }
                catch (Exception e)
                {
                    _telemetryClient.TrackException(e);
                }

                return(insertedAnnotation);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #9
0
        public async Task <PhotoContract> GetAsync(string id)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController GetAsync invoked");

                return(await _repository.GetPhoto(id));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <ReportContract> PostAsync(ReportContract report)
        {
            try
            {
                var registrationReference = await ValidateAndReturnCurrentUserId();

                return(await _repository.InsertReport(report, registrationReference));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #11
0
        public async Task <UserContract> UpdateUserProfile(UserContract user)
        {
            try
            {
                _telemetryClient.TrackEvent("UserController UpdateUserProfile invoked");

                var currentUserId = await ValidateAndReturnCurrentUserId();

                // A user should only be able to update his/her own profile.
                if (!currentUserId.Equals(user.RegistrationReference))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                // Check if the user owns the photo that is passed in as profile photo
                var photo = await _repository.GetPhoto(user.ProfilePhotoId);

                if (!photo.User.UserId.Equals(user.UserId))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                // Refreshing profile photo url
                user.ProfilePhotoUrl = photo.ThumbnailUrl;

                var existingUser = await _repository.UpdateUser(user);

                return(existingUser);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #12
0
        public async Task DeleteAsync(string id)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController DeleteAsync invoked");
                var registrationReference = await ValidateAndReturnCurrentUserId();

                await _repository.DeletePhoto(id, registrationReference);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <PhotoContract> > GetAsync([FromUri] int count)
        {
            try
            {
                _telemetryClient.TrackEvent("HeroPhotoController GetAsync invoked ");
                var daysOld = int.Parse(WebConfigurationManager.AppSettings["HeroImagesDaysOld"]);

                return(await _repository.GetHeroPhotos(count, daysOld));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <PagedResponse <PhotoContract> > GetAsync(string id, [FromUri] string continuationToken = null)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync(id, continuation) invoked");

                var stream = await _repository.GetCategoryPhotoStream(id, continuationToken);

                return(stream);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <CategoryContract> > GetAsync()
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync invoked");

                var categories = await _repository.GetCategories();

                return(categories);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <IList <CategoryPreviewContract> > GetAsync([FromUri] int numberOfThumbnails)
        {
            try
            {
                _telemetryClient.TrackEvent("CategoryController GetAsync(numberOfThumbnails) invoked");
                var categoriesPreviewList = await _repository.GetCategoriesPreview(numberOfThumbnails);

                // Order by categories with the latest photo added.
                IList <CategoryPreviewContract> orderedCategoriesPreviewList =
                    categoriesPreviewList.OrderByDescending(x => x.PhotoThumbnails[0].CreatedAt).ToList();
                return(orderedCategoriesPreviewList);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Beispiel #17
0
        public async Task <PhotoContract> PostAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                int goldIncrement;
                int.TryParse(WebConfigurationManager.AppSettings["NewPhotoAward"], out goldIncrement);

                return(await _repository.InsertPhoto(photo, goldIncrement));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
        public async Task <UserContract> PostAsync(StringWrapper receipt)
        {
            _telemetryClient.TrackEvent("IapController PostAsync invoked");

            if (receipt == null)
            {
                throw ServiceExceptions.IapValidationException("IapController: Receipt is null");
            }

            var registrationReference = await ValidateAndReturnCurrentUserId();

            UserContract returnUser;

            try
            {
                int goldValue;

                var xmlReceipt = new XmlDocument();
                xmlReceipt.LoadXml(receipt.Data);

                var iapReceipt = _iapValidator.ValidateXmlSignature(xmlReceipt);

                var user = await _repository.GetUser(null, registrationReference);

                iapReceipt.UserId = user.UserId;

                var productKey = "Iap";

                if (!string.IsNullOrEmpty(iapReceipt.ProductId))
                {
                    productKey = "Iap" + iapReceipt.ProductId.Trim();
                    int.TryParse(WebConfigurationManager.AppSettings[productKey], out goldValue);
                }
                else
                {
                    throw ServiceExceptions.IapValidationException(
                              $"IapController: Product not found in configuration: {productKey}");
                }

                iapReceipt.GoldIncrement = goldValue;

                returnUser = await _repository.InsertIapPurchase(iapReceipt);
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
            catch (IapValidationException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == IapValidationError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.IapValidationException(ex.Message);
            }

            return(returnUser);
        }