// Create or Update public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata) { string data = string.Empty; string ids = FirstParameter(parameters); long id = 0; long.TryParse(ids, out id); ApiResponse <TaxScheduleDTO> response = new ApiResponse <TaxScheduleDTO>(); TaxScheduleDTO postedCategory = null; try { postedCategory = MerchantTribe.Web.Json.ObjectFromJson <TaxScheduleDTO>(postdata); } catch (Exception ex) { response.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(MerchantTribe.Web.Json.ObjectToJson(response)); } TaxSchedule item = new TaxSchedule(); item.FromDto(postedCategory); if (id < 1) { TaxSchedule existing = MTApp.OrderServices.TaxSchedules.FindByName(item.Name); if (existing == null) { // Create MTApp.OrderServices.TaxSchedules.Create(item); } else { item.Id = existing.Id; } } else { MTApp.OrderServices.TaxSchedules.Update(item); } response.Content = item.ToDto(); data = MerchantTribe.Web.Json.ObjectToJson(response); return(data); }
/// <summary> /// Allows the REST API to create or update a tax schedule /// </summary> /// <param name="parameters"> /// Parameters passed in the URL of the REST API call. If there is a first parameter found in the /// URL, the method will assume it is the tax schedule ID and that this is an update, otherwise it assumes to create a /// tax schedule. /// </param> /// <param name="querystring">Name/value pairs from the REST API call querystring. This is not used in this method.</param> /// <param name="postdata">Serialized (JSON) version of the TaxScheduleDTO object</param> /// <returns>TaxDTO - Serialized (JSON) version of the tax schedule</returns> public override string PostAction(string parameters, NameValueCollection querystring, string postdata) { var data = string.Empty; var ids = FirstParameter(parameters); long id = 0; long.TryParse(ids, out id); var response = new ApiResponse <TaxScheduleDTO>(); TaxScheduleDTO postedCategory = null; try { postedCategory = Json.ObjectFromJson <TaxScheduleDTO>(postdata); } catch (Exception ex) { response.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(Json.ObjectToJson(response)); } var item = new TaxSchedule(); item.FromDto(postedCategory); if (id < 1) { var existing = HccApp.OrderServices.TaxSchedules.FindByNameForThisStore(item.Name); if (existing == null) { // Create HccApp.OrderServices.TaxSchedules.Create(item); } else { item.Id = existing.Id; } } else { HccApp.OrderServices.TaxSchedules.Update(item); } response.Content = item.ToDto(); data = Json.ObjectToJson(response); return(data); }
// List or Find Single public override string GetAction(string parameters, System.Collections.Specialized.NameValueCollection querystring) { string data = string.Empty; if (string.Empty == (parameters ?? string.Empty)) { // List ApiResponse <List <TaxScheduleDTO> > response = new ApiResponse <List <TaxScheduleDTO> >(); List <TaxSchedule> results = MTApp.OrderServices.TaxSchedules.FindAll(MTApp.CurrentStore.Id); List <TaxScheduleDTO> dto = new List <TaxScheduleDTO>(); foreach (TaxSchedule item in results) { dto.Add(item.ToDto()); } response.Content = dto; data = MerchantTribe.Web.Json.ObjectToJson(response); } else { // Find One Specific Category ApiResponse <TaxScheduleDTO> response = new ApiResponse <TaxScheduleDTO>(); string ids = FirstParameter(parameters); long id = 0; long.TryParse(ids, out id); TaxSchedule item = MTApp.OrderServices.TaxSchedules.Find(id); if (item == null) { response.Errors.Add(new ApiError("NULL", "Could not locate that item. Check id and try again.")); } else { response.Content = item.ToDto(); } data = MerchantTribe.Web.Json.ObjectToJson(response); } return(data); }