public async Task <IActionResult> AddAssignment(AddAssignmentDto newAssignment) { ServiceResponse <List <GetAssignmentDto> > response = await _assignmentService.AddAssignment(newAssignment); if (response.Success == false) { return(BadRequest(response)); } return(Ok(response)); }
public async Task <ActionResult <AssignmentDto> > AddAssignment(AddAssignmentDto assignmentDto) { if (assignmentDto == null) { return(BadRequest(InvalidAssignmentError)); } if (!PermissionDataFiltersExtensions.HasAccessToLocation(User, Db, assignmentDto.LocationId)) { return(Forbid()); } var assignment = assignmentDto.Adapt <Assignment>(); var createdAssignment = await AssignmentService.CreateAssignment(assignment); return(Ok(createdAssignment.Adapt <AssignmentDto>())); }
public async Task <ServiceResponse <GetAssignmentDto> > SubmitAssignment(AddAssignmentDto assignmentDto) { ServiceResponse <GetAssignmentDto> response = new ServiceResponse <GetAssignmentDto>(); if (assignmentDto == null) { response.Data = null; response.Message = "There is no data"; response.Success = false; return(response); } Course course = await _context.Courses.Include(c => c.Instructors).Include(c => c.Sections).ThenInclude(s => s.ProjectGroups) .FirstOrDefaultAsync(c => c.Id == assignmentDto.CourseId); if (course == null) { response.Data = null; response.Message = "There is no course under this id"; response.Success = false; return(response); } if (course.Instructors.FirstOrDefault(u => u.UserId == GetUserId()) == null) { response.Data = null; response.Message = "You are not allowed to post assignments for this course"; response.Success = false; return(response); } if (assignmentDto.Title == null) { response.Data = null; response.Message = "You should add a title to your submission"; response.Success = false; return(response); } if (assignmentDto.AssignmenDescription == null) { response.Data = null; response.Message = "You should add a description to your submission"; response.Success = false; return(response); } if ((assignmentDto.DueDate - DateTime.Now).TotalDays < 3) { response.Data = null; response.Message = "A deadline cannot be closer to publishment date less than 3 days"; response.Success = false; return(response); } Assignment assignment = new Assignment { CreatedAt = DateTime.Now, DueDate = assignmentDto.DueDate, AfilliatedCourseId = assignmentDto.CourseId, AcceptedTypes = assignmentDto.AcceptedTypes, AssignmentDescription = assignmentDto.AssignmenDescription, CanBeGradedByStudents = assignmentDto.CanBeGradedByStudents, IsItGraded = assignmentDto.IsItGraded, VisibilityOfSubmission = assignmentDto.VisibiltyOfSubmission, MaxFileSizeInBytes = assignmentDto.MaxFileSizeInBytes, HasFile = false, FilePath = "", Title = assignmentDto.Title }; await _context.Assignments.AddAsync(assignment); await _context.SaveChangesAsync(); foreach (Section s in course.Sections) { foreach (ProjectGroup pg in s.ProjectGroups) { await _submissionService.AddSubmission(new AddSubmissionDto { AssignmentId = assignment.Id, ProjectGroupId = pg.Id }); } } response.Data = _mapper.Map <GetAssignmentDto>(assignment); response.Data.FileEndpoint = string.Format("Assignment/File/{0}", assignment.Id); return(response); }
public async Task <ServiceResponse <List <GetAssignmentDto> > > AddAssignment(AddAssignmentDto newAssignment) { ServiceResponse <List <GetAssignmentDto> > serviceResponse = new ServiceResponse <List <GetAssignmentDto> >(); List <string> allowedStatuses = GetAllowedStatuses(); List <string> allowedPriorities = GetAllowedPriorities(); if (!allowedStatuses.Contains(newAssignment.Status)) { serviceResponse.Success = false; serviceResponse.Message = "Entered Status is not allowed."; } else if (!allowedPriorities.Contains(newAssignment.Priority)) { serviceResponse.Success = false; serviceResponse.Message = "Entered Priority is not allowed."; } else if (DateTime.Compare(newAssignment.StartDate, newAssignment.EndDate) > 0) { serviceResponse.Success = false; serviceResponse.Message = "Start date is later than End date."; } else { Assignment assignment = _mapper.Map <Assignment>(newAssignment); assignment.CreateDate = DateTime.UtcNow; assignment.EditDate = DateTime.UtcNow; assignment.UserIdEdit = GetUserId(); assignment.User = await _context.Users.FirstOrDefaultAsync(u => u.Id == GetUserId()); await _context.Assignments.AddAsync(assignment); await _context.SaveChangesAsync(); serviceResponse.Data = (_context.Assignments.Where(a => a.User.Id == GetUserId()).Select(a => _mapper.Map <GetAssignmentDto>(a))).ToList(); } return(serviceResponse); }