private static Result <PersonalProfileDto> ValidateSkills(PersonalProfileDto profileDto)
 {
     return(profileDto.Skills
            .Select(a => ValidateSkill(a))
            .Traverse()
            .Map(_ => profileDto));
 }
Esempio n. 2
0
            public TestState()
            {
                DefaultPersonalProfile = BuildPersonalProfile();

                var entities = DefaultPersonalProfile.MapToEntities();

                Mock <IPostPersonalProfileDependencies> dependencies = new Mock <IPostPersonalProfileDependencies>();

                dependencies.Setup(a => a.SavePersonalProfile(It.IsAny <PersonalProfileEntity>()))
                .Returns(entities.Item1.Success().Async());

                dependencies.Setup(a => a.SaveInterests(It.IsAny <List <InterestEntity> >()))
                .Returns(entities.Item3.Success().Async());

                dependencies.Setup(a => a.SaveSkills(It.IsAny <List <SkillEntity> >()))
                .Returns(entities.Item2.Success().Async());

                dependencies.Setup(a => a.CommitTransaction())
                .Returns(Task.CompletedTask);

                dependencies.Setup(a => a.GetUser(Username))
                .Returns(Task.FromResult(UserIdEntity.Create(Username, UserId)));

                _dependencies = dependencies;

                Subject = new PostPersonalProfile(_dependencies.Object);
            }
        private static Result <PersonalProfileDto> ValidateProfile(this PersonalProfileDto profileDto)
        {
            List <Error> errors = new List <Error>();

            if (profileDto.Description.Length > 5000)
            {
                errors.Add(Error.Create("El campo descripción no puede ser mayor de 5000"));
            }

            if (profileDto.Email.Length > 50)
            {
                errors.Add(Error.Create("El campo email no puede ser mayor de 50"));
            }

            if (profileDto.FirstName.Length > 50)
            {
                errors.Add(Error.Create("El campo nombre propio no puede ser mayor de 50"));
            }

            if (profileDto.GitHub.Length > 50)
            {
                errors.Add(Error.Create("El campo Github no puede ser mayor de 50"));
            }

            if (profileDto.LastName.Length > 50)
            {
                errors.Add(Error.Create("El campo Apellido no puede ser mayor de 50"));
            }

            if (profileDto.Phone.Length > 50)
            {
                errors.Add(Error.Create("El campo Teléfono no puede ser mayor de 50"));
            }

            if (profileDto.UserName.Length > 50)
            {
                errors.Add(Error.Create("El campo Usuario no puede ser mayor de 50"));
            }

            if (string.IsNullOrWhiteSpace(profileDto.UserName))
            {
                errors.Add(Error.Create("El campo Usuario debe contener algún valor"));
            }

            if (profileDto.UserName.Length < 5)
            {
                errors.Add(Error.Create("El campo usuario debe ser de almenos de 5 caracteres"));
            }

            if (profileDto.Website.Length > 50)
            {
                errors.Add(Error.Create("El campo website debe ser almenos de 5 caracteres"));
            }


            return(errors.Any() ?
                   Result.Failure <PersonalProfileDto>(errors.ToImmutableArray())
                : profileDto);
        }
 private Task <Result <PersonalProfileDto> > ValidateUsuerId(PersonalProfileDto personalProfile)
 {
     if (personalProfile.UserId == null)
     {
         return(Result.Failure <PersonalProfileDto>("UsuarioId no encontrado").Async());
     }
     return(personalProfile.Success().Async());
 }
