Implement controller
This commit is contained in:
parent
2aace65848
commit
c9d251a794
60
AutoWorkshopShopApp/ApiClient.cs
Normal file
60
AutoWorkshopShopApp/ApiClient.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace AutoWorkshopShopApp
|
||||
{
|
||||
public class ApiClient
|
||||
{
|
||||
private static readonly HttpClient _client = new();
|
||||
|
||||
public static string? Password { get; set; }
|
||||
|
||||
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 DeleteRequest(string RequestUrl)
|
||||
{
|
||||
var Response = _client.DeleteAsync(RequestUrl);
|
||||
var Result = Response.Result.Content.ReadAsStringAsync().Result;
|
||||
|
||||
if (!Response.Result.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception(Result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -6,4 +6,13 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AutoWorkshopContracts\AutoWorkshopContracts.csproj" />
|
||||
<ProjectReference Include="..\AutoWorkshopDataModels\AutoWorkshopDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,4 +1,6 @@
|
||||
using AutoWorkshopShopApp.Models;
|
||||
using AutoWorkshopContracts.BindingModels;
|
||||
using AutoWorkshopContracts.ViewModels;
|
||||
using AutoWorkshopShopApp.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Diagnostics;
|
||||
|
||||
@ -8,21 +10,136 @@ namespace AutoWorkshopShopApp.Controllers
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
public HomeController(ILogger<HomeController> Logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_logger = Logger;
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
if (ApiClient.Password == null)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
return View(ApiClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist?password={ApiClient.Password}"));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Enter()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
[HttpPost]
|
||||
public void Enter(string Password)
|
||||
{
|
||||
bool ResOut = ApiClient.GetRequest<bool>($"/api/shop/authentication?password={Password}");
|
||||
if (!ResOut)
|
||||
{
|
||||
Response.Redirect("../Home/Enter");
|
||||
return;
|
||||
}
|
||||
ApiClient.Password = Password;
|
||||
Response.Redirect("Index");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Create()
|
||||
{
|
||||
if (ApiClient.Password == null)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
return View("Shop");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void Create(int Id, string ShopName, string Address, DateTime OpeningDate, int MaxCount)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address))
|
||||
{
|
||||
throw new Exception("Название или адрес не может быть пустым");
|
||||
}
|
||||
if (OpeningDate == default(DateTime))
|
||||
{
|
||||
throw new Exception("Дата открытия не может быть пустой");
|
||||
}
|
||||
|
||||
ApiClient.PostRequest($"api/shop/createshop?password={ApiClient.Password}", new ShopBindingModel
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
OpeningDate = OpeningDate,
|
||||
RepairsMaxCount = MaxCount
|
||||
});
|
||||
Response.Redirect("Index");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Update(int Id)
|
||||
{
|
||||
if (ApiClient.Password == null)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
return View("Shop", ApiClient.GetRequest<ShopRepairViewModel>($"api/shop/getshop?shopId={Id}&password={ApiClient.Password}"));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void Update(int Id, string ShopName, string Address, DateTime OpeningDate, int MaxCount)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address))
|
||||
{
|
||||
throw new Exception("Название или адрес не может быть пустым");
|
||||
}
|
||||
if (OpeningDate == default(DateTime))
|
||||
{
|
||||
throw new Exception("Дата открытия не может быть пустой");
|
||||
}
|
||||
ApiClient.PostRequest($"api/shop/updateshop?password={ApiClient.Password}", new ShopBindingModel
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
OpeningDate = OpeningDate,
|
||||
RepairsMaxCount = MaxCount
|
||||
});
|
||||
Response.Redirect("../Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void Delete(int Id)
|
||||
{
|
||||
ApiClient.DeleteRequest($"api/shop/deleteshop?shopId={Id}&password={ApiClient.Password}");
|
||||
Response.Redirect("../Index");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Supply()
|
||||
{
|
||||
if (ApiClient.Password == null)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
|
||||
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist?password={ApiClient.Password}");
|
||||
ViewBag.Pizzas = ApiClient.GetRequest<List<RepairViewModel>>($"api/main/getpizzalist");
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void Supply(int Shop, int Repair, int Count)
|
||||
{
|
||||
ApiClient.PostRequest($"api/shop/makesypply?password={ApiClient.Password}", new SupplyBindingModel
|
||||
{
|
||||
ShopId = Shop,
|
||||
RepairId = Repair,
|
||||
Count = Count
|
||||
});
|
||||
Response.Redirect("Index");
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
|
@ -1,27 +1,30 @@
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
using AutoWorkshopShopApp;
|
||||
|
||||
var Builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllersWithViews();
|
||||
Builder.Services.AddControllersWithViews();
|
||||
|
||||
var app = builder.Build();
|
||||
var App = Builder.Build();
|
||||
ApiClient.Connect(Builder.Configuration);
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
if (!App.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
App.UseExceptionHandler("/Home/Error");
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
App.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
App.UseHttpsRedirection();
|
||||
App.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
App.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
App.UseAuthorization();
|
||||
|
||||
app.MapControllerRoute(
|
||||
App.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||
|
||||
app.Run();
|
||||
App.Run();
|
||||
|
@ -5,5 +5,6 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"IPAddress": "http://localhost:5224/"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user