public async Task <IActionResult> CreateboardEnrollment([FromBody] BoardEnrollmentResource boardEnrollmentResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var boardEnrollment = mapper.Map <BoardEnrollmentResource, BoardEnrollment>(boardEnrollmentResource);

            boardEnrollment.Lecturer = await lecturerRepository.GetLecturer(boardEnrollmentResource.LecturerID);

            boardEnrollment.Board = await boardRepository.GetBoard(boardEnrollmentResource.BoardID);

            if (boardEnrollment.Score != null)
            {
                boardEnrollment.isMarked = true;
            }

            boardEnrollmentRepository.UpdateRecommendations(boardEnrollment, boardEnrollmentResource);

            boardEnrollmentRepository.AddBoardEnrollment(boardEnrollment);

            await unitOfWork.Complete();

            boardEnrollment = await boardEnrollmentRepository.GetBoardEnrollment(boardEnrollment.BoardEnrollmentId);

            var result = mapper.Map <BoardEnrollment, BoardEnrollmentResource>(boardEnrollment);

            return(Ok(result));
        }
        public void UpdateGrades(BoardEnrollment boardEnrollment, BoardEnrollmentResource boardEnrollmentResource)
        {
            var totalScore = 0.0;

            foreach (var gradeInformation in boardEnrollmentResource.GradeInformation)
            {
                var grade = context.Grades
                            .Include(g => g.BoardEnrollment)
                            .FirstOrDefault(g => g.BoardEnrollment.BoardEnrollmentId == boardEnrollment.BoardEnrollmentId &&
                                            g.GradeDescription.Equals(gradeInformation.GradeDescription));
                if (grade != null)
                {
                    grade.Score           = gradeInformation.Score;
                    grade.Comment         = gradeInformation.Comment;
                    grade.BoardEnrollment = boardEnrollment;
                }
                totalScore += gradeInformation.Score.Value;
            }
            boardEnrollment.Score = totalScore;
        }
        public async Task <IActionResult> UpdateboardEnrollment(int id, [FromBody] BoardEnrollmentResource boardEnrollmentResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var boardEnrollment = await boardEnrollmentRepository.GetBoardEnrollment(id);

            if (boardEnrollment == null)
            {
                return(NotFound());
            }

            var lecturer = await lecturerRepository.GetLecturer(boardEnrollmentResource.LecturerID);

            var board = await boardRepository.GetBoard(boardEnrollmentResource.BoardID);

            //some bug cannot explain, cannot ignore lecturer and board when map BEResource -> BE
            //so we must change these into null
            boardEnrollmentResource.Lecturer = null;
            boardEnrollmentResource.Board    = null;

            mapper.Map <BoardEnrollmentResource, BoardEnrollment>(boardEnrollmentResource, boardEnrollment);
            boardEnrollment.Board    = board;
            boardEnrollment.Lecturer = lecturer;

            boardEnrollmentRepository.UpdateGrades(boardEnrollment, boardEnrollmentResource);
            // boardEnrollmentRepository.UpdateRecommendations(boardEnrollment, boardEnrollmentResource);

            if (boardEnrollment.Score != null)
            {
                boardEnrollment.isMarked = true;
            }

            await unitOfWork.Complete();

            var result = mapper.Map <BoardEnrollment, BoardEnrollmentResource>(boardEnrollment);

            return(Ok(result));
        }
        public void UpdateRecommendations(BoardEnrollment boardEnrollment, BoardEnrollmentResource boardEnrollmentResource)
        {
            if (boardEnrollmentResource.Recommendations != null && boardEnrollmentResource.Recommendations.Count >= 0)
            {
                //remove old tagprojects
                var oldRecommendations = boardEnrollment.Recommendations.Where(p => !boardEnrollmentResource.Recommendations.Any(id => id == p.Description)).ToList();
                foreach (Recommendation recommendation in oldRecommendations)
                {
                    recommendation.IsDeleted = true;
                    boardEnrollment.Recommendations.Remove(recommendation);
                }
                //project.TagProjects.Clear();

                //add new tagprojects
                var newRecommendations = boardEnrollmentResource.Recommendations.Where(t => !boardEnrollment.Recommendations.Any(id => id.Description == t));
                foreach (var recommendation in newRecommendations)
                {
                    boardEnrollment.Recommendations.Add(new Recommendation {
                        IsDeleted = false, IsDone = false, Description = recommendation
                    });
                    //project.TagProjects.Add(a);
                }
            }
        }