Beispiel #1
0
        public HttpResponseMessage GetWorkingTeachersByLastNameSorted([FromUri] string lastName)
        {
            string userId   = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
            string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value;

            logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Teacher Collection - " +
                        "By Last Name: " + lastName + " - Sorted Asc By Name");

            try
            {
                IList <Teacher> teachers = teachersService.GetWorkingTeachersByLastNameSorted(lastName);
                if (teachers.Count == 0)
                {
                    logger.Info("Teachers by last name: " + lastName + " were not found.");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Teachers by last name: " + lastName + ", were not found."));
                }
                if (userRole == "admin")
                {
                    IEnumerable <TeacherDTOForAdmin> dtos = toDTO.ConvertToTeacherDTOListForAdmin((List <Teacher>)teachers);

                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dtos));
                }
                else if (userRole == "teacher")
                {
                    IEnumerable <TeacherDTOForTeacher> dtos = toDTO.ConvertToTeacherDTOListForTeacher((List <Teacher>)teachers);

                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dtos));
                }
                else
                {
                    IEnumerable <TeacherDTOForStudentAndParent> dtos = toDTO.ConvertToTeacherDTOListForStudentAndParent((List <Teacher>)teachers);

                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dtos));
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }