Exemple #1
0
        public async Task <IActionResult> Put(int id, [FromBody] Course courseUpdateValue)
        {
            var courseToEdit = await nhSession.Query <Course>()
                               .Where(s => s.Id == id)
                               .SingleOrDefaultAsync();

            if (courseToEdit == null)
            {
                return(NotFound(Json(APIResult.New().WithError("Could not update course as it was not Found"))));
            }
            else
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    courseToEdit.Name     = courseUpdateValue.Name;
                    courseToEdit.Location = courseUpdateValue.Location;
                    courseToEdit.Teacher  = await nhSession.GetAsync <Teacher>(courseUpdateValue.Teacher.Id);

                    await nhSession.SaveOrUpdateAsync(courseToEdit);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(courseToEdit)));
                }
            }
        }
        public async Task <IActionResult> Post(int courseId, int studentId, [FromBody] float grade)
        {
            using (var tr = nhSession.BeginTransaction())
            {
                var course = await nhSession.GetAsync <Course>(courseId);

                var student = await nhSession.GetAsync <Student>(studentId);

                if (course != null && student != null)
                {
                    var studentGrade = new StudentGrade();
                    studentGrade.Course  = course;
                    studentGrade.Student = student;
                    studentGrade.Grade   = grade;
                    await nhSession.SaveAsync(studentGrade);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(studentGrade)));
                }
                else
                {
                    return(BadRequest(Json(APIResult.New().WithError("Either Course or Student does not exist"))));
                }
            }
        }
Exemple #3
0
        public async Task <IActionResult> Put(int id, [FromBody] Student studentUpdateValue)
        {
            var studentToEdit = await nhSession.Query <Student>()
                                .Where(s => s.Id == id)
                                .SingleOrDefaultAsync();

            if (studentToEdit == null)
            {
                return(NotFound(APIResult.New().WithError("Could not update student as it was not Found")));
            }
            else
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    studentToEdit.FirstName = studentUpdateValue.FirstName;
                    studentToEdit.LastName  = studentUpdateValue.LastName;
                    studentToEdit.Age       = studentUpdateValue.Age;

                    await nhSession.SaveOrUpdateAsync(studentToEdit);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(studentToEdit)));
                }
            }
        }
        public async Task <IActionResult> GetByCourse(int courseId)
        {
            var studentGrades = await nhSession
                                .Query <StudentGrade>()
                                .Where(c => c.Course.Id == courseId)
                                .ToListAsync();

            return(Json(APIResult.New().WithSuccess().WithResult(studentGrades)));
        }
Exemple #5
0
        public async Task <IActionResult> Get(int currentPageNo = 1, int pageSize = 20)
        {
            var courses = await nhSession
                          .Query <Course>()
                          .Skip((currentPageNo - 1) * pageSize)
                          .Take(pageSize)
                          .ToListAsync();

            return(Json(APIResult.New().WithSuccess().WithResult(courses)));
        }
        public async Task <IActionResult> Get(int id)
        {
            var teacher = await nhSession
                          .Query <Teacher>()
                          .Where(s => s.Id == id)
                          .SingleOrDefaultAsync();

            if (teacher == null)
            {
                return(NotFound(Json(APIResult.New().WithError("Teacher Not found")))); // Returns a 204 No Content response
            }
            else
            {
                return(Json(APIResult.New().WithSuccess().WithResult(teacher)));
            }
        }
        public async Task <IActionResult> Post([FromBody] Teacher teacher)
        {
            if (!string.IsNullOrEmpty(teacher.FirstName))
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    await nhSession.SaveAsync(teacher);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(teacher)));
                }
            }
            else
            {
                return(BadRequest(Json(APIResult.New().WithError("Teacher's name was not given"))));
            }
        }
Exemple #8
0
        public async Task <IActionResult> Delete(int id)
        {
            var studentToDelete = await nhSession.Query <Course>()
                                  .Where(s => s.Id == id)
                                  .SingleOrDefaultAsync();

            if (studentToDelete == null)
            {
                return(NotFound(Json(APIResult.New().WithError("Could not delete course as it was not Found"))));
            }
            else
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    await nhSession.DeleteAsync(studentToDelete);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(true)));
                }
            }
        }
        public async Task <IActionResult> Put(int id, [FromBody] Teacher teacherUpdateValue)
        {
            var teacherToEdit = await nhSession.Query <Teacher>()
                                .Where(s => s.Id == id)
                                .SingleOrDefaultAsync();

            if (teacherToEdit == null)
            {
                return(NotFound(APIResult.New().WithError("Could not update teacher as it was not Found")));
            }
            else
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    await nhSession.SaveOrUpdateAsync(teacherUpdateValue);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(teacherUpdateValue)));
                }
            }
        }
        public async Task <IActionResult> Put([FromBody] StudentGrade studentGradeUpdate)
        {
            var studentGradeToEdit = await nhSession.Query <StudentGrade>()
                                     .Where(s => s.Course.Id == studentGradeUpdate.Course.Id && s.Student.Id == studentGradeUpdate.Student.Id)
                                     .SingleOrDefaultAsync();

            if (studentGradeToEdit == null)
            {
                return(NotFound(Json(APIResult.New().WithError("Could not update student as it was not Found"))));
            }
            else
            {
                using (var tr = nhSession.BeginTransaction())
                {
                    studentGradeToEdit.Grade = studentGradeUpdate.Grade;

                    await nhSession.SaveOrUpdateAsync(studentGradeToEdit);

                    await tr.CommitAsync();

                    return(Json(APIResult.New().WithSuccess().WithResult(studentGradeToEdit)));
                }
            }
        }