public async Task <IActionResult> Post([FromBody] NewSituationViewDto data) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _situationService.CreateSituation(data); if (result != null) { return(Ok(result)); } return(BadRequest()); }
public async Task <NewSituationViewDto> CreateSituation(NewSituationViewDto newSituation) { try { if (newSituation != null) { Situations situation = new Situations(); situation.Name = newSituation.Name; situation.YdsToPin = newSituation.YdsToPin; situation.WindSpeed = newSituation.WindSpeed; situation.Elevation = Convert.ToString(newSituation.Elevation); situation.WindDirection = newSituation.WindDirection; //situation.NotSituationHandicapId = newSituation.NotSituationHandicapId; situation.StartCoordinate = newSituation.StartCoordinate; situation.TargetCoordinate = newSituation.TargetCoordinate; //situation.BookId = newSituation.BookId; //situation.ImageFileId = newSituation.ImageFileId; situation.LineType = newSituation.LineType; situation.PinCoordinate = newSituation.PinCoordinate; situation.IsFirstHole = newSituation.IsFirstHole; situation.NextHoleSituationId = newSituation.NextHoleSituationId; situation.VoiceOverUrl = newSituation.VoiceOverUrl; situation.IsBegining = newSituation.VoiceOverUrl == null ? 2 : newSituation.IsBegining; situation.SituationCategory = _context.SituationCategories.Where (c => c.Id == newSituation.SituationCategoryId) .Select(c => c).FirstOrDefault(); situation.Book = _context.PictureBooks.Where (b => b.Id == newSituation.BookId) .Select(b => b).FirstOrDefault(); situation.ImageFile = _context.PictureBooks.Where (i => i.Id == newSituation.ImageFileId) .Select(i => i).FirstOrDefault(); situation.BookId = situation.Book.Id; situation.ImageFileId = situation.ImageFile.Id; var sid = _context.Situations.Add(situation); await _context.SaveChangesAsync(); //NewSituationAttributes var sattributes = newSituation.AttributesId.ToList(); for (int i = 0; i < sattributes.Count; i++) { SituationAttributes situationAttributes = new SituationAttributes(); situationAttributes.AttributesId = sattributes[i]; situationAttributes.SituationId = sid.Entity.Id; _context.SituationAttributes.Add(situationAttributes); } await _context.SaveChangesAsync(); //NewSituationHandicap if (newSituation.HandicapId != null) { var shandicaps = newSituation.HandicapId.ToList(); for (int i = 0; i < shandicaps.Count; i++) { SituationHandicaps situationHandicap = new SituationHandicaps(); situationHandicap.HandicapId = shandicaps[i]; situationHandicap.SituationId = sid.Entity.Id; var h = _context.SituationHandicaps.Add(situationHandicap); } await _context.SaveChangesAsync(); } //NewSituationQuestions - NewSituationAnswers if (newSituation.Questions != null) { var squestions = newSituation.Questions.ToList(); for (int i = 0; i < squestions.Count; i++) { SituationQuestions situationQuestions = new SituationQuestions(); situationQuestions.Question = squestions[i].Question; situationQuestions.SituationId = sid.Entity.Id; situationQuestions.SituationId1 = sid.Entity.Id; var qestionId = _context.SituationQuestions.Add(situationQuestions); await _context.SaveChangesAsync(); var sanswers = squestions[i].Answers.ToList(); for (int j = 0; j < sanswers.Count; j++) { Answers answers = new Answers(); answers.Answer = sanswers[j].Answer; answers.DidYouKnowLink = sanswers[j].DidYouKnowLink; answers.LessonLink = sanswers[j].LessonLink; answers.Response = sanswers[j].Response; answers.RulesLink = sanswers[j].RulesLink; answers.Score = sanswers[j].Score; answers.SituationQuestionsId = qestionId.Entity.Id; answers.Qid = qestionId.Entity.Id; _context.Answers.Add(answers); await _context.SaveChangesAsync(); } } } newSituation.Id = situation.Id; return(newSituation); } return(null); } catch (System.Exception ex) { throw ex; } }