Пример #1
0
        public async Task <SpectraAnswer> CreateAnswerAsync(string probDescription, string ansDescription,
                                                            List <string> probPics, List <string> ansPics, FormulaConfig formula, float ionPeak = -1)
        {
            if ((formula == null || !formula.IsValid()) && ionPeak < 0)
            {
                return(null);
            }
            var probPictures = probPics.Select(pic => new ProblemPicture(pic));
            var ansPictures  = ansPics.Select(pic => new AnswerPicture(pic));
            var answer       = new SpectraAnswer(probDescription, ansDescription);

            if (formula != null && formula.IsValid())
            {
                // NMR
                var nmrProb = new NMRProblem(formula);
                nmrProb.Answer = answer;
                await _context.NmrProblems.AddAsync(nmrProb);
            }

            if (ionPeak >= 0)
            {
                // Mass
                var massProb = new MassProblem(ionPeak);
                massProb.Answer = answer;
                await _context.MassProblems.AddAsync(massProb);
            }

            // add pictures to DB set
            foreach (var pic in ansPictures)
            {
                pic.SpectraAnswer = answer;
                await _context.AnswerPics.AddRangeAsync(pic);
            }

            foreach (var pic in probPictures)
            {
                pic.SpectraAnswer = answer;
                await _context.ProblemPics.AddAsync(pic);
            }

            await _context.SaveChangesAsync();

            return(RetrieveAnswerById(answer.Id));
        }
Пример #2
0
        public async Task <SpectraAnswer> UpdateAnswerAsync(int id, string probDescription, string ansDescription,
                                                            List <string> probPics, List <string> ansPics,
                                                            float ionPeak = -1, FormulaConfig formula = null)
        {
            var ans = RetrieveAnswerById(id);

            if (ans == null || (ionPeak < 0 && (formula == null || !formula.IsValid())))
            {
                return(null);
            }

            ans.ProblemDescription = probDescription;
            ans.AnswerDescription  = ansDescription;
            var probPictures = probPics.Select(pic => new ProblemPicture(pic)
            {
                SpectraAnswer = ans
            }).ToList();
            var ansPictures = ansPics.Select(pic => new AnswerPicture(pic)
            {
                SpectraAnswer = ans
            }).ToList();

            // if not any ,then do nothing to old pictures
            if (ansPics.Any())
            {
                _context.AnswerPics.RemoveRange(ans.AnswerPictures);
                await _context.AnswerPics.AddRangeAsync(ansPictures);
            }

            if (probPics.Any())
            {
                _context.ProblemPics.RemoveRange(ans.ProblemPictures);
                await _context.ProblemPics.AddRangeAsync(probPictures);
            }


            // delete old nmr or mass problem
            if (ans.MassProblem != null)
            {
                _context.MassProblems.Remove(ans.MassProblem);
            }
            if (ans.NmrProblem != null)
            {
                _context.NmrProblems.Remove(ans.NmrProblem);
            }

            if (formula != null && formula.IsValid())
            {
                // nmr
                var nmrProblem = new NMRProblem(formula);
                nmrProblem.Answer = ans;
                await _context.NmrProblems.AddAsync(nmrProblem);
            }

            if (ionPeak >= 0)
            {
                // mass
                var massProblem = new MassProblem(ionPeak);
                massProblem.Answer = ans;
                await _context.MassProblems.AddAsync(massProblem);
            }


            var answerEntity = _context.SpectraAnswers.Attach(ans);

            answerEntity.State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(ans);
        }