summaryrefslogtreecommitdiff
path: root/Models
diff options
context:
space:
mode:
Diffstat (limited to 'Models')
-rw-r--r--Models/Book.cs2
-rw-r--r--Models/Group.cs12
-rw-r--r--Models/GroupUserAccount.cs10
-rw-r--r--Models/LibraryContext.cs9
-rw-r--r--Models/UserAccount.cs30
5 files changed, 61 insertions, 2 deletions
diff --git a/Models/Book.cs b/Models/Book.cs
index 49bd527..4a05865 100644
--- a/Models/Book.cs
+++ b/Models/Book.cs
@@ -19,4 +19,4 @@ namespace LibraryAPI.Models {
[StringLength(128, ErrorMessage = "Title too long")]
public string? Title { get; set; }
}
-} \ No newline at end of file
+}
diff --git a/Models/Group.cs b/Models/Group.cs
new file mode 100644
index 0000000..f5ed59d
--- /dev/null
+++ b/Models/Group.cs
@@ -0,0 +1,12 @@
+using System.ComponentModel.DataAnnotations;
+using LibraryAPI.Validators;
+
+namespace LibraryAPI.Models {
+ public class Group {
+ public long Id { get; set; }
+ public ICollection<GroupUserAccount>? Users { get; set; }
+ [Required]
+ [Name]
+ public string? Name {get; set; }
+ }
+}
diff --git a/Models/GroupUserAccount.cs b/Models/GroupUserAccount.cs
new file mode 100644
index 0000000..706c2e9
--- /dev/null
+++ b/Models/GroupUserAccount.cs
@@ -0,0 +1,10 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace LibraryAPI.Models {
+ public class GroupUserAccount {
+ public long GroupId { get; set; }
+ public Group? Group { get; set; }
+ public long UserAccountId { get; set; }
+ public UserAccount? UserAccount { get; set; }
+ }
+}
diff --git a/Models/LibraryContext.cs b/Models/LibraryContext.cs
index b07ba54..3f56b0c 100644
--- a/Models/LibraryContext.cs
+++ b/Models/LibraryContext.cs
@@ -4,6 +4,13 @@ namespace LibraryAPI.Models {
public class LibraryContext : DbContext {
public DbSet<Author>? Authors { get; set; }
public DbSet<Book>? Books { get; set; }
+ public DbSet<UserAccount>? Users { get; set; }
+ public DbSet<Group>? Groups { get; set; }
+ public DbSet<GroupUserAccount>? GroupUserAccounts { get; set; }
public LibraryContext(DbContextOptions<LibraryContext> options) : base(options) {}
+
+ protected override void OnModelCreating(ModelBuilder modelBuilder) {
+ modelBuilder.Entity<GroupUserAccount>().HasKey(sc => new { sc.UserAccountId, sc.GroupId });
+ }
}
-} \ No newline at end of file
+}
diff --git a/Models/UserAccount.cs b/Models/UserAccount.cs
new file mode 100644
index 0000000..e3184ed
--- /dev/null
+++ b/Models/UserAccount.cs
@@ -0,0 +1,30 @@
+using System.ComponentModel.DataAnnotations;
+using Microsoft.AspNetCore.Identity;
+
+namespace LibraryAPI.Models {
+ public class UserAccount {
+ public long Id { get; set; }
+ [Required]
+ public string? Email { get; set; }
+ private string? _password;
+ [Required]
+ public string? Password { get { return _password; } set { _password = HashPassword(value); } }
+ public ICollection<GroupUserAccount>? Groups { get; set; }
+ private PasswordHasher<UserAccount> _hasher;
+
+ public UserAccount() {
+ _hasher = new PasswordHasher<UserAccount>();
+ }
+
+ public PasswordVerificationResult VerifyPassword(string to_verify) {
+ return _hasher.VerifyHashedPassword(this, Password, to_verify);
+ }
+
+ private string? HashPassword(string? unhashed_password) {
+ if(unhashed_password != null)
+ return _hasher.HashPassword(this, unhashed_password);
+
+ return null;
+ }
+ }
+}