public async Task <ActionResult <StudentGroupDto> > PutStudentGroup(long id, [FromBody] UpdateStudentGroupDto studentGroupDto) { var updatedStudentGroup = await _studentGroupService .UpdateStudentGroupAsync(id, studentGroupDto); return(updatedStudentGroup.ToActionResult()); }
// if we set StudentIds or MentorsIds to null, they won't update public async Task <Result <StudentGroupDto> > UpdateStudentGroupAsync(long groupId, UpdateStudentGroupDto updatedStudentGroupDto) { try { if (updatedStudentGroupDto == null) { return(Result <StudentGroupDto> .GetError(ErrorCode.ValidationError, "UpdateStudentGroupDto is null")); } var foundStudentGroup = await _unitOfWork.StudentGroupRepository.GetByIdAsync(groupId); if (foundStudentGroup == null) { return(Result <StudentGroupDto> .GetError(ErrorCode.NotFound, "Student Group not found")); } if (await _unitOfWork.StudentGroupRepository.IsGroupNameExistAsync(updatedStudentGroupDto.Name)) { return(Result <StudentGroupDto> .GetError(ErrorCode.UnprocessableEntity, "Group name already exists")); } foundStudentGroup.Name = updatedStudentGroupDto.Name ?? foundStudentGroup.Name; if (updatedStudentGroupDto.StartDate != null) { foundStudentGroup.StartDate = (DateTime?)(updatedStudentGroupDto.StartDate) ?? foundStudentGroup.StartDate; } if (updatedStudentGroupDto.FinishDate != null) { foundStudentGroup.FinishDate = (DateTime?)(updatedStudentGroupDto.FinishDate) ?? foundStudentGroup.FinishDate; } if (updatedStudentGroupDto.CourseId != 0) { foundStudentGroup.Course = await _unitOfWork.CourseRepository.GetByIdAsync(updatedStudentGroupDto.CourseId); } if (updatedStudentGroupDto.StudentIds != null) { var newStudentsOfStudentGroup = updatedStudentGroupDto.StudentIds.Select(x => new StudentOfStudentGroup { StudentGroupId = foundStudentGroup.Id, StudentId = x }).ToList(); _unitOfWork.StudentGroupRepository.UpdateManyToMany(foundStudentGroup.StudentsOfStudentGroups, newStudentsOfStudentGroup); } if (updatedStudentGroupDto.MentorIds != null) { var newMentorsOfStudentGroup = updatedStudentGroupDto.MentorIds.Select(x => new MentorOfStudentGroup { StudentGroupId = foundStudentGroup.Id, MentorId = x }).ToList(); _unitOfWork.MentorRepository.UpdateMentorGroups(foundStudentGroup.MentorsOfStudentGroups, newMentorsOfStudentGroup); } await _unitOfWork.CommitAsync(); return(Result <StudentGroupDto> .GetSuccess(_mapper.Map <StudentGroupDto>(foundStudentGroup))); } catch (Exception ex) { _logger.LogError(ex.Message); _unitOfWork.Rollback(); return(Result <StudentGroupDto> .GetError(ErrorCode.InternalServerError, "Internal error")); } }
public async Task <ActionResult <UpdateStudentGroupDto> > PutStudentGroup(long id, UpdateStudentGroupDto studentGroupDto) { var isStudentGroupNameExist = await _studentGroupService .IsGroupNameExistAsync(studentGroupDto.Name); if (isStudentGroupNameExist.Data) { return(Result <StudentGroupDto> .Error(ErrorCode.UnprocessableEntity, "Group name already exists").ToActionResult()); } var updatedStudentGroup = await _studentGroupService .UpdateStudentGroupAsync(id, studentGroupDto); return(updatedStudentGroup.ToActionResult()); }
public async Task <Result <UpdateStudentGroupDto> > UpdateStudentGroupAsync(long groupId, UpdateStudentGroupDto studentGroupDto) { try { if (studentGroupDto == null) { return(Result <UpdateStudentGroupDto> .Error(ErrorCode.ValidationError, "UpdateStudentGroupDto is null")); } var updatedEntity = _mapper.Map <StudentGroup>(studentGroupDto); var foundStudentGroup = await _unitOfWork.StudentGroupRepository.GetByIdAsync(groupId); if (foundStudentGroup == null) { return(null); } foundStudentGroup.Name = updatedEntity.Name ?? foundStudentGroup.Name; if (updatedEntity.StartDate != null) { foundStudentGroup.StartDate = (DateTime?)(updatedEntity.StartDate) ?? foundStudentGroup.StartDate; } if (updatedEntity.FinishDate != null) { foundStudentGroup.FinishDate = (DateTime?)(updatedEntity.FinishDate) ?? foundStudentGroup.FinishDate; } if (updatedEntity.CourseId != 0) { foundStudentGroup.Course = await _unitOfWork.CourseRepository.GetByIdAsync(updatedEntity.CourseId); } await _unitOfWork.CommitAsync(); return(Result <UpdateStudentGroupDto> .Success(_mapper.Map <UpdateStudentGroupDto>(updatedEntity))); } catch { _unitOfWork.Rollback(); return(Result <UpdateStudentGroupDto> .Error(ErrorCode.InternalServerError, "Internal error")); } }
public async Task UpdateStudentGroup() { //Arrange long notExistingGroupId = -10; var updateStudentGroupDto = new UpdateStudentGroupDto() { Name = "new_test_name", CourseId = 2, StartDate = DateTime.Now, FinishDate = DateTime.Now.AddMonths(3), StudentIds = new List <long>(), MentorIds = new List <long>() }; var existingNameStudentGroupDto = new UpdateStudentGroupDto() { Name = "Test_name", CourseId = 4, StartDate = DateTime.Now, FinishDate = DateTime.Now.AddMonths(3), StudentIds = new List <long>(), MentorIds = new List <long>() }; var existingStudentGroup = new StudentGroup() { Id = 10, Name = "Test_name", CourseId = 3, StartDate = DateTime.Now.AddMonths(3), FinishDate = DateTime.Now.AddMonths(6), StudentsOfStudentGroups = new List <StudentOfStudentGroup>(), MentorsOfStudentGroups = new List <MentorOfStudentGroup>() }; var existingCourse = new Course() { Id = 2, Name = "AAA" }; var studentGroupRepositoryMock = new Mock <IStudentGroupRepository>(); var studentsRepositoryMock = new Mock <IStudentRepository>(); var mentorsRepositoryMock = new Mock <IMentorRepository>(); var courseRepositoryMock = new Mock <ICourseRepository>(); studentGroupRepositoryMock.Setup(x => x.GetByIdAsync(existingStudentGroup.Id)) .ReturnsAsync(existingStudentGroup); studentGroupRepositoryMock.Setup(x => x.GetByIdAsync(notExistingGroupId)) .ReturnsAsync((StudentGroup)null); studentGroupRepositoryMock.Setup(x => x.IsGroupNameExistAsync(updateStudentGroupDto.Name)) .ReturnsAsync(false); studentGroupRepositoryMock.Setup(x => x.IsGroupNameExistAsync(existingNameStudentGroupDto.Name)) .ReturnsAsync(true); courseRepositoryMock.Setup(x => x.GetByIdAsync(updateStudentGroupDto.CourseId)) .ReturnsAsync(existingCourse); studentGroupRepositoryMock.Setup(x => x.UpdateManyToMany(It.IsAny <IList <StudentOfStudentGroup> >(), It.IsAny <IList <StudentOfStudentGroup> >())); mentorsRepositoryMock.Setup(x => x.UpdateMentorGroups(It.IsAny <IList <MentorOfStudentGroup> >(), It.IsAny <IList <MentorOfStudentGroup> >())); _unitOfWorkMock.Setup(x => x.StudentGroupRepository).Returns(studentGroupRepositoryMock.Object); _unitOfWorkMock.Setup(x => x.StudentRepository).Returns(studentsRepositoryMock.Object); _unitOfWorkMock.Setup(x => x.MentorRepository).Returns(mentorsRepositoryMock.Object); _unitOfWorkMock.Setup(x => x.CourseRepository).Returns(courseRepositoryMock.Object); var studentGroupService = new StudentGroupService( _unitOfWorkMock.Object, _mapper, _loggerMock.Object ); //Act var successResult = await studentGroupService.UpdateStudentGroupAsync(existingStudentGroup.Id, updateStudentGroupDto); var groupNameExistResult = await studentGroupService.UpdateStudentGroupAsync(existingStudentGroup.Id, existingNameStudentGroupDto); var groupNotExistResult = await studentGroupService.UpdateStudentGroupAsync(notExistingGroupId, updateStudentGroupDto); var nullGroupResult = await studentGroupService.UpdateStudentGroupAsync(existingStudentGroup.Id, null); //Assert Assert.NotNull(successResult.Data); Assert.Equal(successResult.Data.Name, successResult.Data.Name); Assert.Equal(ErrorCode.UnprocessableEntity, groupNameExistResult.Error.Code); Assert.Equal(ErrorCode.NotFound, groupNotExistResult.Error.Code); Assert.Equal(ErrorCode.ValidationError, nullGroupResult.Error.Code); }