diff --git a/VeterinaryView/VeterinaryShowOwnerApp/APIOwner.cs b/VeterinaryView/VeterinaryShowOwnerApp/APIOwner.cs new file mode 100644 index 0000000..b726146 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/APIOwner.cs @@ -0,0 +1,44 @@ +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryShowOwnerApp +{ + public static class APIOwner + { + private static readonly HttpClient _client = new(); + public static OwnerViewModel? Owner { 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(string requestUrl) + { + var response = _client.GetAsync(requestUrl); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + throw new Exception(result); + } + } + public static void PostRequest(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); + } + } + } +} diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs index 8892729..c994250 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.ViewModels; using VeterinaryShowOwnerApp.Models; namespace VeterinaryShowOwnerApp.Controllers @@ -15,18 +17,198 @@ namespace VeterinaryShowOwnerApp.Controllers public IActionResult Index() { - return View(); + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + return + View(APIOwner.GetRequest>($"api/main/getpets?ownerId={APIOwner.Owner.Id}")); } - + [HttpGet] public IActionResult Privacy() { - return View(); + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIOwner.Owner); + } + [HttpPost] + public void Privacy(string login, string password, string fio) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIOwner.PostRequest("api/owner/updatedata", new OwnerBindingModel + { + Id = APIOwner.Owner.Id, + OwnerFIO = fio, + Login = login, + Password = password + }); + APIOwner.Owner.OwnerFIO = fio; + APIOwner.Owner.Login = login; + APIOwner.Owner.Password = password; + Response.Redirect("Index"); } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } + [HttpGet] + public IActionResult Enter() + { + return View(); + } + [HttpPost] + public void Enter(string login, string password) + { + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIOwner.Owner = APIOwner.GetRequest($"api/owner/login?login={login}&password={password}"); + if (APIOwner.Owner == null) + { + throw new Exception("Неверный логин/пароль"); + } + Response.Redirect("Index"); + } + [HttpGet] + public IActionResult Register() + { + return View(); + } + [HttpPost] + public void Register(string login, string password, string fio) + { + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIOwner.PostRequest("api/client/register", new + OwnerBindingModel + { + OwnerFIO = fio, + Login = login, + Password = password + }); + Response.Redirect("Enter"); + return; + } + public IActionResult CreatePet() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + [HttpPost] + public void CreatePet(string name, string type, string breed, string gender) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(breed) || string.IsNullOrEmpty(gender)) + { + throw new Exception("Ошибка введённых данных"); + } + APIOwner.PostRequest("api/pet/createpet", new PetBindingModel + { + PetName = name, + PetType = type, + PetBreed = breed, + PetGender = gender, + OwnerId = APIOwner.Owner.Id + }); + Response.Redirect("Index"); + } + public IActionResult DeletePet() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Owners = APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}"); + return View(); + } + [HttpPost] + public void DeletePet(int pet) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIOwner.PostRequest("api/pet/deletepet", new PetBindingModel + { + Id = pet + }); + Response.Redirect("Index"); + } + public IActionResult UpdatePet() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}"); + return View(); + } + [HttpPost] + public void UpdatePet(int pet, string name, string type, string breed, string gender) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(breed) || string.IsNullOrEmpty(gender)) + { + throw new Exception("Ошибка введённых данных"); + } + APIOwner.PostRequest("api/pet/updatepet", new PetBindingModel + { + Id = pet, + PetName = name, + PetType = type, + PetBreed = breed, + PetGender = gender, + OwnerId = APIOwner.Owner.Id, + }); + Response.Redirect("Index"); + } + [HttpGet] + public Tuple? GetPet(int petId) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + var result = APIOwner.GetRequest>>($"api/pet/getpet?petid={petId}"); + if (result == null) + { + return default; + } + string table = ""; + result.Item1.MedicineAnimals.Clear(); + for (int i = 0; i < result.Item2.Count; i++) + { + var animal = result.Item2[i]; + table += ""; + table += $"{animal}"; + table += ""; + } + return Tuple.Create(result.Item1, table); + } } } \ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Program.cs b/VeterinaryView/VeterinaryShowOwnerApp/Program.cs index 559dd3a..5f76ad4 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/Program.cs +++ b/VeterinaryView/VeterinaryShowOwnerApp/Program.cs @@ -1,10 +1,12 @@ +using VeterinaryShowOwnerApp; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); - +APIOwner.Connect(builder.Configuration); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj b/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj index c78c9c7..0f888e1 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj +++ b/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj @@ -6,4 +6,24 @@ enable + + + + + + + + + + + + + + + + + + + + diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePet.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePet.cshtml new file mode 100644 index 0000000..8a1eb39 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePet.cshtml @@ -0,0 +1,39 @@ +@{ + ViewData["Title"] = "CreatePet"; +} + +
+

