private bool isLearnerCourseAppraisalExistsById(long learnerId, LearnerAppraisal request)
 {
     return(_context.LearnerAppraisal.Where(x => x.learnerId == request.learnerId && x.courseId == request.courseId && x.appraisalId == request.appraisalId && x.id != request.id).Any());
 }
        public object AddLearnerAppraisal(long learnerId, Learner request)
        {
            string result            = "";
            int    countDuplicate    = 0;
            int    countNoRecord     = 0;
            bool   isValidationError = true;
            var    arr = new ArrayList();

            foreach (LearnerAppraisal x in request.LearnerAppraisal)
            {
                //if (isLearnerCourseAppraisalExists(learnerId, x) == true)
                //{
                //    countDuplicate++;
                //    //arr.Add(new RequestValidationModel { Name = "User Group ID", Parameter = "userGroupId", Message = "Please select user group" });
                //}

                if (x.courseId == 0 || x.appraisalId == 0)
                {
                    isValidationError = false;
                    if (x.courseId == 0)
                    {
                        arr.Add(new RequestValidationModel {
                            Name = "Course ID", Parameter = "courseId", Message = "Answer for course appraisal " + x.recommendation + " dont have course assigned"
                        });
                    }
                    else if (x.appraisalId == 0)
                    {
                        arr.Add(new RequestValidationModel {
                            Name = "Appraisal ID", Parameter = "appraisalId", Message = "Answer for course appraisal " + x.recommendation + " dont have appraisal assigned"
                        });
                    }
                }
                else
                {
                    if (x.id == 0)
                    {
                        if (isLearnerCourseAppraisalExists(learnerId, x) == true)
                        {
                            countDuplicate++;
                        }
                        else
                        {
                            LearnerAppraisal la = new LearnerAppraisal();
                            la.learnerId      = learnerId;
                            la.courseId       = x.courseId;
                            la.appraisalId    = x.appraisalId;
                            la.recommendation = x.recommendation;
                            la.rating         = x.rating;
                            la.createdAt      = DateTime.Now;
                            _context.LearnerAppraisal.Add(la);
                            _context.SaveChanges();
                        }
                    }
                    else
                    {
                        var la = _context.LearnerAppraisal.Where(i => i.id == x.id).FirstOrDefault();
                        if (la != null)
                        {
                            if (isLearnerCourseAppraisalExistsById(learnerId, x) == true)
                            {
                                countDuplicate++;
                            }
                            else
                            {
                                la.recommendation = x.recommendation;
                                la.rating         = x.rating;
                                _context.SaveChanges();
                            }
                        }
                        else
                        {
                            countNoRecord++;
                        }
                    }
                }
            }

            if (countDuplicate > 0)
            {
                arr.Add(new GenericResult {
                    Response = false, Message = "There are (" + countDuplicate + ") record that already exists"
                });
            }
            else if (countNoRecord > 0)
            {
                arr.Add(new GenericResult {
                    Response = false, Message = "There are (" + countNoRecord + ") entry that is not valid due to to ID mismatch"
                });
            }
            else if (isValidationError == true)
            {
                arr.Add(new GenericResult {
                    Response = true, Message = "Appraisal has been successfully saved"
                });
            }


            return(arr);
        }