public async Task <Student> CreateStudent(Student student) { if (string.IsNullOrWhiteSpace(student.FirstName) || string.IsNullOrWhiteSpace(student.LastName) || string.IsNullOrWhiteSpace(student.Email) || string.IsNullOrWhiteSpace(student.PhoneNumber)) { return(null); } var newStudent = new Student() { FirstName = student.FirstName, LastName = student.LastName, Age = student.Age, Email = student.Email, Courses = student.Courses, PhoneNumber = student.PhoneNumber, }; if (newStudent == null) { return(null); } await _db.Students.AddAsync(newStudent); await _db.SaveChangesAsync(); return(newStudent); }
public async Task <Technology> Create(Technology technology) { _context.Entry(technology).State = Microsoft.EntityFrameworkCore.EntityState.Added; await _context.SaveChangesAsync(); return(technology); }
public async Task <IActionResult> AddStudent(int id) { if (int.TryParse(Request.Form["StudentId"], out var studentId) && Enum.TryParse <LetterGrade>(Request.Form["LetterGrade"], out var letterGrade)) { var student = await studentService.GetAsync(studentId); var course = await courseService.GetAsync(id); if (course != null && student != null) { var preexisting = ctx.Enrollments .Where(e => e.CourseId == id && e.StudentId == studentId) .FirstOrDefault(); if (preexisting == null) { await ctx.Enrollments.AddAsync(new Enrollment() { StudentId = studentId, CourseId = id, Grade = letterGrade }); } else if (preexisting.Grade != letterGrade) { preexisting.Grade = letterGrade; ctx.Enrollments.Update(preexisting); } await ctx.SaveChangesAsync(); } } return(RedirectToAction("View", new { Id = id })); }
public async Task <IActionResult> PutClass([FromRoute] Guid id, [FromBody] Class @class) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != @class.Id) { return(BadRequest()); } _dbContext.Entry(@class).State = EntityState.Modified; try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (await _dbContext.Classes.FindAsync(id) == null) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> Create(StudentViewModel model) { var teacherId = _context.Users .Where(u => u.Login == User.Identity.Name) .Select(u => u.TeacherId) .FirstOrDefault(); var principalId = _context.Users .Where(u => u.Login == User.Identity.Name) .Select(u => u.PrincipalId) .FirstOrDefault(); if (teacherId == null && principalId == null) { TempData["message"] = "Brak wymaganych uprawnien."; return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { _context.Addresses.Add(model.Address); model.PersonalData.AddressId = model.Address.AddressId; _context.PersonalDatas.Add(model.PersonalData); _context.Students.Add(model.Student); model.Student.PersonalDataId = model.PersonalData.PersonalDataId; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ClassId"] = new SelectList(_context.Classes, "ClassId", "Name", model.Classes.ClassId); return(View(model)); }
public async Task <IActionResult> PutStudent(string id, Student student) { if (id != student.StudentId) { return(BadRequest()); } _context.Entry(student).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Course> CreateCourse(Course course) { if (string.IsNullOrWhiteSpace(course.Name)) { return(null); } var newCourse = new Course() { Name = course.Name, Points = course.Points, Students = course.Students, Assignments = course.Assignments, Grade = course.Grade, Teacher = course.Teacher, Description = course.Description }; if (newCourse == null) { return(null); } await _db.Courses.AddAsync(newCourse); await _db.SaveChangesAsync(); return(newCourse); }
public async Task <IActionResult> Create([Bind("RealName,HireDate,OfficeAssignment")] Instructor instructor, string [] selectedCourses) { if (selectedCourses != null) { instructor.CourseAssignments = new List <CourseAssignment>(); foreach (var courseId in selectedCourses) { var course = new CourseAssignment { CourseId = Convert.ToInt32(courseId), InstructorId = instructor.Id }; instructor.CourseAssignments.Add(course); } } if (ModelState.IsValid) { _context.Add(instructor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedCourseData(instructor); return(View(instructor)); }
public async Task <Transcript> Create(Transcript transcript) { _context.Entry(transcript).State = EntityState.Added; await _context.SaveChangesAsync(); return(transcript); }
public async Task <IActionResult> PutTechnology(int id, Technology technology) { if (id != technology.Id) { return(BadRequest()); } _context.Entry(technology).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TechnologyExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create(StudentDto dto) { //CSRF参考资料: http://www.freebuf.com/articles/web/55965.html try { if (ModelState.IsValid) { var entity = new Student { RealName = dto.RealName, EnrollmentDate = dto.EnrollmentDate }; _context.Add(entity); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException ex) { ModelState.AddModelError("", "无法进行数据的保存,请仔细检查你的数据,是否异常。"); } return(View(dto)); }
public async Task <Teacher> CreateTeacher(Teacher teacher) { if (string.IsNullOrWhiteSpace(teacher.FirstName) || string.IsNullOrWhiteSpace(teacher.LastName) || string.IsNullOrWhiteSpace(teacher.Email) || string.IsNullOrWhiteSpace(teacher.PhoneNumber)) { return(null); } var newTeacher = new Teacher() { FirstName = teacher.FirstName, LastName = teacher.LastName, Age = teacher.Age, Email = teacher.Email, PhoneNumber = teacher.PhoneNumber, }; if (newTeacher == null) { return(null); } await _db.Teachers.AddAsync(newTeacher); await _db.SaveChangesAsync(); return(newTeacher); }
public async Task <IActionResult> PutEmployee(long id, Employee employee) { if (id != employee.EmployeeId) { return(BadRequest()); } _context.Entry(employee).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Student> Create(Student student) { _context.Entry(student).State = Microsoft.EntityFrameworkCore.EntityState.Added; await _context.SaveChangesAsync(); return(student); }
public async Task <IActionResult> PutEvent([FromRoute] int id, [FromBody] Event @event) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != @event.Id) { return(BadRequest()); } _context.Entry(@event).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EventExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Student> Create(NewStudentDto inboundData) { // Add the student Student student = new Student() { FirstName = inboundData.Name.Split(" ").First <string>(), LastName = inboundData.Name.Split(" ").Last <string>(), DateOfBirth = inboundData.Dob }; _context.Entry(student).State = Microsoft.EntityFrameworkCore.EntityState.Added; await _context.SaveChangesAsync(); Course course = await _context.Courses.FirstOrDefaultAsync(c => c.CourseCode == inboundData.CourseCode); // Add student to course Enrollment enrollment = new Enrollment() { CourseId = course.Id, StudentId = student.Id }; _context.Entry(enrollment).State = EntityState.Added; await _context.SaveChangesAsync(); return(student); }
public async Task <Grade> Create(Grade grade) { _context.Entry(grade).State = Microsoft.EntityFrameworkCore.EntityState.Added; await _context.SaveChangesAsync(); return(grade); }
public async Task <IActionResult> PutEnrollment(int id, Enrollment enrollment) { if (id != enrollment.CourseId) { return(BadRequest()); } _context.Entry(enrollment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EnrollmentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Course> Create(Course course) { _context.Entry(course).State = Microsoft.EntityFrameworkCore.EntityState.Added; await _context.SaveChangesAsync(); return(course); }
public async Task <IActionResult> Create(TeachersViewModel model) { using (var context = new SchoolDbContext()) { var teacherId = context.Users .Where(u => u.Login == User.Identity.Name) .Select(u => u.TeacherId) .FirstOrDefault(); var principalId = context.Users .Where(u => u.Login == User.Identity.Name) .Select(u => u.PrincipalId) .FirstOrDefault(); if (teacherId == null && principalId == null) { TempData["message"] = "Brak wymaganych uprawnien."; return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var logins = context.Users.Select(u => u.Login).ToList(); if (logins.Contains(model.User.Login)) { return(NotFound("login already exists")); } context.Addresses.Add(model.Address); await context.SaveChangesAsync(); var pdata = model.PersonalData; pdata.Address = model.Address; context.PersonalDatas.Add(pdata); await context.SaveChangesAsync(); var teacher = model.Teacher; teacher.PersonalData = pdata; context.Teachers.Add(teacher); await context.SaveChangesAsync(); var user = new Users { Login = model.User.Login, Password = model.User.Password, TeacherId = teacher.TeacherId }; var result = await _userManager.CreateAsync(user, user.Password); await context.SaveChangesAsync(); return(RedirectToAction("Get")); } return(View()); } }
[ValidateAntiForgeryToken] // 跨站请求验证 很重要 public async Task <IActionResult> Create(StudentDto dto) { try { if (ModelState.IsValid) { var entity = new Student { RealName = dto.RealName, EnrollmentDate = dto.EnrollmentDate }; _context.Add(entity); await _context.SaveChangesAsync(); //return View(student); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException) // 这种异常DbUpdateException 是指数据有问题 { ModelState.AddModelError("", "无法进行数据保存,请检查数据是否异常"); } return(View(dto)); }
public async Task <Student> InsertStudentAsync(Student student) { _context.Students.Add(student); await _context.SaveChangesAsync(); return(student); }
public async Task OnPostDeleteAsync(int id) { _context.Courses.Attach(new Course { Id = id }).State = EntityState.Deleted; await _context.SaveChangesAsync(); }
public async Task <IdentityResult> CreateAsync(Users user, CancellationToken cancellationToken) { db.Add(user); await db.SaveChangesAsync(cancellationToken); return(await Task.FromResult(IdentityResult.Success)); }
public async Task <ActionResult <Teacher> > PostTeacher(Teacher teacher) { _context.Teachers.Add(teacher); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTeacher", new { id = teacher.TeacherId }, teacher)); }
public async Task <int> CreateClass(CreateClassDto classDto) { var @class = _mapper.Map <Class>(classDto); _context.Classes.Add(@class); await _context.SaveChangesAsync(); return(@class.Id); }
public async Task EnrollStudent(int courseId, long studentId) { // TODO: make sure course/student exist var enrollment = new Enrollment { CourseId = courseId, StudentId = studentId }; dbContext.Enrollments.Add(enrollment); await dbContext.SaveChangesAsync(); }
//POST public async Task <IHttpActionResult> Post(Department department) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Courses.Add(department); await db.SaveChangesAsync(); return(Created(department)); }
public async Task <IActionResult> Create([Bind("Id,Title,Credits")] Course course) { if (ModelState.IsValid) { _context.Add(course); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(course)); }
public async Task <IActionResult> Create([Bind("ID,CourseCode,Technology,Price")] Course course) { if (ModelState.IsValid) { _context.Add(course); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(course)); }