/// <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) { _telemetryClient.TrackException(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) { _telemetryClient.TrackException(e); throw new ServiceException("PostAnnotation error", e); } }
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> /// Converts the given contract to a data model. /// </summary> /// <param name="annotationContract">The data contract.</param> /// <returns>The data model.</returns> public static Annotation ToDataModel(this AnnotationContract annotationContract) { return(new Annotation { CreatedTime = annotationContract.CreatedAt, Text = annotationContract.Text, From = annotationContract.From.ToDataModel(), Id = annotationContract.Id, PhotoId = annotationContract.PhotoId, GoldCount = annotationContract.GoldCount }); }
/// <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 Task <AnnotationContract> InsertAnnotation(AnnotationContract annotation) { throw new NotImplementedException(); }
/// <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)); }