The photo data contract.
        /// <summary>
        /// Converts the given data model into a photo data contract.
        /// </summary>
        /// <param name="photo">The data model.</param>
        /// <returns>The photo data contract.</returns>
        public static PhotoContract ToDataContract(this Photo photo)
        {
            var photoContract = new PhotoContract
            {
                Id = photo.Id,
                CategoryId = photo.CategoryId,
                CategoryName = photo.CategoryName,
                ThumbnailUrl = photo.ThumbnailUrl,
                StandardUrl = photo.StandardUrl,
                HighResolutionUrl = photo.HighResolutionUrl,
                User = photo.User.ToDataContract(),
                Description = photo.Caption,
                CreatedAt = photo.CreatedAt,
                NumberOfAnnotations = photo.NumberOfAnnotations,
                NumberOfGoldVotes = photo.GoldCount,
                Status = photo.Status
            };

            return photoContract;
        }
 /// <summary>
 /// Updates the status of the stored photo.
 /// </summary>
 /// <param name="photoContract">Photo object.</param>
 /// <returns>PhotoContract containing updated data.</returns>
 public Task<PhotoContract> UpdatePhotoStatus(PhotoContract photoContract)
 {
     return _repository.UpdatePhotoStatus(photoContract);
 }
 /// <summary>
 /// Insert the photo object into storage.
 /// </summary>
 /// <param name="photo">Photo Object.</param>
 /// <param name="goldIncrement">Gold to award for new photo.</param>
 /// <returns>New PhotoContract with updated user balance.</returns>
 public Task<PhotoContract> InsertPhoto(PhotoContract photo, int goldIncrement)
 {
     return _repository.InsertPhoto(photo, goldIncrement);
 }
        /// <summary>
        /// Updates an existing photo object's category and description fields.
        /// </summary>
        /// <param name="photoContract">Photo object.</param>
        /// <returns>PhotoContract containing updated data.</returns>
        public async Task<PhotoContract> UpdatePhoto(PhotoContract photoContract)
        {
            var photoDocument = GetPhotoDocument(photoContract.Id);

            photoDocument.CategoryId = photoContract.CategoryId;
            photoDocument.Description = photoContract.Description;

            await ReplacePhotoDocument(photoDocument);

            return photoContract;
        }
        /// <summary>
        /// Updates the status of the stored photo object.
        /// </summary>
        /// <param name="photoContract">Photo object.</param>
        /// <returns>PhotoContract containing updated data.</returns>
        public async Task<PhotoContract> UpdatePhotoStatus(PhotoContract photoContract)
        {
            var photoDocument = GetPhotoDocument(photoContract.Id);

            photoDocument.Status = photoContract.Status;

            await ReplacePhotoDocument(photoDocument);

            return photoContract;
        }
Beispiel #6
0
        /// <summary>
        /// Uploads the photo.
        /// </summary>
        /// <param name="stream">The memory stream.</param>
        /// <param name="localPath">The local path.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="categoryId">The id of the assocaited category.</param>
        /// <returns>The uploaded photo.</returns>
        public async Task<Photo> UploadPhoto(Stream stream, string localPath, string caption, string categoryId)
        {
            try
            {
                var sasContracts = await GetSasUrls();

                // Upload photos into azure
                foreach (var sasContract in sasContracts)
                {
                    var blob =
                        new CloudBlockBlob(
                            new Uri($"{sasContract.FullBlobUri}{sasContract.SasToken}"));

                    var sideLength = sasContract.SasPhotoType.ToSideLength();

                    var resizedStream = await BitmapTools.Resize(
                        stream.AsRandomAccessStream(), sideLength,
                        sideLength);

                    await blob.UploadFromStreamAsync(resizedStream);
                }

                var photoContract = new PhotoContract
                {
                    CategoryId = categoryId,
                    Description = caption,
                    OSPlatform = AnalyticsInfo.VersionInfo.DeviceFamily,
                    User = AppEnvironment.Instance.CurrentUser.ToDataContract(),
                    ThumbnailUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Thumbnail)?
                            .FullBlobUri.ToString(),
                    StandardUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Standard)?
                            .FullBlobUri.ToString(),
                    HighResolutionUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.HighRes)?
                            .FullBlobUri.ToString()
                };

                var responsePhotoContract =
                    await _mobileServiceClient.InvokeApiAsync<PhotoContract, PhotoContract>("Photo",
                        photoContract,
                        HttpMethod.Post, 
                        null);

                return responsePhotoContract.ToDataModel();
            }
            catch (Exception e)
            {
                throw new ServiceException("UploadPhoto error", e);
            }
        }
        /// <summary>
        /// Insert the photo object into storage.
        /// </summary>
        /// <param name="photo">Photo Object.</param>
        /// <param name="goldIncrement">Gold to award for new photo.</param>
        /// <returns>New PhotoContract with updated user balance.</returns>
        public async Task<PhotoContract> InsertPhoto(PhotoContract photo, int goldIncrement)
        {
            var document = _documentClient.CreateDocumentQuery<PhotoDocument>(DocumentCollectionUri)
                .Where(d => d.DocumentType == PhotoDocument.DocumentTypeIdentifier)
                .Where(d => d.DocumentVersion == _currentDocumentVersion)
                .Where(u => u.Id == photo.Id)
                .AsEnumerable().FirstOrDefault();

            if (document != null)
            {
                throw new DataLayerException(DataLayerError.DuplicateKeyInsert, $"Photo with Id={photo.Id} already exists");
            }

            var photoDocument = PhotoDocument.CreateFromContract(photo);
            photoDocument.Status = PhotoStatus.Active;
            photoDocument.CreatedDateTime.Date = DateTime.UtcNow;
            photoDocument.ModifiedAt = photoDocument.CreatedDateTime;

            // Set the category name on insert
            photoDocument.CategoryName = GetCategoryDocument(photo.CategoryId).Name;

            var resourceResponse = await _documentClient.CreateDocumentAsync(DocumentCollectionUri, photoDocument);

            // Handle gold balance changes and create transaction record
            await ExecuteGoldTransactionSproc(photo.User.UserId, SystemUserId, goldIncrement,
                GoldTransactionType.PhotoGoldTransaction, resourceResponse.Resource.Id);

            return await GetPhoto(resourceResponse.Resource.Id);
        }
        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);
            }
        }
        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 Task<PhotoContract> UpdatePhotoStatus(PhotoContract photoContract)
 {
     throw new NotImplementedException();
 }
 public Task<PhotoContract> InsertPhoto(PhotoContract photo, int goldIncrement)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Creates a <see cref="PhotoDocument" /> from a <see cref="PhotoContract" />.
 /// </summary>
 /// <returns>The <see cref="PhotoDocument" />.</returns>
 public static PhotoDocument CreateFromContract(PhotoContract contract)
 {
     return new PhotoDocument
     {
         Id = contract.Id,
         CategoryId = contract.CategoryId,
         CategoryName = contract.CategoryName,
         UserId = contract.User.UserId,
         ThumbnailUrl = contract.ThumbnailUrl,
         StandardUrl = contract.StandardUrl,
         HighResolutionUrl = contract.HighResolutionUrl,
         Description = contract.Description,
         CreatedDateTime = new DateDocument
         {
             Date = contract.CreatedAt
         },
         ModifiedAt = new DateDocument
         {
             Date = contract.CreatedAt
         },
         Annotations =
             contract.Annotations?.Select(AnnotationDocument.CreateFromContract).ToList() ??
             new List<AnnotationDocument>(),
         GoldCount = contract.NumberOfGoldVotes,
         OSPlatform = contract.OSPlatform,
         Status = contract.Status
     };
 }