Esempio n. 1
0
        public IHttpActionResult GetAllByStudentName([FromUri] string firstName, [FromUri] string lastName)
        {
            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 GradesService GetAllByStudentName metod. Admin ID: {0}", adminId);
                    var retVal1 = service.GetAllByStudentName(firstName, lastName);
                    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 GradesService GetAllByStudentNameAndTeacherId method. Teacher ID: {0}", teacherId);
                    var retVal2 = service.GetAllByStudentNameAndTeacherId(firstName, lastName, 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 GradesService GetAllByStudentNameAndParentId method. Parent ID: {0}", parentId);
                    var retVal3 = service.GetAllByStudentNameAndParentId(firstName, lastName, parentId);
                    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)
            {
                logger.Warn("Caught exception with message {0}. Returning bad request.", e.Message);
                return(BadRequest(e.Message));
            }
        }