Created database

This commit is contained in:
t.ruspekhofer 2022-02-15 21:59:44 +01:00
parent e068536abe
commit eec8e419ba
19 changed files with 618 additions and 4 deletions

View file

@ -0,0 +1,27 @@
using Lieb.Models;
namespace Lieb.Data
{
public class DbInitializer
{
public static void Initialize(LiebContext context)
{
// Look for any students.
if (context.Users.Any())
{
return; // DB has been seeded
}
var users = new User[]
{
new User{DiscordUserId=0, Name="Sarah",Birthday=DateTime.Parse("1992-01-15")},
new User{DiscordUserId=0, Name="Lisa",Birthday=DateTime.Parse("1991-02-15")},
new User{DiscordUserId=0, Name="Simon",Birthday=DateTime.Parse("2019-09-01")}
};
context.Users.AddRange(users);
context.SaveChanges();
}
}
}

47
Lieb/Data/LiebContext.cs Normal file
View file

@ -0,0 +1,47 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Lieb.Models;
using Lieb.Models.Raid;
namespace Lieb.Data
{
public class LiebContext : DbContext
{
public LiebContext (DbContextOptions<LiebContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<GuildWars2Account> GuildWars2Account { get; set; }
public DbSet<Equipped> Equipped { get; set; }
public DbSet<RaidRole> RaidRoles { get; set; }
public DbSet<PlannedRaid> PlannedRaids { get; set; }
public DbSet<PlannedRaidRole> PlannedRaidRoles { get; set; }
public DbSet<Raid> Raids { get; set; }
public DbSet<RaidReminder> RaidReminders { get; set; }
public DbSet<RaidSignUp> RaidSignUps { get; set; }
public DbSet<RandomRaid> RandomRaids { get; set; }
public DbSet<SignUpHistory> SignUpHistories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("User");
modelBuilder.Entity<GuildWars2Account>().ToTable("GuildWars2Account");
modelBuilder.Entity<Equipped>().ToTable("Equipped");
modelBuilder.Entity<RaidRole>().ToTable("RaidRole");
modelBuilder.Entity<PlannedRaid>().ToTable("PlannedRaid");
modelBuilder.Entity<PlannedRaidRole>().ToTable("PlannedRaidRole");
modelBuilder.Entity<Raid>().ToTable("Raid");
modelBuilder.Entity<RaidReminder>().ToTable("RaidReminder");
modelBuilder.Entity<RaidSignUp>().ToTable("RaidSignUp");
modelBuilder.Entity<RandomRaid>().ToTable("RandomRaid");
modelBuilder.Entity<SignUpHistory>().ToTable("SignUpHistory");
}
}
}

View file

@ -6,4 +6,18 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Folder Include="Pages\Raids\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.2" />
</ItemGroup>
</Project>

View file

@ -5,7 +5,7 @@
public int UserId { get; set; }
public ulong DiscordUserId { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime Birthday { get; set; }
public DateTime? Birthday { get; set; }
public ICollection<GuildWars2Account> GuildWars2Accounts { get; set; } = new List<GuildWars2Account>();
}
}

View file

@ -0,0 +1,44 @@
@page
@model Lieb.Pages.Users.CreateModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>User</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="User.DiscordUserId" class="control-label"></label>
<input asp-for="User.DiscordUserId" class="form-control" />
<span asp-validation-for="User.DiscordUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.Name" class="control-label"></label>
<input asp-for="User.Name" class="form-control" />
<span asp-validation-for="User.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.Birthday" class="control-label"></label>
<input asp-for="User.Birthday" class="form-control" />
<span asp-validation-for="User.Birthday" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View file

@ -0,0 +1,45 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Lieb.Data;
using Lieb.Models;
namespace Lieb.Pages.Users
{
public class CreateModel : PageModel
{
private readonly Lieb.Data.LiebContext _context;
public CreateModel(Lieb.Data.LiebContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public User User { get; set; }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Users.Add(User);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}

View file

@ -0,0 +1,40 @@
@page
@model Lieb.Pages.Users.DeleteModel
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>User</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.DiscordUserId)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.DiscordUserId)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Birthday)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Birthday)
</dd>
</dl>
<form method="post">
<input type="hidden" asp-for="User.UserId" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-page="./Index">Back to List</a>
</form>
</div>

View file

