Exemplo n.º 1
0
        public IHttpActionResult GetGradeById(int gradeId)
        {
            var userData = IdentityHelper.GetLoggedInUser(RequestContext);

            logger.Info("Get Grade {@gradeId} by {@userData}", gradeId, userData);

            // --- Dispatching

            var userInfo = Utilities.WebApi.IdentityHelper.FetchUserData(RequestContext);

            if (userInfo.UserId == null)
            {
                return(Unauthorized());
            }

            var userId = userInfo.UserId ?? 0;

            GradeDto grade = null;

            if (userInfo.IsAdmin)
            {
                grade = gradesService.GetGradeDtoById(gradeId);
            }
            else if (userInfo.IsTeacher)
            {
                grade = gradesService.GetGradesByParameters(gradeId: gradeId, teacherId: userData.UserId).FirstOrDefault();
            }
            else if (userInfo.IsStudent)
            {
                grade = gradesService.GetGradesByParameters(gradeId: gradeId, studentId: userData.UserId).FirstOrDefault();
            }
            else if (userInfo.IsParent)
            {
                grade = gradesService.GetGradesByParameters(gradeId: gradeId, parentId: userData.UserId).FirstOrDefault();
            }
            else
            {
                logger.Error("Authenticated user with no role --- this should not happen");
                return(InternalServerError());
            }

            if (grade == null)
            {
                return(NotFound());
            }

            return(Ok(grade));
        }