sannlynnhtun-coding commited on
Commit
258d719
·
1 Parent(s): c8d434a

feat: add database schema and seed data for Library Management System

Browse files
Database/Models/Book.cs CHANGED
@@ -20,8 +20,6 @@ public partial class Book
20
  public DateTime CreatedAt { get; set; }
21
 
22
  public string? Description { get; set; }
23
-
24
- public string? CoverUrl { get; set; }
25
 
26
  public int TotalCopies { get; set; }
27
 
 
20
  public DateTime CreatedAt { get; set; }
21
 
22
  public string? Description { get; set; }
 
 
23
 
24
  public int TotalCopies { get; set; }
25
 
Database/Models/LibraryManagementContext.cs CHANGED
@@ -29,10 +29,6 @@ public partial class LibraryManagementContext : DbContext
29
 
30
  public virtual DbSet<Wishlist> Wishlists { get; set; }
31
 
32
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
33
- #warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
34
- => optionsBuilder.UseSqlServer("Server=DESKTOP-BP9A061;Database=LibraryManagement;User Id=sa;Password=sasa@123;TrustServerCertificate=True;");
35
-
36
  protected override void OnModelCreating(ModelBuilder modelBuilder)
37
  {
38
  modelBuilder.Entity<Book>(entity =>
 
29
 
30
  public virtual DbSet<Wishlist> Wishlists { get; set; }
31
 
 
 
 
 
32
  protected override void OnModelCreating(ModelBuilder modelBuilder)
33
  {
34
  modelBuilder.Entity<Book>(entity =>
Database/db.sql ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- Library Management System Database Schema (MSSQL)
2
+ CREATE DATABASE LibraryManagement;
3
+ GO
4
+
5
+ USE LibraryManagement;
6
+ GO
7
+
8
+ -- 1. Categories Table
9
+ CREATE TABLE Categories (
10
+ Id INT IDENTITY(1,1) PRIMARY KEY,
11
+ Name NVARCHAR(100) NOT NULL,
12
+ CONSTRAINT UQ_Categories_Name UNIQUE (Name)
13
+ );
14
+ GO
15
+
16
+ -- 2. Books Table
17
+ CREATE TABLE Books (
18
+ Id INT IDENTITY(1,1) PRIMARY KEY,
19
+ Title NVARCHAR(200) NOT NULL,
20
+ ISBN NVARCHAR(20) NOT NULL,
21
+ Author NVARCHAR(100) NOT NULL,
22
+ Status NVARCHAR(20) NOT NULL DEFAULT 'Available',
23
+ IsActive BIT NOT NULL DEFAULT 1,
24
+ CreatedAt DATETIME NOT NULL DEFAULT (GETDATE()),
25
+ Description NVARCHAR(MAX) NULL,
26
+ TotalCopies INT NOT NULL DEFAULT 1,
27
+ AvailableCopies INT NOT NULL DEFAULT 1,
28
+ UpdatedAt DATETIME NULL,
29
+ CONSTRAINT UQ_Books_ISBN UNIQUE (ISBN)
30
+ );
31
+ GO
32
+
33
+ -- 3. Users Table
34
+ CREATE TABLE Users (
35
+ Id INT IDENTITY(1,1) PRIMARY KEY,
36
+ FullName NVARCHAR(100) NOT NULL,
37
+ Email NVARCHAR(100) NOT NULL,
38
+ PasswordHash NVARCHAR(MAX) NOT NULL,
39
+ PhoneNumber NVARCHAR(20) NULL,
40
+ Role NVARCHAR(20) NOT NULL DEFAULT 'Member',
41
+ IsActive BIT NOT NULL DEFAULT 1,
42
+ CreatedAt DATETIME NOT NULL DEFAULT (GETDATE()),
43
+ UpdatedAt DATETIME NULL,
44
+ StudentId NVARCHAR(20) NULL,
45
+ BanStatus BIT NULL,
46
+ SuspensionEndDate DATETIME NULL,
47
+ Address NVARCHAR(255) NULL,
48
+ CONSTRAINT UQ_Users_Email UNIQUE (Email)
49
+ );
50
+ GO
51
+
52
+ -- 4. Memberships Table
53
+ CREATE TABLE Memberships (
54
+ Id INT IDENTITY(1,1) PRIMARY KEY,
55
+ Type NVARCHAR(50) NOT NULL,
56
+ MaxBooks INT NOT NULL,
57
+ BorrowingDays INT NOT NULL,
58
+ Price DECIMAL(10, 2) NOT NULL,
59
+ DurationMonths INT NOT NULL,
60
+ CONSTRAINT IX_Memberships_Type UNIQUE (Type)
61
+ );
62
+ GO
63
+
64
+ -- 5. BookCategories (Many-to-Many Join Table)
65
+ CREATE TABLE BookCategories (
66
+ BookId INT NOT NULL,
67
+ CategoryId INT NOT NULL,
68
+ CONSTRAINT PK_BookCategories PRIMARY KEY (BookId, CategoryId),
69
+ CONSTRAINT FK_BookCategories_Books FOREIGN KEY (BookId) REFERENCES Books(Id) ON DELETE CASCADE,
70
+ CONSTRAINT FK_BookCategories_Categories FOREIGN KEY (CategoryId) REFERENCES Categories(Id) ON DELETE CASCADE
71
+ );
72
+ GO
73
+
74
+ -- 6. Borrowings Table
75
+ CREATE TABLE Borrowings (
76
+ Id INT IDENTITY(1,1) PRIMARY KEY,
77
+ UserId INT NOT NULL,
78
+ BookId INT NOT NULL,
79
+ BorrowDate DATETIME NOT NULL DEFAULT (GETDATE()),
80
+ DueDate DATETIME NOT NULL,
81
+ ReturnDate DATETIME NULL,
82
+ Status NVARCHAR(20) NOT NULL DEFAULT 'Active',
83
+ FineAmount DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
84
+ IsFinePaid BIT NOT NULL DEFAULT 0,
85
+ CONSTRAINT FK_Borrowings_Users FOREIGN KEY (UserId) REFERENCES Users(Id),
86
+ CONSTRAINT FK_Borrowings_Books FOREIGN KEY (BookId) REFERENCES Books(Id)
87
+ );
88
+ GO
89
+
90
+ -- 7. UserSubscriptions Table
91
+ CREATE TABLE UserSubscriptions (
92
+ Id INT IDENTITY(1,1) PRIMARY KEY,
93
+ UserId INT NOT NULL,
94
+ MembershipId INT NOT NULL,
95
+ StartDate DATETIME NOT NULL,
96
+ ExpiryDate DATETIME NOT NULL,
97
+ IsActive BIT NOT NULL DEFAULT 1,
98
+ CONSTRAINT FK_UserSubscriptions_Users FOREIGN KEY (UserId) REFERENCES Users(Id),
99
+ CONSTRAINT FK_UserSubscriptions_Memberships FOREIGN KEY (MembershipId) REFERENCES Memberships(Id)
100
+ );
101
+ GO
102
+
103
+ -- 8. Wishlists Table
104
+ CREATE TABLE Wishlists (
105
+ Id INT IDENTITY(1,1) PRIMARY KEY,
106
+ UserId INT NOT NULL,
107
+ BookId INT NOT NULL,
108
+ CONSTRAINT FK_Wishlists_Users FOREIGN KEY (UserId) REFERENCES Users(Id),
109
+ CONSTRAINT FK_Wishlists_Books FOREIGN KEY (BookId) REFERENCES Books(Id)
110
+ );
111
+ GO
Database/seed_data.sql ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- Seed Data for Library Management System
2
+ USE LibraryManagement;
3
+ GO
4
+
5
+ -- 1. Insert Categories
6
+ SET IDENTITY_INSERT Categories ON;
7
+ INSERT INTO Categories (Id, Name) VALUES
8
+ (1, 'Fiction'),
9
+ (2, 'Science'),
10
+ (3, 'History'),
11
+ (4, 'Biography'),
12
+ (5, 'Technology'),
13
+ (6, 'Philosophy'),
14
+ (7, 'Self-Help'),
15
+ (8, 'Fantasy'),
16
+ (9, 'Mystery'),
17
+ (10, 'Business');
18
+ SET IDENTITY_INSERT Categories OFF;
19
+ GO
20
+
21
+ -- 2. Insert Memberships
22
+ SET IDENTITY_INSERT Memberships ON;
23
+ INSERT INTO Memberships (Id, Type, MaxBooks, BorrowingDays, Price, DurationMonths) VALUES
24
+ (1, 'Basic', 2, 7, 10.00, 1),
25
+ (2, 'Standard', 5, 14, 25.00, 3),
26
+ (3, 'Premium', 10, 30, 80.00, 12),
27
+ (4, 'Student', 3, 15, 15.00, 6);
28
+ SET IDENTITY_INSERT Memberships OFF;
29
+ GO
30
+
31
+ -- 3. Insert Users (Password is 'Password123' hashed typically, but using placeholder for SQL)
32
+ SET IDENTITY_INSERT Users ON;
33
+ INSERT INTO Users (Id, FullName, Email, PasswordHash, PhoneNumber, Role, IsActive, CreatedAt, Address) VALUES
34
+ (1, 'Admin User', 'admin@library.com', 'AQAAAAEAACcQAAAAE...', '09123456789', 'Admin', 1, GETDATE(), '123 Main St, Central City'),
35
+ (2, 'John Doe', 'john.doe@gmail.com', 'AQAAAAEAACcQAAAAE...', '09234567890', 'Member', 1, GETDATE(), '456 Oak Ave, North Side'),
36
+ (3, 'Jane Smith', 'jane.smith@yahoo.com', 'AQAAAAEAACcQAAAAE...', '09345678901', 'Member', 1, GETDATE(), '789 Pine Rd, South Village'),
37
+ (4, 'David Wilson', 'david.w@university.edu', 'AQAAAAEAACcQAAAAE...', '09456789012', 'Member', 1, GETDATE(), '101 University Dr, Campus'),
38
+ (5, 'Sarah Miller', 'sarah.m@gmail.com', 'AQAAAAEAACcQAAAAE...', '09567890123', 'Member', 1, GETDATE(), '202 Lake View, West Coast');
39
+ SET IDENTITY_INSERT Users OFF;
40
+ GO
41
+
42
+ -- 4. Insert Books
43
+ SET IDENTITY_INSERT Books ON;
44
+ INSERT INTO Books (Id, Title, ISBN, Author, Status, IsActive, CreatedAt, Description, TotalCopies, AvailableCopies) VALUES
45
+ (1, 'The Great Gatsby', '9780743273565', 'F. Scott Fitzgerald', 'Available', 1, GETDATE(), 'A classic novel of the Jazz Age.', 5, 5),
46
+ (2, 'A Brief History of Time', '9780553380163', 'Stephen Hawking', 'Available', 1, GETDATE(), 'Exploring the origin and fate of the universe.', 3, 3),
47
+ (3, 'Steve Jobs', '9781451648539', 'Walter Isaacson', 'Available', 1, GETDATE(), 'The exclusive biography of Steve Jobs.', 2, 2),
48
+ (4, 'The Pragmatic Programmer', '9780135957059', 'Andrew Hunt', 'Available', 1, GETDATE(), 'Your journey to mastery in software development.', 4, 4),
49
+ (5, 'Sapiens', '9780062316097', 'Yuval Noah Harari', 'Available', 1, GETDATE(), 'A brief history of humankind.', 6, 6),
50
+ (6, 'The Hobbit', '9780547928227', 'J.R.R. Tolkien', 'Available', 1, GETDATE(), 'A prelude to The Lord of the Rings.', 8, 8),
51
+ (7, 'Atomic Habits', '9780735211292', 'James Clear', 'Available', 1, GETDATE(), 'An easy & proven way to build good habits.', 10, 10),
52
+ (8, 'The Da Vinci Code', '9780307474278', 'Dan Brown', 'Available', 1, GETDATE(), 'A murder in the Louvre leads to a complex mystery.', 5, 5),
53
+ (9, 'Lean In', '9780385349949', 'Sheryl Sandberg', 'Available', 1, GETDATE(), 'Women, work, and the will to lead.', 3, 3),
54
+ (10, 'Clean Code', '9780132350884', 'Robert C. Martin', 'Available', 1, GETDATE(), 'A handbook of agile software craftsmanship.', 4, 4);
55
+ SET IDENTITY_INSERT Books OFF;
56
+ GO
57
+
58
+ -- 5. Book-Category Mapping
59
+ INSERT INTO BookCategories (BookId, CategoryId) VALUES
60
+ (1, 1), -- Great Gatsby - Fiction
61
+ (2, 2), -- Brief History - Science
62
+ (3, 4), -- Steve Jobs - Biography
63
+ (4, 5), -- Pragmatic Programmer - Technology
64
+ (5, 3), -- Sapiens - History
65
+ (6, 8), -- Hobbit - Fantasy
66
+ (7, 7), -- Atomic Habits - Self-Help
67
+ (8, 9), -- Da Vinci Code - Mystery
68
+ (9, 10), -- Lean In - Business
69
+ (10, 5); -- Clean Code - Technology
70
+ GO
71
+
72
+ -- 6. User Subscriptions
73
+ INSERT INTO UserSubscriptions (UserId, MembershipId, StartDate, ExpiryDate, IsActive) VALUES
74
+ (2, 2, '2026-01-01', '2026-04-01', 1),
75
+ (3, 3, '2026-01-01', '2027-01-01', 1),
76
+ (4, 4, '2026-02-15', '2026-08-15', 1),
77
+ (5, 1, '2026-04-01', '2026-05-01', 1);
78
+ GO
79
+
80
+ -- 7. Some Borrowing History
81
+ INSERT INTO Borrowings (UserId, BookId, BorrowDate, DueDate, ReturnDate, Status) VALUES
82
+ (2, 1, '2026-04-10', '2026-04-24', '2026-04-15', 'Returned'),
83
+ (2, 4, '2026-04-16', '2026-04-30', NULL, 'Active'),
84
+ (3, 7, '2026-04-01', '2026-05-01', NULL, 'Active'),
85
+ (4, 10, '2026-04-18', '2026-05-03', NULL, 'Active');
86
+ GO