public async Task <ServiceResponse <string> > DeletePeerGrade(DeletePeerGradeDto deletePeerGradeDto) { ServiceResponse <string> response = new ServiceResponse <string>(); User user = await _context.Users .FirstOrDefaultAsync(u => u.Id == GetUserId()); PeerGrade peerGrade = await _context.PeerGrades .FirstOrDefaultAsync(pg => pg.Id == deletePeerGradeDto.Id); if (peerGrade == null) { response.Data = "Not allowed"; response.Message = "There is no peer grade with this Id"; response.Success = false; return(response); } if (user == null || (user != null && peerGrade.ReviewerId != GetUserId())) { response.Data = "Not allowed"; response.Message = "You are not authorized to delete this peer grade"; response.Success = false; return(response); } ProjectGroup projectGroup = await _context.ProjectGroups .Include(g => g.GroupMembers) .FirstOrDefaultAsync(rg => rg.Id == peerGrade.ProjectGroupId); PeerGradeAssignment pga = await _context.PeerGradeAssignments.Include(pga => pga.PeerGrades) .FirstOrDefaultAsync(pga => pga.CourseId == projectGroup.AffiliatedCourseId); if (pga.DueDate < deletePeerGradeDto.LastEdited) { response.Data = null; response.Message = "Due date has passed for the peer grade assignment"; response.Success = false; return(response); } _context.PeerGrades.Remove(peerGrade); await _context.SaveChangesAsync(); if (pga != null) { _context.PeerGradeAssignments.Update(pga); await _context.SaveChangesAsync(); } response.Data = "Successful"; response.Message = "Peer grade is successfully cancelled"; response.Success = true; return(response); }
public async Task <IActionResult> Delete(int peerGradeId) { DeletePeerGradeDto dto = new DeletePeerGradeDto { Id = peerGradeId, LastEdited = DateTime.Now }; ServiceResponse <string> response = await _peerGradeService.DeletePeerGrade(dto); if (response.Success) { return(Ok(response)); } return(NotFound(response)); }