public FieldModel(ApiField field) { if (field == null) { throw new ArgumentNullException(nameof(field)); } _field = field; }
private async void AddItemCommandHandler(object p) { // Prompt to create the new item var dialog = new AddItemDialog(); // Show the dialog and wait for a response var result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { try { // Get the category var category = p as ApiCategory; // Create an item var item = new ApiItem() { Name = dialog.ItemName }; // Save the item to the api var r = await KryptPadApi.SaveItemAsync(category.Id, item); // Set the item item.Id = r.Id; // If a template was selected, create a couple of fields to start with if (dialog.SelectedItemTemplate != null) { var templateFields = dialog.SelectedItemTemplate.Fields; // A template was selected, add all the fields from the template foreach (var templateField in templateFields) { // Create field var field = new ApiField() { Name = templateField.Name, FieldType = templateField.FieldType }; // Send to api await KryptPadApi.SaveFieldAsync(category.Id, item.Id, field); } } // Navigate to item edit page NavigationHelper.Navigate(typeof(NewItemPage), new EditItemPageParams() { Category = category, Item = item }); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception) { // Failed await DialogHelper.ShowConnectionErrorMessageDialog(); } } }
/// <summary> /// Creates a new field /// </summary> /// <param name="profileId"></param> /// <param name="categoryId"></param> /// <param name="itemId"></param> /// <param name="field"></param> /// <param name="passphrase"></param> /// <returns></returns> public static async Task<SuccessResponse> SaveFieldAsync(int categoryId, int itemId, ApiField field) { using (var client = new HttpClient()) { // Authorize the request. await AuthorizeRequest(client); // Add passphrase to message AddPassphraseHeader(client); // Create content to send var content = JsonContent(field); // Execute request HttpResponseMessage response; if (field.Id == 0) { // Create response = await client.PostAsync(GetUrl($"api/profiles/{CurrentProfile.Id}/categories/{categoryId}/items/{itemId}/fields"), content); } else { // Update response = await client.PutAsync(GetUrl($"api/profiles/{CurrentProfile.Id}/categories/{categoryId}/items/{itemId}/fields/{field.Id}"), content); } // Check if the response is a success code if (response.IsSuccessStatusCode) { // Get the response content var data = await response.Content.ReadAsStringAsync(); // Create SuccessResponse object return new SuccessResponse(Convert.ToInt32(data)); } else { throw await CreateException(response); } } }