Ejemplo n.º 1
0
        /// <summary>
        /// User sign in.
        /// </summary>
        /// <param name="name">Full name.</param>
        /// <param name="email">Email address.</param>
        /// <param name="applyModel">The view model.</param>
        /// <returns></returns>
        public async Task <IActionResult> SignIn(string name, string email, string applyModel)
        {
            var viewModel = JsonConvert.DeserializeObject <ApplyViewModel>(applyModel);

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email))
            {
                ViewModelError.AddViewModelError(ModelState, "ControllerError", "Please provide name and email");
                return(View("Apply", viewModel));
            }

            using (var client = new HttpClient())
            {
                // sign in
                var response = await client.PostAsync(UrlBuilder.SignInUri(_webApiSettings.BaseUrl, name, email), null);

                // Sign in succeded
                if (response.IsSuccessStatusCode)
                {
                    var data = await response.Content.ReadAsStringAsync();

                    var signInStatus = JsonConvert.DeserializeObject <SigninStatus>(data);
                    viewModel.SignedInPerson = new SignedInPerson(name, email, signInStatus.PersonId);

                    // 1. Create a new briefcase (for the single post) or get existing briefcase
                    response = await client.PostAsync(UrlBuilder.BriefcaseAddPostUri(_webApiSettings.BaseUrl, 0, viewModel.SignedInPerson.PersonId, viewModel.Post.Id), null);

                    if (response.IsSuccessStatusCode)
                    {
                        var briefcaseId = await response.Content.ReadAsStringAsync();

                        viewModel.BriefcaseId = int.Parse(briefcaseId);
                    }
                    else
                    {
                        // Try to get existing brieafcase id for person
                        var briefcase = Db.Briefcase.FirstOrDefault(x => x.PersonId == viewModel.SignedInPerson.PersonId);
                        if (briefcase != null)
                        {
                            viewModel.BriefcaseId = briefcase.Id;
                        }
                        else
                        {
                            ViewModelError.AddViewModelError(ModelState, "ControllerError", "An error occurred, please try again");
                            return(View("Apply", viewModel));
                        }
                    }

                    // 2. Create the interview
                    response = await client.PostAsync(UrlBuilder.InterviewCreateUri(_webApiSettings.BaseUrl, viewModel.BriefcaseId, null), null);

                    if (response.IsSuccessStatusCode)
                    {
                        data = await response.Content.ReadAsStringAsync();

                        viewModel.Interview   = JsonConvert.DeserializeObject <Interview>(data);
                        viewModel.CurrentStep = 0;
                    }
                    else
                    {
                        ViewModelError.AddViewModelError(ModelState, "ControllerError", "An error occurred, please try again");
                        return(View("Apply", viewModel));
                    }
                }
                // Failed
                else
                {
                    ViewModelError.AddViewModelError(ModelState, "ControllerError", "An error occurred, please try again");
                    return(View("Apply", viewModel));
                }
            }

            return(View("Step", viewModel));
        }