Esempio n. 5
0
        public static PersonalProfileEntity Map(this PersonalProfileDto profileDto, int userId, IDataProtector protector)
        {
            string encriptedPhone = protector.Protect(profileDto.Phone);
            string encriptedEmail = protector.Protect(profileDto.Email);

            return(PersonalProfileEntity.Create(userId, profileDto.Id, profileDto.FirstName, profileDto.LastName, profileDto.Description,
                                                encriptedPhone, encriptedEmail, profileDto.Website, profileDto.GitHub));
        }
 public async Task <Result <UserIdEntity> > Create(PersonalProfileDto personalProfile)
 {
     return(await CreateNewUser(personalProfile)
            .Bind(x => SavePersonalProfile(x, personalProfile))
            .Bind(x => SaveSkills(x, personalProfile))
            .Bind(x => SaveInterests(x, personalProfile))
            .Bind(CommitTransaction));
 }
 public async Task <Result <UserIdEntity> > Create(PersonalProfileDto personalProfile)
 {
     return(await CreateNewUser(personalProfile)
            .Bind(x => SavePersonalProfile(x, personalProfile))
            .Bind(x => SaveSkills(x, personalProfile))
            .Bind(x => SaveInterests(x, personalProfile))
            .Bind(CommitTransaction)
            .Then(async _ => await SendConfirmationEmail(personalProfile)));//TODO: create then async in the library
 }
 public void Update(PersonalProfileDto profile, int userId)
 {
     using (var uow = UnitOfWorkProvider.Create())
     {
         var entity = PersonalProfileRepository.GetByUserId(userId);
         Mapper.Map(profile, entity);
         uow.Commit();
     }
 }
        //TODO: not sure if this bit should be here
        private async Task <Result <PersonalProfileDto> > ValidateUserName(PersonalProfileDto personalProfile)
        {
            UserIdEntity?user = await _dependencies.GetUser(personalProfile.UserName);

            if (user != null && user.UserId != personalProfile.UserId)
            {
                return(Result.Failure <PersonalProfileDto>($"El nombre de usuario {personalProfile.UserName} ya está en uso"));
            }

            return(personalProfile);
        }
        public async Task TestInsertPerfilPersonal_Then_ModifyIt()
        {
            IServiceCollection services = BuildDependencies();

            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                string username = Guid.NewGuid().ToString();

                PersonalProfileDto defaultPRofile = BuildPersonalProfile(username);
                var departmentAppService          = serviceProvider.GetRequiredService <PerfilPersonalController>();
                await departmentAppService.Post(defaultPRofile);

                ResultDto <PersonalProfileDto> resultUserStep1 = await departmentAppService.Get(username);

                Assert.Empty(resultUserStep1.Errors);
                PersonalProfileDto userStep1 = resultUserStep1.Value;
                Assert.Empty(userStep1.Skills);
                Assert.Equal(defaultPRofile.FirstName, userStep1.FirstName);
                Assert.Equal(defaultPRofile.Website, userStep1.Website);
                Assert.Equal(defaultPRofile.LastName, userStep1.LastName);

                SkillDto skill = new SkillDto()
                {
                    Id          = null,
                    Name        = "nombre1",
                    Punctuation = 10m
                };
                userStep1.Skills.Add(skill);

                InterestDto interest = new InterestDto()
                {
                    Id       = null,
                    Interest = "interes pero debe contener 15 caracteres"
                };
                userStep1.Interests.Add(interest);
                var _ = await departmentAppService.Put(userStep1);

                //TODO: change back to get
                ResultDto <PersonalProfileDto> resultUserStep2 = await departmentAppService.Get(username);

                Assert.Empty(resultUserStep2.Errors);
                PersonalProfileDto userStep2 = resultUserStep1.Value;
                Assert.Single(userStep2.Skills);
                Assert.Equal(skill.Name, userStep2.Skills.First().Name);
                Assert.Single(userStep2.Interests);
                Assert.Equal(interest.Interest, userStep2.Interests.First().Interest);
                Assert.Equal(defaultPRofile.FirstName, userStep2.FirstName);
                Assert.Equal(defaultPRofile.Website, userStep2.Website);
                Assert.Equal(defaultPRofile.LastName, userStep2.LastName);
                Assert.Equal(defaultPRofile.Email, userStep2.Email);
            }
        }
 public async Task <Result <PersonalProfileDto> > Create(PersonalProfileDto personalProfile)
 {
     return(await ValidateUsuerId(personalProfile)
            .Bind(ValidateProfile)
            .Bind(ValidateUserName)
            .Bind(MapToEntities)
            .Then(x => _dependencies.SavePersonalProfile(x.Item1))
            .Then(x => _dependencies.SaveSkills(x.Item2))
            .Then(x => _dependencies.SaveInterests(x.Item3))
            .Ignore()
            .Bind(CommitTransaction)
            .MapAsync(_ => Task.FromResult(personalProfile)));
 }
 public async Task <Result <PersonalProfileDto> > Create(PersonalProfileDto personalProfile)
 {
     return(await ValidateUsuerId(personalProfile)
            .Bind(ValidateProfile)
            .Bind(ValidateUserName)
            .Bind(MapToEntities)
            .Bind(SavePersonalProfile)
            .Bind(SaveSkills)
            .Bind(SaveInterests)
            .Ignore()
            .Bind(CommitTransaction)
            .MapAsync(_ => personalProfile));
 }
        private static Result <PersonalProfileDto> ValidateInterests(PersonalProfileDto profileDto)
        {
            return(profileDto.Interests
                   .Select(a => ValidateInterest(a.Interest))
                   .Traverse()
                   .Map(_ => profileDto));

            Result <Unit> ValidateInterest(string interest)
            {
                if (interest.Length > 250 || interest.Length < 15)
                {
                    return(Result.Failure(Error.Create("Los intereses deben contener entre 15 y 250 caracteres")));
                }
                return(Result.Unit);
            }
        }
