예제 #1
0
        public async Task <IActionResult> UpdateProfile(UpdateCandidateProfileViewModel input)
        {
            var candidateId = this.candidatesService.GetCandidateIdByUsername(this.User.Identity.Name);

            if (candidateId == null)
            {
                return(this.RedirectToAction(nameof(this.CreateProfile)));
            }

            if (!this.ModelState.IsValid)
            {
                input.ImageExtensions = this.allowedExtensions;
                input.LanguagesList   = this.languages;
                input.SkillsList      = this.skills;
                return(this.View(input));
            }

            if (input.ProfilePicture != null && !this.allowedExtensions.Any(ae => input.ProfilePicture.FileName.EndsWith(ae)))
            {
                this.ModelState.AddModelError(string.Empty, GlobalConstants.FileExtensionNotSupported);
                input.ImageExtensions = this.allowedExtensions;
                return(this.View(input));
            }

            var updateResult = await this.candidatesService.UpdateProfileAsync(candidateId, input);

            if (updateResult == null)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            this.TempData["Success"] = GlobalConstants.ProfileSuccessfullyUpdated;
            return(this.RedirectToAction(nameof(this.MyProfile)));
        }
예제 #2
0
        public IActionResult Put(int Id, [FromBody] UpdateCandidateProfileViewModel vm)
        {
            return(ApiAction(() =>
            {
                var contract = _mapper.Map <UpdateCandidateProfileContract>(vm);
                contract.Id = Id;
                _CandidateProfileService.Update(contract);

                return Accepted(new { Id });
            }));
        }
        public async Task UpdateProfileShouldUpdateProfileWhenValidIdIsPassed()
        {
            AutoMapperInitializer.InitializeMapper();
            var context = InMemoryDbContextInitializer.InitializeContext();
            await context.Candidates.AddRangeAsync(this.SeedTestData());

            await context.SaveChangesAsync();

            var candidatesRepository = new EfDeletableEntityRepository <Candidate>(context);
            var languagesRepository  = new EfDeletableEntityRepository <CandidateLanguage>(context);
            var skillsRepository     = new EfDeletableEntityRepository <CandidateSkill>(context);
            var cloudinary           = new Cloudinary(new Account(CloudinaryConfig.CloudName, CloudinaryConfig.ApiKey, CloudinaryConfig.ApiSecret));
            var candidatesService    = this.GetMockedService(candidatesRepository, languagesRepository, skillsRepository, cloudinary);

            var profilePicture = this.PrepareImage();

            var model = new UpdateCandidateProfileViewModel
            {
                AboutMe        = "some info",
                ContactAddress = "address",
                FirstName      = "Georgi",
                LastName       = "Georgiev",
                LanguagesIds   = new List <int> {
                    1, 2, 3
                },
                SkillsIds = new List <int> {
                    4, 5, 6
                },
                ProfilePicture = profilePicture,
            };

            var candidateId = await candidatesService.UpdateProfileAsync("First", model);

            Assert.NotNull(candidateId);

            var record = await context.Candidates.Include(c => c.Languages).Include(c => c.Skills).FirstOrDefaultAsync(c => c.Id == "First");

            Assert.Equal(model.FirstName, record.FirstName);
            Assert.Equal(model.LastName, record.LastName);
            Assert.Equal(model.AboutMe, record.AboutMe);
            Assert.Null(record.PhoneNumber);
            Assert.NotNull(record.ProfilePictureUrl);
            Assert.Equal(3, record.Languages.Count());
            Assert.Equal(3, record.Skills.Count());
        }
        public async Task UpdateProfileCorrectlyRemovesOldLanguagesAndSkills()
        {
            AutoMapperInitializer.InitializeMapper();
            var context = InMemoryDbContextInitializer.InitializeContext();
            var candidatesRepository = new EfDeletableEntityRepository <Candidate>(context);
            var languagesRepository  = new EfDeletableEntityRepository <CandidateLanguage>(context);
            var skillsRepository     = new EfDeletableEntityRepository <CandidateSkill>(context);
            var candidatesService    = new CandidatesService(candidatesRepository, languagesRepository, skillsRepository, null);

            var candidateId = await candidatesService.CreateProfileAsync(new CreateCandidateProfileInputModel { FirstName = "Ivan", LastName = "Ivanov", ApplicationUserId = "1", LanguagesIds = new List <int> {
                                                                                                                    1
                                                                                                                }, SkillsIds = new List <int> {
                                                                                                                    1
                                                                                                                } });

            var updateModel = new UpdateCandidateProfileViewModel
            {
                LanguagesIds = new List <int> {
                    2, 3
                },
                SkillsIds = new List <int> {
                    4, 5
                },
            };

            await candidatesService.UpdateProfileAsync(candidateId, updateModel);

            var candidateLanguages = await context.Candidates
                                     .Where(c => c.Id == candidateId)
                                     .Select(c => c.Languages.Select(l => l.LanguageId).ToList())
                                     .FirstOrDefaultAsync();

            var candidateSkills = await context.Candidates
                                  .Where(c => c.Id == candidateId)
                                  .Select(c => c.Skills.Select(l => l.SkillId).ToList())
                                  .FirstOrDefaultAsync();

            Assert.Equal(2, candidateLanguages.Count);
            Assert.Equal(2, candidateSkills.Count);
            Assert.Contains(2, candidateLanguages);
            Assert.Contains(3, candidateLanguages);
            Assert.Contains(4, candidateSkills);
            Assert.Contains(5, candidateSkills);
        }
