use random IDs instead of just sequential numbers

This commit is contained in:
Lynne Megido 2020-09-20 13:43:08 +10:00
parent b61ec6b165
commit 68c6fe6395
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
15 changed files with 41 additions and 18 deletions

View file

@ -21,7 +21,7 @@ namespace Snootalogue.Controllers {
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<Document>> GetDocumentById(int id) { public async Task<ActionResult<Document>> GetDocumentById(string id) {
var document = await _context.Document.FindAsync(id); var document = await _context.Document.FindAsync(id);
if (document == null) { if (document == null) {

View file

@ -9,7 +9,7 @@ using Snootalogue.Data;
namespace snootalogue.Migrations namespace snootalogue.Migrations
{ {
[DbContext(typeof(SnootalogueContext))] [DbContext(typeof(SnootalogueContext))]
[Migration("20200917125618_InitialCreate")] [Migration("20200920033715_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -20,9 +20,8 @@ namespace snootalogue.Migrations
modelBuilder.Entity("Snootalogue.Models.Document", b => modelBuilder.Entity("Snootalogue.Models.Document", b =>
{ {
b.Property<int>("ID") b.Property<string>("ID")
.ValueGeneratedOnAdd() .HasColumnType("TEXT");
.HasColumnType("INTEGER");
b.Property<string>("Authors") b.Property<string>("Authors")
.HasColumnType("jsonb"); .HasColumnType("jsonb");

View file

@ -11,8 +11,7 @@ namespace snootalogue.Migrations
name: "Document", name: "Document",
columns: table => new columns: table => new
{ {
ID = table.Column<int>(nullable: false) ID = table.Column<string>(nullable: false),
.Annotation("Sqlite:Autoincrement", true),
Filename = table.Column<string>(nullable: true), Filename = table.Column<string>(nullable: true),
Hash = table.Column<string>(nullable: true), Hash = table.Column<string>(nullable: true),
Size = table.Column<long>(nullable: false), Size = table.Column<long>(nullable: false),

View file

@ -18,9 +18,8 @@ namespace snootalogue.Migrations
modelBuilder.Entity("Snootalogue.Models.Document", b => modelBuilder.Entity("Snootalogue.Models.Document", b =>
{ {
b.Property<int>("ID") b.Property<string>("ID")
.ValueGeneratedOnAdd() .HasColumnType("TEXT");
.HasColumnType("INTEGER");
b.Property<string>("Authors") b.Property<string>("Authors")
.HasColumnType("jsonb"); .HasColumnType("jsonb");

View file

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Snootalogue.Data;
using System.Runtime;
namespace Snootalogue.Models { namespace Snootalogue.Models {
public class Document { public class Document {
public int ID { get; set; } public string ID { get; set; }
public string Filename { get; set; } public string Filename { get; set; }
public string Hash { get; set; } public string Hash { get; set; }
[UIHint("FileSize")] [UIHint("FileSize")]
@ -20,5 +22,24 @@ namespace Snootalogue.Models {
public List<string> Tags { get; set; } public List<string> Tags { get; set; }
public Boolean Read { get; set; } public Boolean Read { get; set; }
public string Notes { get; set; } public string Notes { get; set; }
public static string NewID(SnootalogueContext context) {
// - no vowels to avoid (most) (non-welsh) naughty words
// - no l1I because it's always confusing
// - no 0
string idCharacters = "bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ23456789";
var rng = new Random();
var collision = true;
string id = "";
while (collision) {
id = "";
for (var i = 0; i < 7; i++) {
id += idCharacters[rng.Next(idCharacters.Length)];
}
collision = context.Document.Find(id) != null;
}
return id;
}
} }
} }

View file

@ -18,6 +18,7 @@ namespace Snootalogue.Models {
context.Document.AddRange( context.Document.AddRange(
new Document { new Document {
ID = Document.NewID(context),
Title = "The Peebler Chronicles", Title = "The Peebler Chronicles",
Filename = "peebler.pdf", Filename = "peebler.pdf",
Size = Convert.ToInt64(0.5 * 1024 * 1024), Size = Convert.ToInt64(0.5 * 1024 * 1024),
@ -27,6 +28,7 @@ namespace Snootalogue.Models {
Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0" Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0"
}, },
new Document { new Document {
ID = Document.NewID(context),
Title = "Smooched by a Wifeoid", Title = "Smooched by a Wifeoid",
Filename = "smooched_by_a_wifeoid.pdf", Filename = "smooched_by_a_wifeoid.pdf",
Size = 7 * 1024 * 1024, Size = 7 * 1024 * 1024,
@ -37,6 +39,7 @@ namespace Snootalogue.Models {
Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0" Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0"
}, },
new Document { new Document {
ID = Document.NewID(context),
Title = "What on Boo Earth? My Girlfriend can Walk on the Ceiling?!", Title = "What on Boo Earth? My Girlfriend can Walk on the Ceiling?!",
Filename = "ceiling_gf.pdf", Filename = "ceiling_gf.pdf",
Size = Convert.ToInt64(22.5 * 1024 * 1024), Size = Convert.ToInt64(22.5 * 1024 * 1024),

View file

@ -1,4 +1,4 @@
@page "{id:int}" @page "{id}"
@model Snootalogue.Pages.Documents.DetailsModel @model Snootalogue.Pages.Documents.DetailsModel
@{ @{

View file

@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents {
_context = context; _context = context;
} }
public async Task<IActionResult> OnGetAsync(int? id) { public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) { if (id == null) {
return NotFound(); return NotFound();
} }

View file

@ -1,4 +1,4 @@
@page "{id:int}" @page "{id}"
@model Snootalogue.Pages.Documents.EditModel @model Snootalogue.Pages.Documents.EditModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View file

@ -17,7 +17,7 @@ namespace Snootalogue.Pages.Documents {
_context = context; _context = context;
} }
public async Task<IActionResult> OnGetAsync(int? id) { public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) { if (id == null) {
return NotFound(); return NotFound();
} }

View file

@ -1,4 +1,4 @@
@page "{id:int}" @page "{id}"
@model Snootalogue.Pages.Documents.ViewModel @model Snootalogue.Pages.Documents.ViewModel
@{ @{

View file

@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents {
_context = context; _context = context;
} }
public async Task<IActionResult> OnGetAsync(int? id) { public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) { if (id == null) {
return NotFound(); return NotFound();
} }

View file

@ -43,6 +43,7 @@ namespace Snootalogue.Pages {
// Document d = _context.Document. // Document d = _context.Document.
Document d = new Document { Document d = new Document {
ID = Document.NewID(_context),
Title = ud.Title, Title = ud.Title,
Authors = ud.Authors.Split(",").ToList<string>(), Authors = ud.Authors.Split(",").ToList<string>(),
Category = ud.Category, Category = ud.Category,

View file

@ -16,6 +16,7 @@ Snootalogue runs on port **5000**, so make sure you don't have anything currentl
```bash ```bash
git clone https://git.bune.city/lynnesbian/Snootalogue git clone https://git.bune.city/lynnesbian/Snootalogue
cd Snootalogue cd Snootalogue
mkdir wwwroot/Content
if ! which dotnet-ef; then dotnet tool install --global dotnet-ef; fi if ! which dotnet-ef; then dotnet tool install --global dotnet-ef; fi
dotnet ef database update dotnet ef database update
dotnet run dotnet run

View file

@ -7,7 +7,7 @@ using System.ComponentModel.DataAnnotations;
namespace Snootalogue.ViewModels { namespace Snootalogue.ViewModels {
public class EditDocument { public class EditDocument {
[Required] [Required]
public int ID { get; set; } public string ID { get; set; }
[Required] [Required]
public string Title { get; set; } public string Title { get; set; }
[Required] [Required]