public static EbayShippingRule ToModel(this ebay_shippingrule row, Currency currency) { return(new EbayShippingRule() { id = row.id, name = row.ebay_shippingservice.description, cost = row.cost.ToString("n" + currency.decimalCount), shipToLocations = string.Join(", ", row.ebay_shippingrule_locations.Select(x => x.description).ToArray()) }); }
public ActionResult EbayServicesSave(long profileid, int serviceid, decimal cost, string destinations) { var service = db.ebay_shippingservices.SingleOrDefault(x => x.id == serviceid); if (service == null) { return(SendJsonErrorResponse("Shipping service is invalid")); } var profile = MASTERdomain.ebay_shippingprofiles.SingleOrDefault(x => x.id == profileid); if (profile == null) { return(SendJsonErrorResponse("Shipping profile is invalid")); } // check if already exist if (profile.ebay_shippingrules.Any(x => x.ebay_shippingservice.isInternational == service.isInternational && x.serviceid == serviceid)) { return(SendJsonErrorResponse("Shipping service already exists")); } // check limits if (service.isInternational) { if (profile.ebay_shippingrules.Count(x => x.ebay_shippingservice.isInternational) == 5) { return(SendJsonErrorResponse("Maximum of 5 shipping services allowed")); } } else { if (profile.ebay_shippingrules.Count(x => !x.ebay_shippingservice.isInternational) == 5) { return(SendJsonErrorResponse("Maximum of 5 shipping services allowed")); } } // create rule var rule = new ebay_shippingrule { cost = cost, serviceid = serviceid }; profile.ebay_shippingrules.Add(rule); // create shiptodestination entries if (service.isInternational) { string[] destArray; if (string.IsNullOrEmpty(destinations)) { destArray = new[] { "Worldwide" }; } else { destArray = destinations.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } var hashset = new HashSet <string>(destArray); foreach (var entry in hashset) { var loc = db.ebay_shippinglocations.SingleOrDefault(x => x.location == entry && x.siteid == profile.siteid); if (loc != null) { var dest = new ebay_shippingrule_location(); dest.description = loc.description; dest.location = loc.location; rule.ebay_shippingrule_locations.Add(dest); } } } // save repository.Save("EbayServicesSave"); var currency = MASTERdomain.currency.ToCurrency(); var viewmodel = rule.ToModel(currency); var view = this.RenderViewToString("EbayServicesSingleRow", viewmodel); return(Json(view.ToJsonOKData())); }