APIClient + фикс методов удаления в контроллерах

This commit is contained in:
abazov73 2023-04-07 15:13:22 +04:00
parent 5ce425651d
commit 84a1e0d0fc
7 changed files with 105 additions and 6 deletions

View File

@ -73,10 +73,14 @@ namespace BankRestApi.Controllers
}
}
[HttpDelete]
public void DeleteDeal(DealBindingModel model)
public void DeleteDeal(int dealId)
{
try
{
DealBindingModel model = new DealBindingModel
{
Id = dealId,
};
_deal.Delete(model);
}
catch (Exception ex)

View File

@ -47,10 +47,14 @@ namespace BankRestApi.Controllers
}
}
[HttpDelete]
public void DeleteOperator(OperatorBindingModel model)
public void DeleteOperator(int operatorId)
{
try
{
OperatorBindingModel model = new OperatorBindingModel
{
Id = operatorId
};
_operator.Delete(model);
}
catch (Exception ex)

View File

@ -73,10 +73,14 @@ namespace BankRestApi.Controllers
}
}
[HttpDelete]
public void DeletePayment(PaymentBindingModel model)
public void DeletePayment(int paymentId)
{
try
{
PaymentBindingModel model = new PaymentBindingModel
{
Id = paymentId
};
_payment.Delete(model);
}
catch (Exception ex)

View File

@ -74,10 +74,14 @@ namespace BankRestApi.Controllers
}
}
[HttpDelete]
public void DeleteTransfer(TransferBindingModel model)
public void DeleteTransfer(int transferId)
{
try
{
TransferBindingModel model = new TransferBindingModel
{
Id = transferId
};
_transfer.Delete(model);
}
catch (Exception ex)

View File

@ -0,0 +1,74 @@
using System.Net.Http.Headers;
using System.Text;
using BankContracts.ViewModels;
using Newtonsoft.Json;
namespace OperatorApp
{
public class APIClientcs
{
private static readonly HttpClient _client = new();
public static OperatorViewModel? Operator { get; set; } = null;
public static void Connect(IConfiguration configuration)
{
_client.BaseAddress = new Uri(configuration["IPAddress"]);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public static T? GetRequest<T>(string requestUrl)
{
var response = _client.GetAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (response.Result.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception(result);
}
}
public static void PostRequest<T>(string requestUrl, T model)
{
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var response = _client.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
}
public static void PatchRequest<T>(string requestUrl, T model)
{
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var response = _client.PatchAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
}
public static void DeleteRequest<T>(string requestUrl)
{
var response = _client.DeleteAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BankContracts\BankContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"IPAddress": "http://localhost:5225"
}