Exemple #1
0
        public ApiResponse <QuizApiModel> UpdateQuiz(QuizApiModel quizApiModel)
        {
            var response = new ApiResponse <QuizApiModel>();

            var quiz = _context.Quizes.FirstOrDefault(x => x.Id == quizApiModel.Id);

            quiz.Description       = quizApiModel.Description;
            quiz.Duration          = quizApiModel.Duration;
            quiz.PassingPercentage = quizApiModel.PassingPercentage;

            _context.Entry(quiz).State = EntityState.Modified;
            int rowAffected = _context.SaveChanges();

            response.IsSucceeded = rowAffected > 0;
            response.Result      = quizApiModel;

            return(response);
        }
        public ApiResponse <UserAccountApiModel> UpdateUserProfile(UserAccountApiModel userProfile)
        {
            var response = new ApiResponse <UserAccountApiModel>();

            var user = _context.Users.FirstOrDefault(x => x.Id == userProfile.Id);

            if (user != null)
            {
                user.Email            = userProfile.Email;
                user.ContactNumber    = userProfile.ContactNumber;
                user.ProfileImagePath = userProfile.ProfileImagePath;

                _context.Entry(user).State = EntityState.Modified;
                int rowAffected = _context.SaveChanges();
                response.IsSucceeded = rowAffected > 0;
            }

            response.DisplayMessage = response.IsSucceeded? "Your profile has successfully updated":"Failed , Please try after some time";
            response.Result         = userProfile;

            return(response);
        }
        public Task UpdateQuestion(QuestionApiModel questionApiModel)
        {
            var question = _context.Questions.FirstOrDefault(x => x.Id == questionApiModel.Id);

            using (DbContextTransaction dbTran = _context.Database.BeginTransaction())
            {
                try
                {
                    question.Name = questionApiModel.Name;
                    _context.Entry(question).State = EntityState.Modified;

                    List <Option> options = new List <Option>();
                    questionApiModel.Options.ToList().ForEach(
                        x => options.Add(question.Options
                                         .Where(o => o.Code == x.Code)
                                         .Select(s => { s.Name = x.Name; return(s); })
                                         .FirstOrDefault()));


                    foreach (var option in options)
                    {
                        _context.Entry(option).State = EntityState.Modified;
                    }

                    _context.SaveChanges();
                    dbTran.Commit();
                }
                catch (DbEntityValidationException ex)
                {
                    dbTran.Rollback();
                    throw;
                }
            }

            return(Task.FromResult(0));
        }