@ -0,0 +1,60 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Lieb.Data;
using Lieb.Models;
namespace Lieb.Pages.Users
{
public class DeleteModel : PageModel
{
private readonly Lieb.Data.LiebContext _context;
public DeleteModel(Lieb.Data.LiebContext context)
{
_context = context;
}
[BindProperty]
public User User { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
User = await _context.Users.FirstOrDefaultAsync(m => m.UserId == id);
if (User == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
User = await _context.Users.FindAsync(id);
if (User != null)
{
_context.Users.Remove(User);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
}
}

View file

@ -0,0 +1,37 @@
@page
@model Lieb.Pages.Users.DetailsModel
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>User</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.DiscordUserId)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.DiscordUserId)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Birthday)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Birthday)
</dd>
</dl>
</div>
<div>
<a asp-page="./Edit" asp-route-id="@Model.User.UserId">Edit</a> |
<a asp-page="./Index">Back to List</a>
</div>

View file

@ -0,0 +1,41 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Lieb.Data;
using Lieb.Models;
namespace Lieb.Pages.Users
{
public class DetailsModel : PageModel
{
private readonly Lieb.Data.LiebContext _context;
public DetailsModel(Lieb.Data.LiebContext context)
{
_context = context;
}
public User User { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
User = await _context.Users.FirstOrDefaultAsync(m => m.UserId == id);
if (User == null)
{
return NotFound();
}
return Page();
}
}
}

View file

@ -0,0 +1,45 @@
@page
@model Lieb.Pages.Users.EditModel
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>User</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="User.UserId" />
<div class="form-group">
<label asp-for="User.DiscordUserId" class="control-label"></label>
<input asp-for="User.DiscordUserId" class="form-control" />
<span asp-validation-for="User.DiscordUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.Name" class="control-label"></label>
<input asp-for="User.Name" class="form-control" />
<span asp-validation-for="User.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="User.Birthday" class="control-label"></label>
<input asp-for="User.Birthday" class="form-control" />
<span asp-validation-for="User.Birthday" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="./Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View file

@ -0,0 +1,78 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Lieb.Data;
using Lieb.Models;
namespace Lieb.Pages.Users
{
public class EditModel : PageModel
{
private readonly Lieb.Data.LiebContext _context;
public EditModel(Lieb.Data.LiebContext context)
{
_context = context;
}
[BindProperty]
public User User { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
User = await _context.Users.FirstOrDefaultAsync(m => m.UserId == id);
if (User == null)
{
return NotFound();
}
return Page();
}
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(User).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(User.UserId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool UserExists(int id)
{
return _context.Users.Any(e => e.UserId == id);
}
}
}

View file

@ -0,0 +1,48 @@
@page
@model Lieb.Pages.Users.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.User[0].DiscordUserId)
</th>
<th>
@Html.DisplayNameFor(model => model.User[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.User[0].Birthday)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.User) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.DiscordUserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Birthday)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.UserId">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.UserId">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.UserId">Delete</a>
</td>
</tr>
}
</tbody>
</table>

View file

@ -0,0 +1,30 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Lieb.Data;
using Lieb.Models;
namespace Lieb.Pages.Users
{
public class IndexModel : PageModel
{
private readonly Lieb.Data.LiebContext _context;
public IndexModel(Lieb.Data.LiebContext context)
{
_context = context;
}
public IList<User> User { get;set; }
public async Task OnGetAsync()
{
User = await _context.Users.ToListAsync();
}
}
}

View file

@ -0,0 +1,18 @@
<environment names="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment names="Staging,Production">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.17.0/jquery.validate.min.js"
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha384-rZfj/ogBloos6wzLGpPkkOr/gpkBNLZ6b6yLy4o+ok+t/SAKlL5mvXLr0OXNi1Hp">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.9/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha384-ifv0TYDWxBHzvAk2Z0n8R434FL1Rlv/Av18DXE43N/1rvHyOG4izKst0f2iSLdds">
</script>
</environment>

View file

@ -1,13 +1,21 @@
using Discord.OAuth2;
using Discord.OAuth2;
using Lieb.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<LiebContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("LiebContext")));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddServerSideBlazor();
builder.Services.AddAuthentication(opt =>
{
@ -33,7 +41,20 @@ if (!app.Environment.IsDevelopment())
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
else
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<LiebContext>();
context.Database.EnsureCreated();
DbInitializer.Initialize(context);
}
app.UseHttpsRedirection();

View file

@ -0,0 +1,8 @@
{
"dependencies": {
"mssql1": {
"type": "mssql",
"connectionId": "ConnectionStrings:LiebContext"
}
}
}

View file

@ -0,0 +1,8 @@
{
"dependencies": {
"mssql1": {
"type": "mssql.local",
"connectionId": "ConnectionStrings:LiebContext"
}
}
}

View file

@ -9,5 +9,8 @@
"AppId": "942448872335220806",
"AppSecret": "5tsNxK9LtFpNqxqQnLkSJ1B0HJ7P7YUF"
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"LiebContext": "Server=(localdb)\\mssqllocaldb;Database=LiebContext-f9dae0eb-8d5c-495d-ab99-da7951e0638b;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}