public ActionResult Create(TestCreateViewModel viewModel) { if (ModelState.IsValid) { viewModel.Test.Descripcion = viewModel.Test.Enunciado; db.Tests.Add(viewModel.Test); db.SaveChanges(); if (viewModel.ImageFile != null) { viewModel.Test.UrlImagen = viewModel.Test.Id + ".jpg"; string nameAndLocation = "~/media/upload/Test/" + viewModel.Test.UrlImagen; viewModel.ImageFile.SaveAs(Server.MapPath(nameAndLocation)); db.Entry(viewModel.Test).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Create", "Tests", new { id = viewModel.Test.BloqueId })); } viewModel.Inicializar(viewModel.Test.BloqueId); return(View(viewModel)); }
public void CreateTest(TestCreateViewModel testDTO, HttpPostedFileBase image) { Test test = new Test { CreateDate = DateTime.Now, Difficult = testDTO.selectedDifficult, IdTheme = Int32.Parse(testDTO.selectedTheme), TestDescription = testDTO.TestDescription, TestName = testDTO.TestName, Time = new TimeSpan(0, testDTO.selectedTime, 0), }; foreach (QuestionForTestViewModel question in testDTO.Questions) { if (question.Chosen == true) { test.Questions.Add(Database.Questions.Get(question.IdQuestion)); test.TotalScore += Database.Questions.Get(question.IdQuestion).Score; } } test.QuestionsNumber = test.Questions.Count; if (image != null) { test.ImageMimeType = image.ContentType; test.TestImage = new byte[image.ContentLength]; image.InputStream.Read(test.TestImage, 0, image.ContentLength); } Database.Tests.Add(test); Database.Complete(); }
public async Task <TestGetViewModel> Post([FromBody] TestCreateViewModel testModel) { var entity = new Test(); SetClientIp(entity); testModel.Hydrate(entity); var view = new TestGetViewModel(entity); await _testService.Create(entity); return(view); }
public ActionResult EditTest(int id) { TestCreateViewModel model = _testService.GetCreateModel(id); if (model != null) { return(View(model)); } else { return(HttpNotFound()); } }
public ActionResult EditTest(TestCreateViewModel model, HttpPostedFileBase file) { if (ModelState.IsValid) { _testService.UpdateTest(model, file); return(RedirectToAction("GetInfoTest", "Test", model.IdTest)); } else { model = _testService.GetCreateModel(null); return(View(model)); } }
public ActionResult CreateNewTest(TestCreateViewModel model, HttpPostedFileBase file) { if (ModelState.IsValid && !model.Questions.TrueForAll(x => x.Chosen == false)) { _testService.CreateTest(model, file); return(RedirectToAction("GetInfoTest", "Test")); } else { if (!model.Questions.TrueForAll(x => x == null)) { ModelState.AddModelError("Questions[0].IdQuestion", "Test must contains questions!"); } model = _testService.GetCreateModel(null); return(View(model)); } }
public IActionResult Create(int?id) { if (id == null) { return(NotFound()); } if (!DoesUserHaveAccess(Convert.ToInt32(id))) { return(NotFound()); } var experimentVm = db.Experiments. Select(m => new ExperimentNameViewModel { ExperimentId = m.ExperimentId, Name = m.Name }). FirstOrDefault(m => m.ExperimentId == id); if (experimentVm == null) { return(NotFound()); } TestCreateViewModel testVm = new TestCreateViewModel { ExperimentId = experimentVm.ExperimentId, ExperimentName = experimentVm.Name, ExperimentSensorId = db.ExperimentSensors.FirstOrDefault(m => m.ExperimentId == experimentVm.ExperimentId).SensorId, }; testVm.ExperimentSensorName = db.Sensors.FirstOrDefault(m => m.SensorId == testVm.ExperimentSensorId).Name; return(View(testVm)); }
public async Task <IActionResult> Create([Bind("Name,Metadata,DurationSeconds,ExperimentId,ExperimentName,ExperimentSensorName")] TestCreateViewModel testVm) { if (!DoesUserHaveAccess(testVm.ExperimentId)) { return(NotFound()); } if (ModelState.IsValid) { Tests test = new Tests { Name = testVm.Name, Metadata = testVm.Metadata, ExperimentId = testVm.ExperimentId, StartedTime = DateTime.Now }; test.EndedTime = test.StartedTime.AddSeconds(testVm.DurationSeconds); db.Add(test); await db.SaveChangesAsync().ConfigureAwait(true); var sensorId = db.ExperimentSensors.FirstOrDefault(m => m.ExperimentId == test.ExperimentId).SensorId; var sensor = await db.Sensors.FirstOrDefaultAsync(m => m.SensorId == sensorId).ConfigureAwait(true); var sensorConnectorCommand = CommandBuilder.BuildSensorListenerStartCommand(test.TestId, sensor.IpAddress, sensor.Port, testVm.DurationSeconds); System.Diagnostics.Process.Start( @"SensorListener\SensorListener", sensorConnectorCommand); testVm.StartedTime = test.StartedTime; return(View("Measurement", testVm)); } return(View(testVm)); }
// GET: Admin/Tests/Create public ActionResult Create(int id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Bloque bloque = db.Bloques.SingleOrDefault(bl => bl.BloqueId == id); if (bloque == null) { return(HttpNotFound()); } TestCreateViewModel viewModel = new TestCreateViewModel(); viewModel.Inicializar(id); viewModel.Test = new Test(); viewModel.Test.TipoEjercicioId = (int)TiposDeEjerciciosId.Test; viewModel.Test.BloqueId = id; viewModel.Test.SubTemaId = bloque.SubTemaId; viewModel.Test.AreaId = viewModel.bloque.AreaId; return(View(viewModel)); }
public void UpdateTest(TestCreateViewModel model, HttpPostedFileBase image) { Test test = Database.Tests.Get(model.IdTest); test.CreateDate = DateTime.Now; test.Difficult = model.selectedDifficult; test.TestDescription = model.TestDescription; test.TestName = model.TestName; test.Time = new TimeSpan(0, model.selectedTime, 0); test.Theme = Database.Themes.Get(Int32.Parse(model.selectedTheme)); test.TotalScore = 0; foreach (QuestionForTestViewModel question in model.Questions) { if (question.Chosen == true) { test.Questions.Add(Database.Questions.Get(question.IdQuestion)); } } foreach (Question question in test.Questions) { test.TotalScore += question.Score; } test.QuestionsNumber = test.Questions.Count; if (image != null) { test.ImageMimeType = image.ContentType; test.TestImage = new byte[image.ContentLength]; image.InputStream.Read(test.TestImage, 0, image.ContentLength); } Database.Tests.Update(test); }
public IActionResult Measurement(TestCreateViewModel testVm) { return(View(testVm)); }
public TestCreateViewModel GetCreateModel(int?id) { if (!id.HasValue) { TestCreateViewModel model = new TestCreateViewModel { Theme = new SelectList(Database.Themes.GetAll(), "IdTheme", "ThemeName") }; IEnumerable <Question> questions = Database.Questions.GetAll(); model.Questions = new List <QuestionForTestViewModel>(); foreach (Question question in questions) { model.Questions.Add(new QuestionForTestViewModel { IdQuestion = question.IdQuestion, QuestionText = question.QuestionText, Difficult = question.Difficult, Theme = question.Theme.ThemeName, Chosen = false }); } return(model); } else { Test test = Database.Tests.Get(id.Value); if (test != null) { TestCreateViewModel model = new TestCreateViewModel { Theme = new SelectList(Database.Themes.GetAll(), "IdTheme", "ThemeName"), selectedDifficult = test.Difficult, TestDescription = test.TestDescription, selectedTime = test.Time.Minutes, IdTest = test.IdTest, TestName = test.TestName, ImageMimeType = test.ImageMimeType, TestImage = test.TestImage }; if (test.Theme == null) { model.selectedTheme = ""; } else { model.selectedTheme = test.Theme.ThemeName; } model.Questions = new List <QuestionForTestViewModel>(); foreach (Question question in Database.Questions.GetAll()) { if (!test.Questions.Contains(question)) { model.Questions.Add(new QuestionForTestViewModel { IdQuestion = question.IdQuestion, QuestionText = question.QuestionText, Difficult = question.Difficult, Theme = question.Theme.ThemeName, Chosen = false }); } } return(model); } else { return(null); } } }