예제 #5
0
        public async Task <string> UpdateProfileAsync(string candidateId, UpdateCandidateProfileViewModel model)
        {
            var candidate = this.candidatesRepository
                            .All()
                            .FirstOrDefault(c => c.Id == candidateId);

            if (candidate == null)
            {
                return(null);
            }

            candidate.FirstName      = model.FirstName;
            candidate.LastName       = model.LastName;
            candidate.PhoneNumber    = model.PhoneNumber;
            candidate.ContactAddress = model.ContactAddress;
            candidate.Education      = model.Education;
            candidate.AboutMe        = model.SanitizedAboutMe;

            if (model.ProfilePicture != null)
            {
                if (candidate.ProfilePictureUrl != null)
                {
                    CloudinaryService.DeleteFile(this.cloudinary, model.ApplicationUserId + PictureNameAddIn);
                }

                var pictureUrl = await CloudinaryService.UploadImageAsync(this.cloudinary, model.ProfilePicture, model.ApplicationUserId + PictureNameAddIn);

                if (pictureUrl == null)
                {
                    return(null);
                }

                candidate.ProfilePictureUrl = pictureUrl;
            }

            candidate.ModifiedOn = DateTime.UtcNow;

            var candidateLanguagesIds = this.candidateLanguagesRepository
                                        .AllAsNoTracking()
                                        .Where(cl => cl.CandidateId == candidateId)
                                        .Select(cl => cl.LanguageId)
                                        .ToList();

            // Add new ones
            foreach (var languageId in model.LanguagesIds)
            {
                if (!candidateLanguagesIds.Contains(languageId))
                {
                    var language = new CandidateLanguage
                    {
                        LanguageId  = languageId,
                        CandidateId = candidate.Id,
                        CreatedOn   = DateTime.UtcNow,
                    };
                    await this.candidateLanguagesRepository.AddAsync(language);
                }
            }

            // Delete old ones
            foreach (var languageId in candidateLanguagesIds)
            {
                if (!model.LanguagesIds.Contains(languageId))
                {
                    var languages = this.candidateLanguagesRepository
                                    .All()
                                    .Where(cl => cl.LanguageId == languageId &&
                                           cl.CandidateId == candidate.Id)
                                    .FirstOrDefault();

                    this.candidateLanguagesRepository.Delete(languages);
                }
            }

            var candidateSkillsIds = this.candidateSkillsRepository
                                     .AllAsNoTracking()
                                     .Where(cs => cs.CandidateId == candidateId)
                                     .Select(cs => cs.SkillId)
                                     .ToList();

            // Add new ones
            foreach (var skillId in model.SkillsIds)
            {
                if (!candidateSkillsIds.Contains(skillId))
                {
                    var skill = new CandidateSkill
                    {
                        SkillId     = skillId,
                        CandidateId = candidate.Id,
                        CreatedOn   = DateTime.UtcNow,
                    };
                    await this.candidateSkillsRepository.AddAsync(skill);
                }
            }

            // Delete old ones
            foreach (var skillId in candidateSkillsIds)
            {
                if (!model.SkillsIds.Contains(skillId))
                {
                    var skills = this.candidateSkillsRepository
                                 .All()
                                 .Where(cs => cs.SkillId == skillId &&
                                        cs.CandidateId == candidate.Id)
                                 .FirstOrDefault();

                    this.candidateSkillsRepository.Delete(skills);
                }
            }

            try
            {
                this.candidatesRepository.Update(candidate);
                await this.candidatesRepository.SaveChangesAsync();

                await this.candidateLanguagesRepository.SaveChangesAsync();

                await this.candidateSkillsRepository.SaveChangesAsync();

                return(candidate.Id);
            }
            catch (Exception)
            {
                return(null);
            }
        }