From ce98b203554e6dc9574a0478341a8fa0dec3ddd8 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Tue, 10 Oct 2017 12:31:56 +0300 Subject: [PATCH] V5 customFields --- Retailcrm/Retailcrm.csproj | 1 + Retailcrm/Versions/V5/CustomFields.cs | 194 ++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 Retailcrm/Versions/V5/CustomFields.cs diff --git a/Retailcrm/Retailcrm.csproj b/Retailcrm/Retailcrm.csproj index 58508c2..3519c56 100644 --- a/Retailcrm/Retailcrm.csproj +++ b/Retailcrm/Retailcrm.csproj @@ -64,6 +64,7 @@ + diff --git a/Retailcrm/Versions/V5/CustomFields.cs b/Retailcrm/Versions/V5/CustomFields.cs new file mode 100644 index 0000000..5fff152 --- /dev/null +++ b/Retailcrm/Versions/V5/CustomFields.cs @@ -0,0 +1,194 @@ +namespace Retailcrm.Versions.V5 +{ + using System; + using System.Collections.Generic; + using System.Web.Script.Serialization; + + public partial class Client + { + public Response CustomFieldsList(Dictionary filter = null, int page = 0, int limit = 0) + { + Dictionary parameters = new Dictionary(); + + if (filter != null && filter.Count > 0) + { + parameters.Add("filter", filter); + } + + if (page > 0) + { + parameters.Add("page", page); + } + + if (limit > 0) + { + parameters.Add("limit", limit); + } + + return Request.MakeRequest("/custom-fields", Request.MethodGet, parameters); + } + + public Response CustomFieldsCreate(Dictionary customField, string entity = "") + { + List types = new List + { + "boolean", "date", "dictionary", "email", "integer", "numeric", "string", "text" + }; + + if (customField.Count < 1) + { + throw new ArgumentException("Parameter `customField` must contains a data"); + } + + if (!customField.ContainsKey("code")) + { + throw new ArgumentException("Parameter `customField` should contain `code`"); + } + + if (!customField.ContainsKey("name")) + { + throw new ArgumentException("Parameter `customField` should contain `name`"); + } + + if (!customField.ContainsKey("type")) + { + throw new ArgumentException("Parameter `customField` should contain `type`"); + } + + if (!types.Contains(customField["type"].ToString())) + { + throw new ArgumentException( + "Parameter `customField` should contain `type` & value of type should be on of `boolean|date|dictionary|email|integer|numeric|string|text`" + ); + } + + return Request.MakeRequest( + $"/custom-fields/{entity}/create", + Request.MethodPost, + new Dictionary + { + { "customField", new JavaScriptSerializer().Serialize(customField) } + } + ); + } + + public Response CustomFieldsGet(string code, string entity) + { + return Request.MakeRequest( + $"/custom-fields/{entity}/{code}", + Request.MethodGet + ); + } + + public Response CustomFieldsUpdate(Dictionary customField, string entity = "") + { + if (customField.Count < 1) + { + throw new ArgumentException("Parameter `customField` must contains a data"); + } + + if (!customField.ContainsKey("code")) + { + throw new ArgumentException("Parameter `customField` should contain `code`"); + } + + if (!customField.ContainsKey("name")) + { + throw new ArgumentException("Parameter `customField` should contain `name`"); + } + + return Request.MakeRequest( + $"/custom-fields/{entity}/{customField["code"].ToString()}/edit", + Request.MethodPost, + new Dictionary + { + { "customField", new JavaScriptSerializer().Serialize(customField) } + } + ); + } + + public Response CustomDictionaryList(Dictionary filter = null, int page = 0, int limit = 0) + { + Dictionary parameters = new Dictionary(); + + if (filter != null && filter.Count > 0) + { + parameters.Add("filter", filter); + } + + if (page > 0) + { + parameters.Add("page", page); + } + + if (limit > 0) + { + parameters.Add("limit", limit); + } + + return Request.MakeRequest("/custom-fields/dictionaries", Request.MethodGet, parameters); + } + + public Response CustomDictionaryCreate(Dictionary customDictionary) + { + if (customDictionary.Count < 1) + { + throw new ArgumentException("Parameter `customDictionary` must contains a data"); + } + + if (!customDictionary.ContainsKey("code")) + { + throw new ArgumentException("Parameter `customDictionary` should contain `code`"); + } + + if (!customDictionary.ContainsKey("name")) + { + throw new ArgumentException("Parameter `customDictionary` should contain `name`"); + } + + return Request.MakeRequest( + "/custom-fields/dictionaries/create", + Request.MethodPost, + new Dictionary + { + { "customDictionary", new JavaScriptSerializer().Serialize(customDictionary) } + } + ); + } + + public Response CustomDictionaryGet(string code) + { + return Request.MakeRequest( + $"/custom-fields/dictionaries/{code}", + Request.MethodGet + ); + } + + public Response CustomDictionaryUpdate(Dictionary customDictionary) + { + if (customDictionary.Count < 1) + { + throw new ArgumentException("Parameter `customDictionary` must contains a data"); + } + + if (!customDictionary.ContainsKey("code")) + { + throw new ArgumentException("Parameter `customDictionary` should contain `code`"); + } + + if (!customDictionary.ContainsKey("name")) + { + throw new ArgumentException("Parameter `customDictionary` should contain `name`"); + } + + return Request.MakeRequest( + $"/custom-fields/dictionaries/{customDictionary["code"].ToString()}/edit", + Request.MethodPost, + new Dictionary + { + { "customDictionary", new JavaScriptSerializer().Serialize(customDictionary) } + } + ); + } + } +}