public ActionResult Create(IndividualViewModel model) { try { if (ModelState.IsValid) { var repository = new IndividualRepository(context); var emailExiste = repository.Query(x => x.Email == model.Email).Count > 0; if (!emailExiste) { var entity = MapperHelper.Map <Individual>(model); repository.Insert(entity); context.SaveChanges(); } else { ModelState.AddModelError("Email", "El email está ocupado"); return(View(model)); } } return(RedirectToAction("Index")); } catch { return(View()); } }
public async Task <IHttpActionResult> PutCourse() { Course course = new Course(); string pathImage; var httpRequest = HttpContext.Current.Request; //Upload Image course.Code = httpRequest["Code"]; var postedFile = httpRequest.Files["Image"]; course.Name = httpRequest["Name"]; course.Description = httpRequest["Description"]; course.MaxDegree = int.Parse(httpRequest["MaxDegree"]); course.MinDegree = int.Parse(httpRequest["MinDegree"]); course.StageID = int.Parse(httpRequest["StageID"]); // semester sem = (semester)Enum.Parse(typeof(semester), httpRequest["Semester"]); //course.Semester = sem; course.Semester = httpRequest["Semester"]; //Custom filename if (postedFile != null) { pathImage = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); pathImage += DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); string filePath = ""; filePath = HttpContext.Current.Server.MapPath("~/Content/Images/" + pathImage); pathImage = "http://localhost:51851/Content/Images/" + pathImage; course.Image = pathImage; postedFile.SaveAs(filePath); } if (!ModelState.IsValid) { return(BadRequest(ModelState));//400 } try { Course oldCourse = db.Courses.FirstOrDefault(p => p.Code == course.Code); oldCourse.Name = course.Name; oldCourse.Description = course.Description; oldCourse.MaxDegree = course.MaxDegree; oldCourse.MinDegree = course.MinDegree; oldCourse.StageID = course.StageID; oldCourse.Semester = course.Semester; if (postedFile != null) { oldCourse.Image = course.Image; } db.SaveChanges(); return(StatusCode(HttpStatusCode.NoContent)); } catch (Exception ex) { return(BadRequest(ex.Message));//400 } }
public T Add(T newEntity) { T savedEntity = this.dbSet.Add(newEntity); _ctx.SaveChanges(); return(savedEntity); }
protected override Tutor CreateEntity(Tutor entity) { Tutor insertedTutor = entity; insertedTutor.UserName = string.Format("{0}.{1}", entity.FirstName, entity.LastName); insertedTutor.Password = Helpers.RandomString(8); ctx.Tutors.Add(insertedTutor); ctx.SaveChanges(); return(entity); }
public ActionResult Create([Bind(Include = "CategoryId,CategoryName,IsActive")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public ActionResult Create(ProductModel model) { var imageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0) { ModelState.AddModelError("ImageUpload", "This field is required"); } else if (!imageTypes.Contains(model.ImageUpload.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (ModelState.IsValid) { var product = new Product(); product.ProductName = model.ProductName; product.Price = model.Price; product.CategoryId = model.CategoryId; // Save image to folder and get path var imageName = String.Format("{0:yyyyMMdd-HHmmssfff}", DateTime.Now); var extension = System.IO.Path.GetExtension(model.ImageUpload.FileName).ToLower(); using (var img = System.Drawing.Image.FromStream(model.ImageUpload.InputStream)) { product.Image = String.Format("/ProductImages/{0}{1}", imageName, extension); product.Thumb = String.Format("/ProductImages/{0}_thumb{1}", imageName, extension); // Save thumbnail size image, 100 x 100 SaveToFolder(img, extension, new Size(100, 100), product.Thumb); // Save large size image, 600 x 600 SaveToFolder(img, extension, new Size(600, 600), product.Image); } db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } // If any error return back to the page ViewBag.Categories = db.Categories.Where(x => x.IsActive == true); return(View(model)); }
public void Topic_ShoulInsertOk() { var context = new LearningContext(); var topicRepository = new TopicRepository(context); topicRepository.Insert(new Topic { Name = "Entity Framework", Descripcion = "Applyling EF6 Data Access Stratagy" } ); topicRepository.Insert(new Topic { Name = "Entity Framework2", Descripcion = "Applyling EF6 Data Access Stratagy" } ); var individual = new IndividualRepository(context); individual.Insert(new Individual { Name = "Benigno", Email = "*****@*****.**" } ); individual.Insert(new Individual { Name = "Antonio", Email = "*****@*****.**" } ); context.SaveChanges(); }
public void Topic_ShouldInsertOK() { var context = new LearningContext(); var topicRepository = new TopicRepository(context); List <Topic> topics = new List <Topic> { new Topic { Name = "Entity Framework", Description = "Entity Framework courses for everyone" }, new Topic { Name = "Bootstrap", Description = "Bootstrap courses for everyone" }, new Topic { Name = "JQuery", Description = "JQuery courses for everyone" }, new Topic { Name = "JavaScript", Description = "JavaScript courses for everyone" } }; topicRepository.InsertRange(topics); context.SaveChanges(); Assert.IsFalse(false); }
public ActionResult NewAssignment(NewAssignmentCoursesViewModel model) { var repository = new AssignedCourseRepository(context); try { if (ModelState.IsValid) { var entity = MapperHelper.Map <AssignedCourse>(model); entity.IsCompleted = false; repository.Insert(entity); context.SaveChanges(); return(RedirectToAction("Index")); } model.CoursesList = PopulateCourses(model.CourseId); model.IndividualList = PopulateIndividuals(model.IndividualId); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; return(View(model)); } return(View(model)); }
public ActionResult Create(CourseViewModel model) { try { var repository = new CourseRepository(context); var topicRepository = new TopicRepository(context); var topics = topicRepository.Query(null, "Name"); model.AvailableTopics = MapperHelper.Map <ICollection <TopicViewModel> >(topics); if (ModelState.IsValid) { var entity = MapperHelper.mapper.Map <Course>(model); repository.InsertCourse(entity, model.SelectedTopics); context.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; return(View(model)); } }
public void Topic_ShouldInsertOk() { // // TODO: Agregar aquí la lógica de las pruebas // var context = new LearningContext(); var topicRepository = new TopicRepository(context); topicRepository.Insert(new Topic { Name = "Entity Framework", Description = "Applying EF6 Data Acces Strategy" }); context.SaveChanges(); }
public void Topic_ShouldInsertOK() { try { var context = new LearningContext(); var topicRepository = new TopicRepository(context); topicRepository.Insert(new Topic { Name = "Entity Framework ", Description = "Applying EF6 Data Access Strategy" }); context.SaveChanges(); Assert.IsFalse(false); } catch (Exception ex) { Assert.IsFalse(true); } }
public ActionResult Create(ProductModel2 model) { var imageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0) { ModelState.AddModelError("ImageUpload", "This field is required"); } else if (!imageTypes.Contains(model.ImageUpload.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (ModelState.IsValid) { var product = new Product2(); product.ProductName = model.ProductName; product.Price = model.Price; product.CategoryId = model.CategoryId; using (var binaryReader = new BinaryReader(model.ImageUpload.InputStream)) product.Image = binaryReader.ReadBytes(model.ImageUpload.ContentLength); db.Products2.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } // If any error return back to the page ViewBag.Categories = db.Categories.Where(x => x.IsActive == true); return(View(model)); }
public void Save() { _learningContext.SaveChanges(); }
public bool SaveAll() { return(_context.SaveChanges() > 0); }
protected override Subject CreateEntity(Subject entity) { ctx.Subjects.Add(entity); ctx.SaveChanges(); return entity; }
public School AddSchool(School school) { Domain.Principal newPrincipal = _mapper.Map <Domain.Principal>(school.Principal); Domain.School newSchool = new Domain.School() { Name = school.Name, Photo = school.Photo }; _context.Schools.Add(newSchool); _context.SaveChanges(); newPrincipal.SchoolId = newSchool.Id; _context.Principals.Add(newPrincipal); _context.SaveChanges(); school.Id = newSchool.Id; return(school); }
public bool SaveAll() { return(_ctx.SaveChanges() > 0); }