//[Route("{PromotionID?}")] //[HttpGet] public HttpResponseMessage GetPromotion(PromotionQueryRequest <dynamic> request) { var response = new BaseResponse <dynamic>(); var claimPrincipal = (ClaimsPrincipal)RequestContext.Principal; var customerId = claimPrincipal.Claims.Where(c => c.Type == "CustomerId").Select(c => c.Value).SingleOrDefault(); var cDomain = new CustomerDomain(); var id = Int32.Parse(customerId); var customer = cDomain.GetCustomerById(id); var resp = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }; var pDomain = new PromotionDomain(); Membership member = customer.MembershipVM.ToEntity(); request.Membership = member; var promotion = pDomain.GetPromotion(request).ToList(); if (promotion.Count == 0) { var res = BaseResponse <dynamic> .Get(false, "Không tìm thấy khuyến mãi nào", null, ResultEnum.PromotionNotFound); resp.Content = new JsonContent(res); resp.StatusCode = HttpStatusCode.NotFound; return(resp); } try { response = BaseResponse <dynamic> .Get(false, "Thành công", null, ResultEnum.Success); if (promotion.Count == 1) { response.Data = promotion.FirstOrDefault(); } else { response.Data = promotion; } } catch (ApiException e) { resp.StatusCode = e.StatusCode; response = BaseResponse <dynamic> .Get(e.Success, e.ErrorMessage, null, e.ErrorStatus); } catch (Exception e) { response = BaseResponse <dynamic> .Get(false, e.Message, null, ResultEnum.InternalError); resp.StatusCode = HttpStatusCode.InternalServerError; } resp.Content = new JsonContent(response); return(resp); }
public HttpResponseMessage CheckVoucher(CheckVoucherViewModel request) { //Logger.Log("Store " + model.terminalId); //Logger.Log("|CheckVoucherCode| begin method"); var response = new BaseResponse <dynamic>(); var claimPrincipal = (ClaimsPrincipal)RequestContext.Principal; var customerId = claimPrincipal.Claims.Where(c => c.Type == "CustomerId").Select(c => c.Value).SingleOrDefault(); var cDomain = new CustomerDomain(); var id = Int32.Parse(customerId); var customer = cDomain.GetCustomerById(id); var oDomain = new OrderDomain(); var resp = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }; try { DateTime time = DataService.Models.Utils.GetCurrentDateTime(); var pDomain = new PromotionDomain(); var orderVM = new OrderAPIViewModel(); var voucher = pDomain.GetVoucher(request.VoucherCode); var mbs = customer.MembershipVM; orderVM.OrderDetails = request.Data; orderVM.StoreID = request.StoreId; AddInfo(orderVM, request); oDomain.CalculateOrderPrice(orderVM, time); //temp: each voucher has only 1 detail now var applyResult = pDomain.IsVoucherValidFor(voucher, orderVM, mbs); orderVM = pDomain.ApplyPromotionToOrder(orderVM, applyResult, mbs); response = BaseResponse <dynamic> .Get(true, "Thành công", orderVM, ResultEnum.Success); } catch (ApiException e) { resp.StatusCode = e.StatusCode; response = BaseResponse <dynamic> .Get(e.Success, e.ErrorMessage, null, e.ErrorStatus); } catch (Exception e) { resp.StatusCode = HttpStatusCode.InternalServerError; response = BaseResponse <dynamic> .Get(false, e.ToString(), null, ResultEnum.InternalError); } resp.Content = new JsonContent(response); return(resp); }
public HttpResponseMessage GetVoucher([FromUri] VoucherQueryRequest <dynamic> request) { var response = new BaseResponse <List <VoucherAPIViewModel> >(); var claimPrincipal = (ClaimsPrincipal)RequestContext.Principal; var customerId = claimPrincipal.Claims.Where(c => c.Type == "CustomerId").Select(c => c.Value).SingleOrDefault(); var cDomain = new CustomerDomain(); var id = Int32.Parse(customerId); var customer = cDomain.GetCustomerById(id); var resp = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }; var pDomain = new PromotionDomain(); Membership member = customer.MembershipVM.ToEntity(); request.MembershipVM = member; request.BrandId = customer.BrandId; //if (voucher.Count == 0) //{ // var res = BaseResponse<dynamic>.Get(false, "Không tìm thấy voucher nào", null, ResultEnum.VoucherNotFound); // resp.Content = new JsonContent(res); // resp.StatusCode = HttpStatusCode.NotFound; // return resp; //} try { var voucher = pDomain.GetVoucher(request).ToList(); response = BaseResponse <List <VoucherAPIViewModel> > .Get(false, "Thành công", voucher, ResultEnum.Success); } catch (ApiException e) { resp.StatusCode = e.StatusCode; response = BaseResponse <List <VoucherAPIViewModel> > .Get(e.Success, e.ErrorMessage, null, e.ErrorStatus); } catch (Exception e) { response = BaseResponse <List <VoucherAPIViewModel> > .Get(false, e.Message, null, ResultEnum.InternalError); resp.StatusCode = HttpStatusCode.InternalServerError; } resp.Content = new JsonContent(response); return(resp); }
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); } }