public async Task <IActionResult> Edit(IFormFile file, int projectGradeId, decimal maxGrade, decimal grade, string comment) { EditProjectGradeDto dto = new EditProjectGradeDto { File = file, Id = projectGradeId, LastEdited = DateTime.Now, MaxGrade = maxGrade, Grade = grade, Comment = comment }; ServiceResponse <ProjectGradeInfoDto> response = await _projectGradeService.EditProjectGrade(dto); if (response.Success) { return(Ok(response)); } return(NotFound(response)); }
public async Task <ServiceResponse <ProjectGradeInfoDto> > EditProjectGrade(EditProjectGradeDto editProjectGradeDto) { ServiceResponse <ProjectGradeInfoDto> response = new ServiceResponse <ProjectGradeInfoDto>(); User user = await _context.Users .FirstOrDefaultAsync(u => u.Id == GetUserId()); ProjectGrade projectGrade = await _context.ProjectGrades .Include(pg => pg.GradedProjectGroup) .FirstOrDefaultAsync(pg => pg.Id == editProjectGradeDto.Id); ProjectGroup projectGroup = await _context.ProjectGroups.Include(pg => pg.AffiliatedCourse).Include(c => c.AffiliatedSection) .FirstOrDefaultAsync(rg => rg.Id == projectGrade.GradedProjectGroupID); if (projectGrade == null) { response.Data = null; response.Message = "There is no project grade with this Id."; response.Success = false; return(response); } if (editProjectGradeDto.MaxGrade < editProjectGradeDto.Grade) { response.Data = null; response.Message = "Grade should be less than or equal to the max grade"; response.Success = false; return(response); } projectGrade.MaxGrade = projectGrade.MaxGrade; // ask projectGrade.Grade = projectGrade.Grade; projectGrade.Description = editProjectGradeDto.Comment; projectGrade.LastEdited = editProjectGradeDto.LastEdited; // ask if (editProjectGradeDto.File != null) { var target = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/{3}/{4}", "StaticFiles/ProjectGradeFiles", projectGrade.GradedProjectGroup.AffiliatedCourseId, projectGrade.GradedProjectGroup.AffiliatedSection, projectGrade.GradedProjectGroupID, user.Id)); Directory.CreateDirectory(target); if (editProjectGradeDto.File.Length > 0) { projectGrade.HasFile = true; string oldfile = Directory.GetFiles(target).FirstOrDefault(); string extension = Path.GetExtension(editProjectGradeDto.File.FileName); var filePath = Path.Combine(target, string.Format("{0}_Section{1}_Group{2}_{3}_GroupFeedback" , projectGroup.AffiliatedCourse.Name.Trim().Replace(" ", "_"), projectGroup.AffiliatedSection, projectGroup.Id, user.Name.Trim().Replace(" ", "_")) + extension); projectGrade.FilePath = filePath; if (File.Exists(oldfile)) { File.Delete(oldfile); } using (var stream = new FileStream(filePath, FileMode.Create)) { await editProjectGradeDto.File.CopyToAsync(stream); } } } ProjectGradeInfoDto projectGradeInfoDto = new ProjectGradeInfoDto { Id = projectGrade.Id, MaxGrade = editProjectGradeDto.MaxGrade, Grade = editProjectGradeDto.Grade, Comment = editProjectGradeDto.Comment, LastEdited = editProjectGradeDto.LastEdited, userInProjectGradeDto = new UserInProjectGradeDto { Id = user.Id, email = user.Email, name = user.Name }, GradingUserId = user.Id, projectGroupInProjectGradeDto = new ProjectGroupInProjectGradeDto { Id = user.Id, AffiliatedSectionId = projectGrade.GradedProjectGroup.AffiliatedSectionId, AffiliatedCourseId = projectGrade.GradedProjectGroup.AffiliatedCourseId, ConfirmationState = projectGrade.GradedProjectGroup.ConfirmationState, ConfirmedUserNumber = projectGrade.GradedProjectGroup.ConfirmedUserNumber, ProjectInformation = projectGrade.GradedProjectGroup.ProjectInformation, ConfirmedGroupMembers = projectGrade.GradedProjectGroup.ConfirmedGroupMembers }, FileEndpoint = string.Format("ProjectGrade/DownloadById/{0}", projectGrade.Id), GradedProjectGroupID = projectGrade.GradedProjectGroup.Id, HasFile = projectGrade.HasFile }; _context.ProjectGrades.Update(projectGrade); await _context.SaveChangesAsync(); response.Data = projectGradeInfoDto; response.Message = "You successfully edited project grade"; response.Success = true; return(response); }