antoc0der 2024-04-27 15:50:08 +04:00
commit 1e2ec0f0a4
12 changed files with 512 additions and 16 deletions

View File

@ -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<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);
}
}
}
}

View File

@ -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<List<PetViewModel>>($"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<OwnerViewModel>($"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<List<PetViewModel>>($"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<List<PetViewModel>>($"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<PetViewModel, string>? GetPet(int petId)
{
if (APIOwner.Owner == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
var result = APIOwner.GetRequest<Tuple<PetViewModel, List<string>>>($"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 += "<tr>";
table += $"<td>{animal}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
}
}

View File

@ -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())
{

View File

@ -6,4 +6,24 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Content Remove="Views\Home\CreatePet.cshtml" />
<Content Remove="Views\Home\DeletePet.cshtml" />
<Content Remove="Views\Home\UpdatePet.cshtml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Views\Home\CreatePet.cshtml" />
<Compile Include="Views\Home\DeletePet.cshtml" />
<Compile Include="Views\Home\UpdatePet.cshtml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VeterinaryContracts\VeterinaryContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,39 @@
@{
ViewData["Title"] = "CreatePet";
}
<div class="text-center">
<h2 class="display-4">Создание животного</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Кличка:</div>
<div class="col-8">
<input type="text" name="name" id="name" />
</div>
</div>
<div class="row">
<div class="col-4">Вид:</div>
<div class="col-8">
<input type="text" name="type" id="type" />
</div>
</div>
<div class="row">
<div class="col-4">Порода:</div>
<div class="col-8">
<input type="text" name="breed" id="breed" />
</div>
</div>
<div class="row">
<div class="col-4">Пол:</div>
<div class="col-8">
<input type="text" name="gender" id="gender" />
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4">
<input type="submit" value="Создать" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -0,0 +1,18 @@
@{
ViewData["Title"] = "DeletePet";
}
<div class="text-center">
<h2 class="display-4">Удаление животного</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Животное:</div>
<div class="col-8">
<select id="pet" name="pet" class="form-control" asp-items="@(new SelectList(@ViewBag.Pets, "Id", "PetName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4"></div>
<div class="col-8"><input type="submit" value="Удалить" class="btn btn-danger" /></div>
</div>
</form>

View File

@ -0,0 +1,20 @@
@{
ViewData["Title"] = "Enter";
}
<div class="text-center">
<h2 class="display-4">Вход в приложение</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Login:</div>
<div class="col-8"><input type="text" name="login" /></div>
</div>
<div class="row">
<div class="col-4">Пароль:</div>
<div class="col-8"><input type="password" name="password" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Вход" class="btn btnprimary" /></div>
</div>
</form>

View File

@ -1,8 +1,70 @@
@{
ViewData["Title"] = "Home Page";
@using VeterinaryContracts.ViewModels
@model List<PetViewModel>
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<h1 class="display-4">Животное</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<p>
<a asp-action="CreateMedicine">Создать животное</a>
<a asp-action="UpdateMedicine">Обновить животное</a>
<a asp-action="DeleteMedicine">Удалить животное</a>
</p>
<table class="table">
<thead>
<tr>
<th>
Номер
</th>
<th>
Кличка
</th>
<th>
Вид
</th>
<th>
Порода
</th>
<th>
Пол
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem =>
item.Id)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.PetName)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.PetType)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.PetBreed)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.PetGender)
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -1,6 +1,28 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
@using VeterinaryContracts.ViewModels
<p>Use this page to detail your site's privacy policy.</p>
@model OwnerViewModel
@{
ViewData["Title"] = "Privacy Policy";
}
<div class="text-center">
<h2 class="display-4">Личные данные</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Login:</div>
<div class="col-8"><input type="text" name="email" value="@Model.Login" /></div>
</div>
<div class="row">
<div class="col-4">Пароль:</div>
<div class="col-8"><input type="password" name="password" value="@Model.Password" /></div>
</div>
<div class="row">
<div class="col-4">ФИО:</div>
<div class="col-8"><input type="text" name="fio" value="@Model.OwnerFIO" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
</div>
</form>

View File

@ -0,0 +1,28 @@
@{
ViewData["Title"] = "Register";
}
<div class="text-center">
<h2 class="display-4">Регистрация</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Login:</div>
<div class="col-8"><input type="text" name="login" /></div>
</div>
<div class="row">
<div class="col-4">Пароль:</div>
<div class="col-8"><input type="password" name="password" /></div>
</div>
<div class="row">
<div class="col-4">ФИО:</div>
<div class="col-8"><input type="text" name="fio" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4">
<input type="submit" value="Регистрация"
class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -0,0 +1,58 @@
@{
ViewData["Title"] = "UpdateMedicine";
}
<div class="text-center">
<h2 class="display-4">Редактирование животного</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Животное:</div>
<div class="col-8">
<select id="pet" name="pet" class="form-control" asp-items="@(new SelectList(@ViewBag.Pets, "Id", "PetName"))"></select>
</div>
</div>
<div class="row">
<div class="col-4">Кличка:</div>
<div class="col-8"><input type="text" name="name" id="name" class="form-control" /></div>
</div>
<div class="row">
<div class="col-4">Вид:</div>
<div class="col-8"><input type="text" id="type" name="type" class="form-control" /></div>
</div>
<div class="row">
<div class="col-4">Порода:</div>
<div class="col-8"><input type="text" id="breed" name="breed" class="form-control" /></div>
</div>
<div class="row">
<div class="col-4">Пол:</div>
<div class="col-8"><input type="text" id="gender" name="gender" class="form-control" /></div>
</div>
</form>
@section Scripts
{
<script>
function check() {
var pet = $('#pet').val();
if (pet) {
$.ajax({
method: "GET",
url: "/Home/GetPet",
data: { petId: pet },
success: function (result) {
$('#name').val(result.item1.petName);
$('#type').val(result.item1.petType);
$('#breed').val(result.item1.petBreed);
$('#gender').val(result.item1.petGender);
}
});
};
}
check();
$('#pet').on('change', function () {
check();
});
</script>
}

View File

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