Esempio n. 14
0
        public static PostPersonalProfileWrapper MapToWraperEntities(this PersonalProfileDto profileDto, IDataProtector protector)
        {
            if (profileDto.UserId == null)
            {
                throw new Exception("Your are trying to build an entity with a null user, that cannot be done");
            }

            string encriptedPhone = protector.Protect(profileDto.Phone);
            string encriptedEmail = protector.Protect(profileDto.Email);

            PersonalProfileEntity personalProfile = PersonalProfileEntity.Create((int)profileDto.UserId, profileDto.Id, profileDto.FirstName, profileDto.LastName, profileDto.Description,
                                                                                 encriptedPhone, encriptedEmail, profileDto.Website, profileDto.GitHub);

            List <SkillEntity> skills = profileDto.Skills.Select(a => SkillEntity.Create(profileDto.UserId, a.Id, a.Name, a.Punctuation)).ToList();

            List <InterestEntity> interests = profileDto.Interests.Select(a => InterestEntity.Create(a.Id, profileDto.UserId, a.Interest)).ToList();

            return(new PostPersonalProfileWrapper(personalProfile, skills, interests));
        }
        public void Insert(PersonalProfileDto profile, string userName)
        {
            using (var uow = UnitOfWorkProvider.Create())
            {
                var user = UserRepository.GetByUserName(userName);

                if (user == null)
                {
                    throw new UIException("Neexistuje meno užívateľa, profil sa nedá uložiť");
                }

                var entity = PersonalProfileRepository.InitializeNew();
                Mapper.Map(profile, entity);

                entity.User = user;
                PersonalProfileRepository.Insert(entity);

                uow.Commit();
            }
        }
