public HttpResponseMessage AddFixedRateShipMethod(FixedRateShipMethodDisplay method) { var response = Request.CreateResponse(HttpStatusCode.OK); try { var shipCountry = _shipCountryService.GetByKey(method.ShipMethod.ShipCountryKey); var provider = _fixedRateShippingGatewayProvider; var rateTableType = FixedRateShippingGatewayMethod.QuoteType.VaryByWeight; if (method.GatewayResource.ServiceCode == "VBP") { rateTableType = FixedRateShippingGatewayMethod.QuoteType.VaryByPrice; } var merchelloGwShipMethod = (IFixedRateShippingGatewayMethod)provider.CreateShipMethod(rateTableType, shipCountry, method.ShipMethod.Name); merchelloGwShipMethod = method.ToFixedRateShipMethod(merchelloGwShipMethod); provider.SaveShippingGatewayMethod(merchelloGwShipMethod); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, String.Format("{0}", ex.Message)); } return response; }
/// <summary> /// Maps changes made in the <see cref="FixedRateShipMethodDisplay"/> to the <see cref="IFixedRateShippingGatewayMethod"/> /// </summary> /// <param name="fixedRateShipMethodDisplay">The <see cref="FixedRateShipMethodDisplay"/> to map</param> /// <param name="destination">The <see cref="IFixedRateShippingGatewayMethod"/> to have changes mapped to</param> /// <returns>The updated <see cref="IFixedRateShippingGatewayMethod"/></returns> /// <remarks> /// /// Note: after calling this mapping, the changes are still not persisted to the database as the .Save() method is not called. /// /// * For testing you will have to use the static .Save(IGatewayProviderService ..., as MerchelloContext.Current will likely be null /// /// </remarks> internal static IFixedRateShippingGatewayMethod ToFixedRateShipMethod(this FixedRateShipMethodDisplay fixedRateShipMethodDisplay, IFixedRateShippingGatewayMethod destination) { // RUSTY HELP: Cannot assign to these properties. How should I do this mapping? // Shipmethod destination.ShipMethod.Name = fixedRateShipMethodDisplay.ShipMethod.Name; if (destination.ShipMethod.Provinces.Any() && fixedRateShipMethodDisplay.ShipMethod.Provinces.Any()) { var provinceCollection = new ProvinceCollection <IShipProvince>(); foreach (var province in fixedRateShipMethodDisplay.ShipMethod.Provinces) { provinceCollection.Add( new ShipProvince(province.Code, province.Name) { AllowShipping = province.AllowShipping, RateAdjustment = province.RateAdjustment, RateAdjustmentType = province.RateAdjustmentType } ); } destination.ShipMethod.Provinces = provinceCollection; } // Rate table var existingRows = fixedRateShipMethodDisplay.RateTable.Rows.Where(x => !x.Key.Equals(Guid.Empty)).ToArray(); foreach (var mapRow in existingRows) { var row = destination.RateTable.Rows.FirstOrDefault(x => x.Key == mapRow.Key); if (row != null) { row.Rate = mapRow.Rate; } } // remove existing rows that previously existed but were deleted in the UI var removers = destination.RateTable.Rows.Where( row => !row.Key.Equals(Guid.Empty) && existingRows.All(x => x.Key != row.Key && !x.Key.Equals(Guid.Empty))); foreach (var remove in removers) { destination.RateTable.DeleteRow(remove); } // add any new rows foreach (var newRow in fixedRateShipMethodDisplay.RateTable.Rows.Where(x => x.Key == Guid.Empty)) { destination.RateTable.AddRow(newRow.RangeLow, newRow.RangeHigh, newRow.Rate); } return(destination); }
public HttpResponseMessage PutFixedRateShipMethod(FixedRateShipMethodDisplay method) { var response = Request.CreateResponse(HttpStatusCode.OK); try { var shipCountry = _shipCountryService.GetByKey(method.ShipMethod.ShipCountryKey); var provider = _fixedRateShippingGatewayProvider; var merchelloMethod = (IFixedRateShippingGatewayMethod)provider.GetAllShippingGatewayMethods(shipCountry).FirstOrDefault(m => m.ShipMethod.Key == method.ShipMethod.Key); if (merchelloMethod != null) { merchelloMethod = method.ToFixedRateShipMethod(merchelloMethod); } else { return Request.CreateResponse(HttpStatusCode.NotFound); } provider.SaveShippingGatewayMethod(merchelloMethod); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, String.Format("{0}", ex.Message)); } return response; }
public HttpResponseMessage DeleteRateTableShipMethod(FixedRateShipMethodDisplay method) { var shipmethodService = ((ServiceContext) MerchelloContext.Services).ShipMethodService; var methodToDelete = shipmethodService.GetByKey(method.ShipMethod.Key); if (methodToDelete == null) return Request.CreateResponse(HttpStatusCode.NotFound); shipmethodService.Delete(methodToDelete); return Request.CreateResponse(HttpStatusCode.OK); }