using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PostgreSqlDotnetCore.Models; using Microsoft.AspNetCore.Mvc.Rendering; using System.Threading.Tasks; using Npgsql; using PostgreSqlDotnetCore.Data; namespace PostgreSqlDotnetCore.Controllers { public class VetCenterController : BaseController { /* public VetCenterController(UserManager userManager) : base(userManager) { } */ private readonly ApplicationDbContext db; public VetCenterController(UserManager userManager, ApplicationDbContext context) : base(userManager) { db = context ?? throw new ArgumentNullException(nameof(context)); } public async Task Create() { // Set if user is authenticated UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); ViewBag.isAuthenticated = await getCrrentUser(); if (customerClass == null) { return RedirectToAction("AccessDenied", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); // Fetch cities for dropdown var citiess = await db.CitiesObj.ToListAsync(); ViewBag.Citiess = new SelectList(citiess, "id", "name"); return View(); } /* public async Task Index() { var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.isAuthenticated = User.Identity.IsAuthenticated; // Check if the user is an admin UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); ViewBag.hasAccess = customerClass != null; return View(vetCenters); }*/ /* public async Task Index() { var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.isAuthenticated = User.Identity.IsAuthenticated; // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); // Проверете дали корисникот е администратор или менаџер UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); ViewBag.hasAccess = customerClass != null; return View(vetCenters); } */ public async Task Index() { ViewBag.isAuthenticated = await getCrrentUser(); ViewBag.hasAccess = await checkAuthorizationAsync(); var vetCenters = await db.VetCentersWithCity.ToListAsync(); // ViewBag.isAuthenticated = User.Identity.IsAuthenticated; // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); ViewBag.hasAccess = customerClass != null; return View(vetCenters); } /*public async Task Details(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } VetCenter vetClass = await db.VetCentersObj.FindAsync(id); UsersClass customerClass = await getCrrentUser(); ViewBag.isAuthenticated = customerClass; if (vetClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(vetClass); } */ public async Task Details(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } // Логирајте го ID-то за дебугирање Console.WriteLine($"ID: {id}"); // Обидете се да најдете запис во view VetCenterWithCity vetClass = await db.VetCentersWithCity .Where(v => v.id == id) .FirstOrDefaultAsync(); if (vetClass == null) { return RedirectToAction("NotExist", "Error"); } UsersClass customerClass = await getCrrentUser(); ViewBag.isAuthenticated = customerClass; return View(vetClass); } [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass) { if (ModelState.IsValid) { db.VetCentersObj.Add(vetClass); await db.SaveChangesAsync(); return RedirectToAction("Index"); } // If model is invalid, repopulate the cities for dropdown var citiess = await db.CitiesObj.ToListAsync(); ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); return View(vetClass); } /*public async Task Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass) { // Логирање на параметрите Console.WriteLine($"Parameters: {vetClass.name}, {vetClass.adress}, {vetClass.description}, {vetClass.workinghours}, {vetClass.phonenumber}, {vetClass.latitude}, {vetClass.longitude}, {vetClass.citiesid}"); if (ModelState.IsValid) { // Повик на складираната процедура var parameters = new[] { new NpgsqlParameter("@name", vetClass.name), new NpgsqlParameter("@adress", vetClass.adress), new NpgsqlParameter("@description", vetClass.description), new NpgsqlParameter("@workinghours", vetClass.workinghours), new NpgsqlParameter("@phonenumber", vetClass.phonenumber), new NpgsqlParameter("@latitude", (decimal)vetClass.latitude), new NpgsqlParameter("@longitude", (decimal)vetClass.longitude), new NpgsqlParameter("@citiesid", vetClass.citiesid) }; await db.Database.ExecuteSqlRawAsync("CALL project.AddVetCenter(@name, @adress, @description, @workinghours, @phonenumber, @latitude, @longitude, @citiesid)", parameters); return RedirectToAction("Index"); } // Ако моделот не е валиден, повторно пополнете ги градовите за паѓачкиот мени var citiess = await db.CitiesObj.ToListAsync(); ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); return View(vetClass); } */ public async Task Edit(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } VetCenter vetClass = await db.VetCentersObj.FindAsync(id); if (vetClass == null) { return RedirectToAction("NotExist", "Error"); } // Check for permission UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); //UsersClass customerClass = await checkAuthorizationAsync(); ViewBag.isAuthenticated = await getCrrentUser(); if (customerClass == null) { return RedirectToAction("AccessDenied", "Error"); } ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); // Fetch cities for dropdown var citiess = await db.CitiesObj.ToListAsync(); ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); return View(vetClass); } [HttpPost] [ValidateAntiForgeryToken] public async Task Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass) { if (ModelState.IsValid) { db.Entry(vetClass).State = EntityState.Modified; await db.SaveChangesAsync(); return RedirectToAction("Index"); } // If model is invalid, repopulate the cities for dropdown var citiess = await db.CitiesObj.ToListAsync(); ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); return View(vetClass); } public async Task Delete(int? id) { UsersClass customerClass = await checkAuthorizationAsync(); ViewBag.isAuthenticated = await getCrrentUser(); if (id == null) { return RedirectToAction("NotExist", "Error"); } VetCenter vetClass = await db.VetCentersObj.FindAsync(id); if (vetClass == null) { return RedirectToAction("NotExist", "Error"); } ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(vetClass); } [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { VetCenter vetClass = await db.VetCentersObj.FindAsync(id); db.VetCentersObj.Remove(vetClass); await db.SaveChangesAsync(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } public async Task IndexWithSearch(string searchTerm) { if (string.IsNullOrEmpty(searchTerm)) { var vetCenters = await db.VetCentersObj.ToListAsync(); return View(vetCenters); } else { var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync(); return View(searchResults); } } } }