public BaseResponse<CustomerAPIViewModel> UpdateCustomer(CustomerAPIViewModel customer)
 {
     var customerService = this.Service<ICustomerService>();
     try
     {
         var customerVM = customerService.UpdateCustomer(customer);
         if (customerVM == null)
         {
             throw ApiException.Get(false, ConstantManager.MES_UPDATE_FAIL, ResultEnum.UpdateFail, HttpStatusCode.InternalServerError);
         }
         var account = customerVM.MembershipVM.AccountVMs;
         foreach (var item in account)
         {
             if (item.Type == (int)AccountTypeEnum.CreditAccount)
             {
                 customerVM.Balance = item.Balance == null ? 0 : item.Balance.Value;
             }
             if (item.Type == (int)AccountTypeEnum.PointAccount)
             {
                 customerVM.Point = item.Balance == null ? 0 : item.Balance.Value;
             }
         }
         return BaseResponse<CustomerAPIViewModel>.Get(true, ConstantManager.MES_UPDATE_SUCCESS, customerVM, ResultEnum.Success);
     }
     catch (Exception e)
     {
         if(e is ApiException)
         {
             throw e;
         }
         else
         {
             throw ApiException.Get(false, e.ToString(), ResultEnum.InternalError, HttpStatusCode.InternalServerError);
         }
         
     }
     
 }
