AdminController
目录
1、 AdminController
1.1、 UpdateFaculty
1.1.1、 // Check if a new image file is provided
1.1.2、 // CHECKING FOLDER EXIST OR NOT - IF NOT THEN CREATE F0LDER
1.1.3、 // READY SEND PATH TO IMAGE TO DB
1.1.4、 DeleteFaculty
1.1.5、 // If the faculty has no associated courses, delete it
using ITM_College.Data;
using ITM_College.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ITM_College.Controllers
{
public class AdminController : Controller
{
private readonly ITM_CollegeContext db;
public AdminController(ITM_CollegeContext db)
{
this.db = db;
}
[HttpPost]
public IActionResult UpdateFaculty(FacultyAndDepartment updatedFaculty, IFormFile img)
{
// Check if the faculty ID is valid
if (updatedFaculty.FacultyTable.FacultyId <= 0)
{
ViewBag.error = "Invalid faculty ID.";
return RedirectToAction("Faculty");
}
// Retrieve the existing faculty from the database
var existingFaculty = db.Faculties.FirstOrDefault(f => f.FacultyId == updatedFaculty.FacultyTable.FacultyId);
if (existingFaculty == null)
{
ViewBag.error = "Faculty not found.";
return RedirectToAction("Faculty");
}
if (img != null && img.Length > 0)
{
// GETTING IMAGE FILE EXTENSION
var fileExt = System.IO.Path.GetExtension(img.FileName).Substring(1);
// GETTING IMAGE NAME
var random = Path.GetFileName(img.FileName);
// GUID ID COMBINE WITH IMAGE NAME - TO ESCAPE IMAGE NAME REDENDNCY
var FileName = Guid.NewGuid() + random;
// GET PATH OF CUSTOM IMAGE FOLDER
string imgFolder = Path.Combine(HttpContext.Request.PathBase.Value, "wwwroot/admincss/Faculty");
if (!Directory.Exists(imgFolder))
{
Directory.CreateDirectory(imgFolder);
}
// MAKING CUSTOM AND COMBINE FOLDER PATH WITH IMAHE
string filepath = Path.Combine(imgFolder, FileName);
// COPY IMAGE TO REAL PATH TO DEVELOPER PATH
using (var stream = new FileStream(filepath, FileMode.Create))
{
img.CopyTo(stream);
}
var dbAddress = Path.Combine("admincss/Faculty", FileName);
// EQUALIZE TABLE (MODEL) PROPERTY WITH CUSTOM PATH
updatedFaculty.FacultyTable.FacultyImg = dbAddress;
//MYIMAGES/imagetodbContext.JGP
existingFaculty.FacultyImg = updatedFaculty.FacultyTable.FacultyImg;
}
// Update other properties of the faculty
existingFaculty.FacultyName = updatedFaculty.FacultyTable.FacultyName;
existingFaculty.FacultyEmail = updatedFaculty.FacultyTable.FacultyEmail;
existingFaculty.FacultyPassword = updatedFaculty.FacultyTable.FacultyPassword;
existingFaculty.Gender = updatedFaculty.FacultyTable.Gender;
existingFaculty.FacultyDepartment = updatedFaculty.FacultyTable.FacultyDepartment;
// Save changes to the database
db.SaveChanges();
ViewBag.message = "Faculty updated successfully.";
return RedirectToAction("Faculty", new { message = ViewBag.message });
}
public IActionResult DeleteFaculty(int id)
{
var faculty = db.Faculties.Include(f => f.Courses).FirstOrDefault(f => f.FacultyId == id);
if (faculty == null)
{
// Faculty not found, handle error
ViewBag.error = "Faculty not found.";
return RedirectToAction("Faculty", new { error = ViewBag.error });
}
if (faculty.Courses.Any())
{
// Faculty cannot be deleted because it has courses associated with it
ViewBag.error = "Cannot delete faculty. It has associated courses.";
return RedirectToAction("Faculty", new { error = ViewBag.error });
}
db.Faculties.Remove(faculty);
db.SaveChanges();
ViewBag.message = "Faculty deleted successfully.";
return RedirectToAction("Faculty", new { message = ViewBag.message });
}
// Faculty Controller End