public async Task <string> Create(Question question) { if (ModelState.IsValid) { var exists = await _context.Questions.AsNoTracking().FirstOrDefaultAsync(q => q.Text == question.Text); if (exists == null) { question.Id = 0; question.Status = QuestionStatus.Pending; question.StudentId = HttpContext.Session.GetInt32("ID"); var assignment = await _context.Assignments.AsNoTracking().FirstOrDefaultAsync(a => a.Id == question.AssignmentId); question.QuestionConcepts = new List <QuestionConcept>() { new QuestionConcept() { ConceptId = assignment.ConceptId } }; await _context.AddAsync(question); await _context.SaveChangesAsync(); return("Intrebare adaugata cu succes!"); } else { return("Aceasta intrebare exista deja. Mai incearca!"); } } return("Completeaza cu date valide!"); }
public async Task <IActionResult> SaveTest(Test model) { var test = JsonConvert.DeserializeObject <Test>(TempData["Test"] as string); test.Name = model.Name; test.ProfessorId = HttpContext.Session.GetInt32("ID"); try { await _context.Tests.AddAsync(test); await _context.SaveChangesAsync(); return(StatusCode(200)); } catch (Exception e) { return(BadRequest()); } }
public async Task <IActionResult> Create([Bind("Deadline,ProfessorId,Concept,Group")] Assignment assignment) { if (ModelState.IsValid) { var group = _context.Groups.AsNoTracking().FirstOrDefault(g => g.Name == assignment.Group.Name); if (group == null) { group = new Group() { Name = assignment.Group.Name }; _context.Add(group); await _context.SaveChangesAsync(); } var concept = _context.Concepts.AsNoTracking().FirstOrDefault(c => c.Name == assignment.Concept.Name); if (concept == null) { concept = new Concept() { Name = assignment.Concept.Name }; _context.Add(concept); await _context.SaveChangesAsync(); } assignment.GroupId = group.Id; assignment.ConceptId = concept.Id; assignment.Group = null; assignment.Concept = null; assignment.ProfessorId = (int)HttpContext.Session.GetInt32("ID"); _context.Add(assignment); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Create), new { message = "Tema trimisa cu succes" })); } ViewData["Group"] = new SelectList(_context.Groups.AsNoTracking(), "Id", "Name", assignment.Group); ViewData["Concept"] = new SelectList(_context.Concepts.AsNoTracking(), "Id", "Name", assignment.Concept); return(View(assignment)); }