diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240427075947_InitialCreate.cs b/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240427075947_InitialCreate.cs index e742177..6d65bbd 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240427075947_InitialCreate.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Migrations/20240427075947_InitialCreate.cs @@ -74,7 +74,7 @@ namespace VeterinaryDatabaseImplement.Migrations column: x => x.DoctorId, principalTable: "Doctors", principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( @@ -120,7 +120,7 @@ namespace VeterinaryDatabaseImplement.Migrations column: x => x.DrugId, principalTable: "Drugs", principalColumn: "Id", - onDelete: ReferentialAction.Restrict); + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_Purchases_Owners_OwnerId", column: x => x.OwnerId, @@ -227,7 +227,7 @@ namespace VeterinaryDatabaseImplement.Migrations column: x => x.DoctorId, principalTable: "Doctors", principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_Services_Visits_VisitId", column: x => x.VisitId, diff --git a/VeterinaryView/VeterinaryShowDoctorApp/APIDoctor.cs b/VeterinaryView/VeterinaryShowDoctorApp/APIDoctor.cs new file mode 100644 index 0000000..ebc32a5 --- /dev/null +++ b/VeterinaryView/VeterinaryShowDoctorApp/APIDoctor.cs @@ -0,0 +1,46 @@ +using System.Net.Http.Headers; +using System.Text; +using VeterinaryContracts.ViewModels; +using Newtonsoft.Json; + + +namespace VeterinaryShowDoctorApp +{ + public static class APIDoctor + { + private static readonly HttpClient _doctor = new(); + public static DoctorViewModel? Doctor { get; set; } = null; + public static void Connect(IConfiguration configuration) + { + _doctor.BaseAddress = new Uri(configuration["IPAddress"]); + _doctor.DefaultRequestHeaders.Accept.Clear(); + _doctor.DefaultRequestHeaders.Accept.Add(new + MediaTypeWithQualityHeaderValue("application/json")); + } + public static T? GetRequest(string requestUrl) + { + var response = _doctor.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 = _doctor.PostAsync(requestUrl, data); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) + { + throw new Exception(result); + } + } + } +} diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs index b994ae2..5219ce1 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowDoctorApp/Controllers/HomeController.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using VeterinaryShowDoctorApp.Models; +using VeterinaryContracts.ViewModels; +using VeterinaryContracts.BindingModels; + namespace VeterinaryShowDoctorApp.Controllers { @@ -15,13 +18,208 @@ namespace VeterinaryShowDoctorApp.Controllers public IActionResult Index() { - return View(); + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIDoctor.GetRequest>($"api/medication/getmedications?doctorid={APIDoctor.Doctor.Id}")); } public IActionResult Privacy() + { + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIDoctor.Doctor); + } + + [HttpPost] + public void Privacy(string login, string password, string fio) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIDoctor.PostRequest("api/doctor/updatedata", new DoctorBindingModel + { + Id = APIDoctor.Doctor.Id, + DoctorFIO = fio, + Login = login, + Password = password + }); + APIDoctor.Doctor.DoctorFIO = fio; + APIDoctor.Doctor.Login = login; + APIDoctor.Doctor.Password = password; + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Enter() { return View(); } + [HttpPost] + public void Enter(string login, string password) + { + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIDoctor.Doctor = + APIDoctor.GetRequest($"api/doctor/login?login={login}&password={password}"); + if (APIDoctor.Doctor == 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("Введите логин, пароль и ФИО"); + } + APIDoctor.PostRequest("api/doctor/register", new + DoctorBindingModel + { + DoctorFIO = fio, + Login = login, + Password = password + }); + Response.Redirect("Enter"); + return; + } + + public IActionResult CreateMedication() + { + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpPost] + public void CreateMedication(string name, int price) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name) || price <= 0) + { + throw new Exception("Ошибка в введенных данных"); + } + APIDoctor.PostRequest("api/medication/createmedication", new MedicationBindingModel + { + MedicationName = name, + Price = price, + DoctorId = APIDoctor.Doctor.Id + }); + Response.Redirect("Index"); + } + + + public IActionResult DeleteMedication() + { + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Medications = APIDoctor.GetRequest>($"api/medication/getmedications?doctorid={APIDoctor.Doctor.Id}"); + return View(); + } + + [HttpPost] + public void DeleteMedication(int medication) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIDoctor.PostRequest("api/medication/deletemedication", new MedicationBindingModel + { + Id = medication + }); + Response.Redirect("Index"); + } + + public IActionResult UpdateMedication() + { + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Medications = APIDoctor.GetRequest>($"api/medication/getmedications?doctorid={APIDoctor.Doctor.Id}"); + return View(); + } + + [HttpPost] + public void UpdateMedication(int medication, string name, int price) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name) || price <= 0) + { + throw new Exception("Ошибка в введенных данных"); + } + APIDoctor.PostRequest("api/medication/updatemedication", new MedicationBindingModel + { + Id = medication, + MedicationName = name, + Price = price, + DoctorId = APIDoctor.Doctor.Id + }); + Response.Redirect("Index"); + } + + public IActionResult CreateDrug() + { + if (APIDoctor.Doctor == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpPost] + public void CreateDrug(string name, int price) + { + if (APIDoctor.Doctor == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name) || price <= 0) + { + throw new Exception("Ошибка в введенных данных"); + } + APIDoctor.PostRequest("api/medication/createmedication", new MedicationBindingModel + { + MedicationName = name, + Price = price, + DoctorId = APIDoctor.Doctor.Id + }); + Response.Redirect("Index"); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() diff --git a/VeterinaryView/VeterinaryShowDoctorApp/Program.cs b/VeterinaryView/VeterinaryShowDoctorApp/Program.cs index 559dd3a..4e91254 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/Program.cs +++ b/VeterinaryView/VeterinaryShowDoctorApp/Program.cs @@ -1,10 +1,13 @@ +using VeterinaryShowDoctorApp; + + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); - +APIDoctor.Connect(builder.Configuration); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/VeterinaryView/VeterinaryShowDoctorApp/VeterinaryShowDoctorApp.csproj b/VeterinaryView/VeterinaryShowDoctorApp/VeterinaryShowDoctorApp.csproj index c78c9c7..8a9094f 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/VeterinaryShowDoctorApp.csproj +++ b/VeterinaryView/VeterinaryShowDoctorApp/VeterinaryShowDoctorApp.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/VeterinaryView/VeterinaryShowDoctorApp/appsettings.json b/VeterinaryView/VeterinaryShowDoctorApp/appsettings.json index 10f68b8..a47a897 100644 --- a/VeterinaryView/VeterinaryShowDoctorApp/appsettings.json +++ b/VeterinaryView/VeterinaryShowDoctorApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "IPAddress": "http://localhost:5025/" }