Exemplo n.º 1
0
 public async Task<HttpResponseMessage> Post(SaveUrlRequest model)
 {
     if (!ModelState.IsValid)
     {
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
     ValidationResult<SavedUrl> result = await _urlService.Save(User.Identity.GetUserId(), model);
     if (result.Failed)
     {
         result.AddToModelState(ModelState);
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
     return Request.CreateResponse(HttpStatusCode.OK, result.Item);
 }
Exemplo n.º 2
0
        public async Task<ValidationResult<SavedUrl>> Save(string userId, SaveUrlRequest model)
        {
            NoSqlSavedUrl savedUrl = new NoSqlSavedUrl(userId, model.Url)
            {
                Description = model.Description,
                NumberOfVisits = 0,
                SavedAt = DateTimeOffset.UtcNow,
                Title = model.Title,
                Tags = model.Tags
            };

            SavedUrlQueueItem queueItem = new SavedUrlQueueItem
            {
                Url = model.Url,
                UserId = userId
            };

            try
            {
                await _urlRepository.Save(savedUrl);
            }
            catch (UniqueKeyViolation)
            {
                ValidationResult<SavedUrl> result = new ValidationResult<SavedUrl>();
                result.AddError("Link already exists");
                return result;
            }

            await Task.WhenAll(new[]
            {
                _urlRepository.Save(new DateOrderedUrl(userId, savedUrl.SavedAt, savedUrl.Url)),
                _queue.EnqueueAsync(queueItem)
            });
            
            return new ValidationResult<SavedUrl>(_mapperFactory.GetSavedUrlMapper().Map(savedUrl));
        }