using AutoMapper; using DMBD.Kernel.DTOs; using DMBD.Kernel.Model; using DMBD.Kernel.Service; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; namespace DMBD_App.Controllers { public class StudentController : Controller { private readonly IService _services; private readonly IMapper _mapper; public StudentController(IService services,IMapper mapper) { _services = services; _mapper = mapper; } public async Task Index() { return View(await _services.GetAllAsync()); } public async Task Save() { var students = await _services.GetAllAsync(); var studentDto = _mapper.Map>(students.ToList()); ViewBag.students = new SelectList(studentDto, "Id", "TcNo"); return View(); } [HttpPost] public async Task Save(StudentDto studentDto) { if (ModelState.IsValid) { await _services.AddAsync(_mapper.Map(studentDto)); return RedirectToAction(nameof(Index)); } var students = await _services.GetAllAsync(); var studentsDto = _mapper.Map>(students.ToList()); ViewBag.students = new SelectList(studentsDto, "Id", "Name"); return View(); } [ServiceFilter(typeof(NotFoundFilter))] public async Task Update(int id) { var student = await _services.GetByIdAsync(id); var students = await _services.GetAllAsync(); var studentsDto = _mapper.Map>(students.ToList()); ViewBag.students = new SelectList(studentsDto, "Id", "Name", student.StudentId); return View(_mapper.Map(student)); } [HttpPost] public async Task Update(StudentDto studentDto) { if (ModelState.IsValid) { await _services.UpdateAsync(_mapper.Map(studentDto)); return RedirectToAction(nameof(Index)); } var students = await _services.GetAllAsync(); var studentsDto = _mapper.Map>(students.ToList()); ViewBag.students = new SelectList(studentsDto, "Id", "Name", studentDto.StudentId); return View(studentDto); } public async Task Remove(int id) { var student = await _services.GetByIdAsync(id); await _services.RemoveAsync(student); return RedirectToAction(nameof(Index)); } } }