1.WEB,API -> connection string appsettings.json ############################################################ 2.DataModel - Client.cs using System.ComponentModel.DataAnnotations; namespace Kolokwium.Model.DataModels; public class Car{ [Key] public string VinId{get;set;}=null!; public string Vendor{get;set;}=null!; public string Brand{get;set;}=null!; public string Type{get;set;}=null!; public int Capacity{get;set;} public DateTime ProductionDate{get;set;} public ICollection Drivers{get;set;}=null!; } -------------------------------------------------------- using System.ComponentModel.DataAnnotations; namespace Kolokwium.Model.DataModels; public class Driver{ [Key] public int LicenseNumerId{get;set;} public string FirstName{get;set;}=null!; public string LastName{get;set;}=null!; public string Address{get;set;}=null!; public ICollection Cars{get;set;}=null!; } ############################################################# 3.VM - ClientVm.cs using System.ComponentModel.DataAnnotations; using Kolokwium.Model.DataModels; namespace Kolokwium.ViewModel.VM; public class CarVm { [Key] public string? VinId{get;set;} public string Vendor{get;set;}=null!; public string Brand{get;set;}=null!; public string Type{get;set;}=null!; public int Capacity{get;set;} public DateTime ProductionDate{get;set;} public ICollection Drivers{get;set;}=null!; } -------------------------------------------------------- VM - ModifyClientVm.cs using System.ComponentModel.DataAnnotations; namespace Kolokwium.ViewModel.VM; public class ModifyCarVm { [Key] public string? VinId{get;set;} public string Vendor{get;set;}=null!; public string Brand{get;set;}=null!; public string Type{get;set;}=null!; public int Capacity{get;set;} public DateTime ProductionDate{get;set;} } ############################################################# 4.Services ->tworzymy folder Interfaces - IStudentService.cs using System.Linq.Expressions; using Kolokwium.Model.DataModels; using Kolokwium.ViewModel.VM; namespace Kolokwium.Services.Interfaces; public interface ICarService { IEnumerable GetCarVms(Expression>? predicate = null); CarVm GetCarVm(Expression> predicate); Car Modify(ModifyCarVm ModifyCarVm); void DeleteCar(string 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 CarService : BaseService, ICarService { public CarService(ApplicationDbContext dbContext, IMapper mapper, ILogger logger) : base(dbContext, mapper, logger) { } public Car Modify(ModifyCarVm ModifyCarVm) { try { if(ModifyCarVm == null) throw new Exception("Error"); var Car = Mapper.Map(ModifyCarVm); if(!string.IsNullOrEmpty(ModifyCarVm.VinId)) { DbContext.Cars.Add(Car); DbContext.SaveChanges(); return Car; } else { var oldCar = DbContext.Cars.Find(ModifyCarVm.VinId); if(oldCar == null) throw new Exception(); oldCar.VinId = Car.VinId; oldCar.Vendor = Car.Vendor; oldCar.Brand = Car.Brand; oldCar.Type = Car.Type; oldCar.Capacity = Car.Capacity; DbContext.SaveChanges(); return oldCar; } } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } public void DeleteCar(string id) { try { var Car = DbContext.Cars.FirstOrDefault(x => x.VinId == id); if(Car == null) throw new Exception(); DbContext.Cars.Remove(Car); DbContext.SaveChanges(); } catch(Exception e) { Logger.LogError(e,e.Message); } } public CarVm GetCarVm(Expression> predicate) { try { if(predicate == null) throw new Exception(); var Car = DbContext.Cars.FirstOrDefault(predicate); if(Car == null) throw new Exception(); var CarVm = Mapper.Map(Car); return CarVm; } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } public IEnumerable GetCarVms(Expression>? predicate = null) { try { var Cars = DbContext.Cars.AsQueryable(); if(predicate != null) { Cars.Where(predicate); } var CarsVm = Mapper.Map>(Cars); return CarsVm; } catch(Exception e) { Logger.LogError(e,e.Message); throw; } } } ######################################################################################################### 6.Configuration -> MainProfile.cs //ZRÓB USINGI ctrl .//*2 CreateMap(); CreateMap(); CreateMap(); ########################################################################################### 7.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 CarController : BaseController { private readonly ICarService CarService1; public CarController(ILogger logger, IMapper mapper, IStringLocalizer localizer, ICarService CarService) : base(logger, mapper, localizer) { CarService1 = CarService; } public IActionResult Index() { var CarsVm = CarService1.GetCarVms(); return View(CarsVm); } public IActionResult Modify(string? id = null) { if(id == null) return View(); else { var Car = CarService1.GetCarVm(x => x.VinId == id); var ModifyCarVm = Mapper.Map(Car); return View(ModifyCarVm); } } [HttpPost] public IActionResult Modify(ModifyCarVm ModifyCarVm) { if(ModifyCarVm == null) throw new Exception(); CarService1.Modify(ModifyCarVm); return RedirectToAction(nameof(Index)); } public IActionResult DeleteCar(string id) { CarService1.DeleteCar(id); return RedirectToAction(nameof(Index)); } } ##################################################################### 8.Web -> Program.cs//*2 builder.Services.AddTransient(); ##################################################################### 9.dotnet aspnet-codegenerator view -p Kolokwium.Web Index List -m StudentVm -outDir Views/Student -scripts -udl ######################################################################## 10.Shared -> Layout ########################################################################### 11.DAL -> DbContext//*3 public virtual DbSet Students {get; set;} = null!; ########################################################################## 12.dotnet aspnet-codegenerator view -p Kolokwium.Web Modify Create -m ModifyStudentVm -outDir Views/Student -scripts -udl //Wpisuj ręcznie lepiej xddd ########################################################################### 13.Web -> Views -> Index Wywal Details Zmienić akcje @Html.ActionLink("Edit", "Modify", new {id = item.Id}) | @Html.ActionLink("Delete", "DeleteSportowiec", new {id = item.Id}) W 10 linii zmień Create na Modify xdddd ######################################################################### 14.Usunąć ID z Modify.cshtml Dodaj na poczatku w @: var clients=ViewBag.Clients; potem zamien diva z ClientId
############################################################################# Polecenia: 1. dotnet ef migrations add migrancja1 --project Kolokwium.DAL --startup-project Kolokwium.Web 2. dotnet ef database update --project Kolokwium.DAL --startup-project Kolokwium.Web 2*3. dotnet aspnet-codegenerator view -p Kolokwium.Web Index List -m StudentVm -outDir Views/Student -scripts -udl 2*4. dotnet aspnet-codegenerator view -p Kolokwium.Web Modify Create -m ModifyStudentVm -outDir Views/Student -scripts -udl 5. dotnet run --project Kolokwium.Web