ソースを参照

add controller for player

Florian 3 年 前
コミット
e358957dfb

+ 3 - 0
.vs/ProjectSettings.json

@@ -0,0 +1,3 @@
+{
+  "CurrentProjectSetting": null
+}

+ 3 - 2
.vs/VSWorkspaceState.json

@@ -1,7 +1,8 @@
 {
   "ExpandedNodes": [
-    ""
+    "",
+    "\\Dotnet"
   ],
-  "SelectedNode": "\\C:\\Repositories\\Warhammer\\Deathwatch",
+  "SelectedNode": "\\Dotnet\\Warhammer.sln",
   "PreviewInSolutionExplorer": false
 }

BIN
.vs/Warhammer/v16/.suo


BIN
.vs/Warhammer/v16/TestStore/0/000.testlog


BIN
.vs/Warhammer/v16/TestStore/0/testlog.manifest


BIN
.vs/slnx.sqlite


+ 25 - 0
Dotnet/.dockerignore

@@ -0,0 +1,25 @@
+**/.classpath
+**/.dockerignore
+**/.env
+**/.git
+**/.gitignore
+**/.project
+**/.settings
+**/.toolstarget
+**/.vs
+**/.vscode
+**/*.*proj.user
+**/*.dbmdl
+**/*.jfm
+**/azds.yaml
+**/bin
+**/charts
+**/docker-compose*
+**/Dockerfile*
+**/node_modules
+**/npm-debug.log
+**/obj
+**/secrets.dev.yaml
+**/values.dev.yaml
+LICENSE
+README.md

+ 3 - 3
Dotnet/Warhammer.sln

@@ -1,9 +1,9 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio Version 17
-VisualStudioVersion = 17.1.32421.90
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.32901.82
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Warhammer", "Warhammer\Warhammer.csproj", "{CAC4117F-9DAA-4631-8AFE-7CB1FE2183F6}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Warhammer", "Warhammer\Warhammer.csproj", "{CAC4117F-9DAA-4631-8AFE-7CB1FE2183F6}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 87 - 0
Dotnet/Warhammer/Controllers/PlayerController.cs

@@ -0,0 +1,87 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Warhammer.Controllers
+{
+    public class PlayerController : Controller
+    {
+        // GET: Player
+        public ActionResult Index()
+        {
+            return View();
+        }
+
+        // GET: Player/Details/5
+        public ActionResult Details(int id)
+        {
+            return View();
+        }
+
+        // GET: Player/Create
+        public ActionResult Create()
+        {
+            return View();
+        }
+
+        // POST: Player/Create
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Create(IFormCollection collection)
+        {
+            try
+            {
+                return RedirectToAction(nameof(Index));
+            }
+            catch
+            {
+                return View();
+            }
+        }
+
+        // GET: Player/Edit/5
+        public ActionResult Edit(int id)
+        {
+            return View();
+        }
+
+        // POST: Player/Edit/5
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Edit(int id, IFormCollection collection)
+        {
+            try
+            {
+                return RedirectToAction(nameof(Index));
+            }
+            catch
+            {
+                return View();
+            }
+        }
+
+        // GET: Player/Delete/5
+        public ActionResult Delete(int id)
+        {
+            return View();
+        }
+
+        // POST: Player/Delete/5
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Delete(int id, IFormCollection collection)
+        {
+            try
+            {
+                return RedirectToAction(nameof(Index));
+            }
+            catch
+            {
+                return View();
+            }
+        }
+    }
+}

+ 46 - 0
Dotnet/Warhammer/DataLayer/DbContextInitializer.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Warhammer.Models;
+
+namespace Warhammer.DataLayer
+{
+    public static class DbContextInitializer
+    {
+        public static void Initialize(DeathwatchDbContext context)
+        {
+            context.Database.EnsureCreated();
+
+            // Look for any students.
+            if (context.Players.Any())
+            {
+                return;   // DB has been seeded
+            }
+
+            var players = new Player[]
+            {
+                new Player
+                {
+                      Experience = 17400,
+                      CC = 52,
+                      CT = 40,
+                      F = 46,
+                      E = 45,
+                      AG = 50,
+                      INT = 34,
+                      PER = 43,
+                      FM = 43,
+                      SOC = 40          
+                }
+            };
+            foreach (Player player in players)
+            {
+                context.Players.Add(player);
+            }
+            context.SaveChanges();
+
+        }
+    }
+}
+

