public IActionResult AddDesease([FromBody] Disease disease) { log.Info($"DiseaseController::AddDisease.Adding Disease {disease.Name}."); if (!ModelState.IsValid) { log.Error($"DiseaseController::AddDisease. Invalid Model of Disease submitted."); return(BadRequest("Invalid data submitted")); } try { var addedDisease = _service.AddDiseaseAsync(disease); log.Info($"DiseaseController::AddDisease. Disease {disease.Name} added."); return(Created("disease/", addedDisease.Id)); } catch (ArgumentException ex) { log.Error($"DiseaseController::AddDisease. Disease {disease.Name} not added due to duplicated name."); return(BadRequest(ex.Message)); }; }
public async Task <IActionResult> UploadFile() { #region Code that needs a better place if (Request.Form.Files.Count != 1) { return(BadRequest(new FeedbackDTO("only one file allowed at a time"))); } var file = Request.Form.Files.First(); var streamReader = new StreamReader(file.OpenReadStream()); var symptomList = new List <string>(); while (!streamReader.EndOfStream) { var strings = (await streamReader.ReadLineAsync()).Split(',').ToList().Skip(1).ToList(); symptomList.AddRange(strings); } streamReader.Close(); symptomList = symptomList.GroupBy(s => s).Select(x => x.First()).ToList(); var symptomsDictionary = new Dictionary <string, SymptomDTO>(); foreach (string symptomName in symptomList) { symptomsDictionary.Add(symptomName, await _symptomService.AddSymptomAsync(new SymptomDTO { Name = symptomName })); } streamReader = new StreamReader(file.OpenReadStream()); while (!streamReader.EndOfStream) { var line = (await streamReader.ReadLineAsync()).Split(','); var newDisease = new DiseaseDTO { Name = line.First(), Symptoms = new List <SymptomDTO>() }; var diseaseSymptoms = line.Skip(1).ToList(); foreach (var symptom in diseaseSymptoms) { SymptomDTO foundSymptom; if (symptomsDictionary.TryGetValue(symptom, out foundSymptom)) { newDisease.Symptoms.Add(foundSymptom); } } await _diseaseService.AddDiseaseAsync(newDisease); } streamReader.Close(); #endregion return(Ok()); }
public async Task <IActionResult> Post([FromBody] DiseaseDTO disease) { if (disease.Id != null) { return(BadRequest(new FeedbackDTO("ID can not be set when posting"))); } if (disease?.Symptoms == null || disease.Symptoms.Count < 1) { return(BadRequest(new FeedbackDTO("Disease must have symptoms"))); } if (!disease.Symptoms.TrueForAll(s => s.Id != null)) { return(BadRequest(new FeedbackDTO("Symptoms must have IDs"))); } return(Ok(await _diseaseService.AddDiseaseAsync(disease))); }