API Client и что-то еще...

This commit is contained in:
Allllen4a 2024-04-30 19:38:49 +04:00
parent 34a004158e
commit 0365a4074f
3 changed files with 92 additions and 104 deletions

View File

@ -1,82 +1,67 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BeautySalonContracts.ViewModels;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
namespace ClientApp
namespace BeutySalonClientApp
{
public class APIClient : Controller
public class APIClient
{
// GET: APIClient
public ActionResult Index()
private static readonly HttpClient _client = new();
public static ClientViewModel? Client { get; set; } = null;
public static void Connect(IConfiguration configuration)
{
return View();
_client.BaseAddress = new Uri(configuration["IPAddress"]!);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
// GET: APIClient/Details/5
public ActionResult Details(int id)
public static T? GetRequest<T>(string requestUrl)
{
return View();
}
// GET: APIClient/Create
public ActionResult Create()
{
return View();
}
// POST: APIClient/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
var response = _client.GetAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(requestUrl);
if (response.Result.IsSuccessStatusCode)
{
return RedirectToAction(nameof(Index));
return JsonConvert.DeserializeObject<T>(result);
}
catch
else
{
return View();
throw new Exception(result);
}
}
// GET: APIClient/Edit/5
public ActionResult Edit(int id)
public static void PostRequest<T>(string requestUrl, T model)
{
return View();
}
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json");
// POST: APIClient/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
var response = _client.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
throw new Exception(result);
}
}
// GET: APIClient/Delete/5
public ActionResult Delete(int id)
public static O? PostRequestWithResult<I, O>(string requestUrl, I model)
{
return View();
}
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json");
// POST: APIClient/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
var response = _client.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (response.Result.IsSuccessStatusCode)
{
return RedirectToAction(nameof(Index));
return JsonConvert.DeserializeObject<O>(result);
}
catch
else
{
return View();
return default;
}
}
}

View File

@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BeautySalonContracts\BeautySalonContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -1,83 +1,78 @@
using Microsoft.AspNetCore.Http;
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.ViewModels;
using ClientApp;
using Microsoft.AspNetCore.Mvc;
namespace ClientApp.Controllers
namespace BeutySalonClientApp.Controllers
{
public class EvaluationController : Controller
{
// GET: EvaluationController
public ActionResult Index()
public IActionResult Create()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedure = APIClient.GetRequest<List<ProcedureViewModel>>($"api/procedure/getall?userId={APIClient.Client.Id}");
return View();
}
// GET: EvaluationController/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: EvaluationController/Create
public ActionResult Create()
{
return View();
}
// POST: EvaluationController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
public void Create(string pointsProcedure, string pointsCosmetics, int procedureId)
{
try
if (APIClient.Client == null)
{
return RedirectToAction(nameof(Index));
throw new Exception("403");
}
catch
APIClient.PostRequest("api/rating/create", new EvaluationBindingModel
{
return View();
}
ClientId = APIClient.Client.Id,
PointsProcedure = double.Parse(pointsProcedure.Replace(".", ",")),
PointsCosmetics = double.Parse(pointsCosmetics.Replace(".", ",")),
ProcedureId = procedureId
});
Response.Redirect("/Home/Rating");
}
// GET: EvaluationController/Edit/5
public ActionResult Edit(int id)
public IActionResult Update(int id)
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Evaluation = APIClient.GetRequest<EvaluationBindingModel>
($"api/rating/get?id={id}");
ViewBag.Procedure = APIClient.GetRequest<List<ProcedureViewModel>>($"api/procedure/getall?userId={APIClient.Client.Id}");
return View();
}
// POST: EvaluationController/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
public void Update(int id, string pointsProcedure, string pointsCosmetics, int procedureId)
{
try
if (APIClient.Client == null)
{
return RedirectToAction(nameof(Index));
throw new Exception("403");
}
catch
APIClient.PostRequest("api/rating/update", new EvaluationBindingModel
{
return View();
}
Id = id,
PointsProcedure = double.Parse(pointsProcedure.Replace(".", ",")),
PointsCosmetics = double.Parse(pointsCosmetics.Replace(".", ",")),
ProcedureId = procedureId
});
Response.Redirect("/Home/Evaluation");
}
// GET: EvaluationController/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: EvaluationController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
public void Delete(int id)
{
try
if (APIClient.Client == null)
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
return;
}
APIClient.PostRequest($"api/evaluation/delete",
new ServiceBindingModel() { Id = id });
return;
}
}
}