Esempio n. 16
0
            public TestState()
            {
                DefaultPersonalProfile = BuildPersonalProfile();
                IDataProtectionProvider provider  = new EphemeralDataProtectionProvider();
                IDataProtector          protector = provider.CreateProtector("test");

                var entities = DefaultPersonalProfile.MapToWraperEntities(protector);

                Mock <IPutPersonalProfileDependencies> dependencies = new Mock <IPutPersonalProfileDependencies>();

                //TODO: modify the scenario to test as well updates.
                dependencies.Setup(a => a.InsertPersonalProfile(It.IsAny <PersonalProfileEntity>()))
                .Returns(entities.personalProfile.Success().Async());

                dependencies.Setup(a => a.UpdatePersonalProfile(It.IsAny <PersonalProfileEntity>()))
                .Returns(entities.personalProfile.Success().Async());

                dependencies.Setup(a => a.InsertInterests(It.IsAny <List <InterestEntity> >()))
                .Returns(entities.interestEntities.Success().Async());

                dependencies.Setup(a => a.UpdateInterests(It.IsAny <List <InterestEntity> >()))
                .Returns(entities.interestEntities.Success().Async());

                dependencies.Setup(a => a.InsertSkills(It.IsAny <List <SkillEntity> >()))
                .Returns(entities.skillEntities.Success().Async());

                dependencies.Setup(a => a.UpdateSkills(It.IsAny <List <SkillEntity> >()))
                .Returns(entities.skillEntities.Success().Async());

                dependencies.Setup(a => a.CommitTransaction())
                .Returns(Task.CompletedTask);

                dependencies.Setup(a => a.GetUser(Username))
                .Returns(Task.FromResult(UserIdEntity.Create(Username, UserId)));

                _dependencies = dependencies;

                Subject = new PutPersonalProfile(_dependencies.Object, provider);
            }
    public async Task WhenInsertInformation_returnCorrect()
    {
        IWebHostBuilder webHostBuilder =
            new WebHostBuilder()
            .ConfigureTestServices(serviceCollection =>
        {
            serviceCollection
            .AddScoped <IPostPersonalProfileDependencies, StubIPostPersonalProfileDependencies>();
        })
            .UseStartup <Startup>();

        PersonalProfileDto defaultPersonalProfileDto = GetPersonalProfile();
        string             serializedProfile         = JsonSerializer.Serialize(defaultPersonalProfileDto);

        using (TestServer server = new TestServer(webHostBuilder))
            using (HttpClient client = server.CreateClient())
            {
                var result = await client.PostAsync("/api/PerfilPersonal/returnonlyid",
                                                    new StringContent(serializedProfile, Encoding.UTF8, "application/json"));

                result.EnsureSuccessStatusCode();
            }
    }
 private async Task <Result <UserIdEntity> > CreateNewUser(PersonalProfileDto personalProfile)
 {
     return(await _dependencies.InsertUserId(personalProfile.UserName));
 }
        public static (PersonalProfileEntity, List <SkillEntity>, List <InterestEntity>) MapToEntities(this PersonalProfileDto profileDto)
        {
            PersonalProfileEntity personalProfile = PersonalProfileEntity.Create((int)profileDto.UserId, profileDto.Id, profileDto.FirstName, profileDto.LastName, profileDto.Description,
                                                                                 profileDto.Phone, profileDto.Email, profileDto.Website, profileDto.GitHub);

            List <SkillEntity> skills = profileDto.Skills.Select(a => SkillEntity.Create(profileDto.UserId, a.Id, a.Name, a.Punctuation)).ToList();

            List <InterestEntity> interests = profileDto.Interests.Select(a => InterestEntity.Create(a.Id, profileDto.UserId, a.Interest)).ToList();

            return(personalProfile, skills, interests);
        }
 public async Task <Result <PersonalProfileDto> > Post(PersonalProfileDto profileDto)
 {
     return(await _postPersonalProfile.Create(profileDto));
 }
Esempio n. 21
0
 private Task <Result <bool> > SendConfirmationEmail(PersonalProfileDto personalProfile)
 => _dependencies.SendEmail($"{personalProfile.UserName}@mail.com", "personal profile created",
                            "congratulations");
Esempio n. 22
0
 public async Task <Result <int?> > PostId(PersonalProfileDto profileDto)
 {
     return(await _postPersonalProfile.Create(profileDto)
            .MapAsync(x => x.UserId));
 }
 public async Task <Result <PersonalProfileDto> > Put(PersonalProfileDto profileDto)
 {
     return(await _putPersonalProfile.Create(profileDto)
            .Bind(x => GetProfile(x.UserName)));
 }
 private Task <Result <PostPersonalProfileWrapper> > MapToEntities(PersonalProfileDto personalProfile)
 {
     return(personalProfile.MapToWraperEntities(_protector).Success().Async());
 }
 private Task <Result <(PersonalProfileEntity, List <SkillEntity>, List <InterestEntity>)> > MapToEntities(PersonalProfileDto personalProfile)
 => personalProfile.MapToEntities().Success().Async();
 private Task <Result <PersonalProfileDto> > ValidateProfile(PersonalProfileDto personalProfile)
 => personalProfile.ValidateDto().Async();
 private async Task <Result <UserIdEntity> > SavePersonalProfile(UserIdEntity user, PersonalProfileDto personalProfile)
 {
     return(await _dependencies.InsertPersonalProfile(personalProfile.Map(Convert.ToInt32(user.UserId), _protector))
            .MapAsync(_ => user));
 }
 private async Task <Result <UserIdEntity> > SaveInterests(UserIdEntity user, PersonalProfileDto personalProfile)
 {
     return(await _dependencies.InsertInterests(personalProfile.Interests.Map(Convert.ToInt32(user.UserId)))
            .MapAsync(_ => user));
 }
 public static Result <PersonalProfileDto> ValidateDto(this PersonalProfileDto profileDto)
 {
     return(ValidateProfile(profileDto)
            .Bind(ValidateSkills)
            .Bind(ValidateInterests));
 }