Snootalogue/Models/Document.cs

46 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Snootalogue.Data;
using System.Runtime;
namespace Snootalogue.Models {
public class Document {
public string ID { get; set; }
public string Filename { get; set; }
public string Hash { get; set; }
[UIHint("FileSize")]
public long Size { get; set; }
public string Title { get; set; }
[UIHint("CommaSeparatedList")]
public List<string> Authors { get; set; }
public string Category { get; set; }
[Display(Name = "Date added")]
public DateTime DateAdded { get; set; }
[UIHint("CommaSeparatedList")]
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;
}
}
}