Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
        public async Task <EditSituationViewModel> UpdateSituation(int id, EditSituationViewModel newSituation)
        {
            try
            {
                Situations oldSituation = _context.Situations.Where
                                              (os => os.Id == id)
                                          .Select(os => os).First();
                oldSituation.Deleted = true;
                _context.Entry(oldSituation).State = EntityState.Modified;
                _context.SaveChanges();

                // 2.) insert new situation based on newSituationViewModel
                Situations situation = new Situations();
                situation.Elevation           = Convert.ToString(newSituation.Elevation);
                situation.LineType            = newSituation.LineType;
                situation.Name                = newSituation.Name;
                situation.PinCoordinate       = newSituation.PinCoordinate;
                situation.StartCoordinate     = newSituation.StartCoordinate;
                situation.TargetCoordinate    = newSituation.TargetCoordinate;
                situation.WindDirection       = newSituation.WindDirection;
                situation.WindSpeed           = newSituation.WindSpeed;
                situation.IsFirstHole         = newSituation.IsFirstHole;
                situation.NextHoleSituationId = newSituation.NextHoleSituationId;
                situation.YdsToPin            = newSituation.YdsToPin;
                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).First();
                situation.Book = _context.PictureBooks.Where
                                     (b => b.Id == newSituation.BookId)
                                 .Select(b => b).First();
                situation.ImageFile = _context.PictureBooks.Where
                                          (i => i.Id == newSituation.ImageFileId)
                                      .Select(i => i).First();

                var sid = _context.Situations.Add(situation);  //sid.Id;
                _context.SaveChanges();

                //NewSituationAttributes
                var sa = newSituation.AttributesId.ToList();
                for (int i = 0; i < sa.Count; i++)
                {
                    SituationAttributes situationAttributes = new SituationAttributes();
                    situationAttributes.AttributesId = sa[i];
                    situationAttributes.SituationId  = sid.Entity.Id;
                    _context.SituationAttributes.Add(situationAttributes);
                    _context.SaveChanges();
                }

                //NewSituationHandicap
                var sh = newSituation.HandicapId.ToList();
                for (int i = 0; i < sh.Count; i++)
                {
                    SituationHandicaps situationHandicap = new SituationHandicaps();
                    situationHandicap.HandicapId  = sh[i];
                    situationHandicap.SituationId = sid.Entity.Id;
                    var h = _context.SituationHandicaps.Add(situationHandicap);
                    _context.SaveChanges();
                }

                //NewSituationQuestions - NewSituationAnswers
                var q = newSituation.Questions.ToList();
                for (int i = 0; i < q.Count; i++)
                {
                    SituationQuestions situationQuestions = new SituationQuestions();
                    situationQuestions.Question     = q[i].Question;
                    situationQuestions.SituationId  = sid.Entity.Id;
                    situationQuestions.SituationId1 = sid.Entity.Id;
                    var qes = _context.SituationQuestions.Add(situationQuestions);
                    _context.SaveChanges();

                    var a = q[i].Answers.ToList();
                    for (int j = 0; j < a.Count; j++)
                    {
                        Answers answers = new Answers();
                        answers.Answer               = a[j].Answer;
                        answers.DidYouKnowLink       = a[j].DidYouKnowLink;
                        answers.LessonLink           = a[j].LessonLink;
                        answers.Response             = a[j].Response;
                        answers.RulesLink            = a[j].RulesLink;
                        answers.Score                = a[j].Score;
                        answers.SituationQuestionsId = qes.Entity.Id;
                        answers.Qid = qes.Entity.Id;
                        _context.Answers.Add(answers);
                        _context.SaveChanges();
                    }
                }
                newSituation.Id = situation.Id;

                return(newSituation);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }