Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions lesson_25/createdb/create_users_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Create library_users table
CREATE TABLE IF NOT EXISTS library_users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
firstName TEXT NOT NULL,
lastName TEXT NOT NULL,
password TEXT NOT NULL
);

-- Insert sample users with bcrypt hashed passwords
-- Note: These passwords are bcrypt hashes of "password123" for demonstration purposes
INSERT OR REPLACE INTO library_users (id, email, firstName, lastName, password) VALUES
('550e8400-e29b-41d4-a716-446655440001', 'john.doe@email.com', 'John', 'Doe', '$2a$10$N9qo8uLOickgx2ZMRZoMye.FqUf.tZeHTQZ.H6eSSHhFXFjjKJWg6'),
('550e8400-e29b-41d4-a716-446655440002', 'jane.smith@email.com', 'Jane', 'Smith', '$2a$10$N9qo8uLOickgx2ZMRZoMye.FqUf.tZeHTQZ.H6eSSHhFXFjjKJWg6'),
('550e8400-e29b-41d4-a716-446655440003', 'bob.wilson@email.com', 'Bob', 'Wilson', '$2a$10$N9qo8uLOickgx2ZMRZoMye.FqUf.tZeHTQZ.H6eSSHhFXFjjKJWg6'),
('550e8400-e29b-41d4-a716-446655440004', 'alice.brown@email.com', 'Alice', 'Brown', '$2a$10$N9qo8uLOickgx2ZMRZoMye.FqUf.tZeHTQZ.H6eSSHhFXFjjKJWg6'),
('550e8400-e29b-41d4-a716-446655440005', 'trishtan.husser@email.com', 'Trishtan', 'Husser', '$2a$10$N9qo8uLOickgx2ZMRZoMye.FqUf.tZeHTQZ.H6eSSHhFXFjjKJWg6');

-- Verify the data was inserted
SELECT COUNT(*) as user_count FROM library_users;
SELECT * FROM library_users ORDER BY lastName, firstName;
19 changes: 15 additions & 4 deletions lesson_25/createdb/createdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
import sqlite3

# Step 1: Load the CSV file into a pandas DataFrame
media_items_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/media_items.csv')
guests_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/guests.csv')
checked_out_items_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/checked_out_items.csv')
media_items_df = pd.read_csv('../db/db_app/src/main/resources/csv/media_items.csv')
guests_df = pd.read_csv('../db/db_app/src/main/resources/csv/guests.csv')
checked_out_items_df = pd.read_csv('../db/db_app/src/main/resources/csv/checked_out_items.csv')
checked_out_items_df['due_date'] = pd.to_datetime(checked_out_items_df['due_date']).values.astype(np.int64)

# Step 2: Create a connection to the SQLite database
# Note: This will create the database file if it doesn't exist already
os.makedirs('/workspaces/code-society-25-2/lesson_25/db/db_app/src/main/resources/sqlite/', exist_ok=True)
os.makedirs('../db/db_app/src/main/resources/sqlite/', exist_ok=True)
conn = sqlite3.connect('../db/db_app/src/main/resources/sqlite/data.db')

# Step 3: Write the DataFrame to the SQLite database
media_items_df.to_sql('media_items', conn, if_exists='replace', index=False)
guests_df.to_sql('guests', conn, if_exists='replace', index=False)
checked_out_items_df.to_sql('checked_out_items', conn, if_exists='replace', index=False)

# Step 4: Execute the SQL script to create users table and add sample data
with open('create_users_table.sql', 'r') as sql_file:
sql_script = sql_file.read()
# Execute each statement separately
for statement in sql_script.split(';'):
if statement.strip():
conn.execute(statement)
conn.commit()

print("Database created successfully with all tables including library_users!")

# Don't forget to close the connection
conn.close()
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.codedifferently.lesson25.models.LibraryDataModel;
import com.codedifferently.lesson25.repository.LibraryGuestRepository;
import com.codedifferently.lesson25.repository.LibraryUserRepository;
import com.codedifferently.lesson25.repository.MediaItemRepository;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,13 +14,15 @@ public final class LibraryDbDataLoader implements LibraryDataLoader {

@Autowired private MediaItemRepository mediaItemsRepository;
@Autowired private LibraryGuestRepository libraryGuestRepository;
@Autowired private LibraryUserRepository libraryUserRepository;

@Override
public LibraryDataModel loadData() throws IOException {
var model = new LibraryDataModel();

model.mediaItems = mediaItemsRepository.findAll();
model.guests = libraryGuestRepository.findAll();
model.users = libraryUserRepository.findAll();

return model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LibraryDataModel {

public List<MediaItemModel> mediaItems;
public List<LibraryGuestModel> guests;
public List<LibraryUserModel> users;

public List<MediaItem> getMediaItems() {
List<MediaItem> results = new ArrayList<>();
Expand Down Expand Up @@ -59,4 +60,8 @@ public Map<String, List<CheckoutModel>> getCheckoutsByEmail() {
}
return results;
}

public List<LibraryUserModel> getUsers() {
return users != null ? users : new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.codedifferently.lesson25.models;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.UUID;

@Entity
@Table(name = "library_users")
public class LibraryUserModel {

@Id public String id;
public String email;
public String firstName;
public String lastName;
public String password;

// Default constructor for JPA
public LibraryUserModel() {}

// Constructor for creating new users
public LibraryUserModel(String email, String firstName, String lastName, String password) {
this.id = UUID.randomUUID().toString();
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
}

// Getters and setters
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getFullName() {
return firstName + " " + lastName;
}

@Override
public String toString() {
return "LibraryUserModel{"
+ "id='"
+ id
+ '\''
+ ", email='"
+ email
+ '\''
+ ", firstName='"
+ firstName
+ '\''
+ ", lastName='"
+ lastName
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.codedifferently.lesson25.repository;

import com.codedifferently.lesson25.models.LibraryUserModel;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;

public interface LibraryUserRepository extends CrudRepository<LibraryUserModel, String> {

@Override
List<LibraryUserModel> findAll();

Optional<LibraryUserModel> findByEmail(String email);

List<LibraryUserModel> findByFirstNameContainingIgnoreCase(String firstName);

List<LibraryUserModel> findByLastNameContainingIgnoreCase(String lastName);
}
30 changes: 30 additions & 0 deletions lesson_25/db/db_app/src/main/resources/queries/trishtanhusser.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Lesson 25 SQL Queries by Trishtan Husser
-- Query exercises for database operations

-- Query 1: Count of media items by type
-- This query returns the counts of media items grouped by their type
SELECT type, COUNT(*) as count
FROM media_items
GROUP BY type
ORDER BY count DESC;

-- Query 2: Sum of total pages checked out by guests
-- This query calculates the total pages from all checked out items
SELECT SUM(mi.pages) as total_pages_checked_out
FROM checked_out_items coi
JOIN media_items mi ON coi.itemId = mi.id
WHERE mi.pages IS NOT NULL;

-- Query 3: All guests with their corresponding checked out items (LEFT JOIN)
-- This query shows all 5 guests and any corresponding records in the checked_out_items table
SELECT g.name,
g.email,
g.type,
coi.itemId,
coi.dueDate,
mi.title,
mi.type as media_type
FROM guests g
LEFT JOIN checked_out_items coi ON g.email = coi.email
LEFT JOIN media_items mi ON coi.itemId = mi.id
ORDER BY g.name, coi.dueDate;
Binary file modified lesson_25/db/db_app/src/main/resources/sqlite/data.db
Binary file not shown.