using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using VeterinaryContracts.BindingModels; using VeterinaryContracts.ViewModels; using VeterinaryShowOwnerApp.Models; using VeterinaryShowOwnerApp; using System.Reflection; using VeterinaryDataModels.Models; using VeterinaryContracts.SearchModels; using Microsoft.Extensions.Hosting; using System.Globalization; namespace VeterinaryShowOwnerApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } return View(APIOwner.GetRequest>($"api/pet/getpets?ownerId={APIOwner.Owner.Id}")); } [HttpGet] public IActionResult Privacy() { 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/owner/register", new OwnerBindingModel { OwnerFIO = fio, Login = login, Password = password }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult Pets() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } return View(APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}")); } public IActionResult CreatePet() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Medications = APIOwner.GetRequest>($"api/Medication/GetMedications?doctorid={APIOwner.Owner.Id}"); return View(); } [HttpPost] public void CreatePet(string name, string type, string breed, string gender, int doctor) { 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, MedicationId = doctor }); Response.Redirect("Index"); } public IActionResult DeletePet() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Pets = 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}"); ViewBag.Medications = APIOwner.GetRequest>($"api/Medication/GetMedications?doctorid={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, }); 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; } return result; } [HttpGet] public IActionResult Visits() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } return View(APIOwner.GetRequest>($"api/visit/getvisits?ownerid={APIOwner.Owner.Id}")); } public IActionResult CreateVisit() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}"); ViewBag.Doctors = APIOwner.GetRequest>($"api/doctor/getdoctors"); return View(); } [HttpPost] public void CreateVisit(string name, List pets, DateTime dateTime, int doctor) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(name) || dateTime < DateTime.Now) { throw new Exception("Ошибка в введенных данных"); } Dictionary a = new Dictionary(); foreach (int pet in pets) { a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); } APIOwner.PostRequest("api/visit/createvisit", new VisitBindingModel { VisitName = name, VisitPet = a, DateVisit = dateTime, DoctorId = doctor, OwnerId = APIOwner.Owner.Id, }); Response.Redirect("Visits"); } public IActionResult DeleteVisit() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Visits = APIOwner.GetRequest>($"api/visit/getvisits?ownerid={APIOwner.Owner.Id}"); return View(); } [HttpPost] public void DeleteVisit(int visit) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIOwner.PostRequest("api/visit/deletevisit", new VisitBindingModel { Id = visit }); Response.Redirect("Visits"); } public IActionResult UpdateVisit() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Visits = APIOwner.GetRequest>($"api/visit/getvisits?ownerid={APIOwner.Owner.Id}"); return View(); } [HttpPost] public void UpdateVisit(int visit, string name) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Ошибка в введенных данных"); } APIOwner.PostRequest("api/visit/updatevisit", new VisitBindingModel { Id = visit, VisitName = name, OwnerId = APIOwner.Owner.Id, }); Response.Redirect("Visits"); } [HttpGet] public Tuple? GetVisit(int visitId) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } var result = APIOwner.GetRequest>($"api/visit/getvisit?visitid={visitId}"); if (result == null) { return default; } return result; } public IActionResult Purchases() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } return View(APIOwner.GetRequest>($"api/purchase/getpurchases?ownerid={APIOwner.Owner.Id}")); } public IActionResult CreatePurchase() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}"); ViewBag.Drugs = APIOwner.GetRequest>($"api/drug/getdrugs?doctorid={null}"); return View(); } [HttpPost] public void CreatePurchase(int drug, string name, List pets, int count) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } Dictionary a = new Dictionary(); foreach (int pet in pets) { a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); } APIOwner.PostRequest("api/purchase/createpurchase", new PurchaseBindingModel { OwnerId = APIOwner.Owner.Id, DrugId = drug, PuchaseName = name, PurchasePet = a, DateCreate = DateTime.Now }); Response.Redirect("Purchases"); } [HttpPost] public double Calc(int count, int drug) { var price = APIOwner.GetRequest($"api/drug/getonedrug?drugId={drug}"); return count * (price?.Price ?? 1); } [HttpGet] public Tuple>? GetPurchase(int purchaseId) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } var result = APIOwner.GetRequest>>($"api/purchase/getpurchase?purchaseid={purchaseId}"); if (result == null) { return default; } return result; } [HttpGet] public IActionResult ServicePetReport() { if (APIOwner.Owner == null) { return Redirect("~/Home/Enter"); } ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets?ownerid={APIOwner.Owner.Id}"); return View(); } [HttpPost] public void ServicePetReport(List pets, string type) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (pets.Count <= 0) { throw new Exception("Количество должно быть больше 0"); } if (string.IsNullOrEmpty(type)) { throw new Exception("Неверный тип отчета"); } if (type == "docx") { APIOwner.PostRequest("api/reportowner/createservicelistwordfile", new ReportServicesBindingModel { Pets = pets, FileName = "D:\\U on Drive\\4 semester\\RPP2\\Cursovaya\\github\\wordfile.docx" }); Response.Redirect("GetWordFile"); } else { APIOwner.PostRequest("api/reportowner/createservicelistexcelfile", new ReportServicesBindingModel { Pets = pets, FileName = "D:\\U on Drive\\4 semester\\RPP2\\Cursovaya\\github\\excelfile.xlsx" }); Response.Redirect("GetExcelFile"); } } [HttpGet] public IActionResult GetWordFile() { return new PhysicalFileResult("D:\\U on Drive\\4 semester\\RPP2\\Cursovaya\\github\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); } public IActionResult GetExcelFile() { return new PhysicalFileResult("D:\\U on Drive\\4 semester\\RPP2\\Cursovaya\\github\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } [HttpGet] public IActionResult Report() { ViewBag.Report = new List(); return View(); } [HttpGet] public string GetPetsReport(DateTime dateFrom, DateTime dateTo) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } List result; try { string dateFromS = dateFrom.ToString("s", CultureInfo.InvariantCulture); string dateToS = dateTo.ToString("s", CultureInfo.InvariantCulture); result = APIOwner.GetRequest> ($"api/reportowner/getvisitsdrugsreport?datefrom={dateFromS}&dateto={dateToS}&ownerid={APIOwner.Owner.Id}")!; } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания отчета"); throw; } string table = ""; table += "

Предварительный отчет

"; table += "
"; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; table += ""; foreach (var visit in result) //foreach (var medication in result) { table += ""; table += ""; table += $""; table += $""; table += $""; table += $""; table += ""; foreach (var medicament in visit.Medicaments) { table += ""; table += $""; table += $""; table += $""; table += $""; table += ""; } foreach (var drug in visit.Purchases) { table += ""; table += $""; table += $""; table += $""; table += $""; table += ""; } table += ""; } table += "
ДатаВизитМедикаментРекомендация
{visit.PetName}
{medicament.MedicationName}
{drug.DateCreate}{drug.PuchaseName}
"; table += "
"; return table; } [HttpPost] public void Report(DateTime dateFrom, DateTime dateTo) { if (APIOwner.Owner == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } APIOwner.PostRequest("api/reportowner/sendvisitsdrugsreporttoemail", new ReportVisitsDrugsBindingModel { FileName = "D:\\U on Drive\\4 semester\\RPP2\\Cursovaya\\github\\pdffile.pdf", OwnerId = APIOwner.Owner.Id, DateFrom = dateFrom, DateTo = dateTo, Email = APIOwner.Owner.Login }); Response.Redirect("Report"); } } }