public StudentModule() : base("/students") { var studentList = StudentCollection.GetInstance(); var teacherList = TeacherCollection.GetInstance(); Get("/", args => { var studentRequestParams = this.Bind <StudentRequestParams>(); var teacherId = studentRequestParams.TeacherId; return(teacherId != null ? Response.AsJson(teacherList.GetTeacherById(teacherId).Students) : Response.AsJson(studentList.GetStudents())); }); Post("/", _ => { var student = this.Bind <Student>(); studentList.AddStudent(student); return(HttpStatusCode.Created); }); Put("/{studentId}", args => { var updates = this.Bind <PutParams>(); string studentId = args.studentId; var studentToUpdate = studentList.GetStudentById(studentId); studentList.Update(studentToUpdate, updates); return(Response.AsJson(studentToUpdate)); }); }
public HomeModule() { var teacherList = TeacherCollection.GetInstance(); var studentList = StudentCollection.GetInstance(); Get("/teachers", args => Response.AsJson(teacherList.GetTeachers())); Post("/teachers", _ => { Console.WriteLine(1); var teacher = this.Bind <Teacher>(); teacherList.AddTeacher(teacher); return(HttpStatusCode.Created); }); Put("/teachers/{teacherId}", args => { try { var putBody = this.Bind <PutBody>(); string teacherId = args.teacherId; var teacherToUpdate = teacherList.GetTeacherById(teacherId); var studentToAdd = studentList.GetStudentById(putBody.StudentId); teacherToUpdate.AddStudent(studentToAdd); return(Response.AsJson(teacherToUpdate)); } catch (Exception e) { Console.WriteLine(e.Message); return(HttpStatusCode.InternalServerError); } }); }
public TeacherModule() : base("/teachers") { var teacherList = TeacherCollection.GetInstance(); var studentList = StudentCollection.GetInstance(); Get("/", args => Response.AsJson(teacherList.GetTeachers())); Post("/", _ => { Console.WriteLine("I am here"); var teacher = this.Bind <Teacher>(); teacherList.AddTeacher(teacher); return(HttpStatusCode.Created); }); Put("/{teacherId}", args => { var putBody = this.Bind <PutBody>(); string teacherId = args.teacherId; var teacherToUpdate = teacherList.GetTeacherById(teacherId); var studentToAdd = studentList.GetStudentById(putBody.StudentId); teacherToUpdate.AddStudent(studentToAdd); return(Response.AsJson(teacherToUpdate)); }); }