Beispiel #1
0
        public JsonResult Get(int childId)
        {
            try
            {
                var child = _repository.GetChildById(childId);

                var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == child.NurseryId.ToString());
                if (User.IsInRole("Admin") || matchingNurs != null)
                {
                    if (child == null)
                    {
                        return(Json(null));
                    }

                    if (child.ClassId == null)
                    {
                        return(Json(Mapper.Map <ChildViewModel>(child)));
                    }

                    var cls    = _repository.GetClassById((int)child.ClassId);
                    var result = Mapper.Map <ChildViewModel>(child);
                    result.ClassName = cls.Name;

                    return(Json(result));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get child {childId}", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Error occurred finding child id"));
            }

            Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            return(Json($"You are unauthorized to view this child {childId}"));
        }