예제 #1
0
        public async Task <IActionResult> FillProfile()
        {
            var user = await userManager.FindByNameAsync(User.Identity.Name);

            var selectedItems = context.UserSkills.Where(i => i.AppUserId == user.Id).Select(i => i.SkillId);

            if (selectedItems == null)
            {
                ViewBag.Skills = new MultiSelectList(context.Skills, "Id", "SkillName");
            }

            ViewBag.Skills = new MultiSelectList(context.Skills, "Id", "SkillName", selectedItems);

            if (!await userManager.IsInRoleAsync(user, "Freelancer"))
            {
                return(BadRequest());
            }


            FillFreelanceProfileVM model = new FillFreelanceProfileVM()
            {
                Email       = user.Email,
                UserName    = user.UserName,
                FullName    = user.FullName,
                City        = user.City,
                OwnWebSite  = user.WebSiteLink,
                Age         = user.Age,
                BirthDay    = user.BirthDay,
                Experience  = user.Experience,
                ImageUrl    = user.ImageUrl,
                Description = user.Description,
                PhoneNumber = user.PhoneNumber
            };


            return(View(model));
        }
예제 #2
0
        public async Task <IActionResult> FillProfile(FillFreelanceProfileVM vm, int[] skillIds, IFormFile file)
        {
            var user = await userManager.FindByNameAsync(User.Identity.Name);

            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    var filename = $"{Guid.NewGuid().ToString()}{file.FileName}";
                    var path     = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\img", filename);

                    using (var fs = new FileStream(path, FileMode.OpenOrCreate))
                    {
                        await file.CopyToAsync(fs);
                    }
                    vm.ImageUrl      = filename;
                    user.FullName    = vm.FullName;
                    user.BirthDay    = vm.BirthDay;
                    user.Experience  = vm.Experience;
                    user.PhoneNumber = vm.PhoneNumber;
                    user.Description = vm.Description;
                    user.City        = vm.City;
                    user.Age         = vm.Age;
                    user.ImageUrl    = vm.ImageUrl;
                    user.UserName    = vm.UserName;

                    if (user.UserSkills == null)
                    {
                        context.UserSkills.AddRange(skillIds.Select(i => new UserSkill {
                            SkillId = i, AppUserId = user.Id
                        }));
                    }
                    else
                    {
                        context.UpdateManyToMany(
                            context.UserSkills.Where(x => x.AppUserId == user.Id),
                            skillIds.Select(x => new UserSkill {
                            AppUserId = user.Id, SkillId = x
                        }),
                            x => x.SkillId);
                    }

                    var b = await context.SaveChangesAsync();


                    var result = await userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        TempData["OK"] = "Profile sucsessfully filled!!!";
                        return(Redirect("/"));
                    }
                }
            }
            var selectedItems = context.UserSkills.Where(i => i.AppUserId == user.Id).Select(i => i.SkillId);

            if (selectedItems == null)
            {
                ViewBag.Skills = new MultiSelectList(context.Skills, "Id", "SkillName");
            }
            ViewBag.Skills = new MultiSelectList(context.Skills, "Id", "SkillName", selectedItems);
            return(View(vm));
        }