private static async Task <InvoiceResponseDTO> DeleteInvoiceAsync(LoginResponseDTO loginResponseDTO, int Id) { InvoiceResponseDTO invoiceResponseDTO = new InvoiceResponseDTO(); string url = $"https://lobworkshop.azurewebsites.net/api/Invoices/{Id}"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResponseDTO.Token); HttpResponseMessage response = await client.DeleteAsync(url); String strResult = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { APIResult apiResult = JsonConvert.DeserializeObject <APIResult>(strResult, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); if (apiResult.Status == true) { string itemJsonContent = apiResult.Payload.ToString(); Console.WriteLine($"成功刪除一筆發票 : {itemJsonContent}"); invoiceResponseDTO = JsonConvert.DeserializeObject <InvoiceResponseDTO>(itemJsonContent, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); } } return(invoiceResponseDTO); }
static async Task Main(string[] args) { // 登入系統,取得 JTW Token await LoginAsync(); // 從檔案中取得 JWT 權杖 Token string fileContent = await StorageUtility.ReadFromDataFileAsync("", "MyDataFolder", "MyFilename.txt"); LoginResponseDTO loginResponseDTO = JsonConvert.DeserializeObject <LoginResponseDTO>(fileContent); #region CRUD => Retrive 取得該使用者的發票資料 List <InvoiceResponseDTO> invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Create 新增發票資料 InvoiceResponseDTO invoiceResponseDTO = await CreateInvoiceAsync(loginResponseDTO); #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Update 修改發票資料 invoiceResponseDTO = await UpdateInvoiceAsync(loginResponseDTO, invoiceResponseDTO); #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Delete 刪除發票資料 foreach (var item in invoiceResponseDTOs) { await DeleteInvoiceAsync(loginResponseDTO, item.Id); } #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
private static async Task <InvoiceResponseDTO> CreateInvoiceAsync(LoginResponseDTO loginResponseDTO) { InvoiceResponseDTO invoiceResponseDTO = new InvoiceResponseDTO(); string url = "https://lobworkshop.azurewebsites.net/api/Invoices"; InvoiceRequestDTO invoiceRequestDTO = new InvoiceRequestDTO() { InvoiceNo = "123", Memo = "查理王", Date = new DateTime(2019, 05, 20), user = new UserDTO() { Id = loginResponseDTO.Id } }; var httpJsonPayload = JsonConvert.SerializeObject(invoiceRequestDTO); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResponseDTO.Token); HttpResponseMessage response = await client.PostAsync(url, new StringContent(httpJsonPayload, System.Text.Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { String strResult = await response.Content.ReadAsStringAsync(); APIResult apiResult = JsonConvert.DeserializeObject <APIResult>(strResult, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); if (apiResult.Status == true) { string itemJsonContent = apiResult.Payload.ToString(); Console.WriteLine($"成功新增一筆發票 : {itemJsonContent}"); invoiceResponseDTO = JsonConvert.DeserializeObject <InvoiceResponseDTO>(itemJsonContent, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); } } return(invoiceResponseDTO); }
private static async Task <InvoiceResponseDTO> UpdateInvoiceAsync(LoginResponseDTO loginResponseDTO, InvoiceResponseDTO UpdateItem) { InvoiceResponseDTO invoiceResponseDTO = new InvoiceResponseDTO(); string url = $"https://lobworkshop.azurewebsites.net/api/Invoices/{UpdateItem.Id}"; InvoiceRequestDTO invoiceRequestDTO = new InvoiceRequestDTO() { Id = UpdateItem.Id, InvoiceNo = UpdateItem.InvoiceNo, Memo = "修正" + UpdateItem.Memo, Date = UpdateItem.Date.AddDays(5), user = UpdateItem.user }; var httpJsonPayload = JsonConvert.SerializeObject(invoiceRequestDTO); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResponseDTO.Token); HttpResponseMessage response = await client.PutAsync(url, new StringContent(httpJsonPayload, System.Text.Encoding.UTF8, "application/json")); String strResult = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { APIResult apiResult = JsonConvert.DeserializeObject <APIResult>(strResult, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); if (apiResult.Status == true) { string itemJsonContent = apiResult.Payload.ToString(); Console.WriteLine($"成功修改一筆發票 : {itemJsonContent}"); invoiceResponseDTO = JsonConvert.DeserializeObject <InvoiceResponseDTO>(itemJsonContent, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); } } return(invoiceResponseDTO); }