Exemple #1
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ApplicationUser()
            {
                UserName = model.UserName, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                Data.SodexDatabaseDataContext db = new Data.SodexDatabaseDataContext();

                var card = from d in db.MstCards where d.CardNumber.Equals(model.MotherCardNumber) select d;
                if (card.Any())
                {
                    Data.MstUser newUser = new Data.MstUser()
                    {
                        AspNetUserId     = user.Id,
                        Username         = model.UserName,
                        UserTypeId       = 3,
                        FullName         = model.FullName,
                        Address          = model.Address,
                        Email            = model.Email,
                        ContactNumber    = model.ContactNumber,
                        MotherCardNumber = "NA",
                        Status           = "Enable"
                    };

                    db.MstUsers.InsertOnSubmit(newUser);
                    db.SubmitChanges();
                }
                else
                {
                    Data.MstUser newUser = new Data.MstUser()
                    {
                        AspNetUserId     = user.Id,
                        Username         = model.UserName,
                        UserTypeId       = 2,
                        FullName         = model.FullName,
                        Address          = model.Address,
                        Email            = model.Email,
                        ContactNumber    = model.ContactNumber,
                        MotherCardNumber = model.MotherCardNumber,
                        Status           = "Enable"
                    };

                    db.MstUsers.InsertOnSubmit(newUser);
                    db.SubmitChanges();

                    Data.MstCard newCard = new Data.MstCard
                    {
                        CardNumber    = model.MotherCardNumber,
                        FullName      = model.FullName,
                        Address       = model.Address,
                        Email         = model.Email,
                        ContactNumber = model.ContactNumber,
                        UserId        = newUser.Id,
                        Balance       = 0,
                        Particulars   = newUser.FullName + " " + DateTime.Now.ToString(),
                        Status        = "Enable"
                    };

                    db.MstCards.InsertOnSubmit(newCard);
                    db.SubmitChanges();
                }

                return(Ok());
            }
            else
            {
                return(GetErrorResult(result));
            }
        }