コード例 #1
0
        public IHttpActionResult RegistrationDriver(ApplicantDto userdto)
        {
            AuthBL repos = new AuthBL();

            if (ModelState.IsValid == false)
            {
                return(BadRequest());
            }



            IdentityResult result = repos.CreateDriver(userdto);

            if (result.Succeeded)
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    Dictionary <string, string> tokenDetails = null;

                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri("http://localhost:4700/");
                    var login = new Dictionary <string, string>
                    {
                        { "grant_type", "password" },
                        { "username", userdto.Email },
                        { "password", userdto.Password },
                    };
                    var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        tokenDetails = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result);
                        if (tokenDetails != null && tokenDetails.Any())
                        {
                            var          tokenNo = tokenDetails.FirstOrDefault().Value;
                            IdentityUser user    = repos.Find(userdto.Email, userdto.Password);

                            AdminController applicantobj = new AdminController();
                            NewApplicant    applicant    = applicantobj.getApplicant(userdto.NationalID);
                            Driver          driver       = new Driver
                            {
                                UserID        = user.Id,
                                NationalID    = applicant.NationalID,
                                Rate          = 0,
                                numberOfTrips = 0,
                                AvgRate       = 0.0,
                            };
                            applicant.Status = NewApplicantstatus.Accepted;
                            context.Entry(applicant).State = EntityState.Modified;
                            context.SaveChanges();

                            context.Drivers.Add(driver);
                            context.SaveChanges();
                            return(Ok());
                        }
                    }
                }
            }

            return(BadRequest());
        }