public JsonResult Get(int classId) { try { var result = _repository.GetClassById(classId); var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == result.NurseryId.ToString()); if (User.IsInRole("Admin") || matchingNurs != null) { if (result == null) { return(Json(null)); } return(Json(Mapper.Map <ClassViewModel>(result))); } } catch (Exception ex) { _logger.LogError($"Failed to get class {classId}", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Error occurred finding class id")); } Response.StatusCode = (int)HttpStatusCode.Unauthorized; return(Json("Unauthorized to get this class")); }
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}")); }