The annotation data contract.
 /// <summary>
 /// Creates a <see cref="AnnotationDocument" /> from a <see cref="AnnotationContract" />.
 /// </summary>
 /// <returns>The <see cref="AnnotationDocument" />.</returns>
 public static AnnotationDocument CreateFromContract(AnnotationContract contract)
 {
     return new AnnotationDocument
     {
         CreatedDateTime = new DateDocument
         {
             Date = contract.CreatedAt
         },
         Text = contract.Text,
         Id = contract.Id,
         GoldCount = contract.GoldCount,
         From = contract.From.UserId
     };
 }
        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);
            }
        }
 /// <summary>
 /// Inserts the annotation object and performs the required gold transactions.
 /// </summary>
 /// <param name="annotation">Annotation to be inserted.</param>
 /// <returns>AnnotationContract.</returns>
 public Task<AnnotationContract> InsertAnnotation(AnnotationContract annotation)
 {
     return _repository.InsertAnnotation(annotation);
 }
        /// <summary>
        /// Inserts the annotation object and performs the required gold transactions.
        /// </summary>
        /// <param name="annotationContract">Annotation to be inserted.</param>
        /// <returns>AnnotationContract.</returns>
        public async Task<AnnotationContract> InsertAnnotation(AnnotationContract annotationContract)
        {
            var annotationDocument = AnnotationDocument.CreateFromContract(annotationContract);

            annotationDocument.Id = Guid.NewGuid().ToString();
            annotationContract.Id = annotationDocument.Id;
            annotationDocument.CreatedDateTime = new DateDocument
            {
                Date = DateTime.UtcNow
            };

            var photoDocument = PhotoDocument.CreateFromContract(await GetPhoto(annotationContract.PhotoId));

            if (photoDocument.Annotations.Any(a => a.Id == annotationContract.Id))
            {
                throw new DataLayerException(DataLayerError.DuplicateKeyInsert, $"Annotation with Id={annotationContract.Id} already exists");
            }

            photoDocument.Annotations.Add(annotationDocument);
            photoDocument.GoldCount += annotationContract.GoldCount;

            // Handle gold balance changes and create transaction record
            await
                ExecuteGoldTransactionSproc(photoDocument.UserId, annotationContract.From.UserId,
                    annotationContract.GoldCount,
                    GoldTransactionType.PhotoGoldTransaction, annotationContract.PhotoId);

            await ReplacePhotoDocument(photoDocument);

            return annotationContract;
        }
示例#5
0
        /// <summary>
        /// Posts the annotation.
        /// </summary>
        /// <param name="photo">The photo.</param>
        /// <param name="annotationText">The text.</param>
        /// <param name="goldCount">The amount of gold being given.</param>
        /// <returns>The annotation including the id.</returns>
        public async Task<Annotation> PostAnnotation(Photo photo, string annotationText, int goldCount)
        {
            try
            {
                var annotationContract = new AnnotationContract
                {
                    CreatedAt = DateTime.Now,
                    Text = annotationText,
                    From = AppEnvironment.Instance.CurrentUser.ToDataContract(),
                    PhotoId = photo.Id,
                    PhotoOwnerId = photo.User.UserId,
                    GoldCount = goldCount
                };

                var result =
                    await _mobileServiceClient.InvokeApiAsync<AnnotationContract, AnnotationContract>("annotation",
                        annotationContract,
                        HttpMethod.Post,
                        new Dictionary<string, string>());

                if (result != null)
                {
                    photo.GoldCount += result.GoldCount;

                    // In this case we know how much gold we have given,
                    // so we can reduce the cached amount instead of reloading
                    // the current user.
                    if (AppEnvironment.Instance.CurrentUser != null)
                    {
                        AppEnvironment.Instance.CurrentUser.GoldBalance -= result.GoldCount;
                    }
                }

                return result.ToDataModel();
            }
            catch (MobileServiceInvalidOperationException invalidOperationException)
            {
                var content = await invalidOperationException.Response.Content.ReadAsStringAsync();
                var serviceFault = TryGetServiceFault(content);

                if (invalidOperationException.Response.StatusCode == HttpStatusCode.Forbidden
                    && serviceFault?.Code == 3999)
                {
                    throw new InsufficientBalanceException("Users balance is too low",
                        invalidOperationException);
                }

                throw new ServiceException("PostAnnotation error", invalidOperationException);
            }
            catch (Exception e)
            {
                throw new ServiceException("PostAnnotation error", e);
            }
        }
 public Task<AnnotationContract> InsertAnnotation(AnnotationContract annotation)
 {
     throw new NotImplementedException();
 }