// GET: Photos/Add public ActionResult Add(string albumName) { AddPhotoViewModel vm = new AddPhotoViewModel(); vm.AlbumName = albumName; return(View(vm)); }
public async Task <JsonResult> AddPhoto(AddPhotoViewModel addPhotoViewModel) { //1. if new image is added, PostedId is null but PhotoData is not null //2. if existing image is updated, PostedId is not null but PhotoData is null if (addPhotoViewModel.PostedId == null && String.IsNullOrEmpty(addPhotoViewModel.PhotoData)) { ModelState.AddModelError("PhotoData", "Please add an image."); } //1. Is Model valid if (addPhotoViewModel != null && ModelState.IsValid) { var id = HttpContext.User.Identity.GetUserId(); var result = await _postedItemService.SavePostedItem(addPhotoViewModel, id); if (result.Success) { var postedItem = GetPostedItem(result.PostedItem); return(Json(postedItem)); } else { ModelState.AddModelError("SavingError", result.ErrorMessage); } } var modelErrors = ModelState.GetErrorsForJson(); return(Json(new { status = "error", errors = modelErrors, success = false, message = "Could not save/update this post." })); }
public AddPhotoPage() { InitializeComponent(); _addPhotoViewModel = new AddPhotoViewModel(); BindingContext = _addPhotoViewModel; ProgenyCollectionView.ItemsSource = _addPhotoViewModel.ProgenyCollection; }
public ActionResult AddPhoto(AddPhotoViewModel viewModel, HttpPostedFileBase NewImage) { var image = UploadPicture(NewImage, viewModel.Caption); // viewModel.Image = image; when the internet is good uncomment viewModel.Message = "Image Below successfully uploaded"; return View(viewModel); }
private Photo GetMappedPhoto(AddPhotoViewModel viewModel) { var photo = _mapper.Map <Photo>(viewModel); photo.CreationDate = DateTime.Now; photo.Path = UploadFile(viewModel.File, photo.CreationDate); return(photo); }
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); }
public AddPhoto(string fk) { try { InitializeComponent(); BindingContext = new AddPhotoViewModel(Navigation, fk); NavigationPage.SetHasNavigationBar(this, false); } catch (Exception e) { var myerror = e.Message; // error // Log.Fatal(e); }; }
private async Task <PostedItem> SavePostedItemToDb(AddPhotoViewModel addPhotoViewModel) { if (addPhotoViewModel == null) { return(null); } PostedItem postedItem = null; var timeNow = DateTime.UtcNow; //if we already have it in database if (addPhotoViewModel.PostedId != null) { postedItem = await _applicationDbContext.PostedItems.FirstOrDefaultAsync(p => p.Id == addPhotoViewModel.PostedId.Value); if (postedItem != null) { postedItem.Title = addPhotoViewModel.Title; postedItem.Tags.Clear(); await AddTagsToPostedItem(postedItem, addPhotoViewModel.Tags); //If a new photo is uploaded lets remove the old one. if (!String.IsNullOrEmpty(addPhotoViewModel.PhotoData) && postedItem.Photo != null) { _applicationDbContext.Photos.Remove(postedItem.Photo); } } } else { postedItem = new PostedItem { Title = addPhotoViewModel.Title, Inserted = timeNow, PostedItemType = PostedItemType.UserPosted }; await AddTagsToPostedItem(postedItem, addPhotoViewModel.Tags); _applicationDbContext.PostedItems.Add(postedItem); } if (!String.IsNullOrEmpty(postedItem.Title)) { postedItem.Slug = postedItem.Title.Trim().Replace(' ', '-').Replace('.', ' ').Replace('&', '_').TrimEnd(); } postedItem.Updated = timeNow; return(postedItem); }
public JsonResult AddPhoto(AddPhotoViewModel viewModel)//GalleryID + URL + Type ? { try { var model = ModelMapper.ConvertToModel(viewModel); if (model.IsPhoto) { model.Url = this.WriteToFile(viewModel.PhotoFile); } _photoService.Create(model); return(Json(true)); } catch (Exception) { return(Json(false)); } }
public IActionResult Add(AddPhotoViewModel viewModel) { if (!ModelState.IsValid) { return(BadRequest("Photo properties are not valid.")); } if (!IsCurrentUser(viewModel.UserId)) { return(BadRequest("User credentials are not valid.")); } var photo = GetMappedPhoto(viewModel); _photoService.Add(photo); return(Ok(viewModel.AlbumId)); }
public ActionResult Create(AddPhotoViewModel addPostViewModel) { try { if (ModelState.IsValid) { Debug.WriteLine("1"); var photoList = new List <string>(); foreach (var photo in addPostViewModel.Photo) { var uploadFolder = "Upload"; var savePhotoPath = Path.Combine(_env.WebRootPath, uploadFolder, photo.FileName); using (var photoSave = new FileStream(savePhotoPath, FileMode.Create)) { photo.CopyTo(photoSave); } photoList.Add("/" + uploadFolder + "/" + photo.FileName); } Debug.WriteLine("2"); Debug.WriteLine(addPostViewModel.Caption); Debug.WriteLine(String.Join(",", photoList)); _context.GalleryImages.Add(new GalleryImage { Caption = addPostViewModel.Caption, URL = String.Join(",", photoList), IsMeme = false });; var count = _context.SaveChanges(); Debug.WriteLine("3"); return(RedirectToAction(nameof(Index))); } return(View()); } catch { return(View()); } }
public ActionResult Create(AddPhotoViewModel addViewModel, HttpPostedFileBase image = null) { if (ModelState.IsValid) { if (image != null) { var photo = new Photo() { TimeStamp = DateTime.Now, PhotoDesc = addViewModel.Description, PhotoData = new byte[image.ContentLength], PhotoTitle = addViewModel.Title, Link = addViewModel.Link, InLandingPageCarousel = addViewModel.InLandingPageCarousel, InPartnerOrgCarousel = addViewModel.InPartnerOrgCarousel, InPhotoGallery = addViewModel.InPhotoGallery, Credit = addViewModel.Credit, MimeType = image.ContentType }; image.InputStream.Read(photo.PhotoData, 0, image.ContentLength); var imageToResize = Image.FromStream(image.InputStream); photo.ThumbnailPhotoContent = GetImageThumbnail(imageToResize); db.Photos.Add(photo); db.SaveChanges(); return(RedirectToAction("Admin")); } else { return(View()); } } else { return(View()); } }
public IActionResult Add(AddPhotoViewModel addPhotoViewModel) { if (ModelState.IsValid) { PhotoDate newPhotoDate = context.Dates.Single(c => c.ID == addPhotoViewModel.DateID); Photo newPhoto = new Photo { Name = addPhotoViewModel.Name, FocalLength = addPhotoViewModel.FocalLength, Date = newPhotoDate, ShutterSpeed = addPhotoViewModel.Shutterspeed, ISO = addPhotoViewModel.Iso }; context.Photos.Add(newPhoto); context.SaveChanges(); return(Redirect("/Photo")); } return(View(addPhotoViewModel)); }
public IActionResult Add() { AddPhotoViewModel addPhotoViewModel = new AddPhotoViewModel(context.Dates.ToList()); return(View(addPhotoViewModel)); }
private async Task <SaveImageWrapper> SavePhotos(AddPhotoViewModel addPhotoViewModel, PostedItem postedItem) { var saveImageWrapper = new SaveImageWrapper(); if (addPhotoViewModel.StreamData == null) { return(saveImageWrapper); } var photoStreams = new List <Stream> { addPhotoViewModel.StreamData }; var photoDimentionInfos = new List <PhotoDimensionInfo>() { new PhotoDimensionInfo { Width = 450, Height = 450, IsOriginal = false, ReSizeAndCrop = true } }; var addPhotoRequest = new AddPhotoRequest(photoStreams, photoDimentionInfos, postedItem.Slug); var results = await _photoService.AddPhotoAsync(addPhotoRequest); //If any error found during uploading process we will not add anything to database. (ATOMIC) if (results.NewlyAddedPhotoErrorInfos != null && results.NewlyAddedPhotoErrorInfos.Count == 0) { //2. Add photo objects to News Feed object if (results != null && results.NewlyAddedPhotoInfoDetails.Count > 0) { foreach (var photo in results.NewlyAddedPhotoInfoDetails) { var newPhoto = new Photo { Name = photo.AddedPhotoName, FileExtention = photo.ImageFormat, Width = photo.Width, Height = photo.Height, Inserted = DateTime.UtcNow, PhotoType = PhotoType.PostedItem, Container = "photo" }; _applicationDbContext.Photos.Add(newPhoto); postedItem.Photo = newPhoto; } } //5. Save them to database var saveResults = await _applicationDbContext.SaveChangesAsync(); } else if (results.NewlyAddedPhotoErrorInfos.Count > 0) { saveImageWrapper.ErrorMessage = results.NewlyAddedPhotoErrorInfos[0].ErrorMessage; } //var response = new AddUserAvatarResponse(); //6. Lets get the full path on the blob for the new photo var originalPhotoDetails = results.NewlyAddedPhotoInfoDetails.FirstOrDefault(p => p.IsOriginal); var photoName = String.Format(ImageName_450x450_StringFormat, originalPhotoDetails.AddedPhotoName, postedItem.Slug, originalPhotoDetails.ImageFormat); var fullPhotoUrlOnAzureBlobServer = _photoService.GetFullPath(photoName); saveImageWrapper.Url = fullPhotoUrlOnAzureBlobServer; return(saveImageWrapper); }
public ActionResult AddPhoto() { var addPhotoViewModel = new AddPhotoViewModel(); return View(addPhotoViewModel); }