Создание животного

+
+
+
+
Кличка:
+
+ +
+
+
+
Вид:
+
+ +
+
+
+
Порода:
+
+ +
+
+
+
Пол:
+
+ +
+
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/DeletePet.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/DeletePet.cshtml new file mode 100644 index 0000000..dfb5159 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/DeletePet.cshtml @@ -0,0 +1,18 @@ +@{ + ViewData["Title"] = "DeletePet"; +} +
+

Удаление животного

+
+
+
+
Животное:
+
+ +
+
+
+
+
+
+
\ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Enter.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Enter.cshtml new file mode 100644 index 0000000..5775fac --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Enter.cshtml @@ -0,0 +1,20 @@ +@{ + ViewData["Title"] = "Enter"; +} +
+

Вход в приложение

+
+
+
+
Login:
+
+
+
+
Пароль:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Index.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Index.cshtml index d2d19bd..30437d6 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Index.cshtml +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Index.cshtml @@ -1,8 +1,70 @@ -@{ - ViewData["Title"] = "Home Page"; +@using VeterinaryContracts.ViewModels +@model List +@{ + ViewData["Title"] = "Home Page"; } -
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

Животное

+
+
+ @{ + if (Model == null) + { +

Авторизируйтесь

+ return; + } +

+ Создать животное + Обновить животное + Удалить животное +

+ + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + } + +
+ Номер + + Кличка + + Вид + + Порода + + Пол +
+ @Html.DisplayFor(modelItem => + item.Id) + + @Html.DisplayFor(modelItem => + item.PetName) + + @Html.DisplayFor(modelItem => + item.PetType) + + @Html.DisplayFor(modelItem => + item.PetBreed) + + @Html.DisplayFor(modelItem => + item.PetGender) +
+ }
diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Privacy.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Privacy.cshtml index af4fb19..75d10bb 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Privacy.cshtml +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Privacy.cshtml @@ -1,6 +1,28 @@ -@{ - ViewData["Title"] = "Privacy Policy"; -} -

@ViewData["Title"]

+@using VeterinaryContracts.ViewModels -

Use this page to detail your site's privacy policy.

+@model OwnerViewModel + +@{ + ViewData["Title"] = "Privacy Policy"; +} +
+

Личные данные

+
+
+
+
Login:
+
+
+
+
Пароль:
+
+
+
+
ФИО:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Register.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Register.cshtml new file mode 100644 index 0000000..e340961 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/Register.cshtml @@ -0,0 +1,28 @@ +@{ + ViewData["Title"] = "Register"; +} + +
+

Регистрация

+
+
+
+
Login:
+
+
+
+
Пароль:
+
+
+
+
ФИО:
+
+
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/UpdatePet.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/UpdatePet.cshtml new file mode 100644 index 0000000..e1d99d3 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/UpdatePet.cshtml @@ -0,0 +1,58 @@ +@{ + ViewData["Title"] = "UpdateMedicine"; +} + +
+

Редактирование животного

+
+
+
+
Животное:
+
+ +
+
+
+
Кличка:
+
+
+
+
Вид:
+
+
+
+
Порода:
+
+
+
+
Пол:
+
+
+ +
+ +@section Scripts +{ + +} \ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/appsettings.json b/VeterinaryView/VeterinaryShowOwnerApp/appsettings.json index 10f68b8..b7c7c4d 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/appsettings.json +++ b/VeterinaryView/VeterinaryShowOwnerApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "IPAddress": "http://localhost:" }