public async Task <UpdateMentorDto> UpdateMentorAsync(long id, UpdateMentorDto UpdateDto, string accessToken)
 {
     return(await
            _apiUtil.PutAsync($"{_config.Value.Urls.Api.Https}/api/mentors/{id}", UpdateDto, accessToken));
 }
Esempio n. 2
0
        public async Task <ActionResult> PutMentor(long mentorId, [FromBody] UpdateMentorDto mentorModel)
        {
            var updatedMentor = await _mentorService.UpdateMentorAsync(mentorId, mentorModel);

            return(updatedMentor.ToActionResult());
        }
Esempio n. 3
0
        public async Task <Result <MentorDto> > UpdateMentorAsync(long mentorId, UpdateMentorDto mentorModel)
        {
            try
            {
                var foundMentor = await _unitOfWork.MentorRepository.GetByIdAsync(mentorId);

                if (foundMentor == null)
                {
                    return(Result <MentorDto> .Error(ErrorCode.NotFound,
                                                     "Mentor not found"));
                }

                var isEmailChangableTo = await _accountService
                                         .IsEmailChangableToAsync((long)foundMentor.AccountId, mentorModel.Email);

                if (!isEmailChangableTo)
                {
                    return(Result <MentorDto> .Error(ErrorCode.ValidationError,
                                                     "Email is already taken!"));
                }

                foundMentor.Account.Email     = mentorModel.Email ?? foundMentor.Account.Email;
                foundMentor.Account.FirstName = mentorModel.FirstName ?? foundMentor.Account.FirstName;
                foundMentor.Account.LastName  = mentorModel.LastName ?? foundMentor.Account.LastName;

                if (mentorModel.CourseIds != null)
                {
                    var currentMentorCourses = foundMentor.MentorsOfCourses;
                    var newMentorCourses     = new List <MentorOfCourse>();

                    foreach (var newCourseId in mentorModel.CourseIds)
                    {
                        newMentorCourses.Add(new MentorOfCourse
                        {
                            CourseId = newCourseId,
                            MentorId = foundMentor.Id
                        });
                    }

                    _unitOfWork.MentorRepository.UpdateMentorCourses(currentMentorCourses, newMentorCourses);
                }

                if (mentorModel.StudentGroupIds != null)
                {
                    var currentMentorGroups = foundMentor.MentorsOfStudentGroups;
                    var newMentorGroups     = new List <MentorOfStudentGroup>();

                    foreach (var newGroupId in mentorModel.StudentGroupIds)
                    {
                        newMentorGroups.Add(new MentorOfStudentGroup
                        {
                            StudentGroupId = newGroupId,
                            MentorId       = foundMentor.Id
                        });
                    }

                    _unitOfWork.MentorRepository.UpdateMentorGroups(currentMentorGroups, newMentorGroups);
                }

                await _unitOfWork.CommitAsync();

                return(Result <MentorDto> .Success(_mapper.Map <MentorDto>(foundMentor)));
            }
            catch
            {
                _unitOfWork.Rollback();

                return(Result <MentorDto> .Error(ErrorCode.InternalServerError,
                                                 "Cannot update mentor."));
            }
        }
        public async Task <IActionResult> UpdateMentor(long id, UpdateMentorDto data)
        {
            var updatedStudent = await _mentorService.UpdateMentorAsync(id, data, _protector.Unprotect(Request.Cookies["accessToken"]));

            return(RedirectToAction("AllMentors", "Mentors"));
        }
Esempio n. 5
0
        public async Task UpdateMentorAsync()
        {
            //Arrange
            string usedEmail = "*****@*****.**";

            var nonExistingUpdateMentorDto = new UpdateMentorDto();

            var successUpdateMentorDto = new UpdateMentorDto()
            {
                Email     = "*****@*****.**",
                FirstName = "updateTest",
                LastName  = "updateTest"
            };

            var successMentor = new Mentor()
            {
                Id        = 1,
                AccountId = 1,
                Account   = new Account()
                {
                    Id    = 1,
                    Email = "*****@*****.**"
                }
            };


            var alreadyExistingEmailUpdateMentorDto = new UpdateMentorDto()
            {
                Email     = usedEmail,
                FirstName = "updateTest",
                LastName  = "updateTest"
            };

            var alreadyExistingEmailMentor = new Mentor()
            {
                Id        = 2,
                AccountId = 2,
                Account   = new Account()
                {
                    Id    = 2,
                    Email = usedEmail
                }
            };

            _accountServiceMock.Setup(x => x.IsEmailChangableToAsync(
                                          (long)successMentor.AccountId, successUpdateMentorDto.Email))
            .ReturnsAsync(true);

            _accountServiceMock.Setup(x => x.IsEmailChangableToAsync(
                                          (long)alreadyExistingEmailMentor.AccountId,
                                          usedEmail))
            .ReturnsAsync(false);

            var mentorRepositoryMock = new Mock <IMentorRepository>();

            mentorRepositoryMock.Setup(x => x.GetByIdAsync(1))
            .ReturnsAsync(successMentor);
            mentorRepositoryMock.Setup(x => x.GetByIdAsync(2))
            .ReturnsAsync(alreadyExistingEmailMentor);

            _unitOfWorkMock.Setup(x => x.MentorRepository).Returns(mentorRepositoryMock.Object);

            var mentorService = new MentorService(
                _accountServiceMock.Object,
                _unitOfWorkMock.Object,
                _mapper,
                _notificationServiceMock.Object);

            //Act
            var nonExistingIdResult = await mentorService
                                      .UpdateMentorAsync(0, nonExistingUpdateMentorDto);

            var successResult = await mentorService
                                .UpdateMentorAsync(1, successUpdateMentorDto);

            var alreadyExistingEmailResult = await mentorService
                                             .UpdateMentorAsync(2, alreadyExistingEmailUpdateMentorDto);

            //Assert
            Assert.Equal(ErrorCode.NotFound, nonExistingIdResult.Error.Code);

            Assert.NotNull(successResult.Data);
            Assert.Equal(successUpdateMentorDto.Email, successResult.Data.Email);
            Assert.Equal(successUpdateMentorDto.FirstName, successResult.Data.FirstName);
            Assert.Equal(successUpdateMentorDto.LastName, successResult.Data.LastName);

            Assert.Equal(ErrorCode.ValidationError, alreadyExistingEmailResult.Error.Code);
        }