public async Task Get(HttpListenerContext context) { await Task.Run(() => { var students = _database.GetStudents(); var studentsOrdered = students.Result.OrderBy(m => m.id); var studentList = studentsOrdered.Aggregate("The students are: \n", (current, student) => current + $"No. {student.id} - {student.Name}, Age {student.Age}\n"); ContextOperations.Write(studentList, context.Response); }); }
public async Task Delete(HttpListenerContext context) { if (_database.GetStudentByID(id).Result != null) { ContextOperations.Write($"Deleting student number {id} from student list...", context.Response); await _database.DeleteStudentByID(id); } else { ContextOperations.Write("Could not find student with that ID", context.Response); } }
public async Task Delete(HttpListenerContext context) { await Task.Run(async() => { var student = ContextOperations.GetStudentFromRequestBody(context); if (_database.GetStudentByID(student.id).Result != null) { ContextOperations.Write($"Deleting student number {student.id} from student list...", context.Response); await _database.DeleteStudentByID(student.id); } else { ContextOperations.Write("Could not find student with that ID", context.Response); } }); }
public async Task Get(HttpListenerContext context) { await Task.Run(() => { try { var student = _database.GetStudentByID(id).Result; if (student != null) { ContextOperations.Write("Here is the student you are looking for:\n" + $"No. {student.id} - {student.Name}, Age {student.Age}\n", context.Response); } else { ContextOperations.Write($"There is no student with the id number {id}", context.Response); } } catch (Exception e) { ContextOperations.Write(e.Message, context.Response); } }); }
public async Task Post(HttpListenerContext context) { await Task.Run(async() => { var student = ContextOperations.GetStudentFromRequestBody(context); if (student != null) { if (_database.GetStudentByID(student.id).Result == null) { await _database.Update(student); ContextOperations.Write($"{student.Name} added to student list", context.Response); } else { ContextOperations.Write("A student with that ID already exists", context.Response); } } else { ContextOperations.Write("Was unable to process request", context.Response); } }); }
public async Task Get(HttpListenerContext context) { await Task.Run(() => ContextOperations.Write("Welcome to the server! To get the list of students, head to http://localhost:8080/names", context.Response)); }