Example #1
0
        public IHttpActionResult GetAllByValueRange([FromUri] int min, [FromUri] int max)
        {
            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 GetAllByValueRange metod. Admin ID: {0}", adminId);
                    var retVal1 = service.GetAllByValueRange(min, max);
                    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 GetAllByValueRangeAndTeacherId method. Teacher ID: {0}", teacherId);
                    var retVal2 = service.GetAllByValueRangeAndTeacherId(min, max, 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 GetAllByValueRangeAndParentId method. Parent ID: {0}", parentId);
                    var retVal3 = service.GetAllByValueRangeAndParentId(min, max, 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 GradesService method GetAllByValueRangeAndStudentId. Student ID: {0}", userId);
                    var retVal4 = service.GetAllByValueRangeAndStudentId(min, max, 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));
            }
        }