public async Task <IActionResult> UpdateStudent(Student student) { // var res = _studentContext.Student.Where(s => s.IndexNumber == student.IndexNumber).FirstOrDefaultAsync(); try { _studentContext.Attach(student); _studentContext.Entry(student).State = EntityState.Modified; await _studentContext.SaveChangesAsync(); CRUDStudentResponse resp = new CRUDStudentResponse { message = "The Following Student was updated successfully!", firstName = student.FirstName, lastName = student.LastName, index = student.IndexNumber }; return(Ok(resp)); } catch (Exception e) { return(BadRequest(e)); } }
public async Task <IActionResult> DeleteStudent(string index) { // var student = await _studentContext.Student.FindAsync(index); //Instead of first selecting the student and then deleting him/her we can immediately delete a student that we create and use change tracking try { var student = new Student { IndexNumber = index }; _studentContext.Attach(student); _studentContext.Entry(student).State = EntityState.Deleted; await _studentContext.SaveChangesAsync(); CRUDStudentResponse resp = new CRUDStudentResponse { message = "The Following Student was deleted successfully!", firstName = student.FirstName, lastName = student.LastName, index = student.IndexNumber }; return(Ok(resp)); }catch (Exception e) { return(BadRequest(e)); } }
public async Task <IActionResult> PromoteStudents(PromoteStudentRequest request) { var resp = new CRUDStudentResponse(); try { var user = await _studentContext.Database.ExecuteSqlRawAsync("Exec PromoteStudents '" + request.Studies + "', " + request.Semester + ";"); } catch (Exception e) { return(BadRequest("Stored procedure didn't go as planned")); } return(Ok("Semester " + request.Semester + " students have been promoted to the next semester!")); }
public async Task <IActionResult> InsertStudent(Student student) { try { await _studentContext.Student.AddAsync(student); await _studentContext.SaveChangesAsync(); CRUDStudentResponse resp = new CRUDStudentResponse { message = "The Following Student was inserted successfully!", firstName = student.FirstName, lastName = student.LastName, index = student.IndexNumber }; return(Ok(resp)); }catch (Exception e) { return(BadRequest(e)); } }