// Create or Update public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata) { string data = string.Empty; string bvin = FirstParameter(parameters); ApiResponse <CustomerAccountDTO> response = new ApiResponse <CustomerAccountDTO>(); CustomerAccountDTO postedItem = null; try { postedItem = MerchantTribe.Web.Json.ObjectFromJson <CustomerAccountDTO>(postdata); } catch (Exception ex) { response.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(MerchantTribe.Web.Json.ObjectToJson(response)); } CustomerAccount item = new CustomerAccount(); item.FromDto(postedItem); if (bvin == string.Empty) { CustomerAccount existing = MTApp.MembershipServices.Customers.FindByEmail(item.Email); if (existing == null || existing.Bvin == string.Empty) { string clearPassword = querystring["pwd"]; if (clearPassword.Trim().Length < 1) { clearPassword = MerchantTribe.Web.PasswordGenerator.GeneratePassword(10); } // Create bool result = MTApp.MembershipServices.CreateCustomer(item, clearPassword); bvin = item.Bvin; } else { bvin = existing.Bvin; } } else { MTApp.MembershipServices.UpdateCustomer(item); } CustomerAccount resultItem = MTApp.MembershipServices.Customers.Find(bvin); if (resultItem != null) { response.Content = resultItem.ToDto(); // Address Import foreach (AddressDTO a in postedItem.Addresses) { Address addr = new Address(); addr.FromDto(a); MTApp.MembershipServices.CheckIfNewAddressAndAddWithUpdate(resultItem, addr); } } data = MerchantTribe.Web.Json.ObjectToJson(response); return(data); }
/// <summary> /// Allows the REST API to create or update a category /// </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 category ID (bvin) and that this is an update, otherwise it assumes to create /// a category. /// </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 CategoryDTO object</param> /// <returns>CategoryDTO - Serialized (JSON) version of the category</returns> public override string PostAction(string parameters, NameValueCollection querystring, string postdata) { var data = string.Empty; var bvin = FirstParameter(parameters); var response = new ApiResponse <CustomerAccountDTO>(); CustomerAccountDTO postedItem = null; try { postedItem = Json.ObjectFromJson <CustomerAccountDTO>(postdata); } catch (Exception ex) { response.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(Json.ObjectToJson(response)); } var item = new CustomerAccount(); item.FromDto(postedItem); if (bvin == string.Empty) { var existing = HccApp.MembershipServices.Customers.FindByEmail(item.Email).FirstOrDefault(); if (existing == null || existing.Bvin == string.Empty) { var clearPassword = item.Password; if (string.IsNullOrWhiteSpace(clearPassword)) { clearPassword = PasswordGenerator.GeneratePassword(10); } // Create var result = HccApp.MembershipServices.CreateCustomer(item, clearPassword); bvin = item.Bvin; } else { bvin = existing.Bvin; } } else { HccApp.MembershipServices.UpdateCustomer(item); } var resultItem = HccApp.MembershipServices.Customers.Find(bvin); if (resultItem != null) { response.Content = resultItem.ToDto(); // Address Import foreach (var a in postedItem.Addresses) { var addr = new Address(); addr.FromDto(a); HccApp.MembershipServices.CheckIfNewAddressAndAddWithUpdate(resultItem, addr); } } data = Json.ObjectToJson(response); return(data); }