Yunusov_Niyaz 2024-04-27 15:49:05 +04:00
commit 2ee4a9315a
6 changed files with 262 additions and 6 deletions

View File

@ -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,

View File

@ -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<T>(string requestUrl)
{
var response = _doctor.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 = _doctor.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
}
}
}

View File

@ -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<List<MedicationViewModel>>($"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<DoctorViewModel>($"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<List<MedicationViewModel>>($"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<List<MedicationViewModel>>($"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()

View File

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

View File

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

View File

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