Пример #1
0
        public async Task <IActionResult> UpdateBoard(int id, [FromBody] BoardResource boardResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var checkLecturerInformations = boardRepository.CheckLecturerInformations(boardResource.LecturerInformations);

            //case: one percent of score is equal 0 or null
            if (checkLecturerInformations == "nullScorePercent")
            {
                ModelState.AddModelError("Error", "One or more lecturer's percentage of score is not set");
                return(BadRequest(ModelState));
            }

            //case: the total sum of score is not 100
            if (checkLecturerInformations == "sumScorePercentIsNot100")
            {
                ModelState.AddModelError("Error", "Total percentage of score is not equal 100%");
                return(BadRequest(ModelState));
            }

            var board = await boardRepository.GetBoard(id, true);

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

            mapper.Map <BoardResource, Board>(boardResource, board);

            await boardRepository.UpdateBoardEnrollments(board, boardResource);

            await unitOfWork.Complete();

            var group = await groupRepository.GetGroup(boardResource.GroupId);

            board.Group = group;
            await unitOfWork.Complete();

            boardRepository.RemoveOldLecturer(board);
            await unitOfWork.Complete();

            // boardResource.ResultScore = calculateGrade(boardResource.LecturerInformations);

            await boardRepository.AddLecturers(board, boardResource.LecturerInformations);

            await unitOfWork.Complete();

            if (board.BoardEnrollments.Count(c => c.isMarked == true) == board.BoardEnrollments.Count)
            {
                board.isAllScored = true;
                await unitOfWork.Complete();
            }

            var result = mapper.Map <Board, BoardResource>(board);

            return(Ok(result));
        }
Пример #2
0
        public async Task UpdateOrder(Board board, BoardResource boardResource)
        {
            var listBoardInDate = await context.Boards.Where(b => b.DateTime.Date == boardResource.DateTime.Date &&
                                                             b.DateTime.Month == boardResource.DateTime.Month &&
                                                             b.DateTime.Year == boardResource.DateTime.Year).ToListAsync();

            board.Order = listBoardInDate.Count + 1;
        }
Пример #3
0
        public async Task UpdateBoardEnrollments(Board board, BoardResource BoardResource)
        {
            if (BoardResource.BoardEnrollments != null && BoardResource.BoardEnrollments.Count >= 0)
            {
                // //remove old BoardEnrollments
                // board.BoardEnrollments.Clear();

                // //add new enrollments
                // var newBoardEnrollments = context.BoardEnrollments.Where(e => BoardResource.BoardEnrollments.Any(id => id == e.BoardEnrollmentId)).ToList();
                // foreach (var a in newBoardEnrollments)
                // {
                //     board.BoardEnrollments.Add(a);
                // }
                var updatedBoardEnrollments = board.BoardEnrollments.ToList();
                foreach (var boardEnrollment in updatedBoardEnrollments)
                {
                    if (boardEnrollment.BoardRole.BoardRoleName == "Chair")
                    {
                        if (boardEnrollment.Lecturer.LecturerId == BoardResource.LecturerInformations.Chair.LecturerId)
                        {
                            boardEnrollment.Percentage = BoardResource.LecturerInformations.Chair.ScorePercent;
                        }
                        else
                        {
                            boardEnrollment.IsDeleted = true;
                            board.BoardEnrollments.Remove(boardEnrollment);
                            var chairBoardEnrollment = new BoardEnrollment
                            {
                                Board      = board,
                                IsDeleted  = false,
                                isMarked   = false,
                                Percentage = BoardResource.LecturerInformations.Chair.ScorePercent,
                                BoardRole  = await context.BoardRoles.FirstOrDefaultAsync(c => c.BoardRoleName == "Chair"),
                                Lecturer   = await context.Lecturers.FindAsync(BoardResource.LecturerInformations.Chair.LecturerId)
                            };
                            context.BoardEnrollments.Add(chairBoardEnrollment);
                        }
                    }

                    else if (boardEnrollment.BoardRole.BoardRoleName == "Secretary")
                    {
                        if (boardEnrollment.Lecturer.LecturerId == BoardResource.LecturerInformations.Secretary.LecturerId)
                        {
                            boardEnrollment.Percentage = BoardResource.LecturerInformations.Secretary.ScorePercent;
                        }
                        else
                        {
                            boardEnrollment.IsDeleted = true;
                            board.BoardEnrollments.Remove(boardEnrollment);
                            var secretaryBoardEnrollment = new BoardEnrollment
                            {
                                Board      = board,
                                IsDeleted  = false,
                                isMarked   = false,
                                Percentage = BoardResource.LecturerInformations.Secretary.ScorePercent,
                                BoardRole  = await context.BoardRoles.FirstOrDefaultAsync(c => c.BoardRoleName == "Secretary"),
                                Lecturer   = await context.Lecturers.FindAsync(BoardResource.LecturerInformations.Secretary.LecturerId)
                            };
                            context.BoardEnrollments.Add(secretaryBoardEnrollment);
                        }
                    }

                    else if (boardEnrollment.BoardRole.BoardRoleName == "Supervisor")
                    {
                        if (boardEnrollment.Lecturer.LecturerId == BoardResource.LecturerInformations.Supervisor.LecturerId)
                        {
                            boardEnrollment.Percentage = BoardResource.LecturerInformations.Supervisor.ScorePercent;
                        }
                        else
                        {
                            boardEnrollment.IsDeleted = true;
                            board.BoardEnrollments.Remove(boardEnrollment);
                            var supervisorBoardEnrollment = new BoardEnrollment
                            {
                                Board      = board,
                                IsDeleted  = false,
                                isMarked   = false,
                                Percentage = BoardResource.LecturerInformations.Supervisor.ScorePercent,
                                BoardRole  = await context.BoardRoles.FirstOrDefaultAsync(c => c.BoardRoleName == "Supervisor"),
                                Lecturer   = await context.Lecturers.FindAsync(BoardResource.LecturerInformations.Supervisor.LecturerId)
                            };
                            context.BoardEnrollments.Add(supervisorBoardEnrollment);
                        }
                    }

                    else if (boardEnrollment.BoardRole.BoardRoleName == "Reviewer")
                    {
                        if (boardEnrollment.Lecturer.LecturerId == BoardResource.LecturerInformations.Reviewer.LecturerId)
                        {
                            boardEnrollment.Percentage = BoardResource.LecturerInformations.Reviewer.ScorePercent;
                        }
                        else
                        {
                            boardEnrollment.IsDeleted = true;
                            board.BoardEnrollments.Remove(boardEnrollment);
                            var reviewerBoardEnrollment = new BoardEnrollment
                            {
                                Board      = board,
                                IsDeleted  = false,
                                isMarked   = false,
                                Percentage = BoardResource.LecturerInformations.Reviewer.ScorePercent,
                                BoardRole  = await context.BoardRoles.FirstOrDefaultAsync(c => c.BoardRoleName == "Reviewer"),
                                Lecturer   = await context.Lecturers.FindAsync(BoardResource.LecturerInformations.Reviewer.LecturerId)
                            };
                            context.BoardEnrollments.Add(reviewerBoardEnrollment);
                        }
                    }
                }
            }
        }