From 8dcf5c40f98ad131adc2db95197f77ae12434cc9 Mon Sep 17 00:00:00 2001 From: Surya-AI <70279050+Surya-AI@users.noreply.github.com> Date: Sun, 17 Nov 2024 07:23:08 +0530 Subject: [PATCH] Implemented the Province View --- BHServer.csproj | 6 ++ Controllers/ProvinceController.cs | 22 +++++ Data/ApplicationDbContext.cs | 18 ++++ ...0241111135918_AddProvincesToDb.Designer.cs | 59 +++++++++++++ Migrations/20241111135918_AddProvincesToDb.cs | 38 +++++++++ ...1116161946_UpdateProvinceTable.Designer.cs | 62 ++++++++++++++ .../20241116161946_UpdateProvinceTable.cs | 29 +++++++ .../ApplicationDbContextModelSnapshot.cs | 59 +++++++++++++ Models/Province.cs | 21 +++++ Program.cs | 8 ++ Views/Province/Index.cshtml | 82 +++++++++++++++++++ appsettings.json | 7 +- 12 files changed, 410 insertions(+), 1 deletion(-) create mode 100644 Controllers/ProvinceController.cs create mode 100644 Data/ApplicationDbContext.cs create mode 100644 Migrations/20241111135918_AddProvincesToDb.Designer.cs create mode 100644 Migrations/20241111135918_AddProvincesToDb.cs create mode 100644 Migrations/20241116161946_UpdateProvinceTable.Designer.cs create mode 100644 Migrations/20241116161946_UpdateProvinceTable.cs create mode 100644 Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 Models/Province.cs create mode 100644 Views/Province/Index.cshtml diff --git a/BHServer.csproj b/BHServer.csproj index f304257..7813dbb 100644 --- a/BHServer.csproj +++ b/BHServer.csproj @@ -10,6 +10,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Controllers/ProvinceController.cs b/Controllers/ProvinceController.cs new file mode 100644 index 0000000..8713900 --- /dev/null +++ b/Controllers/ProvinceController.cs @@ -0,0 +1,22 @@ +using BHServer.Data; +using BHServer.Models; +using Microsoft.AspNetCore.Mvc; +using System; + +namespace BHServer.Controllers +{ + public class ProvinceController : Controller + { + private readonly ApplicationDbContext _db; + public ProvinceController(ApplicationDbContext db) + { + _db = db; + } + public IActionResult Index() + { + List ObjectProvinceList = _db.Provinces.ToList(); + // If the above line faulters then ensure SQL Server is running + return View(ObjectProvinceList); + } + } +} diff --git a/Data/ApplicationDbContext.cs b/Data/ApplicationDbContext.cs new file mode 100644 index 0000000..cb4f444 --- /dev/null +++ b/Data/ApplicationDbContext.cs @@ -0,0 +1,18 @@ +using BHServer.Models; +using Microsoft.EntityFrameworkCore; + +namespace BHServer.Data +{ + public class ApplicationDbContext : DbContext + { + public ApplicationDbContext(DbContextOptions options) : base(options) + // this will pass along the options from the implementation to the definition + { + } + + public DbSet Provinces { get; set; } // specify the table and it's class + // to run migrations, run `add-migration ` in the NuGet console + // then make the migrations from the console by using `update-database` + // to rollback migrations run the command `remove-migration ` + } +} diff --git a/Migrations/20241111135918_AddProvincesToDb.Designer.cs b/Migrations/20241111135918_AddProvincesToDb.Designer.cs new file mode 100644 index 0000000..9af57b4 --- /dev/null +++ b/Migrations/20241111135918_AddProvincesToDb.Designer.cs @@ -0,0 +1,59 @@ +// +using BHServer.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BHServer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241111135918_AddProvincesToDb")] + partial class AddProvincesToDb + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BHServer.Models.Province", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ATs") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Faction") + .HasColumnType("int"); + + b.Property("Multiplier") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Provinces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20241111135918_AddProvincesToDb.cs b/Migrations/20241111135918_AddProvincesToDb.cs new file mode 100644 index 0000000..1a8e1c0 --- /dev/null +++ b/Migrations/20241111135918_AddProvincesToDb.cs @@ -0,0 +1,38 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BHServer.Migrations +{ + /// + public partial class AddProvincesToDb : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Provinces", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Faction = table.Column(type: "int", nullable: false), + Multiplier = table.Column(type: "int", nullable: false), + ATs = table.Column(type: "nvarchar(max)", nullable: false), + State = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Provinces", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Provinces"); + } + } +} diff --git a/Migrations/20241116161946_UpdateProvinceTable.Designer.cs b/Migrations/20241116161946_UpdateProvinceTable.Designer.cs new file mode 100644 index 0000000..ac6764c --- /dev/null +++ b/Migrations/20241116161946_UpdateProvinceTable.Designer.cs @@ -0,0 +1,62 @@ +// +using BHServer.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BHServer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241116161946_UpdateProvinceTable")] + partial class UpdateProvinceTable + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BHServer.Models.Province", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ATs") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Faction") + .HasColumnType("int"); + + b.Property("Multiplier") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProvinceId") + .HasColumnType("int"); + + b.Property("State") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Provinces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20241116161946_UpdateProvinceTable.cs b/Migrations/20241116161946_UpdateProvinceTable.cs new file mode 100644 index 0000000..292b2dc --- /dev/null +++ b/Migrations/20241116161946_UpdateProvinceTable.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BHServer.Migrations +{ + /// + public partial class UpdateProvinceTable : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ProvinceId", + table: "Provinces", + type: "int", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "ProvinceId", + table: "Provinces"); + } + } +} diff --git a/Migrations/ApplicationDbContextModelSnapshot.cs b/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..65163ca --- /dev/null +++ b/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,59 @@ +// +using BHServer.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BHServer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BHServer.Models.Province", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ATs") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Faction") + .HasColumnType("int"); + + b.Property("Multiplier") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProvinceId") + .HasColumnType("int"); + + b.Property("State") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Provinces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Models/Province.cs b/Models/Province.cs new file mode 100644 index 0000000..a65f8a3 --- /dev/null +++ b/Models/Province.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; + +namespace BHServer.Models +{ + public class Province + { + [Key] + public int Id { get; set; } + public int ProvinceId { get; set; } + [Required] + public string Name { get; set; } + [Required] + public int Faction { get; set; } // 0 - Neutral, 1 - Allies, 2 - Axis + [Required] + public int Multiplier { get; set; } // 0-1 + [Required] + public string ATs { get; set; } // Json String + [Required] + public int State { get; set; } // 0 - Peace, 1 - War + } +} diff --git a/Program.cs b/Program.cs index 0727468..708b52e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,16 @@ +using BHServer.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddDbContext( + options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")) + // `ConnectionString` is defined in the appsettings.json file +); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Views/Province/Index.cshtml b/Views/Province/Index.cshtml new file mode 100644 index 0000000..063bd44 --- /dev/null +++ b/Views/Province/Index.cshtml @@ -0,0 +1,82 @@ +@* + For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +*@ +@{ +} + +@model List + +

Provinces

+ + + + + + + + + + + + + @foreach (var obj in Model) { + + + + @switch (obj.Faction) + { + case 0: + + break; + case 1: + + break; + case 2: + + break; + } + + + @switch (obj.State) + { + case 0: + + break; + case 1: + + break; + } + + } + +
+ Id + + Name + + Faction + + Multiplier + + ATs + + State +
+ @obj.ProvinceId + + @obj.Name + + Neutral + + Allies + + Axis + + @obj.Multiplier + + @obj.ATs + + At Peace + + At War +
\ No newline at end of file diff --git a/appsettings.json b/appsettings.json index 10f68b8..a7b4476 100644 --- a/appsettings.json +++ b/appsettings.json @@ -5,5 +5,10 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Server=CREATORPC;Database=BHDB;Trusted_Connection=True;TrustServerCertificate=True" + // the server name in `Server=` needs to point to an actual live server + // We use the command `update-database` in the NuGet Console to create the database + } }