Beispiel #1
0
        public async Task <IActionResult> Add(int projectGroupId, int revieweeId, int grade, string comment)
        {
            AddPeerGradeDto dto = new AddPeerGradeDto {
                ProjectGroupId = projectGroupId,
                RevieweeId     = revieweeId,
                LastEdited     = DateTime.Now,
                Grade          = grade,
                Comment        = comment
            };
            ServiceResponse <AddPeerGradeDto> response = await _peerGradeService.AddPeerGrade(dto);

            if (response.Success)
            {
                return(Ok(response));
            }
            return(NotFound(response));
        }
Beispiel #2
0
        public async Task <ServiceResponse <AddPeerGradeDto> > AddPeerGrade(AddPeerGradeDto addPeerGradeDto)
        {
            ServiceResponse <AddPeerGradeDto> response = new ServiceResponse <AddPeerGradeDto>();
            User user = await _context.Users.Include(u => u.ProjectGroups)
                        .ThenInclude(g => g.ProjectGroup)
                        .FirstOrDefaultAsync(u => u.Id == GetUserId());

            ProjectGroup projectGroup = await _context.ProjectGroups
                                        .Include(g => g.AffiliatedCourse)
                                        .Include(g => g.GroupMembers)
                                        .FirstOrDefaultAsync(rg => rg.Id == addPeerGradeDto.ProjectGroupId);

            if (projectGroup == null)
            {
                response.Data    = null;
                response.Message = "There is no project group with this id";
                response.Success = false;
                return(response);
            }

            if (!IsUserInGroup(projectGroup, GetUserId()))
            {
                response.Data    = null;
                response.Message = "You are not in this group";
                response.Success = false;
                return(response);
            }

            if (!IsUserInGroup(projectGroup, addPeerGradeDto.RevieweeId))
            {
                response.Data    = null;
                response.Message = "You are not in the same group with the reviewee ";
                response.Success = false;
                return(response);
            }

            PeerGradeAssignment pga = await _context.PeerGradeAssignments.Include(pg => pg.PeerGrades)
                                      .FirstOrDefaultAsync(pga => pga.CourseId == projectGroup.AffiliatedCourseId);

            if (pga == null)
            {
                response.Data    = null;
                response.Message = "Course doesn't have a peer grade assignment";
                response.Success = false;
                return(response);
            }

            if (pga.DueDate < addPeerGradeDto.LastEdited)
            {
                response.Data    = null;
                response.Message = "Due date has passed for the peer grade assignment";
                response.Success = false;
                return(response);
            }

            PeerGrade peerGrade = await _context.PeerGrades
                                  .FirstOrDefaultAsync(pg => pg.ReviewerId == GetUserId() && pg.RevieweeId == addPeerGradeDto.RevieweeId && pg.ProjectGroupId == addPeerGradeDto.ProjectGroupId);

            if (peerGrade != null)
            {
                response.Data    = null;
                response.Message = "You have already peer graded this group member";
                response.Success = false;
                return(response);
            }


            if (pga.MaxGrade < addPeerGradeDto.Grade)
            {
                response.Data    = null;
                response.Message = "Grade should be less than or equal to the max grade";
                response.Success = false;
                return(response);
            }


            PeerGrade createdPeerGrade = new PeerGrade
            {
                MaxGrade       = pga.MaxGrade,
                Grade          = addPeerGradeDto.Grade,
                Comment        = addPeerGradeDto.Comment,
                LastEdited     = addPeerGradeDto.LastEdited,
                ProjectGroupId = addPeerGradeDto.ProjectGroupId,
                ReviewerId     = GetUserId(),
                RevieweeId     = addPeerGradeDto.RevieweeId
            };

            pga.PeerGrades.Add(createdPeerGrade);

            _context.ProjectGroups.Update(projectGroup);
            _context.PeerGrades.Add(createdPeerGrade);
            await _context.SaveChangesAsync();

            response.Data    = addPeerGradeDto;
            response.Message = "You successfully entered peer grade";
            response.Success = true;

            return(response);
        }