当前位置: 首页 > news >正文

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

  1. AdminController 

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;

}

    1. UpdateFaculty

[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");

}

      1. // Check if a new image file is provided

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");

      1. // CHECKING FOLDER EXIST OR NOT - IF NOT THEN CREATE F0LDER 

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);

}

      1. // READY SEND PATH TO  IMAGE TO DB  

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 });

}

      1. DeleteFaculty

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 });

}

      1. // If the faculty has no associated courses, delete it

db.Faculties.Remove(faculty);

db.SaveChanges();

ViewBag.message = "Faculty deleted successfully.";

return RedirectToAction("Faculty", new { message = ViewBag.message });

}

// Faculty Controller End

http://www.lryc.cn/news/362085.html

相关文章:

  • Vue3-Pinia状态管理器
  • 安装存储器的段描述符并加载GDTR
  • 2024年5月架构试题
  • 品牌控价的同时也要做好数据分析
  • 微服务学习Day11-缓存问题学习
  • 虚拟化知识学习
  • 一键生成迷宫-Word插件-大珩助手新功能
  • 运维开发详解(上)
  • react useState基本使用
  • 基于jeecgboot-vue3的Flowable流程-待办任务(二)
  • 1103. 分糖果 II
  • SQL实验 数据的插入、修改和删除操作
  • es初始化
  • 【蓝桥杯】常见的数据结构
  • STM32H750外设ADC之外部触发和注入管理
  • 软设之排序算法对比
  • javascript导入excel文件
  • 「C系列」C 变量及常见问题梳理
  • <网络安全VIP>第二篇《工业控制软件》
  • 微服务:Rabbitmq中的不同交换机的使用Fanout、Direct、Topic(消息队列中间件)
  • 数据结构单链表的前插法实现
  • Cocos Creator开发学习路线
  • C语言:IO操作
  • 算法 | 刷题日记
  • 微信小程序登录接口
  • VBA实战(Excel)(5):介绍一种排列组合算法
  • 迭代器的使用
  • 安卓手机APP开发___广播概述
  • 【封装】Unity切换场景不销毁物体
  • 基于学习的决策树