Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/total games #59

Merged
merged 257 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
257 commits
Select commit Hold shift + click to select a range
d8dd845
chore(readme): Update Readme and .env example to match current state …
Likqez Nov 7, 2024
3f10687
disable ssr
Likqez Nov 15, 2024
ff4e5c8
fix(api): Playlists were disabled on creation
Likqez Nov 15, 2024
f0ac62d
Merge remote-tracking branch 'origin/feat/api' into feat/api
Likqez Nov 15, 2024
373980b
feat(layout): HeaderFooterView now centering like mobile
Likqez Nov 29, 2024
7c5d310
added categories to get requests
synan798 Nov 29, 2024
3d511f3
added categories to post request (getPlaylistCover doesn't work)
synan798 Nov 30, 2024
05ef067
feat(friends): Start implementing database layer
Likqez Nov 30, 2024
a8a731d
fix(playlist): Fix playlist cover not loading
Likqez Nov 30, 2024
d6f80fa
feat(playlist): Add DDL for playlist database schema
Likqez Nov 30, 2024
c6a32f2
feat(friends): Add decline db function
Likqez Nov 30, 2024
09e5ca2
Merge pull request #3 from BeatBuzzer/feat/home-view
synan798 Nov 30, 2024
90aaf2d
added correct return statement
synan798 Nov 30, 2024
f52f50c
feat(api): Add sample code for features with high usage likelihood
Likqez Oct 17, 2024
d073ffb
feat(api): Add Playlist & User Endpoint
Likqez Oct 18, 2024
d29d61e
chore(docs): add typedoc
Likqez Oct 31, 2024
b41df33
feat(api): Implement playlist endpoint
Likqez Oct 31, 2024
028839f
chore(readme): Update Readme and .env example to match current state …
Likqez Nov 7, 2024
7c84399
fix(api): Playlists were disabled on creation
Likqez Nov 15, 2024
20d8674
added categories to get requests
synan798 Nov 29, 2024
b415e8e
added categories to post request (getPlaylistCover doesn't work)
synan798 Nov 30, 2024
e35dd44
fix(playlist): Fix playlist cover not loading
Likqez Nov 30, 2024
04328b0
feat(playlist): Add DDL for playlist database schema
Likqez Nov 30, 2024
6a2bb13
added correct return statement
synan798 Nov 30, 2024
240f4f6
feat(friends): add on delete cascade for user deletion and finished s…
Likqez Dec 2, 2024
06ef470
feat(friends): implement GET `/user/friends` endpoint
Likqez Dec 2, 2024
3a824d2
feat(friends): GET `/user/friends` endpoint also shows outgoing frien…
Likqez Dec 2, 2024
bd81104
feat(friends): implement POST `/user/friends` endpoint for sending fr…
Likqez Dec 2, 2024
882f065
feat(friends): remove unnecessary/redundant DB get functions
Likqez Dec 2, 2024
c2ebc0c
added Start Buttons to home
synan798 Dec 4, 2024
26bf835
feat(friends): Add endpoint to accept and decline friend requests.
Likqez Dec 4, 2024
cd4da14
added game controls and user/opponent view to homepage
synan798 Dec 4, 2024
1fec8f3
added play button to user turn and changed text color to white
synan798 Dec 4, 2024
7d30789
resizes profile picture and play button
synan798 Dec 4, 2024
24ab8b7
fix(friends): fix accept and decline endpoints
Likqez Dec 4, 2024
d1f89a0
feat(friends): Tracking testpage for easy frontend work
Likqez Dec 4, 2024
8db2fea
feat(friends): add incoming/outgoing request attribute
Likqez Dec 4, 2024
c3498a9
chore: fix tsconfig.json
Likqez Dec 4, 2024
0f81c55
added first draft for profile view
synan798 Dec 4, 2024
81dbf5f
added initial functionality
synan798 Dec 4, 2024
203c869
added needed api files for friendlist
synan798 Dec 4, 2024
8e4cff3
feat(friend): friend endpoint now presents basic user info
Likqez Dec 4, 2024
04613af
added working friendlist (name is still uid)
synan798 Dec 4, 2024
84453e6
Merge remote-tracking branch 'origin/feat/api' into feat/friend-system
Likqez Dec 4, 2024
bec55ff
fixef ESLint errors
synan798 Dec 5, 2024
b4b439a
Merge branch 'feat/profile-view' into feat/friend-system
synan798 Dec 5, 2024
09ed9ab
Merge pull request #8 from BeatBuzzer/feat/friend-system
synan798 Dec 5, 2024
2d8d28f
added functional profile view
synan798 Dec 5, 2024
8398ef3
added working user information
synan798 Dec 5, 2024
e4eee09
fiexed url error
synan798 Dec 5, 2024
2ec89e3
removed import, which were added by accident
synan798 Dec 5, 2024
95d73d5
changed size of header/content/footer
synan798 Dec 5, 2024
1620613
Merge pull request #9 from BeatBuzzer/feat/profile-view
synan798 Dec 5, 2024
806abb5
Merge branch 'feat/home-view' of https://github.com/BeatBuzzer/app in…
synan798 Dec 5, 2024
07b5924
Merge pull request #7 from BeatBuzzer/feat/friend-system
synan798 Dec 5, 2024
53c05a1
removed hover effect from play button on user turn
synan798 Dec 5, 2024
4b0a1e7
removed testfriend page
synan798 Dec 5, 2024
f00170e
made opponents turn and users turn into one component
synan798 Dec 5, 2024
c35ffdf
made game buttons responsive
synan798 Dec 5, 2024
c62b699
added types for userview
synan798 Dec 5, 2024
09fbb03
made home view more responsive
synan798 Dec 5, 2024
0c2c624
Fixed ESLint error
synan798 Dec 5, 2024
47046ce
Merge branch 'feat/home-view' of https://github.com/BeatBuzzer/app in…
synan798 Dec 5, 2024
e739760
fix(friends): corrected friend list scrolling behavior
Likqez Dec 6, 2024
a1fc5f2
update footer
Likqez Dec 6, 2024
ec95723
update footer
Likqez Dec 6, 2024
bb33da5
add todo
Likqez Dec 6, 2024
dc9b509
fixed correct overflow for friendlist
synan798 Dec 6, 2024
d11d349
Merge branch 'feat/home-view' into feat/profile-view
synan798 Dec 6, 2024
bc568ae
Merge pull request #10 from BeatBuzzer/feat/profile-view
synan798 Dec 6, 2024
2355f5a
renamed MultipleUsersView and updated scrolling for friends and oppon…
synan798 Dec 6, 2024
2f9006f
Merge pull request #12 from BeatBuzzer/feat/home-view
synan798 Dec 6, 2024
0c1ca82
removed unnecessary comment
synan798 Dec 6, 2024
1ee4fc4
moved friendlist into UserView component with working friends
synan798 Dec 6, 2024
588bf37
Updated UsersView to display all users, no matter the type
synan798 Dec 6, 2024
80da39e
Merge pull request #13 from BeatBuzzer/feat/profile-view
synan798 Dec 6, 2024
0d80041
user spotify visibility setting now respected in friend and suer endp…
Likqez Dec 7, 2024
52f3994
fix UsersView Component List
Likqez Dec 7, 2024
8dee5c2
Updated home view to hopefully function as intended
synan798 Dec 7, 2024
1caf7a7
Merge branch 'main' into feat/home-view
synan798 Dec 7, 2024
9b07ba6
added working playlist view
synan798 Dec 8, 2024
1e4bbb2
removed unnecessary style
synan798 Dec 8, 2024
e176918
added scroll effect for long names
synan798 Dec 8, 2024
989d477
feat(game): Added game init endpoint & logic
Likqez Dec 8, 2024
33d7fe9
update footers to minimalize height and display active page
Likqez Dec 8, 2024
e48df28
enhanced ProfileInformation Component and introduces user state
Likqez Dec 8, 2024
4a50299
fix types of UsersView
Likqez Dec 9, 2024
e6b8fb7
fix useUser always in loading state
Likqez Dec 9, 2024
dd07a89
fixed sizing for long text
synan798 Dec 9, 2024
e48408d
minor changes. reduced complexity of UserView
Likqez Dec 9, 2024
4f9a03b
resized PlaylistsBoxes and added 'Add Playlists' button
synan798 Dec 10, 2024
1f80eb5
replaced svg with mdi
synan798 Dec 10, 2024
949c17b
formatted time string, fits into smaller div when using iPhone SE
synan798 Dec 10, 2024
0ab2ce5
feat(game): Add get games endpoint
Likqez Dec 10, 2024
4ce2318
Merge pull request #16 from BeatBuzzer/feat/home-view
Likqez Dec 10, 2024
7ef65a5
fix(game): remove ability to play against yourself
Likqez Dec 10, 2024
b581ce4
feat(game): Added game init endpoint & logic
Likqez Dec 8, 2024
c32e11d
feat(game): Add get games endpoint
Likqez Dec 10, 2024
5ddeab3
fix(game): remove ability to play against yourself
Likqez Dec 10, 2024
be606ba
Merge remote-tracking branch 'origin/feat/gameplay' into feat/gameplay
Likqez Dec 10, 2024
22c09e9
added levelbar
synan798 Dec 10, 2024
90ff040
Merge pull request #24 from BeatBuzzer/feat/levelbar
synan798 Dec 10, 2024
a7f8cba
feat(game): Homepage fetch & display games from data
Likqez Dec 10, 2024
7026f84
Merge branch 'main' into feat/gameplay
Likqez Dec 10, 2024
02df601
fixed size of levelbar
synan798 Dec 10, 2024
1e3da0e
prettified header
Likqez Dec 10, 2024
b699f1d
add playtest page
Likqez Dec 10, 2024
2abae8b
Merge pull request #22 from BeatBuzzer/feat/gameplay
synan798 Dec 10, 2024
f2f713c
added value for friend requests
synan798 Dec 10, 2024
d03570d
added box for incoming friend requests
synan798 Dec 10, 2024
15220ac
added working friend request by name
synan798 Dec 10, 2024
105ee88
added error field
synan798 Dec 10, 2024
b9c6124
added modal for accepting friend requests
synan798 Dec 11, 2024
e6d9c43
added friend removal functionality
synan798 Dec 11, 2024
45fce37
fixed condition for opening modal
synan798 Dec 11, 2024
dc3d0b0
fixed size of content
synan798 Dec 11, 2024
6f57df3
added automatic refresh on action
synan798 Dec 11, 2024
aba96c8
added cancel of sent request
synan798 Dec 11, 2024
9050e6f
fixed vue warning
synan798 Dec 11, 2024
93cec65
fixed ESLint warnings
synan798 Dec 11, 2024
e6ff093
added modal for sending friend requests
synan798 Dec 12, 2024
d75798c
fixed text moving with the user boxes
synan798 Dec 12, 2024
833e423
removed unnessacary code
synan798 Dec 12, 2024
6ab6fc6
fixed userbox margin
synan798 Dec 13, 2024
8ff3496
changed styling of modal
synan798 Dec 13, 2024
68da8d6
Merge branch 'feat/playlist-view' into feat/friend-requests
synan798 Dec 13, 2024
e24bae8
add small Add additional add friend button
Likqez Dec 13, 2024
ac75051
fixed non-plural naming in UsersView
Likqez Dec 15, 2024
cdc6b35
fix(layout): removed white borders of content
Likqez Dec 15, 2024
390ebce
fixed z-index for usermodal
synan798 Dec 15, 2024
6eb3876
Merge branch 'feat/friend-requests' of https://github.com/BeatBuzzer/…
synan798 Dec 15, 2024
69ee54b
Merge pull request #25 from BeatBuzzer/feat/friend-requests
Likqez Dec 16, 2024
5ab9c96
feat(game): Reworked responses from game endpoint
Likqez Dec 15, 2024
0d58e96
feat(game): storing preview urls in db
Likqez Dec 15, 2024
18570f0
fix some typing errors
Likqez Dec 15, 2024
c94d9f8
fixed wrong fetch sources & fixed game endpoint name
Likqez Dec 15, 2024
4b3be6c
feat(game): added play endpoint and gameplay logic
Likqez Dec 16, 2024
8790c1f
fix(game): fix game init procedure
Likqez Dec 16, 2024
b509605
feat(game): change scoring. Implement test page
Likqez Dec 16, 2024
eb2b914
feat(game): enhance playtest
Likqez Dec 16, 2024
91b58a2
feat(game): enhance playtest
Likqez Dec 16, 2024
bdf933a
feat(game): enhance playtest
Likqez Dec 16, 2024
4e30ad8
feat(game): introduce /play
Likqez Dec 17, 2024
d4c1f28
feat(game): introduce VerticalGameList & GameBox components
Likqez Dec 17, 2024
dad3e52
fix(game): remove edge case stale games from active game view
Likqez Dec 17, 2024
e3dc298
enhance(game): prettify GameBox
Likqez Dec 17, 2024
0718e37
enhance(game): spotify cover image now loaded with 300px instead of 6…
Likqez Dec 17, 2024
489f764
feat(game): Added game end page
Likqez Dec 17, 2024
7122c6e
feat(game): Prettify end view
Likqez Dec 17, 2024
a66ee85
enhance(game): Temp. Change Quick Game Button to create game
Likqez Dec 17, 2024
a1eece5
fix(game): streak now able to update
Likqez Dec 17, 2024
1611e83
fix(game): streak now able to update
Likqez Dec 17, 2024
457f264
chore(fix): change README & one less error in tsdoc
Likqez Dec 17, 2024
daa12dc
Merge pull request #30 from BeatBuzzer/feat/gameplay
synan798 Dec 17, 2024
f69355f
feat(game): Initial user signup #32
Likqez Dec 18, 2024
faabbc3
fixed overflow on playlistview
synan798 Dec 13, 2024
75ad5f9
added modal and fixed gap
synan798 Dec 15, 2024
e307d5b
added modal
synan798 Dec 15, 2024
4642c53
added scopes
synan798 Dec 15, 2024
60b05f1
added modal props
synan798 Dec 15, 2024
dc0b158
added first functionality to modal
synan798 Dec 15, 2024
a0087be
every category now shows up in a seperate box
synan798 Dec 16, 2024
927087c
fixed playlists showing up in every category
synan798 Dec 16, 2024
19d426c
moved function to comunicate with spotify api to useSpotify
synan798 Dec 16, 2024
c6981df
edited animation duration
synan798 Dec 17, 2024
4803ea5
added comments
synan798 Dec 17, 2024
76fb902
added default cover
synan798 Dec 17, 2024
f94e325
added comments and improved code
synan798 Dec 17, 2024
af494bf
Removed debug output
synan798 Dec 17, 2024
6d1ea73
fix(playlist): dont show disabled playlists
Likqez Dec 18, 2024
5fa785f
remove playtest page
Likqez Dec 18, 2024
06e9eb5
fix daily streak not updating at first game after signup
Likqez Dec 18, 2024
d96e36d
fix(tslint): fix errors
Likqez Dec 18, 2024
01b0db6
fix wrong footer link
Likqez Dec 18, 2024
1fb802c
add Dockerfile
Likqez Dec 18, 2024
2594e4d
enhance Dockerfile for layer caching
Likqez Dec 18, 2024
b5b6843
fix Opponents Turn showing wrong name
Likqez Dec 18, 2024
4eae0d1
feat(ci/cd): Automatically build docker image
Likqez Dec 18, 2024
621ef54
feat(ci/cd): change docker image tag
Likqez Dec 18, 2024
3bc9a91
added enabled value to request
synan798 Dec 18, 2024
76c2527
added getUserPlaylist from spotify
synan798 Dec 18, 2024
ef71060
added functionality to add user created playlists to the game
synan798 Dec 18, 2024
3ef5386
added working Start Game button
synan798 Dec 18, 2024
21cab3c
imporoved modal
synan798 Dec 19, 2024
1102b8a
improved styling
synan798 Dec 19, 2024
9adb79a
chore(doc): create TSDoc comment for each server endpoint
Likqez Dec 18, 2024
3a83db9
chore(doc): fix TSLint errors
Likqez Dec 18, 2024
13c32f5
chore(doc): add Project Structure to README
Likqez Dec 18, 2024
5df5f63
chore(doc): add `npm start` command as required
Likqez Dec 18, 2024
7cac387
chore(doc): update README
Likqez Dec 18, 2024
de14798
fix(eslint): fix some type errors
Likqez Dec 18, 2024
14061f6
fix(homeview): fix Turns expanding and ruining the layout
Likqez Dec 19, 2024
140e66c
added working Start Game button
synan798 Dec 18, 2024
7798d4d
imporoved modal
synan798 Dec 19, 2024
fcac54f
improved styling
synan798 Dec 19, 2024
aed5591
fix(homeview): fix Turns expanding and ruining the layout
Likqez Dec 19, 2024
76fb43d
Merge remote-tracking branch 'origin/feat/start-game' into feat/start…
Likqez Dec 19, 2024
e6a4450
fix(homeview): fixed Opponents turn list & hide stale games because o…
Likqez Dec 19, 2024
8330b32
enhance(friends): friendlist now use composable
Likqez Dec 19, 2024
2b95359
improved styling of modals
synan798 Dec 19, 2024
b992c7c
added default values for non regquired props
synan798 Dec 19, 2024
1376a84
removed unnecessary comments
synan798 Dec 19, 2024
80ee56c
imporoved modals for starting game
synan798 Dec 19, 2024
4242a96
added some comments
synan798 Dec 19, 2024
a387ee8
changed type of playlist to the correct type
synan798 Dec 19, 2024
86bacee
propose new start game button
Likqez Dec 19, 2024
940d1bd
changed Date Format and added icons
synan798 Dec 19, 2024
e8e9d59
improved styling
synan798 Dec 19, 2024
b4b3b4a
fixed some eslint warnings
synan798 Dec 19, 2024
712e7f3
fixed some eslint warnings
synan798 Dec 19, 2024
0140902
feat(game): start game modal keeps last played playlist
Likqez Dec 19, 2024
2cb803a
fixed getUserPlaylist
synan798 Dec 19, 2024
23a7c9a
Merge branch 'main' into feat/upload-playlists
synan798 Dec 19, 2024
4b3164c
prettyfied profile information
synan798 Dec 19, 2024
a17ddbf
fixed registration view staying after register
synan798 Dec 19, 2024
bc5cc6e
feat(game): implement random & quickplay functions
Likqez Dec 20, 2024
75adfee
Merge branch 'main' into feat/start-game
Likqez Dec 20, 2024
b98d35f
Merge pull request #41 from BeatBuzzer/feat/start-game
Likqez Dec 20, 2024
8dfad0a
fix(game): missing random player playlist selection
Likqez Dec 20, 2024
0754b98
update README
Likqez Dec 20, 2024
0f0314e
fix(playlist): wrong api endpoint url
Likqez Dec 20, 2024
10ef3bd
enable swagger page
Likqez Dec 20, 2024
88c8f1c
switched userError and fetchUser
synan798 Dec 20, 2024
2552a7a
Merge branch 'main' into fix/register
Likqez Dec 20, 2024
35a1afe
Merge pull request #42 from BeatBuzzer/fix/register
Likqez Dec 20, 2024
f0d3a12
Merge remote-tracking branch 'origin/main' into feat/upload-playlists
Likqez Dec 20, 2024
f0513e6
Merge pull request #37 from BeatBuzzer/feat/upload-playlists
synan798 Dec 20, 2024
96cefd2
fix(register): now applying avatar_url
Likqez Dec 20, 2024
8d541fd
Merge branch 'main' into feat/profile-info
synan798 Dec 20, 2024
748e339
added refresh
synan798 Dec 20, 2024
1f10297
feat(game): Update /play UI
Likqez Dec 20, 2024
953a328
Merge pull request #45 from BeatBuzzer/feat/profile-info
Likqez Dec 20, 2024
d6e5601
feat(game): Update /end UI
Likqez Dec 20, 2024
074422a
fix(game): remove wrong scoreboard while playing
Likqez Dec 20, 2024
1d885d3
feat(game): Update /play UI
Likqez Dec 20, 2024
e980edf
feat(game): Update /end UI
Likqez Dec 20, 2024
b0cf5b5
fix(game): remove wrong scoreboard while playing
Likqez Dec 20, 2024
dffe604
Merge remote-tracking branch 'origin/feat/game' into feat/game
Likqez Dec 20, 2024
65b11a2
Merge pull request #50 from BeatBuzzer/feat/game
synan798 Dec 20, 2024
395a634
feat(game): Add abort game logic
Likqez Dec 20, 2024
d1d3259
check if 8 songs are in a playlist
synan798 Dec 20, 2024
2b5a568
added periodic refresh for user playlists
synan798 Dec 20, 2024
00929d9
fixed ESLint errors
synan798 Dec 21, 2024
b80cdbd
added covers to end screen
synan798 Dec 21, 2024
7204c9d
added animation to start buttons
synan798 Dec 21, 2024
cda210a
removed localhost
synan798 Dec 21, 2024
bfb3bdc
changed interval for refreshing playlists
synan798 Dec 21, 2024
3c66019
added totalgames
synan798 Dec 22, 2024
2854b30
added icon
synan798 Dec 22, 2024
8d2ee44
Merge branch 'feat/testing' into feat/total-games
synan798 Dec 22, 2024
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
SUPABASE_URL="https://yourhost.supabase.co"
SUPABASE_KEY="your-secret-key"
SUPABASE_SERVICE_KEY="your-service-key"
SPOTIFY_CLIENT_ID="your-client-id"
SPOTIFY_CLIENT_SECRET="your-client-secret"
53 changes: 53 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build and Push Docker Image

on:
push:
branches: [ "main" ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/beat-buzzer

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,format=short
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
13 changes: 0 additions & 13 deletions .run/dev -- -o.run.xml

This file was deleted.

197 changes: 197 additions & 0 deletions DB/friendships.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
DO
$$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'friendship_status') THEN
CREATE TYPE friendship_status AS ENUM ('pending', 'accepted', 'declined');
END IF;
END
$$;

CREATE TABLE IF NOT EXISTS friendships
(
friendship_id SERIAL PRIMARY KEY,
user1_id UUID NOT NULL,
user2_id UUID NOT NULL,
status friendship_status NOT NULL,
action_user_id UUID NOT NULL, -- The user who performed the last action
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),

-- Ensure user1_id is always less than user2_id to prevent duplicate friendships
CONSTRAINT ensure_user_order CHECK (user1_id < user2_id),
CONSTRAINT unique_friendship UNIQUE (user1_id, user2_id),

-- Foreign keys
CONSTRAINT fk_user1 FOREIGN KEY (user1_id) REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT fk_user2 FOREIGN KEY (user2_id) REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT fk_action_user FOREIGN KEY (action_user_id) REFERENCES users (id)
);

CREATE INDEX IF NOT EXISTS idx_friendship_user1 ON friendships (user1_id, status);
CREATE INDEX IF NOT EXISTS idx_friendship_user2 ON friendships (user2_id, status);

-- Functions

-- automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER update_friendships_timestamp
BEFORE UPDATE
ON friendships
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

-- Send a friend request
CREATE OR REPLACE FUNCTION send_friend_request(sender_id UUID, receiver_id UUID) RETURNS void AS
$$
DECLARE
smaller_id UUID;
larger_id UUID;
BEGIN
-- Determine order of IDs
IF sender_id < receiver_id THEN
smaller_id := sender_id;
larger_id := receiver_id;
ELSE
smaller_id := receiver_id;
larger_id := sender_id;
END IF;

-- Insert friendship record
INSERT INTO friendships (user1_id, user2_id, status, action_user_id)
VALUES (smaller_id, larger_id, 'pending', sender_id)
ON CONFLICT (user1_id, user2_id) DO UPDATE
SET status = CASE
WHEN friendships.status = 'declined' THEN 'pending'::friendship_status
ELSE friendships.status
END,
action_user_id = sender_id;
END;
$$ LANGUAGE plpgsql;

-- accept friendship request
CREATE OR REPLACE FUNCTION accept_friend_request_by_id(friendship_id_param INT)
RETURNS void AS
$$
BEGIN
UPDATE friendships
SET status = 'accepted'
WHERE friendship_id = friendship_id_param
AND status = 'pending';

IF NOT FOUND THEN
RAISE EXCEPTION 'No pending friend request found with this ID';
END IF;
END;
$$ LANGUAGE plpgsql;


-- decline friendship request
CREATE OR REPLACE FUNCTION decline_friend_request_by_id(friendship_id_param INT)
RETURNS void AS
$$
BEGIN
UPDATE friendships
SET status = 'declined'
WHERE friendship_id = friendship_id_param
AND status = 'pending';

IF NOT FOUND THEN
RAISE EXCEPTION 'No pending friend request found with this ID';
END IF;
END;
$$ LANGUAGE plpgsql;

-- delete friendship
CREATE OR REPLACE FUNCTION remove_friend(user_id UUID, friend_id UUID) RETURNS void AS
$$
DECLARE
smaller_id UUID;
larger_id UUID;
BEGIN
-- Determine order of IDs
IF friend_id < user_id THEN
smaller_id := friend_id;
larger_id := user_id;
ELSE
smaller_id := user_id;
larger_id := friend_id;
END IF;

DELETE
FROM friendships
WHERE user1_id = smaller_id
AND user2_id = larger_id
AND status = 'accepted';

IF NOT FOUND THEN
RAISE EXCEPTION 'No active friendship found between these users';
END IF;
END;
$$ LANGUAGE plpgsql;

-- 2nd version with id
CREATE OR REPLACE FUNCTION remove_friend_by_id(friendship_id_param INT) RETURNS void AS
$$
BEGIN
DELETE
FROM friendships
WHERE friendship_id = friendship_id_param
AND status = 'accepted';

IF NOT FOUND THEN
RAISE EXCEPTION 'No active friendship found with this ID';
END IF;
END;
$$ LANGUAGE plpgsql;

-- retrieve all friends, incoming and outgoing friend requests
CREATE OR REPLACE FUNCTION get_friends(user_id UUID)
RETURNS TABLE
(
friendship_id INT,
friend_id UUID,
friend_username TEXT,
friend_avatar TEXT,
friend_spotify_id TEXT,
friend_spotify_visibility BOOLEAN,
status friendship_status,
action_user_id UUID,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
request_type TEXT
)
AS
$$
BEGIN
RETURN QUERY
SELECT f.friendship_id,
CASE WHEN f.user1_id = user_id THEN f.user2_id ELSE f.user1_id END AS friend_id,
u.username AS friend_username,
u.avatar_url AS friend_avatar,
u.spotify_id AS friend_spotify_id,
u.spotify_visibility AS friend_spotify_visibility,
f.status,
f.action_user_id,
f.created_at,
f.updated_at,
CASE WHEN f.action_user_id = user_id THEN 'outgoing' ELSE 'incoming' END AS request_type
FROM friendships f, users u
WHERE (f.user1_id = user_id OR f.user2_id = user_id)
AND (f.status != 'declined')
AND (CASE WHEN f.user1_id = user_id THEN f.user2_id ELSE f.user1_id END = u.id);
END;
$$ LANGUAGE plpgsql;
-- examples
-- SELECT send_friend_request('sender', 'receiver');
-- SELECT accept_friend_request_by_id(4);
-- SELECT decline_friend_request_by_id(4);
-- SELECT remove_friend('friend_user');
-- SELECT * FROM get_friends('user_id');
Loading
Loading