Esempio n. 1
0
        public async Task<ActionResult> ImportLinkedIn()
        {
            var userManger = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var user = userManger.FindById(this.User.Identity.GetUserId());
            var claim = user.Claims.SingleOrDefault(m => m.ClaimType == "LinkedIn_AccessToken");

            if(claim == null)
            {
                // Redirect to connect LinkedIn
                // Drop a session nugget to get us back here
                Session["LinkLoginRedirect"] = "/Profile/ImportLinkedIn";
                return new Resume.Controllers.AccountController.ChallengeResult("LinkedIn", Url.Action("LinkLoginCallback", "Account"), User.Identity.GetUserId());
            }

            var client = new LinkedInApiClient(HttpContext.GetOwinContext().Request, claim.ClaimValue);
            var profileApi = new LinkedInProfileApi(client);
            var liProfile = await profileApi.GetBasicProfileAsync();

            // Convert the LinkedIn api object into something eaiser to bind to the view
            var importVM = new ImportLinkedInViewModel();
            importVM.FirstName = liProfile.FirstName;
            importVM.LastName = liProfile.LastName;
            importVM.Summary = liProfile.Summary;
            importVM.Positions = liProfile.Positions.Select(p => new Position()
            {
                Company = p.Company.Name,
                StartDate = new DateTime(p.StartDate.Year ?? DateTime.Now.Year, p.StartDate.Month ?? 1, p.StartDate.Day ?? 1),
                EndDate = p.EndDate == null ? (DateTime?)null : new DateTime(p.StartDate.Year ?? DateTime.Now.Year, p.StartDate.Month ?? 1, p.StartDate.Day ?? 1)
            }).ToList();

            return View(importVM);
        }
Esempio n. 2
0
        public ActionResult ImportLinkedIn(ImportLinkedInViewModel importProfile)
        {
            // Update profile values
            var profile = db.Query<Profile>().SingleOrDefault(p => p.OwnerIdentity == User.Identity.Name) ?? new Profile();
            profile.FirstName = importProfile.FirstName;
            profile.LastName = importProfile.LastName;
            profile.Summary = importProfile.Summary;
            profile.OwnerIdentity = User.Identity.Name;

            // Model binder is not reading the positions
            // TODO: Convert to custom model binder
            int i = 0;
            while (true)
            {
                if (i > 99)
                    break;

                var p = string.Format("Positions[{0}]", i);
                var id = Request[p + ".Id"];
                if (id == null)
                    break;

                var position = new Position();
                position.Title = Request[p + ".Title"];
                position.Company = Request[p + ".Company"];
                position.Description = Request[p + ".Description"];
                position.StartDate = DateTime.Parse(Request[p + ".StartDate"]);
                var endDate = Request[p + ".EndDate"];
                if(endDate != null)
                    position.EndDate = DateTime.Parse(Request[p + ".EndDate"]); ;
                importProfile.Positions.Add(position);

                i++;
            }

            using(var tx = db.BeginTransaction())
            {
                db.SaveOrUpdate(profile);

                foreach(var position in importProfile.Positions)
                {
                    position.OwnerIdentity = User.Identity.Name;
                    db.Save(position);
                }

                tx.Commit();
            }

            return View("ImportLinkedInSuccess");
        }