public IEnumerable <FormDTOForStudentAndParents> GetAllForStudentFromStudentForm(string userId) { Student foundUser = db.StudentsRepository.GetByID(userId); if (foundUser == null) { return(null); } Form usersForm = GetByID(foundUser.Form.Id); if (usersForm == null) { return(null); } FormDTOForStudentAndParents dto = toDTO.ConvertToFormDTOForStudentAndParent(usersForm); //mora se vratiti lista IList <FormDTOForStudentAndParents> dtos = new List <FormDTOForStudentAndParents> { dto }; return(dtos); }
public HttpResponseMessage GetForm(int id) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value; logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Form by id: " + id); try { Form form = formsService.GetByID(id); if (form == null) { logger.Info("The form with id: " + id + " was not found."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "The form with id: " + id + " was not found.")); } if (userRole == "admin") { logger.Info("Requesting found form convert for " + userRole + "role."); FormDTOForAdmin dto = toDTO.ConvertToFormDTOForAdmin(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (userRole == "teacher") { logger.Info("Requesting found form convert for " + userRole + "role."); FormDTOForTeacher dto = toDTO.ConvertToFormDTOForTeacher(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (form.Students.Any(x => x.Id == userId) == true || form.Students.Any(x => x.Parent.Id == userId) == true) { logger.Info("Requesting found form convert for " + userRole + " role."); FormDTOForStudentAndParents dto = toDTO.ConvertToFormDTOForStudentAndParent(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else { logger.Info("Authorisation failure. User " + userId + " is not authorised for this request."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Access Denied. " + "We’re sorry, but you are not authorized to perform the requested operation.")); } } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }