Implemented the Province View

sql
Surya-AI 2024-11-17 07:23:08 +05:30
parent c2a4000ec1
commit 8dcf5c40f9
12 changed files with 410 additions and 1 deletions

View File

@ -10,6 +10,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
</ItemGroup>

View File

@ -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<Province> ObjectProvinceList = _db.Provinces.ToList();
// If the above line faulters then ensure SQL Server is running
return View(ObjectProvinceList);
}
}
}

View File

@ -0,0 +1,18 @@
using BHServer.Models;
using Microsoft.EntityFrameworkCore;
namespace BHServer.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
// this will pass along the options from the implementation to the definition
{
}
public DbSet<Province> Provinces { get; set; } // specify the table and it's class
// to run migrations, run `add-migration <name of the migration>` in the NuGet console
// then make the migrations from the console by using `update-database`
// to rollback migrations run the command `remove-migration <name>`
}
}

View File

@ -0,0 +1,59 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ATs")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Faction")
.HasColumnType("int");
b.Property<int>("Multiplier")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("State")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Provinces");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BHServer.Migrations
{
/// <inheritdoc />
public partial class AddProvincesToDb : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Provinces",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Faction = table.Column<int>(type: "int", nullable: false),
Multiplier = table.Column<int>(type: "int", nullable: false),
ATs = table.Column<string>(type: "nvarchar(max)", nullable: false),
State = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Provinces", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Provinces");
}
}
}

View File

@ -0,0 +1,62 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ATs")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Faction")
.HasColumnType("int");
b.Property<int>("Multiplier")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProvinceId")
.HasColumnType("int");
b.Property<int>("State")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Provinces");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BHServer.Migrations
{
/// <inheritdoc />
public partial class UpdateProvinceTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ProvinceId",
table: "Provinces",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ProvinceId",
table: "Provinces");
}
}
}

View File

@ -0,0 +1,59 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ATs")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Faction")
.HasColumnType("int");
b.Property<int>("Multiplier")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProvinceId")
.HasColumnType("int");
b.Property<int>("State")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Provinces");
});
#pragma warning restore 612, 618
}
}
}

21
Models/Province.cs Normal file
View File

@ -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
}
}

View File

@ -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<ApplicationDbContext>(
options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
// `ConnectionString` is defined in the appsettings.json file
);
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -0,0 +1,82 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@model List<Province>
<h1>Provinces</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Faction
</th>
<th>
Multiplier
</th>
<th>
ATs
</th>
<th>
State
</th>
</tr>
</thead>
<tbody>
@foreach (var obj in Model) {
<tr>
<th>
@obj.ProvinceId
</th>
<th>
@obj.Name
</th>
@switch (obj.Faction)
{
case 0:
<th>
Neutral
</th>
break;
case 1:
<th>
Allies
</th>
break;
case 2:
<th>
Axis
</th>
break;
}
<th>
@obj.Multiplier
</th>
<th>
@obj.ATs
</th>
@switch (obj.State)
{
case 0:
<th>
At Peace
</th>
break;
case 1:
<th>
At War
</th>
break;
}
</tr>
}
</tbody>
</table>

View File

@ -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
}
}