Beispiel #1
0
        public async Task <SavePostedItemResponse> SavePostedItem(AddPhotoViewModel addPhotoViewModel, string userId)
        {
            var response = new SavePostedItemResponse();

            var user = _applicationDbContext.Users.FirstOrDefault(u => u.Id == userId);

            if (user == null)
            {
                response.Success      = false;
                response.ErrorMessage = "Problem saving your post.";
                return(response);
            }

            var postedItem = await SavePostedItemToDb(addPhotoViewModel);

            var saveImageWrapper = await SavePhotos(addPhotoViewModel, postedItem);


            postedItem.ApplicationUser   = user;
            postedItem.ApplicationUserId = user.Id;

            if (postedItem.Id > 0 && postedItem.Photo != null && !string.IsNullOrEmpty(postedItem.PhotoUrl))
            {
                await _applicationDbContext.SaveChangesAsync();

                response.PostedItem = postedItem;
                response.Success    = true;
            }
            //If image is not saved lets not create the posted item
            else if (!String.IsNullOrEmpty(saveImageWrapper.Url) && String.IsNullOrEmpty(saveImageWrapper.ErrorMessage))
            {
                postedItem.PhotoUrl = saveImageWrapper.Url;

                //Save the url as well
                await _applicationDbContext.SaveChangesAsync();

                response.PostedItem = postedItem;
                response.Success    = true;
            }
            else
            {
                response.Success      = false;
                response.ErrorMessage = saveImageWrapper.ErrorMessage;
            }

            await _cacheService.RemovePostedItemFromCache(postedItem.Id.ToString());

            var postedItemViewModel = ConvertFromPostedItemToViewModel.Convert(postedItem, userId);
            await _cacheService.AddPostedItemToCache(postedItem.Id.ToString(), postedItemViewModel);

            return(response);
        }