示例#2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
                var array       = context.Scope.ToArray();
                var fbId        = array.GetValue(0).ToString();
                var brandID     = Convert.ToInt32(array.GetValue(1));
                var phone       = array.GetValue(2).ToString();
                var customerId  = Convert.ToInt32(array.GetValue(3));
                //var a = array.GetValue(1);
                //= (int)array.GetValue(1);

                ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);


                if (user == null)
                {
                    if (fbId.Length > 0)
                    {
                        var customerDomain            = new CustomerDomain();
                        CustomerAPIViewModel customer = customerDomain.GetCustomerByBrandIdAndFbId(brandID, fbId);
                        if (customer != null)
                        {
                            ApplicationDbContext db              = new ApplicationDbContext();
                            ApplicationUser      userVM          = db.Users.FirstOrDefault(x => x.Id == customer.AspUserVM.Id);
                            ClaimsIdentity       oAuthIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                          OAuthDefaults.AuthenticationType);

                            ClaimsIdentity cookiesIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                      CookieAuthenticationDefaults.AuthenticationType);

                            AuthenticationProperties propertiesVM = CreateProperties(userVM.UserName);
                            AuthenticationTicket     ticketVM     = new AuthenticationTicket(oAuthIdentityVM, propertiesVM);
                            context.Validated(ticketVM);
                            context.Request.Context.Authentication.SignIn(cookiesIdentityVM);
                        }
                    }
                    else if (phone.Length > 0)
                    {
                        var customerDomain = new CustomerDomain();
                        CustomerAPIViewModel customerByPhone = customerDomain.GetCustomersByPhonenumber(phone, brandID);
                        if (customerByPhone != null)
                        {
                            ApplicationDbContext db              = new ApplicationDbContext();
                            ApplicationUser      userVM          = db.Users.FirstOrDefault(x => x.Id == customerByPhone.AspUserVM.Id);
                            ClaimsIdentity       oAuthIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                          OAuthDefaults.AuthenticationType);

                            ClaimsIdentity cookiesIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                      CookieAuthenticationDefaults.AuthenticationType);

                            AuthenticationProperties propertiesVM = CreateProperties(userVM.UserName);
                            AuthenticationTicket     ticketVM     = new AuthenticationTicket(oAuthIdentityVM, propertiesVM);
                            context.Validated(ticketVM);
                            context.Request.Context.Authentication.SignIn(cookiesIdentityVM);
                        }
                    }
                    else if (customerId > 0)
                    {
                        var customerDomain = new CustomerDomain();
                        CustomerAPIViewModel customerById = customerDomain.GetCustomerById(customerId);
                        if (customerById != null)
                        {
                            ApplicationDbContext db              = new ApplicationDbContext();
                            ApplicationUser      userVM          = db.Users.FirstOrDefault(x => x.Id == customerById.AspUserVM.Id);
                            ClaimsIdentity       oAuthIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                          OAuthDefaults.AuthenticationType);

                            ClaimsIdentity cookiesIdentityVM = await userVM.GenerateUserIdentityAsync(userManager,
                                                                                                      CookieAuthenticationDefaults.AuthenticationType);

                            AuthenticationProperties propertiesVM = CreateProperties(userVM.UserName);
                            AuthenticationTicket     ticketVM     = new AuthenticationTicket(oAuthIdentityVM, propertiesVM);
                            context.Validated(ticketVM);
                            context.Request.Context.Authentication.SignIn(cookiesIdentityVM);
                        }
                    }
                    else
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }
                }
                else
                {
                    ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                        OAuthDefaults.AuthenticationType);

                    ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                          CookieAuthenticationDefaults.AuthenticationType);

                    AuthenticationProperties properties = CreateProperties(user.UserName);
                    AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                    context.Validated(ticket);
                    context.Request.Context.Authentication.SignIn(cookiesIdentity);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public CustomerAPIViewModel CreateWithContrans(string cardcode, CustomerAPIViewModel customer)
        {
            #region call service
            var customerService = this.Service<ICustomerService>();
            var mbsCardService = this.Service<IMembershipService>();
            var accountService = this.Service<IAccountService>();
            var cardService = this.Service<ICardService>();
            #endregion
            int id;
            var customerVM = new CustomerAPIViewModel();
            using (var trans = new TransactionScope(TransactionScopeOption.Required))
            {


                var entity = customer.ToEntity();
                try
                {
                    #region Check phone and email exist
                    // check phone exist //
                    if (!string.IsNullOrEmpty(customer.AccountPhone))
                    {
                        var customerCheck = customerService.GetCustomersByAccountPhone(customer.AccountPhone, customer.BrandId.Value);
                        if (customerCheck != null)
                        {
                            throw ApiException.Get(false, ConstantManager.MES_CUSTOMER_PHONE_EXIST, ResultEnum.CreateFail, HttpStatusCode.BadRequest);
                        }
                    }

                    // check email exist
                    if (!string.IsNullOrEmpty(customer.Email))
                    {
                        var customerCheck = customerService.GetCustomerByEmail(customer.Email, customer.BrandId.Value);
                        if (customerCheck != null)
                        {
                            throw ApiException.Get(false, ConstantManager.MES_CUSTOMER_EMAIL_EXIST, ResultEnum.CreateFail, HttpStatusCode.BadRequest);
                        }
                    }
                    #endregion

                    // Create Customer
                    id = customerService.CreateCustomer(entity);
                    // create Membership
                    var mbsCard = mbsCardService.AddMembership(id, (int)MembershipCardTypeEnum.Newbie);
                    // create Card
                    var newCard = cardService.AddCard(cardcode, customer.BrandId.Value, mbsCard, (int)CardTypeEnum.MobieCard);
                    // create Account Creadit and Point Member
                    accountService.CreateAccountByMemCard(mbsCard.MembershipCode, 0, customer.BrandId.Value, mbsCard.Id, (int)AccountTypeEnum.CreditAccount);
                    accountService.CreateAccountByMemCard(mbsCard.MembershipCode, 0, customer.BrandId.Value, mbsCard.Id, (int)AccountTypeEnum.PointAccount);
                    // End Transaction
                    trans.Complete();
                    trans.Dispose();

                }

                catch (Exception ex)
                {
                    // roll back transaction
                    trans.Dispose();
                    Console.WriteLine("Error Create : " + ex);
                    if (ex is ApiException)
                    {
                        throw ex;
                    }
                    else
                    {
                        throw ApiException.Get(false, ex.ToString(), ResultEnum.CreateFail, HttpStatusCode.InternalServerError);
                    }

                }
               


            }
            //customerVM = customerService.GetCustomerById(id);
            //if (customerVM == null)
            //{
            //    throw ApiException.Get(false, ConstantManager.MES_LOGIN_FAIL, ResultEnum.CreateFail, HttpStatusCode.BadRequest);
            //}
            customerVM = customerService.GetCustomerById(id);
            if(customerVM != null)
            {
                return customerVM;
            }
            return null;
            //return customerVM;
        }
 public int AddCustomer(CustomerAPIViewModel customer)
 {
     var customerService = this.Service<ICustomerService>();
     var entity = customer.ToEntity();
     return customerService.CreateCustomer(entity);
 }