+ 23 - 0
Dotnet/Warhammer/DataLayer/DeathwatchDbContext.cs

@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.EntityFrameworkCore;
+using Warhammer.Models;
+
+namespace Warhammer.DataLayer
+{
+    public class DeathwatchDbContext : DbContext
+    {
+        public DeathwatchDbContext(DbContextOptions<DeathwatchDbContext> options) : base(options)
+        {
+        }
+
+        public virtual DbSet<Player> Players { get; set; }
+
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
+        {
+            modelBuilder.Entity<Player>().ToTable("Players").HasKey(p => p.Id);
+        }
+    }
+}

+ 6 - 2
Dotnet/Warhammer/Models/Player.cs

@@ -1,7 +1,11 @@
-namespace Warhammer.Models
+using System.ComponentModel.DataAnnotations;
+
+namespace Warhammer.Models
 {
-    public class Player
+    public partial class Player
     {
+        [Key]
+        public int Id { get; set; }
         public int Experience { get; set; }
         public int CC { get; set; }
         public int CT { get; set; }

+ 25 - 0
Dotnet/Warhammer/Program.cs

@@ -6,6 +6,8 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using Microsoft.Extensions.DependencyInjection;
+using Warhammer.DataLayer;
 
 namespace Warhammer
 {
@@ -14,6 +16,11 @@ namespace Warhammer
         public static void Main(string[] args)
         {
             CreateHostBuilder(args).Build().Run();
+            var host = CreateHostBuilder(args).Build();
+
+            CreateDbIfNotExists(host);
+
+            host.Run();
         }
 
         public static IHostBuilder CreateHostBuilder(string[] args) =>
@@ -22,5 +29,23 @@ namespace Warhammer
                 {
                     webBuilder.UseStartup<Startup>();
                 });
+
+        private static void CreateDbIfNotExists(IHost host)
+        {
+            using (var scope = host.Services.CreateScope())
+            {
+                var services = scope.ServiceProvider;
+                try
+                {
+                    var context = services.GetRequiredService<DeathwatchDbContext>();
+                    DbContextInitializer.Initialize(context);
+                }
+                catch (Exception ex)
+                {
+                    var logger = services.GetRequiredService<ILogger<Program>>();
+                    logger.LogError(ex, "An error occurred creating the DB.");
+                }
+            }
+        }
     }
 }

+ 5 - 0
Dotnet/Warhammer/Startup.cs

@@ -1,6 +1,7 @@
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.HttpsPolicy;
+using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
@@ -8,6 +9,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using Warhammer.DataLayer;
 
 namespace Warhammer
 {
@@ -24,6 +26,9 @@ namespace Warhammer
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddControllersWithViews();
+
+            services.AddDbContext<DeathwatchDbContext>(options =>
+                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Dotnet/Warhammer/Views/Home/Players.cshtml → Dotnet/Warhammer/Views/Home/Player.cshtml


+ 5 - 2
Dotnet/Warhammer/Warhammer.csproj

@@ -1,13 +1,16 @@
-<Project Sdk="Microsoft.NET.Sdk.Web">
+<Project Sdk="Microsoft.NET.Sdk.Web">
 
   <PropertyGroup>
     <TargetFramework>netcoreapp3.1</TargetFramework>
     <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
+    <StartupObject>Warhammer.Program</StartupObject>
   </PropertyGroup>
 
   <ItemGroup>
     <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.23" />
-    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
+    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
   </ItemGroup>
 
 </Project>

+ 5 - 0
Dotnet/Warhammer/Warhammer.csproj.user

@@ -8,5 +8,10 @@
     <WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
     <WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
     <WebStackScaffolding_LayoutPageFile />
+    <Controller_SelectedScaffolderID>MvcControllerWithActionsScaffolder</Controller_SelectedScaffolderID>
+    <Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
+    <WebStackScaffolding_ControllerDialogWidth>650</WebStackScaffolding_ControllerDialogWidth>
+    <WebStackScaffolding_DbContextDialogWidth>650</WebStackScaffolding_DbContextDialogWidth>
+    <ShowAllFiles>true</ShowAllFiles>
   </PropertyGroup>
 </Project>

+ 4 - 0
Dotnet/Warhammer/appsettings.json

@@ -1,4 +1,8 @@
 {
+
+  "ConnectionStrings": {
+    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Warhammer;Trusted_Connection=True;MultipleActiveResultSets=true"
+  },
   "Logging": {
     "LogLevel": {
       "Default": "Information",