/// <summary> /// Updates the specified category. Properties that are null won't be changed. /// </summary> /// <param name="authUser">The e-mail address of the user to whom the category should be added</param> /// <param name="authToken">A valid API token (linked to the e-mail of the specified user)</param> /// <param name="categoryToUpdate">Category object that specifie the properties of the category to be updated</param> /// <returns>Returns the unique Id of the newly created category</returns> public static async Task <string> UpdateAsync(string authUser, string authToken, WalletCategory categoryToUpdate) { if (categoryToUpdate.Id == null) { throw new ArgumentException("Id of updated category object can't be null"); } var putConformObject = new { name = categoryToUpdate.Name, color = categoryToUpdate.Color, icon = categoryToUpdate.Icon, defaultType = categoryToUpdate.DefaultType.ToValidApiString(), position = categoryToUpdate.Position }; var urlSpecifier = $"category/{categoryToUpdate.Id}"; var contentType = "application/json"; var contentJson = JsonConvert.SerializeObject(putConformObject); var content = new StringContent(contentJson, Encoding.Default, contentType); using (var client = new HttpClient { BaseAddress = new Uri(Constants.BaseUrl) }) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add(Constants.TokenHeaderKey, authToken); client.DefaultRequestHeaders.Add(Constants.UserHeaderKey, authUser); var response = await client.PutAsync(urlSpecifier, content); var responseJson = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <dynamic>(responseJson).id.ToString()); } }
/// <summary> /// Creates a new Category for the specified user, given a valid API token /// <para/>Only the name of the new category is a required property. /// <para/>Default values for other properties:<para/> /// color: #13BA78, /// icon: 0, /// defaultType: <see cref="WalletCategoryType"/>.Expense, /// position: last + 1000, /// </summary> /// <param name="authUser">The e-mail address of the user to whom the category should be added</param> /// <param name="authToken">A valid API token (linked to the e-mail of the specified user)</param> /// <param name="categoryToCreate">Account object that specifies the properties of the category to be created</param> /// <returns>Returns the unique Id of the newly created category</returns> public static async Task <string> CreateNewAsync(string authUser, string authToken, WalletCategory categoryToCreate) { var postConformObject = new { name = categoryToCreate.Name, color = categoryToCreate.Color, icon = categoryToCreate.Icon, defaultType = categoryToCreate.DefaultType.ToValidApiString(), position = categoryToCreate.Position, }; var urlSpecifier = "category"; var contentType = "application/json"; var contentJson = JsonConvert.SerializeObject(postConformObject); var content = new StringContent(contentJson, Encoding.Default, contentType); using (var client = new HttpClient { BaseAddress = new Uri(Constants.BaseUrl) }) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add(Constants.TokenHeaderKey, authToken); client.DefaultRequestHeaders.Add(Constants.UserHeaderKey, authUser); var response = await client.PostAsync(urlSpecifier, content); var responseJson = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <dynamic>(responseJson).id.ToString()); } }
public async Task AssociationDeleteTest() { await categories.DeleteAllAsync(); await wallets.DeleteAllAsync(); await walletsCategories.DeleteAllAsync(); var cat1 = new Category() { Name = "category test 1" }; var cat2 = new Category() { Name = "category test 2" }; var wallet1 = new Wallet() { Name = "wallet test 1" }; var wallet2 = new Wallet() { Name = "wallet test 2" }; await categories.InsertAsync(cat1, cat2); await wallets.InsertAsync(wallet1, wallet2); var categoryFilter = new CategoryFilter() { Name = "category test 1" }; cat1 = (await categories.GetAsync(categoryFilter)).First(); categoryFilter.Name = "category test 2"; cat2 = (await categories.GetAsync(categoryFilter)).First(); var walletFilter = new WalletFilter() { Name = "wallet test 1" }; wallet1 = (await wallets.GetAsync(walletFilter)).First(); walletFilter.Name = "wallet test 2"; wallet2 = (await wallets.GetAsync(walletFilter)).First(); var walletCategory1 = new WalletCategory() { CategoryId = cat1.Id, WalletId = wallet1.Id }; var walletCategory2 = new WalletCategory() { CategoryId = cat2.Id, WalletId = wallet2.Id }; await walletsCategories.InsertAsync(walletCategory1, walletCategory2); Assert.AreEqual(2, (await walletsCategories.GetAllAsync()).Count); await categories.DeleteAsync(cat1.Id); Assert.AreEqual(1, (await walletsCategories.GetAllAsync()).Count); await wallets.DeleteAsync(wallet2.Id); Assert.AreEqual(0, (await walletsCategories.GetAllAsync()).Count); }