public IHttpActionResult GetStudentById(string studentId)
        {
            string role = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value;

            try
            {
                switch (role)
                {
                case "admin":
                    string adminId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling admin access level StudentsService method GetById. Admin ID: {0}", adminId);
                    StudentWithParentGradesClassDTO retVal1 = service.GetById(studentId);
                    if (retVal1 == null)
                    {
                        return(NotFound());
                    }
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal1));

                case "teacher":
                    string teacherId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling teacher access level StudentsService method GetByIdAndTeacherId. Teacher ID: {0}", teacherId);
                    StudentWithParentGradesClassDTO retVal2 = service.GetByIdAndTeacherId(studentId, teacherId);
                    if (retVal2 == null)
                    {
                        return(NotFound());
                    }
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal2));

                case "parent":
                    string parentId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling parent access level StudentsService method GetByIdAndParentId. Parent ID: {0}", parentId);
                    StudentWithParentGradesClassDTO retVal3 = service.GetByIdAndParentId(studentId, parentId);
                    if (retVal3 == null)
                    {
                        return(NotFound());
                    }
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal3));

                default:
                    logger.Warn("BadRequest. There is no method for this role! {0}", role);
                    return(BadRequest());
                }
            }
            catch (Exception e)
            {
                if (e is ArgumentException)
                {
                    logger.Warn("Caught exception with message {0}. Returning bad request.", e.Message);
                    return(BadRequest(e.Message));
                }
                else
                {
                    logger.Warn("Caught exception with message {0}. Returning bad request.", e.Message);
                    return(NotFound());
                }
            }
        }
        public IHttpActionResult GetAll()
        {
            string role = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value;

            try
            {
                switch (role)
                {
                case "admin":
                    string adminId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling admin access level StudentsService GetAll metod. Admin ID: {0}", adminId);
                    var retVal1 = service.GetAll();
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal1));

                case "teacher":
                    string teacherId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling teacher access level StudentsService GetAllByTeacherId method. Teacher ID: {0}", teacherId);
                    var retVal2 = service.GetAllByTeacherId(teacherId);
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal2));

                case "parent":
                    string parentId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Calling parent access level StudentsService GetAllByParentId method. Parent ID: {0}", parentId);
                    var retVal3 = service.GetAllByParentId(parentId);
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal3));

                case "student":
                    string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
                    logger.Info("Caling student access level StudentsService method GetById. Student ID: {0}", userId);
                    StudentWithParentGradesClassDTO retVal4 = service.GetById(userId);
                    logger.Info("Returning ok to browser.");
                    return(Ok(retVal4));

                default:
                    logger.Warn("BadRequest. There is no method for this role! {0}", role);
                    return(BadRequest());
                }
            }
            catch (Exception e)
            {
                logger.Warn("Caught exception with message {0}. Returning bad request.", e.Message);
                return(BadRequest(e.Message));
            }
        }
예제 #3
0
        public static StudentWithParentGradesClassDTO StudentToStudentWithParentGradesClassDTO(Student user)
        {
            StudentWithParentGradesClassDTO retVal = new StudentWithParentGradesClassDTO();

            retVal.Id        = user.Id;
            retVal.FirstName = user.FirstName;
            retVal.LastName  = user.LastName;
            retVal.UserName  = user.UserName;
            retVal.Email     = user.Email;

            if (user.Parent == null)
            {
                retVal.Parent = null;
            }
            else
            {
                retVal.Parent = UserToUserSimpleViewDTO(user.Parent);
            }

            if (user.SchoolClass == null)
            {
                retVal.Class = null;
            }
            else
            {
                retVal.Class = SchoolClassToSchoolClassDTOConverters.SchoolClassToSchoolClassDTO(user.SchoolClass);
            }

            if (user.Grades.Count() < 1)
            {
                retVal.Grades = null;
            }
            else
            {
                retVal.Grades = user.Grades.Select(x => GradeToGradeDTOConverters.GradeToGradeDTO(x));
            }

            logger.Info("Converting Student to StudentWithParentGradesClassDTO");
            return(retVal);
        }