private List <ExamTitle> GetExamTitles() { var titleObjects = new List <ExamTitle>(); if (!_context.ExamTitles.Any()) { var url = "https://www.wired.com"; var web = new HtmlWeb(); var doc = web.Load(url); var htmlNodes = doc.DocumentNode.SelectNodes("//a/h2").Take(5); foreach (var item in htmlNodes) { var hrefNode = doc.DocumentNode.SelectNodes(item.XPath.Split("/h2").First()).First(); titleObjects.Add(new ExamTitle { Id = titleObjects.Count + 1, Title = HttpUtility.HtmlDecode(item.InnerText), Href = hrefNode.Attributes.First().Value }); } _context.ExamTitles.AddRange(titleObjects); _context.SaveChanges(); } else { titleObjects = _context.ExamTitles.ToList(); } return(titleObjects); }
public ErrorsCollection Register(RegisterPostModel registerinfo) { var errors = registerValidator.Validate(registerinfo, context); if (errors != null) { return(errors); } User toAdd = new User { Email = registerinfo.Email, LastName = registerinfo.LastName, FirstName = registerinfo.FirstName, Password = ComputeSha256Hash(registerinfo.Password), Username = registerinfo.Username, DataRegistered = DateTime.Now, UserRole = UserRole.Regular }; //Regular ca default //var regularRole = context // .Users // .FirstOrDefault(ur => ur.Username == UserRoles.Regular); context.Users.Add(toAdd); context.SaveChanges(); return(null); }
//acum mutam logica din Controller pe Service. //Nu il eliminam dar Controller-ul va apela Service si nu va mai apela UI-ul Service-ul public Pachet Create(PachetPostModel pachet, User addedBy) { Pachet toAdd = PachetPostModel.ToPachet(pachet); toAdd.Owner = addedBy; context.Pachete.Add(toAdd); context.SaveChanges(); return(toAdd); }
public ActionResult Create([Bind(Include = "Id,Libelle,Prix")] Produit produit) { if (ModelState.IsValid) { db.Produits.Add(produit); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(produit)); }
public ActionResult Create([Bind(Include = "Id,Nom,Prenom")] Client client) { if (ModelState.IsValid) { db.Client.Add(client); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(client)); }
public ActionResult Create([Bind(Include = "Id,Name")] Subject subject) { if (ModelState.IsValid) { db.Subjects.Add(subject); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(subject)); }
public ActionResult Create([Bind(Include = "Id,Name,SubjectId")] Topic topic) { if (ModelState.IsValid) { db.Topics.Add(topic); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.SubjectId = new SelectList(db.Subjects, "Id", "Name", topic.SubjectId); return(View(topic)); }
public ActionResult Create([Bind(Include = "Id,TopicId,Title,A,B,C,D,E,TrueAnswer,Point")] Question question) { if (ModelState.IsValid) { db.Questions.Add(question); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TopicId = new SelectList(db.Topics, "Id", "Name", question.TopicId); return(View(question)); }
public PackagePostModel AddPackage(PackagePostModel packagePostModel) { var package = PackagePostModel.ToPackage(packagePostModel); if (package == null) { return(null); } context.Packages.Add(package); context.SaveChanges(); return(packagePostModel); }
public void CreateNewActiveOrder(string userId) { var order = new Order { CashierId = userId }; context.Orders.Add(order); context.SaveChanges(); }
public ActionResult Result(int id) { Topic topic = db.Topics.Find(id); Result result = new Result(); User user = Session["User"] as User; var us = db.Users.Where(x => x.Id == user.Id).FirstOrDefault(); result.UserId = user.Id; int falseAnswer = 0; foreach (var item in topic.Questions.ToList()) { if (Request.Form.AllKeys.Contains(item.Id.ToString())) { if (Request.Form.Get(item.Id.ToString()) == item.TrueAnswer) { result.TrueAnswers++; result.TotalPoint += item.Point; } else { result.FalseAnswers++; falseAnswer++; } } else { result.EmptyAnswers++; } } falseAnswer /= 4; int deductedpoints = falseAnswer * 5; if (result.TotalPoint >= deductedpoints) { result.TotalPoint -= deductedpoints; } QuestionViewModel viewmodel = new QuestionViewModel() { Results = result, FullName = us.FullName }; result.QuestionId = 3; db.Results.Add(result); db.SaveChanges(); return(View(viewmodel)); }
public void Create(TimeLogType timeLogType) { using (var connection = new SQLiteConnection(ConnectionString)) { connection.Open(); using (var context = new ExamDbContext(connection, false)) { context.Set <TimeLogType>().Add(timeLogType); context.SaveChanges(); } } }
public UserGetModel Register(RegisterPostModel registerInfo) { User existing = context.Users.FirstOrDefault(u => u.Username == registerInfo.Username); if (existing != null) { return(null); } context.Users.Add(new User { Email = registerInfo.Email, LastName = registerInfo.LastName, FirstName = registerInfo.FirstName, Password = ComputeSha256Hash(registerInfo.Password), Username = registerInfo.Username, UserRole = UserRole.Regular }); context.SaveChanges(); return(Authenticate(registerInfo.Username, registerInfo.Password)); }
private void CheckAnswer(TaskModel CurrentTask) { if (CurrentTask != null) { try { TaskResult taskResult = new TaskResult(); TaskResult lastTaskResult = db.TaskResult.ToList().LastOrDefault(); if (lastTaskResult != null) { taskResult.TaskResultId = lastTaskResult.TaskResultId + 1; } else { taskResult.TaskResultId = 1; } taskResult.Date = DateTime.Now; taskResult.TaskId = CurrentTask.TaskId; taskResult.StudentId = StudentId; if (CurrentTask.StudentAnswer == CurrentTask.Answer) { taskResult.IsSolved = true; } else { taskResult.IsSolved = false; } db.TaskResult.Add(taskResult); db.SaveChanges(); CurrentTask.IsDone = true; if (taskResult.IsSolved) { CurrentTask.Result = "Задание решено правильно!"; Tasks.Remove(CurrentTask); Tasks = Tasks.ToList(); OnPropertyChanged("Tasks"); } else { CurrentTask.Result = "Задание решено не правильно!"; } } catch (Exception ex) { MessageBox.Show("Не удалось сохранить результат! " + ex.Message); } } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ExamDbContext( serviceProvider.GetRequiredService <DbContextOptions <ExamDbContext> >())) { if (context.Courses.Any()) { return; } context.Courses.AddRange( new Course { Id = 1, Title = "x Course", AddedAt = DateTime.Now }, new Course { Id = 2, Title = "x Course", AddedAt = DateTime.Now } ); context.Exams.AddRange( new Exam { Id = 1, Title = "X Exam", Information = "Midterm Exam", NumberOfQuestions = 5, AddedAt = DateTime.Now, StartTime = DateTime.Now, EndTime = DateTime.Now }, new Exam { Id = 2, Title = "y Exam", Information = "Final Exam", NumberOfQuestions = 5, AddedAt = DateTime.Now, StartTime = DateTime.Now, EndTime = DateTime.Now } ); context.SaveChanges(); } }
public ActionResult Register(User user, string confirm) { string hashed = Crypto.HashPassword(user.Password); if (user != null && user.FullName != null && user.Email != null && user.Password != null && user.Password == confirm) { user.Password = hashed; user.Role = RoleEnum.User.ToString(); db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Login", "Manage")); } ; return(Content("Error")); }
public ActionResult Result(FormCollection collection) { try { var results = new Dictionary <string, int>() { { "Answered", 0 }, { "Total", 0 }, { "Correct", 0 }, { "Point", 0 } }; int userid = Convert.ToInt32(Session["UserId"]); User usr = db.Users.FirstOrDefault(x => x.Id == userid); int number = 0; while (number++ <= db.Questions.OrderByDescending(x => x.Id).FirstOrDefault().Id) { if (collection.AllKeys.Contains($"Id {number}")) { results["Total"] += 1; var question = db.Questions.FirstOrDefault(x => x.Id == number); var answer = (VariantEnum)question.TrueAnswer; if (collection.AllKeys.Contains($"A {number}") || collection.AllKeys.Contains($"B {number}") || collection.AllKeys.Contains($"C {number}") || collection.AllKeys.Contains($"D {number}") || collection.AllKeys.Contains($"E {number}")) { results["Answered"] += 1; if (collection.AllKeys.Contains($"{answer} {number}")) { results["Correct"] += 1; results["Point"] += question.Point; } } } } usr.TotalQuestion += results["Total"]; usr.Point += results["Point"]; usr.TrueAnswer += results["Correct"]; db.SaveChanges(); return(View(results)); } catch (Exception) { throw; } }
public ActionResult Register([Bind(Include = "Name,Surname,Email,Password")] User user) { if (ModelState.IsValid) { try { var result = EmailIsUnique(user.Email); user.Password = Crypto.HashPassword(user.Password); db.Users.Add(user); db.SaveChanges(); Session["Logined"] = true; Session["User"] = user.Name; Session["UserId"] = user.Id; return(RedirectToAction("Index", "Home")); } catch (Exception) { throw; } } return(View(user)); }
public static void InitializeDbForTests(ExamDbContext db) { db.Projects.AddRange(GetSeedingProjects()); db.Contacts.AddRange(GetSeedingContacts()); db.SaveChanges(); }
public Exam AddExam(Exam exam) { _context.Exams.Add(exam); _context.SaveChanges(); return(exam); }
public Skelet AddSkelet(Skelet skelet) { _context.Skelets.Add(skelet); _context.SaveChanges(); return(skelet); }
public Student AddStudent(Student student) { _context.Students.Add(student); _context.SaveChanges(); return(student); }
private void FormPlanFromTest() { if (TestTasks.Count > 0) { int nfalse = 0; int ntopic = 0; nfalse = TestTasks.Where(t => t.IsDone == false).Count(); ntopic = db.Topic.Count(); double ndays = (ExamDate - StartDate).TotalDays; double x = ndays / (2 * nfalse + (ntopic - nfalse)); DateTime currentDateTime = StartDate; foreach (Topic topic in db.Topic.ToList()) { var studyings = db.Studying.ToList().Where(st => st.StudentId == CurrentStudent.StudentId && st.TopicId == topic.TopicId); Studying studying; if (studyings.Count() > 0) { studying = studyings.Last(); db.Studying.Remove(studying); } else { studying = new Studying(); } studying.ComplationDate = currentDateTime; studying.StudentId = CurrentStudent.StudentId; studying.TopicId = topic.TopicId; studyings = db.Studying.ToList(); if (studyings.Count() > 0) { studying.StudyingId = studyings.Last().StudyingId + 1; } else { studying.StudyingId = 1; } TaskModel taskModel = TestTasks.Where(t => t.TopicId == topic.TopicId).Last(); if (taskModel != null) { if (taskModel.IsDone == false) { studying.ComplationDate = currentDateTime.AddDays(x * 2); currentDateTime = studying.ComplationDate.Value; } else { studying.ComplationDate = currentDateTime.AddDays(x); currentDateTime = studying.ComplationDate.Value; } } else { studying.ComplationDate = currentDateTime.AddDays(x); currentDateTime = studying.ComplationDate.Value; } if (studying.ComplationDate > ExamDate) { studying.ComplationDate = ExamDate; } db.Studying.Add(studying); db.SaveChanges(); } } }