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}")]
public async Task<ActionResult<Document>> GetDocumentById(int id) {
public async Task<ActionResult<Document>> GetDocumentById(string id) {
var document = await _context.Document.FindAsync(id);
if (document == null) {

View file

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

View file

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

View file

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

View file

@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Snootalogue.Data;
using System.Runtime;
namespace Snootalogue.Models {
public class Document {
public int ID { get; set; }
public string ID { get; set; }
public string Filename { get; set; }
public string Hash { get; set; }
[UIHint("FileSize")]
@ -20,5 +22,24 @@ namespace Snootalogue.Models {
public List<string> Tags { get; set; }
public Boolean Read { 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(
new Document {
ID = Document.NewID(context),
Title = "The Peebler Chronicles",
Filename = "peebler.pdf",
Size = Convert.ToInt64(0.5 * 1024 * 1024),
@ -27,6 +28,7 @@ namespace Snootalogue.Models {
Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0"
},
new Document {
ID = Document.NewID(context),
Title = "Smooched by a Wifeoid",
Filename = "smooched_by_a_wifeoid.pdf",
Size = 7 * 1024 * 1024,
@ -37,6 +39,7 @@ namespace Snootalogue.Models {
Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0"
},
new Document {
ID = Document.NewID(context),
Title = "What on Boo Earth? My Girlfriend can Walk on the Ceiling?!",
Filename = "ceiling_gf.pdf",
Size = Convert.ToInt64(22.5 * 1024 * 1024),

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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