1.WEB,API -> connection string appsettings.json -------------------------------------------------- 2.DataModel - Student.cs namespace Kolokwium.Model.DataModels; public class Student : User { public required string NrIndexu {get; set;} } -------------------------------------------------- 3.VM - StudentVm.cs namespace Kolokwium.ViewModel.VM; public class StudentVm { public int? Id {get; set;} public required string Imie {get; set;} public required string Nazwisko {get; set;} public required string NrIndexu {get; set;} public required string Email {get; set;} } **************************** VM - ModifyStudentVm.cs namespace Kolokwium.ViewModel.VM; public class ModifyStudentVm { public int? Id {get; set;} public required string Imie {get; set;} public required string Nazwisko {get; set;} public required string NrIndexu {get; set;} public required string Email {get; set;} } -------------------------------------------------- 4.Services -> Interfaces - IStudentService.cs using System.Linq.Expressions; using Kolokwium.Model.DataModels; using Kolokwium.ViewModel.VM; namespace Kolokwium.Services.Interfaces; public interface IStudentService { IEnumerable GetStudentVms(Expression>? predicate = null); StudentVm GetStudentVm(Expression> predicate); Student Modify(ModifyStudentVm ModifyStudentVm); void DeleteStudent(int id); } ------------------------------------------------- 5.ConcreteServices -> StudentService.cs using System.Linq.Expressions; using System.Security.Cryptography.X509Certificates; using AutoMapper; using Kolokwium.DAL; using Kolokwium.Model.DataModels; using Kolokwium.Services.Interfaces; using Kolokwium.ViewModel.VM; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Logging; namespace Kolokwium.Services.ConcreteServices; public class StudentService : BaseService, IStudentService { public StudentService(ApplicationDbContext dbContext, IMapper mapper, ILogger logger) : base(dbContext, mapper, logger) { } public Student Modify(ModifyStudentVm ModifyStudentVm) { try { if(ModifyStudentVm == null) throw new Exception("Error"); var student = Mapper.Map(ModifyStudentVm); if(!ModifyStudentVm.Id.HasValue) { DbContext.Students.Add(student); DbContext.SaveChanges(); return student; } else//usunąć ifa albo else { var oldStudent = DbContext.Students.Find(ModifyStudentVm.Id); if(oldStudent == null) throw new Exception(); oldStudent.Imie = student.Imie; oldStudent.Nazwisko = student.Nazwisko; oldStudent.Email = student.Email; oldStudent.NrIndexu = student.NrIndexu; DbContext.SaveChanges(); return oldStudent; } } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } public void DeleteStudent(int id) { try { var student = DbContext.Students.FirstOrDefault(x => x.Id == id); if(student == null) throw new Exception(); DbContext.Students.Remove(student); DbContext.SaveChanges(); } catch(Exception e) { Logger.LogError(e,e.Message); } } public StudentVm GetStudentVm(Expression> predicate) { try { if(predicate == null) throw new Exception(); var student = DbContext.Students.FirstOrDefault(predicate); if(student == null) throw new Exception(); var studentVm = Mapper.Map(student); return studentVm; } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } public IEnumerable GetStudentVms(Expression>? predicate = null) { try { var students = DbContext.Students.AsQueryable(); if(predicate != null) { students.Where(predicate); } var studentsVm = Mapper.Map>(students); return studentsVm; } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } } ---------------------------------------------------------------------------------------------------- 6.Configuration -> MainProfile.cs CreateMap(); CreateMap(); CreateMap(); ---------------------------------------------------------------------------------------------------- 7.---- ---------------------------------------------------------------------------------------------------- 8.--- ---------------------------------------------------------------------------------------------------- 9.Web -> Controllers -> StudentController.cs using AutoMapper; using Kolokwium.Model.DataModels; using Kolokwium.Services.ConcreteServices; using Kolokwium.Services.Interfaces; using Kolokwium.ViewModel.VM; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; namespace Kolokwium.Web.Controllers; public class StudentController : BaseController { private readonly IStudentService studentService1; public StudentController(ILogger logger, IMapper mapper, IStringLocalizer localizer, IStudentService studentService) : base(logger, mapper, localizer) { studentService1 = studentService; } public IActionResult Index() { var studentsVm = studentService1.GetStudentVms(); return View(studentsVm); } public IActionResult Modify(int? id = null) { if(id == null) return View(); else { var student = studentService1.GetStudentVm(x => x.Id == id); var ModifyStudentVm = Mapper.Map(student); return View(ModifyStudentVm); } }//usunąć else,parametr i ifa [HttpPost] public IActionResult Modify(ModifyStudentVm ModifyStudentVm) { if(ModifyStudentVm == null) throw new Exception(); studentService1.Modify(ModifyStudentVm); return RedirectToAction(nameof(Index)); } public IActionResult DeleteStudent(int id) { studentService1.DeleteStudent(id); return RedirectToAction(nameof(Index)); } } --------------------------------------------------------------------------------------------------- 10.Web -> Program.cs builder.Services.AddTransient(); --------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- 12.Shared -> Layout --------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- 14.DAL -> DbContext public virtual DbSet Students {get; set;} = null!; dotnet ef migrations add migrancja1 --project Kolokwium.DAL --startup-project Kolokwium.Web dotnet ef database update --project Kolokwium.DAL --startup-project Kolokwium.Web dotnet aspnet-codegenerator view -p Kolokwium.Web Index List -m StudentVm -outDir Views/Student -scripts -udl dotnet aspnet-codegenerator view -p Kolokwium.Web Modify Create -m ModifyStudentVm -outDir Views/Student -scripts -udl --------------------------------------------------------------------------------------------------- 16.Web -> Views -> Index Zmienić akcje @Html.ActionLink("Edit", "Modify", new {id = item.Id}) | @Html.ActionLink("Delete", "DeleteSportowiec", new {id = item.Id}) --------------------------------------------------------------------------------------------------- 17.Usunąć ID z Modify.cshtml 18.dotnet run --project Kolokwium.Web UWAGI: modyfikacja Student -> index.cshtml Create New nazwe pobrac z web -> controllers -> StudentController