Files
Jan Krivanek e4670b33a1 [PoC] Code testing agent + tests PoC (#433)
* Add code testing agent agents + tests PoC

* Remove AssertionEvaluator.cs change (moved to dev/jankrivanek/agents-evals)
2026-03-30 17:57:08 +02:00

236 lines
10 KiB
C#

using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Net;
using System.IO;
using ContosoUniversity.Data;
using ContosoUniversity.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
namespace ContosoUniversity.Controllers
{
public class CoursesController : BaseController
{
private readonly IWebHostEnvironment _env;
private readonly ILogger<CoursesController> _logger;
public CoursesController(SchoolContext context, IWebHostEnvironment env, ILogger<CoursesController> logger) : base(context)
{
_env = env;
_logger = logger;
}
// GET: Courses
public ActionResult Index()
{
var courses = db.Courses.Include(c => c.Department);
return View(courses.ToList());
}
// GET: Courses/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new StatusCodeResult((int)HttpStatusCode.BadRequest);
}
Course course = db.Courses.Include(c => c.Department).Where(c => c.CourseID == id).Single();
if (course == null)
{
return NotFound();
}
return View(course);
}
// GET: Courses/Create
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name");
return View(new Course());
}
// POST: Courses/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind("CourseID", "Title", "Credits", "DepartmentID", "TeachingMaterialImagePath")] Course course, IFormFile teachingMaterialImage)
{
if (ModelState.IsValid)
{
if (teachingMaterialImage != null && teachingMaterialImage.Length > 0)
{
var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
var fileExtension = Path.GetExtension(teachingMaterialImage.FileName).ToLower();
if (!allowedExtensions.Contains(fileExtension))
{
ModelState.AddModelError("teachingMaterialImage", "Please upload a valid image file (jpg, jpeg, png, gif, bmp).");
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
if (teachingMaterialImage.Length > 5 * 1024 * 1024)
{
ModelState.AddModelError("teachingMaterialImage", "File size must be less than 5MB.");
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
try
{
var uploadsPath = Path.Combine(_env.WebRootPath ?? _env.ContentRootPath, "Uploads", "TeachingMaterials");
Directory.CreateDirectory(uploadsPath);
var fileName = $"course_{course.CourseID}_{Guid.NewGuid()}{fileExtension}";
var filePath = Path.Combine(uploadsPath, fileName);
using (var stream = System.IO.File.Create(filePath))
{
teachingMaterialImage.CopyTo(stream);
}
course.TeachingMaterialImagePath = $"/Uploads/TeachingMaterials/{fileName}";
}
catch (Exception ex)
{
_logger.LogError(ex, "Error uploading file");
ModelState.AddModelError("teachingMaterialImage", "Error uploading file: " + ex.Message);
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
}
db.Courses.Add(course);
db.SaveChanges();
SendEntityNotification("Course", course.CourseID.ToString(), course.Title, EntityOperation.CREATE);
return RedirectToAction("Index");
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
// GET: Courses/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new StatusCodeResult((int)HttpStatusCode.BadRequest);
}
Course course = db.Courses.Find(id);
if (course == null)
{
return NotFound();
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
// POST: Courses/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind("CourseID", "Title", "Credits", "DepartmentID", "TeachingMaterialImagePath")] Course course, IFormFile teachingMaterialImage)
{
if (ModelState.IsValid)
{
if (teachingMaterialImage != null && teachingMaterialImage.Length > 0)
{
var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
var fileExtension = Path.GetExtension(teachingMaterialImage.FileName).ToLower();
if (!allowedExtensions.Contains(fileExtension))
{
ModelState.AddModelError("teachingMaterialImage", "Please upload a valid image file (jpg, jpeg, png, gif, bmp).");
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
if (teachingMaterialImage.Length > 5 * 1024 * 1024)
{
ModelState.AddModelError("teachingMaterialImage", "File size must be less than 5MB.");
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
try
{
var uploadsPath = Path.Combine(_env.WebRootPath ?? _env.ContentRootPath, "Uploads", "TeachingMaterials");
Directory.CreateDirectory(uploadsPath);
var fileName = $"course_{course.CourseID}_{Guid.NewGuid()}{fileExtension}";
var filePath = Path.Combine(uploadsPath, fileName);
if (!string.IsNullOrEmpty(course.TeachingMaterialImagePath))
{
var oldFilePath = Path.Combine(_env.WebRootPath ?? _env.ContentRootPath, course.TeachingMaterialImagePath.TrimStart('/').Replace('/', Path.DirectorySeparatorChar));
if (System.IO.File.Exists(oldFilePath))
{
try { System.IO.File.Delete(oldFilePath); } catch { }
}
}
using (var stream = System.IO.File.Create(filePath))
{
teachingMaterialImage.CopyTo(stream);
}
course.TeachingMaterialImagePath = $"/Uploads/TeachingMaterials/{fileName}";
}
catch (Exception ex)
{
_logger.LogError(ex, "Error uploading file");
ModelState.AddModelError("teachingMaterialImage", "Error uploading file: " + ex.Message);
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
}
db.Entry(course).State = EntityState.Modified;
db.SaveChanges();
SendEntityNotification("Course", course.CourseID.ToString(), course.Title, EntityOperation.UPDATE);
return RedirectToAction("Index");
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
return View(course);
}
// GET: Courses/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new StatusCodeResult((int)HttpStatusCode.BadRequest);
}
Course course = db.Courses.Include(c => c.Department).Where(c => c.CourseID == id).Single();
if (course == null)
{
return NotFound();
}
return View(course);
}
// POST: Courses/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Course course = db.Courses.Find(id);
var courseTitle = course.Title;
if (!string.IsNullOrEmpty(course.TeachingMaterialImagePath))
{
var filePath = Path.Combine(_env.WebRootPath ?? _env.ContentRootPath, course.TeachingMaterialImagePath.TrimStart('/').Replace('/', Path.DirectorySeparatorChar));
if (System.IO.File.Exists(filePath))
{
try { System.IO.File.Delete(filePath); } catch (Exception ex) { _logger.LogWarning(ex, "Error deleting course image"); }
}
}
db.Courses.Remove(course);
db.SaveChanges();
SendEntityNotification("Course", id.ToString(), courseTitle, EntityOperation.DELETE);
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// base controller disposes context
}
base.Dispose(disposing);
}
}
}