diff --git a/RentACar/Data/Data.csproj b/RentACar/Data/Data.csproj index c0a087d..99e24c1 100644 --- a/RentACar/Data/Data.csproj +++ b/RentACar/Data/Data.csproj @@ -15,6 +15,7 @@ + all diff --git a/RentACar/Data/Data/RentACarDbContext.cs b/RentACar/Data/Data/RentACarDbContext.cs index 5e28229..30cf243 100644 --- a/RentACar/Data/Data/RentACarDbContext.cs +++ b/RentACar/Data/Data/RentACarDbContext.cs @@ -20,7 +20,7 @@ namespace Data { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;"); + optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=RentACar;Integrated Security=true;").UseLazyLoadingProxies(); } } @@ -63,7 +63,6 @@ namespace Data modelBuilder.Entity().HasKey(car => car.Id); modelBuilder.Entity().HasOne(rents => rents.User); modelBuilder.Entity().HasOne(rents => rents.Car); - base.OnModelCreating(modelBuilder); } diff --git a/RentACar/Data/Migrations/20220408211811_initial.Designer.cs b/RentACar/Data/Migrations/20220408234818_Initial.Designer.cs similarity index 91% rename from RentACar/Data/Migrations/20220408211811_initial.Designer.cs rename to RentACar/Data/Migrations/20220408234818_Initial.Designer.cs index 9286d5a..4f647df 100644 --- a/RentACar/Data/Migrations/20220408211811_initial.Designer.cs +++ b/RentACar/Data/Migrations/20220408234818_Initial.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Data.Migrations { [DbContext(typeof(RentACarDbContext))] - [Migration("20220408211811_initial")] - partial class initial + [Migration("20220408234818_Initial")] + partial class Initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -68,7 +68,6 @@ namespace Data.Migrations .HasColumnType("datetime2"); b.Property("UserId") - .IsRequired() .HasColumnType("nvarchar(450)"); b.HasKey("Id"); @@ -156,15 +155,15 @@ namespace Data.Migrations b.HasData( new { - Id = "28c75ddc-f972-4532-be46-adae03df32fb", + Id = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", AccessFailedCount = 0, - ConcurrencyStamp = "33c93ad2-8f61-4e3c-8d61-1583ea02a347", + ConcurrencyStamp = "c91c153e-ba2e-4da9-a397-5ac6f098d369", Email = "admin@admin.admin", EmailConfirmed = false, LockoutEnabled = false, - PasswordHash = "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==", + PasswordHash = "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==", PhoneNumberConfirmed = false, - SecurityStamp = "f548cc6f-ef08-4714-9c7a-e4e660cdced3", + SecurityStamp = "b105af8c-ee72-4969-9ba5-98bd13897459", TwoFactorEnabled = false, UserName = "admin" }); @@ -199,14 +198,14 @@ namespace Data.Migrations b.HasData( new { - Id = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", - ConcurrencyStamp = "18c88b14-89fd-4563-927c-23ed1da3d868", + Id = "8af8225c-ac26-406b-9d29-c172f95240c0", + ConcurrencyStamp = "446df763-47b2-4b18-b196-86f63f2a6a8b", Name = "Admin" }, new { - Id = "b79e8165-7e4a-409f-8d27-455dd03ca398", - ConcurrencyStamp = "32443d24-2ba0-4d21-90f4-40971854864c", + Id = "b006be23-eb9f-480d-b127-0f76a4b01d38", + ConcurrencyStamp = "13ae3b09-1b07-46d7-870e-ff9a78ac556d", Name = "Employee" }); }); @@ -298,13 +297,13 @@ namespace Data.Migrations b.HasData( new { - UserId = "28c75ddc-f972-4532-be46-adae03df32fb", - RoleId = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8" + UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", + RoleId = "8af8225c-ac26-406b-9d29-c172f95240c0" }, new { - UserId = "28c75ddc-f972-4532-be46-adae03df32fb", - RoleId = "b79e8165-7e4a-409f-8d27-455dd03ca398" + UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", + RoleId = "b006be23-eb9f-480d-b127-0f76a4b01d38" }); }); @@ -337,9 +336,7 @@ namespace Data.Migrations b.HasOne("Data.Entities.User", "User") .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("UserId"); b.Navigation("Car"); diff --git a/RentACar/Data/Migrations/20220408211811_initial.cs b/RentACar/Data/Migrations/20220408234818_Initial.cs similarity index 93% rename from RentACar/Data/Migrations/20220408211811_initial.cs rename to RentACar/Data/Migrations/20220408234818_Initial.cs index 72b8c7a..5ffb9f5 100644 --- a/RentACar/Data/Migrations/20220408211811_initial.cs +++ b/RentACar/Data/Migrations/20220408234818_Initial.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace Data.Migrations { - public partial class initial : Migration + public partial class Initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -182,7 +182,7 @@ namespace Data.Migrations CarId = table.Column(type: "int", nullable: false), StartDate = table.Column(type: "datetime2", nullable: false), EndDate = table.Column(type: "datetime2", nullable: false), - UserId = table.Column(type: "nvarchar(450)", nullable: false) + UserId = table.Column(type: "nvarchar(450)", nullable: true) }, constraints: table => { @@ -192,7 +192,7 @@ namespace Data.Migrations column: x => x.UserId, principalTable: "AspNetUsers", principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_Rents_Cars_CarId", column: x => x.CarId, @@ -204,27 +204,27 @@ namespace Data.Migrations migrationBuilder.InsertData( table: "AspNetRoles", columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" }, - values: new object[] { "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", "18c88b14-89fd-4563-927c-23ed1da3d868", "Admin", null }); + values: new object[] { "8af8225c-ac26-406b-9d29-c172f95240c0", "446df763-47b2-4b18-b196-86f63f2a6a8b", "Admin", null }); migrationBuilder.InsertData( table: "AspNetRoles", columns: new[] { "Id", "ConcurrencyStamp", "Name", "NormalizedName" }, - values: new object[] { "b79e8165-7e4a-409f-8d27-455dd03ca398", "32443d24-2ba0-4d21-90f4-40971854864c", "Employee", null }); + values: new object[] { "b006be23-eb9f-480d-b127-0f76a4b01d38", "13ae3b09-1b07-46d7-870e-ff9a78ac556d", "Employee", null }); migrationBuilder.InsertData( table: "AspNetUsers", columns: new[] { "Id", "AccessFailedCount", "ConcurrencyStamp", "Email", "EmailConfirmed", "FirstName", "LastName", "LockoutEnabled", "LockoutEnd", "NormalizedEmail", "NormalizedUserName", "PasswordHash", "PersonalNumber", "PhoneNumber", "PhoneNumberConfirmed", "SecurityStamp", "TwoFactorEnabled", "UserName" }, - values: new object[] { "28c75ddc-f972-4532-be46-adae03df32fb", 0, "33c93ad2-8f61-4e3c-8d61-1583ea02a347", "admin@admin.admin", false, null, null, false, null, null, null, "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==", null, null, false, "f548cc6f-ef08-4714-9c7a-e4e660cdced3", false, "admin" }); + values: new object[] { "691a45ed-4399-47a1-a8c2-2a2abac9ba74", 0, "c91c153e-ba2e-4da9-a397-5ac6f098d369", "admin@admin.admin", false, null, null, false, null, null, null, "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==", null, null, false, "b105af8c-ee72-4969-9ba5-98bd13897459", false, "admin" }); migrationBuilder.InsertData( table: "AspNetUserRoles", columns: new[] { "RoleId", "UserId" }, - values: new object[] { "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", "28c75ddc-f972-4532-be46-adae03df32fb" }); + values: new object[] { "8af8225c-ac26-406b-9d29-c172f95240c0", "691a45ed-4399-47a1-a8c2-2a2abac9ba74" }); migrationBuilder.InsertData( table: "AspNetUserRoles", columns: new[] { "RoleId", "UserId" }, - values: new object[] { "b79e8165-7e4a-409f-8d27-455dd03ca398", "28c75ddc-f972-4532-be46-adae03df32fb" }); + values: new object[] { "b006be23-eb9f-480d-b127-0f76a4b01d38", "691a45ed-4399-47a1-a8c2-2a2abac9ba74" }); migrationBuilder.CreateIndex( name: "IX_AspNetRoleClaims_RoleId", diff --git a/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs b/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs index f366a5e..1ae4564 100644 --- a/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs +++ b/RentACar/Data/Migrations/RentACarDbContextModelSnapshot.cs @@ -66,7 +66,6 @@ namespace Data.Migrations .HasColumnType("datetime2"); b.Property("UserId") - .IsRequired() .HasColumnType("nvarchar(450)"); b.HasKey("Id"); @@ -154,15 +153,15 @@ namespace Data.Migrations b.HasData( new { - Id = "28c75ddc-f972-4532-be46-adae03df32fb", + Id = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", AccessFailedCount = 0, - ConcurrencyStamp = "33c93ad2-8f61-4e3c-8d61-1583ea02a347", + ConcurrencyStamp = "c91c153e-ba2e-4da9-a397-5ac6f098d369", Email = "admin@admin.admin", EmailConfirmed = false, LockoutEnabled = false, - PasswordHash = "AQAAAAEAACcQAAAAEMzxMVddhY+nvlkkmN6QRcuyj3Vh4iVvZ9xMarXBCh1APRAhWUU90mP4SOf0qS3A8Q==", + PasswordHash = "AQAAAAEAACcQAAAAEBajAJZYLZCUESCrgsH15V9n0OFqo9MLHWVzA3zHo7aqKbmSoUCv1TAu9j3r+PC/mw==", PhoneNumberConfirmed = false, - SecurityStamp = "f548cc6f-ef08-4714-9c7a-e4e660cdced3", + SecurityStamp = "b105af8c-ee72-4969-9ba5-98bd13897459", TwoFactorEnabled = false, UserName = "admin" }); @@ -197,14 +196,14 @@ namespace Data.Migrations b.HasData( new { - Id = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8", - ConcurrencyStamp = "18c88b14-89fd-4563-927c-23ed1da3d868", + Id = "8af8225c-ac26-406b-9d29-c172f95240c0", + ConcurrencyStamp = "446df763-47b2-4b18-b196-86f63f2a6a8b", Name = "Admin" }, new { - Id = "b79e8165-7e4a-409f-8d27-455dd03ca398", - ConcurrencyStamp = "32443d24-2ba0-4d21-90f4-40971854864c", + Id = "b006be23-eb9f-480d-b127-0f76a4b01d38", + ConcurrencyStamp = "13ae3b09-1b07-46d7-870e-ff9a78ac556d", Name = "Employee" }); }); @@ -296,13 +295,13 @@ namespace Data.Migrations b.HasData( new { - UserId = "28c75ddc-f972-4532-be46-adae03df32fb", - RoleId = "14ca6e11-5721-43c8-a170-b74dbd8dd7f8" + UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", + RoleId = "8af8225c-ac26-406b-9d29-c172f95240c0" }, new { - UserId = "28c75ddc-f972-4532-be46-adae03df32fb", - RoleId = "b79e8165-7e4a-409f-8d27-455dd03ca398" + UserId = "691a45ed-4399-47a1-a8c2-2a2abac9ba74", + RoleId = "b006be23-eb9f-480d-b127-0f76a4b01d38" }); }); @@ -335,9 +334,7 @@ namespace Data.Migrations b.HasOne("Data.Entities.User", "User") .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("UserId"); b.Navigation("Car"); diff --git a/RentACar/Data/Models/Car.cs b/RentACar/Data/Models/Car.cs index c2be692..580d66d 100644 --- a/RentACar/Data/Models/Car.cs +++ b/RentACar/Data/Models/Car.cs @@ -11,13 +11,15 @@ namespace Data.Entities { [Required] [Key] + [Display(Name = "Car ID")] public int Id { get; set; } public string Brand { get; set; } public string Model { get; set; } public int Year { get; set; } + [Display(Name = "Seats")] public int CountPassengerSeats { get; set; } - public string Description { get; set; } + [Display(Name = "Daily price")] public decimal PriceForDay { get; set; } } } diff --git a/RentACar/Data/Models/Rents.cs b/RentACar/Data/Models/Rents.cs index f4a5356..b4659ad 100644 --- a/RentACar/Data/Models/Rents.cs +++ b/RentACar/Data/Models/Rents.cs @@ -12,19 +12,19 @@ namespace Data.Entities public class Rents { public int Id { get; set; } - - [Required] [ForeignKey("CarId")] - public Car Car { get; set; } + public int CarId { get; set; } + public virtual Car Car { get; set; } [Required] + [Display(Name = "Start Date")] public DateTime StartDate { get; set; } [Required] + [Display(Name = "End Date")] public DateTime EndDate { get; set; } - - [Required] [ForeignKey("UserId")] - public User User { get; set; } + public string UserId { get; set; } + public virtual User User { get; set; } } } diff --git a/RentACar/Data/Models/User.cs b/RentACar/Data/Models/User.cs index 926e822..4d720c5 100644 --- a/RentACar/Data/Models/User.cs +++ b/RentACar/Data/Models/User.cs @@ -11,10 +11,11 @@ namespace Data.Entities { public class User : IdentityUser { + [Display(Name = "First Name")] public string FirstName { get; set; } - + [Display(Name = "Last Name")] public string LastName { get; set; } - + [Display(Name = "ID Number")] public string PersonalNumber { get; set; } } diff --git a/RentACar/WebApp/Controllers/RentsController.cs b/RentACar/WebApp/Controllers/RentsController.cs index 8e6fa7e..79b9f1d 100644 --- a/RentACar/WebApp/Controllers/RentsController.cs +++ b/RentACar/WebApp/Controllers/RentsController.cs @@ -61,6 +61,7 @@ namespace WebApp.Controllers [Authorize] public async Task Create(Rents rents) { + if (ModelState.IsValid) { var car = _context.Cars.FirstOrDefault(car => car.Id == 1); diff --git a/RentACar/WebApp/Startup.cs b/RentACar/WebApp/Startup.cs index 11138fa..08d61d0 100644 --- a/RentACar/WebApp/Startup.cs +++ b/RentACar/WebApp/Startup.cs @@ -29,7 +29,7 @@ namespace API public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => - options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDatabaseDeveloperPageExceptionFilter(); services.AddIdentity(options => { @@ -45,7 +45,6 @@ namespace API .AddDefaultUI() .AddDefaultTokenProviders() .AddEntityFrameworkStores(); - services.AddControllersWithViews(); services.AddRazorPages(); } diff --git a/RentACar/WebApp/Views/Cars/Index.cshtml b/RentACar/WebApp/Views/Cars/Index.cshtml index 42a1892..d3d9608 100644 --- a/RentACar/WebApp/Views/Cars/Index.cshtml +++ b/RentACar/WebApp/Views/Cars/Index.cshtml @@ -15,7 +15,7 @@ - ID + @Html.DisplayNameFor(model => model.Id) @Html.DisplayNameFor(model => model.Brand) @@ -27,13 +27,13 @@ @Html.DisplayNameFor(model => model.Year) - Seats + @Html.DisplayNameFor(model => model.CountPassengerSeats) @Html.DisplayNameFor(model => model.Description) - Daily price + @Html.DisplayNameFor(model => model.PriceForDay) @if (this.User.IsInRole("Admin")) { diff --git a/RentACar/WebApp/Views/Rents/Details.cshtml b/RentACar/WebApp/Views/Rents/Details.cshtml index a819364..a36e189 100644 --- a/RentACar/WebApp/Views/Rents/Details.cshtml +++ b/RentACar/WebApp/Views/Rents/Details.cshtml @@ -9,18 +9,72 @@

-
+
+ @Html.DisplayNameFor(model => model.Car.Id) +
+
+ @Html.DisplayFor(model => model.Car.Id) +
+
+ @Html.DisplayNameFor(model => model.Car.Brand) +
+
+ @Html.DisplayFor(model => model.Car.Brand) +
+
+ @Html.DisplayNameFor(model => model.Car.Model) +
+
+ @Html.DisplayFor(model => model.Car.Model) +
+
+ @Html.DisplayNameFor(model => model.Car.CountPassengerSeats) +
+
+ @Html.DisplayFor(model => model.Car.CountPassengerSeats) +
+
+ @Html.DisplayNameFor(model => model.Car.PriceForDay) +
+
+ @Html.DisplayFor(model => model.Car.PriceForDay) +
+
@Html.DisplayNameFor(model => model.StartDate)
-
+
@Html.DisplayFor(model => model.StartDate)
-
+
@Html.DisplayNameFor(model => model.EndDate)
-
+
@Html.DisplayFor(model => model.EndDate)
+
+ @Html.DisplayNameFor(model => model.User.FirstName) +
+
+ @Html.DisplayFor(model => model.User.FirstName) +
+
+ @Html.DisplayNameFor(model => model.User.LastName) +
+
+ @Html.DisplayFor(model => model.User.LastName) +
+
+ @Html.DisplayNameFor(model => model.User.UserName) +
+
+ @Html.DisplayFor(model => model.User.UserName) +
+
+ @Html.DisplayNameFor(model => model.User.Email) +
+
+ @Html.DisplayFor(model => model.User.Email) +
diff --git a/RentACar/WebApp/Views/Rents/Index.cshtml b/RentACar/WebApp/Views/Rents/Index.cshtml index ed6e53f..4a0a2af 100644 --- a/RentACar/WebApp/Views/Rents/Index.cshtml +++ b/RentACar/WebApp/Views/Rents/Index.cshtml @@ -15,16 +15,16 @@ - Car ID + @Html.DisplayNameFor(model => model.Car.Id) - Start Date + @Html.DisplayNameFor(model => model.StartDate) - End Date + @Html.DisplayNameFor(model => model.EndDate) - User + @Html.DisplayNameFor(model => model.User.UserName) @if (this.User.IsInRole("Admin")) { @@ -37,28 +37,28 @@ @foreach (var item in Model) { - - - @Html.DisplayFor(modelItem => item.Car.Id) - - - @Html.DisplayFor(modelItem => item.StartDate) - - - @Html.DisplayFor(modelItem => item.EndDate) - - - @Html.DisplayFor(modelItem => item.User.UserName) - - @if (this.User.IsInRole("Admin")) - { + - Edit | - Details | - Delete + @Html.DisplayFor(modelItem => item.Car.Id) - } - + + @Html.DisplayFor(modelItem => item.StartDate) + + + @Html.DisplayFor(modelItem => item.EndDate) + + + @Html.DisplayFor(modelItem => item.User.UserName) + + @if (this.User.IsInRole("Admin")) + { + + Edit | + Details | + Delete + + } + } diff --git a/RentACar/WebApp/Views/Users/Index.cshtml b/RentACar/WebApp/Views/Users/Index.cshtml index d13fc95..28f3dfb 100644 --- a/RentACar/WebApp/Views/Users/Index.cshtml +++ b/RentACar/WebApp/Views/Users/Index.cshtml @@ -13,22 +13,22 @@ - First name + @Html.DisplayNameFor(model => model.FirstName) - Last name + @Html.DisplayNameFor(model => model.LastName) - ID number + @Html.DisplayNameFor(model => model.PersonalNumber) - Username + @Html.DisplayNameFor(model => model.UserName) - E-mail + @Html.DisplayNameFor(model => model.Email) - Phone number + @Html.DisplayNameFor(model => model.PhoneNumber) Actions