Skip to content

Commit

Permalink
Add sql database and tables for user and session
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Apr 23, 2024
1 parent f9c2e68 commit 34a2445
Show file tree
Hide file tree
Showing 25 changed files with 659 additions and 61 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="localpw"
POSTGRES_URI="localhost"
POSTGRES_PORT="5432"
POSTGRES_DB_NAME="forc_pub"
DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_URI}/${POSTGRES_DB_NAME}"
173 changes: 172 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "forc_pub"
version = "0.1.0"
edition = "2021"

[lib]
name = "forc_pub"
path = "src/lib.rs"

[dependencies]
nanoid = "0.4.0"
hex = "0.4.3"
Expand All @@ -13,3 +17,7 @@ rocket = { version = "0.5.0-rc.2", features = ["tls", "json"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.12.2", features = ["json"] }
thiserror = "1.0.58"
diesel = { version = "2.1.6", features = ["postgres", "uuid", "r2d2"] }
dotenvy = "0.15"
uuid = "1.8.0"
diesel_migrations = "2.1.0"
8 changes: 4 additions & 4 deletions app/src/features/toolbar/components/UserButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ function UserButton() {
<StyledWrapper>
<Button color='inherit' onClick={handleMenu} endIcon={<ArrowDropDownIcon />}>
<img
src={user.avatar_url}
title={user.name}
alt={`${user.name} (TODO: login)`}
src={user.avatarUrl}
title={user.fullName}
alt={user.githubLogin}
style={{ height: '30px', width: '30px', borderRadius: '50%' }}
/>
<div style={{ marginLeft: '10px' }}>{user.name}</div>
<div style={{ marginLeft: '10px' }}>{user.fullName}</div>
</Button>
<Menu
anchorEl={anchorEl}
Expand Down
7 changes: 5 additions & 2 deletions app/src/features/toolbar/hooks/useGithubAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { useSearchParams } from 'react-router-dom';
import { useLocalSession } from '../../../utils/localStorage';

interface AuthenticatedUser {
avatar_url?: string;
name: string;
fullName: string;
email?: string;
githubUrl: string;
githubLogin: string;
isAdmin: boolean;
avatarUrl?: string;
}

interface LoginResponse {
Expand Down
7 changes: 7 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[target.aarch64-apple-darwin]
rustflags = [
'-L',
'/opt/homebrew/opt/libpq/lib',
'-L',
'/opt/homebrew/lib'
]
9 changes: 9 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"]

[migrations_directory]
dir = "migrations"
Empty file added migrations/.keep
Empty file.
6 changes: 6 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
36 changes: 36 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
1 change: 1 addition & 0 deletions migrations/2024-04-19-174554_create_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users
Loading

0 comments on commit 34a2445

Please sign in to comment.