public override async Task <ActionResult <Entities.SurveyResponse> > Post(Entities.SurveyResponse entity) { var appName = Convert.ToString(Request.Headers["AppName"]); var key = Convert.ToString(Request.Headers["AppKey"]); var user = Convert.ToString(Request.Headers["UserName"]); var type = _mgSurveyDbContext.FormTypes.FirstOrDefault(t => t.Name.ToLower() == appName.ToLower() && t.SecretKey.ToLower() == key.ToLower()); if (type == null) { return(BadRequest()); } if (JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _mgSurveyDbContext.SurveyResponses.Add(entity); await _mgSurveyDbContext.SaveChangesAsync(); return(Ok(entity)); // }
public override async Task <ActionResult <Entities.Form> > Put(Entities.Form entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized)); } //validate entity //var schemaName = typeof(Entities.Form).Name; //var schema = _mgSurveyDbContext.ValidationSchemas.FirstOrDefault(s => s.Name == schemaName || s.Code == schemaName); //var validationErrors = _schemaValidatorService.Validate<Entities.Form>(entity, schema); //if (validationErrors != null && validationErrors.Count > 0) //{ // return BadRequest(validationErrors); //} // check if record with same name or code already exists var dbEntity = await _mgSurveyDbContext.Forms.FirstOrDefaultAsync(e => e.Id != entity.Id); if (dbEntity == null) { //validationErrors = new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary(); //validationErrors.AddModelError("Name", "Form with same name already exists"); return(BadRequest()); } _mgSurveyDbContext.Forms.Update(entity); await _mgSurveyDbContext.SaveChangesAsync(); return(Ok(entity)); }
public override async Task <ActionResult <Entities.Form> > Post(Entities.Form entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized));; } //link form validation schema if any if (entity.ValidationSchema != null) { entity.ValidationSchema.Id = Guid.NewGuid().ToString(); entity.ValidationSchema.FormId = entity.Id; } //validate entity //var schemaName = typeof(Entities.Form).Name; //var schema = _mgSurveyDbContext.ValidationSchemas.FirstOrDefault(s => s.Name == schemaName || s.Code == schemaName); _mgSurveyDbContext.Forms.Add(entity); await _mgSurveyDbContext.SaveChangesAsync(); return(Ok(entity)); }
public ActionResult GetSurvey() { try { var appName = Convert.ToString(Request.Headers["AppName"]); var key = Convert.ToString(Request.Headers["AppKey"]); var user = Convert.ToString(Request.Headers["UserName"]); var type = _mgSurveyDbContext.FormTypes.FirstOrDefault(t => t.Name.ToLower() == appName.ToLower() && t.SecretKey.ToLower() == key.ToLower()); if (type == null) { return(BadRequest()); } if (JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized));; } var pformBuilder = PredicateBuilder <Entities.Form> .Builder; var startDateBuilder = pformBuilder.LessThanOrEqual <DateTime>("StartDate", DateTime.Today); var endDateBuilder = pformBuilder.GreaterThanOrEqual <DateTime>("EndDate", DateTime.Today); //var fieldCompared = pBuilder.GreaterThan( // pBuilder.GetJsonFieldValue<DateTime>("EndDate"), // pBuilder.GetJsonFieldValue<DateTime>("StartDate") // ); var andBuilder = pformBuilder.And(startDateBuilder, endDateBuilder); //pBuilder.Or(andBuilder, startDateBuilder); var pSurveyRBuilder = PredicateBuilder <Entities.SurveyResponse> .Builder; var userDataBuilder = pSurveyRBuilder.Equal("UserName", user); var response = _mgSurveyDbContext.Forms.Where(t => t.Type.ToLower() == appName.ToLower() && t.IsActive == true).Where(andBuilder).Include(s => s.SurveyResponses).OrderByDescending(t => t.CreatedDate).FirstOrDefault(); if (response == null) { return(new StatusCodeResult(StatusCodes.Status204NoContent)); } var surveyR = _mgSurveyDbContext.SurveyResponses.Where(t => t.FormId == response.Id).Where(userDataBuilder).FirstOrDefault(); if (surveyR == null) { return(Ok(response)); } else { return(new StatusCodeResult(StatusCodes.Status204NoContent)); } } catch (Exception e) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public IQueryable GetApplicationTypes() { if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(null); } return(_mgSurveyDbContext.FormTypes.Where(t => t.IsActive == true).OrderBy(t => t.Name).AsQueryable()); }
public override IQueryable Get() { if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(null); } return(_mgSurveyDbContext.SurveyResponses.Include(t => t.Form).AsQueryable()); }
public override async Task <ActionResult <Entities.SurveyResponse> > Put(Entities.SurveyResponse entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized)); } _mgSurveyDbContext.SurveyResponses.Update(entity); await _mgSurveyDbContext.SaveChangesAsync(); return(Ok(entity)); }
public override IQueryable Get() { // _mgSurveyDbContext.Forms.Where(PredicateBuilder.Equal<Entities.Form>("StartDate", customDate)).Where(t => t.Name == appName).ToList(); //var pBuilder = PredicateBuilder<Entities.Form>.Builder; //var startDateBuilder = pBuilder.GreaterThan<DateTime>("StartDate", DateTime.Today); //var fieldCompared = pBuilder.GreaterThan( // pBuilder.GetJsonFieldValue<DateTime>("EndDate"), // pBuilder.GetJsonFieldValue<DateTime>("StartDate") // ); //var andBuilder= pBuilder.And(startDateBuilder, fieldCompared); //pBuilder.Or(andBuilder, startDateBuilder); if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(null); } var response = _mgSurveyDbContext.Forms.Include(x => x.SurveyResponses).AsQueryable(); return(response); }
public dynamic GetSurveyReport(string surveyId) { try { if (!JWTTokenDeserializer.IsValidPCSSuperUser(this.HttpContext.Request.Headers["Authorization"].ToString())) { return(new StatusCodeResult(StatusCodes.Status401Unauthorized)); } var surveyResponses = _mgSurveyDbContext.SurveyResponses.Include(x => x.Form.ValidationSchema).Where(t => t.FormId == surveyId).ToList(); if (surveyResponses.Count == 0) { return(new StatusCodeResult(StatusCodes.Status204NoContent)); } var surveyTemplate = surveyResponses.FirstOrDefault().Form; //var validationScheme = _mgSurveyDbContext.ValidationSchemas.FirstOrDefault(t => t.FormId == surveyId); var validationScheme = surveyTemplate.ValidationSchema; var validationEntityData = JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(validationScheme.EntityData), new JsonConverter[] { new MGSurveyJsonConverter() }); var schema = validationEntityData["Schema"]; var fields = JsonConvert.DeserializeObject <List <Field> >(JsonConvert.SerializeObject(schema["Fields"])); // var sfields = JsonConvert.SerializeObject(actual.ToString()); //var schema = JsonConvert.DeserializeObject<Field>(actual["Schema"]); //if (surveyTemplate == null) // return new StatusCodeResult(StatusCodes.Status204NoContent); var templateEntityData = JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(surveyTemplate.EntityData), new JsonConverter[] { new MGSurveyJsonConverter() }); if (templateEntityData == null) { return(new StatusCodeResult(StatusCodes.Status204NoContent)); } // List<Tuple<string, string>> questionsList = new List<Tuple<string, string>>(); var surveyStartDate = templateEntityData["StartDate"]; var surveyEndDate = templateEntityData["EndDate"]; //var templateSchema = templateEntityData["Schema"]; //var templateComponent = (JArray)templateSchema["components"]; //foreach (var item in templateComponent) //{ // if (item["type"].ToString() == "panel") // { // foreach (var subitem in item["components"]) // { // if (subitem["type"] == null || (subitem["type"].ToString() != "button" && subitem["type"].ToString() != "submit")) // { // var question = subitem["label"]; // Tuple<string, string> tp = new Tuple<string, string>(subitem["key"].ToString(), subitem["label"].ToString()); // questionsList.Add(tp); // } // } // } // else // { // if (item["type"] == null || (item["type"].ToString() != "button" && item["type"].ToString() != "submit")) // { // var question = item["label"]; // Tuple<string, string> tp = new Tuple<string, string>(item["key"].ToString(), item["label"].ToString()); // questionsList.Add(tp); // } // } //} List <SurveyResponsDetail> reportData = new List <SurveyResponsDetail>(); foreach (var response in surveyResponses) { var answers = response.EntityData["Data"]; var ans = JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(answers), new JsonConverter[] { new MGSurveyJsonConverter() }); foreach (var qst in fields) { SurveyResponsDetail objDetail = new SurveyResponsDetail(); objDetail.Answers = ans[qst.Name].ToString(); objDetail.Question = qst.Label; objDetail.UserName = response.EntityData["UserName"].ToString(); objDetail.CompanyName = response.EntityData["CompanyName"].ToString(); objDetail.Id = response.Id; objDetail.ResponseDate = response.CreatedDate; objDetail.SurveyName = response.Form.Name; objDetail.SurveyStartDate = Convert.ToString(surveyStartDate); objDetail.SurveyEndDate = Convert.ToString(surveyEndDate); reportData.Add(objDetail); } } return(reportData); } catch (Exception e) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }