示例#1
0
        // // GET api/<controller>/5
        // public IEnumerable<string> Get(int id)
        // {

        // }


        // POST api/<controller>
        public async Task <IHttpActionResult> Enroll([FromBody] Campaign customer)//void Post([FromBody]string value) //async Task<IHttpActionResult> Register(UserModel userModel)
        {
            //Campaign cmpn = new Campaign() { ID = customer.ID, PrntID = customer.PrntID, Phone = customer.Phone, Email = customer.Email };
            //var cvid = Int32.Parse(externalLogin.Viid);

            //ObjectResult objRslt = ctx.usp_insertCustomer(customer.ID,customer.Name,customer.PrntID,customer.PassPrnt,customer.Email,customer.Phone);

            var check1 = _repo2.Campaigns.SingleOrDefault(email => email.Email == customer.Email);

            if (check1 != null)
            {
                var Content = "This Vi email address " + customer.Email.ToString() + " already exists in the database.";
                //};
                return(BadRequest(Content));
            }

            var check = _repo2.Campaigns.Find(customer.ID);

            if (check == null)
            {
                ObjectResult objRslt = ctx.usp_insertCustomer(customer.ID, customer.Name, customer.PrntID, customer.PassPrnt, customer.Email, customer.Phone);
                var          check2  = _repo2.Campaigns.Find(customer.ID);
                if (check2 != null)
                {
                    var       Content = "Success!";
                    UserModel um      = new UserModel();
                    um.UserName        = customer.Email;
                    um.Password        = customer.ID.ToString();
                    um.ConfirmPassword = customer.ID.ToString();
                    IdentityResult result = await _repo.RegisterUser(um);

                    IdentityUser user = await _repo.FindUser(um.UserName, um.Password);

                    user.Email          = check2.Email;
                    user.EmailConfirmed = true;
                    result = await _repo.UpdateUser(user);

                    var check3 = _repo2.Campaigns.Find(customer.PrntID);
                    if (check3 != null)
                    {
                        check3.TokenID = "";
                    }

                    _repo2.SaveChanges();

                    await SendSG(customer);

                    return(Ok(Content));
                }
                else
                {
                    var Content = "This Vi customer ID " + customer.ID.ToString() + " did not save to the database.";
                    //};
                    return(BadRequest(Content));
                }

                //customer.StartDate = DateTime.Now;
                //customer.Rank = "Customer";
                //Campaign success = _repo2.Campaigns.Add(customer);
                //_repo2.SaveChanges();

                //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, customer);

                //return Ok();
            }
            else
            {
                //var message = new HttpResponseMessage(HttpStatusCode.BadRequest)
                //{
                var Content = "This Vi customer ID " + customer.ID.ToString() + " already exists in the database.";
                //};
                return(BadRequest(Content));
                //throw new HttpResponseException(message);
                //return new HttpResponseMessage(HttpStatusCode.NotModified);
            }
            //return Ok();
        }