public void ShouldAddNewTrail() { IEnumerable<Trail>trails = _repositoryFactory.Repository.List<Trail>(); int priorCount =trails.Count(); Trail newTrail = new Trail { Description = "testDescription", Name = "testName", LocationId = 1, TrailTypeId = 1 }; // Begin transaction _repositoryFactory.Repository.BeginTransaction(); // Add the newtrail to the context Trail trail = _repositoryFactory.Repository.Insert(newTrail); // Save the context to the database, causing the database to be updated _repositoryFactory.Repository.SaveChanges(); // At this stage, the database has the new row, so the identity has been incremented and assigned Assert.IsTrue(trail.Id > 0); Assert.IsTrue(newTrail.Id > 0); trails = _repositoryFactory.Repository.List<Trail>(); int postCount =trails.Count(); Assert.AreEqual(postCount, priorCount + 1); // Rollback the transaction, to make the test repeatable _repositoryFactory.Repository.RollbackTransaction(); }
// // GET: /Trail/Create public ActionResult Create() { Trail newTrail = new Trail(); ViewBag.PossibleLocations = _repository.List<Location>(); ViewBag.PossibleTrailTypes = _repository.List<TrailType>(); ViewBag.PossibleDifficulties = _repository.List<Difficulty>(); return View(newTrail); }
public ActionResult Create(Trail trail, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file!= null) { BinaryReader binaryReader = new BinaryReader(file.InputStream); byte[] byteArray = binaryReader.ReadBytes(file.ContentLength); trail.Image = byteArray; } // see Comment 1 if (trail.DifficultyId.HasValue) { Difficulty difficulty = _repository.GetById<Difficulty>(trail.DifficultyId.Value); trail.Difficulty = difficulty; } Location location = _repository.GetById<Location>(trail.LocationId); trail.Location = location; TrailType trailType = _repository.GetById<TrailType>(trail.TrailTypeId); trail.TrailType = trailType; _repository.Insert(trail); _repository.SaveChanges(); return RedirectToAction("Index"); } //================================= var image = WebImage.GetImageFromRequest(); if (image != null) { if (image.Width > 500) { image.Resize(500, ((500 * image.Height) / image.Width)); } //newImage.MimeType = image.ContentType; // file.ContentType var binaryReader = new BinaryReader(file.InputStream); //newImage.Data = byte[] byteArray = binaryReader.ReadBytes(file.ContentLength); binaryReader.Close(); //var filename = Path.GetFileName(image.FileName); //image.Save(Path.Combine("../Uploads/Images", filename)); //filename = Path.Combine("~/Uploads/Images", filename); //string ImageUrl = Url.Content(filename); //string ImageAltText = image.FileName.Substring(0, image.FileName.Length - 4); } //================================= ViewBag.PossibleLocations = _repository.List<Location>(); ViewBag.PossibleTrailTypes = _repository.List<TrailType>(); ViewBag.PossibleDifficulties = _repository.List<Difficulty>(); return View(); }
public void ShouldAddNewTrail() { using (new TransactionScope()) { using (TrailsContext context = new TrailsContext()) { DbSet<Trail> trails = context.Trails; trails.Load(); int priorCount = trails.Local.Count; Trail newTrail = new Trail { Name = "testTrailName", Description = "testDescription", Distance = 32, ElevationGain = 3000M, TrailTypeId = 1, LocationId = 1, DifficultyId = 1 }; // Add the new trail to the context Trail trail = trails.Add(newTrail); // Save the context to the database, causing the database to be updated context.SaveChanges(); // At this stage, the database has the new row, so the identity has been incremented and assigned Assert.IsTrue(trail.Id > 0); Assert.IsTrue(newTrail.Id > 0); trails.Load(); int postCount = trails.Local.Count; Assert.AreEqual(postCount, priorCount + 1); } // Rollback the transaction, to make the test repeatable } }
public ActionResult Edit(Trail trail, HttpPostedFileBase file) { // Get the trail data back from the database and only then populate it from the model, // because the form does not contain every piece of information about the entity; // specifically, it is missing the Image Trail savedTrail = _repository.GetById<Trail>(trail.Id); if (ModelState.IsValid) { UpdateModel(savedTrail); if (file != null) { BinaryReader binaryReader = new BinaryReader(file.InputStream); byte[] byteArray = binaryReader.ReadBytes(file.ContentLength); savedTrail.Image = byteArray; } // see Comment 1 if (trail.DifficultyId.HasValue) { Difficulty difficulty = _repository.GetById<Difficulty>(trail.DifficultyId.Value); trail.Difficulty = difficulty; } else trail.Difficulty = null; if (trail.Location == null || trail.Location.Id != trail.LocationId) { Location location = _repository.GetById<Location>(trail.LocationId); trail.Location = location; } if (trail.TrailType == null || trail.TrailType.Id != trail.TrailTypeId) { TrailType trailType = _repository.GetById<TrailType>(trail.TrailTypeId); trail.TrailType = trailType; } _repository.Update(savedTrail); _repository.SaveChanges(); return RedirectToAction("Index"); } ViewBag.PossibleLocations = _repository.List<Location>(); ViewBag.PossibleTrailTypes = _repository.List<TrailType>(); ViewBag.PossibleDifficulties = _repository.List<Difficulty>(); return View(); }