public static int?AddPhoto(int id, string path) { PhotoRepository photoRep = new PhotoRepository(); photoRep.Create(new Photo { PetId = id, Path = path }); return(photoRep.FindPhoto(id, path)); }
public void CreatePhotoTest() { byte[] rawData = File.ReadAllBytes(@"D:\Users\Joel\Documents\GitHub\FarmPhoto\FarmPhoto\FarmPhoto.Website\App_Data\117700.jpg"); FileInfo info = new FileInfo(@"D:\Users\Joel\Documents\GitHub\FarmPhoto\FarmPhoto\FarmPhoto.Website\App_Data\117700.jpg"); int fileSize = Convert.ToInt32(info.Length); int photoId = _photoRepository.Create(new Photo { Title = "New Photo", Description = "Blah", ImageType = "image/jpeg", PhotoData = rawData, FileSize = fileSize, UserId = 1 }); Assert.IsTrue(photoId > 0); }
public ActionResult Create(Product product, int ProductModelId, int ProductCategoryID, int ProductSubCategoryID, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null) { IRepositoryBase <Photo> repositoryPhoto = new PhotoRepository(); Photo photo = new Photo(); photo.Name = file.FileName; photo.Image = new byte[file.ContentLength]; file.InputStream.Read(photo.Image, 0, file.ContentLength); product.PhotoID = repositoryPhoto.Create(photo); } (repository as IProductRepository).Create(product, ProductModelId, ProductCategoryID, ProductSubCategoryID); return(RedirectToAction("Index")); } else { return(View(product)); } }
public ActionResult AddPhoto(NewPhotoModel photo) { UserModel user = new UserRepository().GetByUsernameWithAlbums(HttpContext.User.Identity.Name, withFollowers: true); ViewBag.Albums = user.Albums; if (photo.Source == "remote") photo.FileInput = null; else photo.PhotoURL = null; ITransaction transaction = null; ISession session = null; try { using (Image img = FileHelper.PrepareImageFromRemoteOrLocal(photo)) { if (img == null) throw new FileUploadException("Can't upload your photo. Please try again later."); if (ModelState.IsValid) { AlbumModel selectedAlbum = null; foreach (AlbumModel album in user.Albums) { if (album.Id == photo.AlbumId) { selectedAlbum = album; break; } } session = SessionProvider.SessionFactory.OpenSession(); transaction = session.BeginTransaction(); string photoName = "photo_" + DateTime.Now.ToString("yyyyMMddHHmmssff"); string path = FileHelper.getPhotoPathWithoutExtension(selectedAlbum, photoName) + ".jpg"; if (string.IsNullOrEmpty(path)) throw new Exception("Can't save image"); path = FileHelper.SavePhoto(img, selectedAlbum, photoName); if (string.IsNullOrEmpty(path)) throw new Exception("Returned path is empty"); PhotoRepository repo = new PhotoRepository(); PhotoModel newPhoto = new PhotoModel() { Path = path, Date = DateTime.Parse(photo.Date), Description = photo.Description, Album = selectedAlbum }; double? locLatitude = img.GPSLatitude(); double? locLongitude = img.GPSLongitude(); if (locLatitude.HasValue && locLongitude.HasValue) { newPhoto.LocationLatitude = locLatitude.Value; newPhoto.LocationLongitude = locLongitude.Value; } repo.Create(newPhoto); System.Diagnostics.Debug.WriteLine("Created db entry " + newPhoto.Id); transaction.Commit(); Helpers.NotifyAlbumObservers(newPhoto.Album); return RedirectToAction("Show", new { id = photo.AlbumId }); } } } catch (FileUploadException ex) { if (transaction != null) { transaction.Rollback(); transaction.Dispose(); } if (session != null) session.Dispose(); ModelState.AddModelError("FileInput", ex.Message); } catch (Exception) { if (transaction != null) { transaction.Rollback(); transaction.Dispose(); } if (session != null) session.Dispose(); ModelState.AddModelError("FileInput", "Can't upload your photo. Please try again later."); } return View(photo); }