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); }
private void AddInfo(OrderAPIViewModel src, CheckVoucherViewModel request) { if (!request.BrandId.HasValue && !request.StoreId.HasValue) { throw ApiException.Get(false, "Thiếu thông tin brand", ResultEnum.BrandIdNotFound, HttpStatusCode.BadRequest); } var prdDomain = new ProductDomain(); src.BrandId = request.BrandId.Value; if (request.BrandId == -1 && request.StoreId > 0) { var storeApi = new DataService.Domain.StoreDomain(); var store = storeApi.GetStoreByStoreId(request.StoreId.Value); src.BrandId = store.BrandId; } else { throw ApiException.Get(false, "Thiếu thông tin brand", ResultEnum.BrandIdNotFound, HttpStatusCode.BadRequest); } foreach (var oD in request.Data) { oD.ProductCode = prdDomain.GetProductById(oD.ProductID).Code; } }