Exemplo n.º 1
0
        public async Task <int> Update(QnAViewModel model)
        {
            try {
                var qna = await _IQnARepository.GetByIdAsync(model.Id);

                if (qna != null)
                {
                    qna.Question     = model.Question;
                    qna.Answer       = model.Answer;
                    qna.Type         = model.Type;
                    qna.IsActive     = model.IsActive;
                    qna.DateModified = DateTime.Now;
                    qna.UserModified = model.UserModified;

                    await _IQnARepository.UpdateAsync(qna);

                    return(qna.Id);
                }
            }
            catch
            {
                return(0);
            }

            return(0);
        }
Exemplo n.º 2
0
        public async Task <int> Create(QnAViewModel model)
        {
            try {
                var list     = _IQnARepository.ListAll().Where(o => o.Type == model.Type && o.IsActive == model.IsActive).Select(q => q.Order).ToList();
                int maxOrder = 0;
                if (list.Count > 0)
                {
                    maxOrder = list.Max() + 1;
                }
                else
                {
                    maxOrder = 1;
                }

                QnA qnA = new QnA()
                {
                    Order        = maxOrder,
                    IsActive     = model.IsActive,
                    Question     = model.Question,
                    Answer       = model.Answer,
                    DateCreated  = DateTime.Now,
                    DateModified = DateTime.Now,
                    Type         = model.Type,
                    UserCreated  = model.UserCreated,
                    UserModified = model.UserModified
                };

                return((await _IQnARepository.AddAsync(qnA)).Id);
            }
            catch (Exception ex) {
                return(0);
            }
        }
        public async Task <IActionResult> UpdateQnA(QnAViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id > 0) //edit
                {
                    model.UserModified = HttpContext.User.Identity.Name;
                    var retValue = await _IQnAService.Update(model);

                    if (retValue > 0)
                    {
                        TempData["MessageSuccess"] = string.Format("{0}", "Thay đổi QnA thành công");
                    }
                    else
                    {
                        TempData["MessageError"] = string.Format("{0}", "Thay đổi QnA không thành công");
                    }
                }
                else //insert
                {
                    model.UserCreated = model.UserModified = HttpContext.User.Identity.Name;
                    var retValue = await _IQnAService.Create(model);

                    if (retValue > 0)
                    {
                        TempData["MessageSuccess"] = string.Format("{0}", "Thêm mới QnA thành công");
                    }
                    else
                    {
                        TempData["MessageError"] = string.Format("{0}", "Thêm mới QnA không thành công");
                    }
                }
            }
            else
            {
                TempData["MessageError"] = string.Format("{0}", "ModelState Invalid");
            }
            return(View(model));
        }