Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 722541a
Author: Dessy Dusichka <[email protected]>
Date:   Mon Sep 23 21:09:01 2024 -0400

    Adding docs on making schema changes, updating contributing docs (#26)

    * docs updates

    * remove unnecessary info

commit 403be01
Author: Luisa <[email protected]>
Date:   Mon Sep 23 19:23:39 2024 -0400

    Get reviews for user (#20)

    Me when reviews are retrieved by user ids

commit ec0ab7a
Merge: f33718a 22b4ed7
Author: gaikwadsid <[email protected]>
Date:   Mon Sep 23 17:18:14 2024 -0400

    Merge pull request #24 from GenerateNU/rest_of_schema

    Rest of schema

commit f33718a
Author: aahil nishad <[email protected]>
Date:   Mon Sep 23 16:21:22 2024 -0400

    Create new review endpoint & review types. (#27)

    * feat: add create review endpoint

    * refactor: fix error handling for create review

    * refactor: make handlers more modular

    * fix: update query to handle edge cases

    * fix: check userid is valid when creating review

    * fix: make pr changes

    * revert update to og script

    ---------

    Co-authored-by: aaronkimbrooktec <[email protected]>
    Co-authored-by: ddusichka <[email protected]>

commit 7ae380f
Author: Dessy Dusichka <[email protected]>
Date:   Mon Sep 23 00:10:47 2024 -0400

    Add nix scripts to lint and run frontend (#28)

    * add scripts for frontend

    * allow console for now and remove test action

    * make prettier happy

    * one more try

commit 22b4ed7
Author: gaikwadsid <[email protected]>
Date:   Sun Sep 22 18:57:24 2024 -0400

    Example for new script

commit e99e4ea
Author: gaikwadsid <[email protected]>
Date:   Sun Sep 22 00:09:46 2024 -0400

    Connected Frontend to Backend (#23)

    * Connected Frontend to Backend

    * Fixed lint?

    * Fix lint

    * Fix lint

    * Fix lint

    * Fix lint

    * Fix lint

    * Fix lint?

    * Fix lint

    * Fix lint

    * prettier fix?

    * lint fix

    * fix lint.

    * Deleted .env file

    ---------

    Co-authored-by: ddusichka <[email protected]>

commit e783ffe
Author: ddusichka <[email protected]>
Date:   Sun Sep 22 00:04:59 2024 -0400

    pause frontend tests

commit 551b292
Author: gaikwadsid <[email protected]>
Date:   Sat Sep 21 21:49:17 2024 -0400

    Fixed syntax errrors

commit df13718
Author: Dessy Dusichka <[email protected]>
Date:   Sat Sep 21 14:48:00 2024 -0400

    Update pull_request_template.md

commit 4f7e72c
Merge: 9816b83 70dd930
Author: Dessy Dusichka <[email protected]>
Date:   Sat Sep 21 11:17:01 2024 -0400

    Merge pull request #22 from GenerateNU/env-help-bump

    Move Task scripts to Nix scripts

commit cefc909
Author: gaikwadsid <[email protected]>
Date:   Fri Sep 20 22:17:35 2024 -0400

    Added data into the tables

commit 9816b83
Merge: 09b17a6 5cfb658
Author: Dessy Dusichka <[email protected]>
Date:   Fri Sep 20 13:27:13 2024 -0400

    Merge pull request #18 from GenerateNU/spotify-oauth-v2

    feat: spotify oauth

commit 70dd930
Author: Jackson Terrill <[email protected]>
Date:   Fri Sep 20 00:49:03 2024 -0400

    Move Task scripts to Nix scripts

commit b663684
Author: gaikwadsid <[email protected]>
Date:   Thu Sep 19 18:38:06 2024 -0400

    Added rest of schema

commit 5cfb658
Merge: 540e431 09b17a6
Author: aaronkimbrooktec <[email protected]>
Date:   Thu Sep 19 17:41:00 2024 -0400

    Merge remote-tracking branch 'origin' into spotify-oauth-v2

commit 540e431
Author: aaronkimbrooktec <[email protected]>
Date:   Thu Sep 19 17:40:53 2024 -0400

    refactor: make oauth sessionvalue store available to all providers

commit 09b17a6
Author: gaikwadsid <[email protected]>
Date:   Thu Sep 19 17:22:18 2024 -0400

    Update pull_request_template.md

commit f98e84a
Author: gaikwadsid <[email protected]>
Date:   Thu Sep 19 17:20:53 2024 -0400

    Update pull_request_template.md

commit d8d6996
Author: aaronkimbrooktec <[email protected]>
Date:   Thu Sep 19 16:15:53 2024 -0400

    refactor: rename and reorganize some things

commit 1fe560a
Author: garrettladley <[email protected]>
Date:   Thu Sep 19 11:47:29 2024 -0400

    feat: back to sessions

commit 5752469
Author: ddusichka <[email protected]>
Date:   Thu Sep 19 10:48:35 2024 -0400

    make favicon error go away

commit 1264a6b
Author: garrettladley <[email protected]>
Date:   Thu Sep 19 09:50:06 2024 -0400

    feat: don't keep track of challenge on our end, clean up, and TODO resolved

commit 01f9218
Merge: eab21a4 ead556c
Author: Garrett Ladley <[email protected]>
Date:   Wed Sep 18 23:45:49 2024 -0400

    Merge branch 'main' into spotify-oauth-v2

commit ead556c
Author: Aaron Kim <[email protected]>
Date:   Wed Sep 18 23:45:17 2024 -0400

    feat: add central error handling (#19)

commit eab21a4
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:17:37 2024 -0400

    nit: make stateValue fields unexported

commit d9c9ddd
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:16:02 2024 -0400

    fix: lint?

commit 9f2b184
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:15:12 2024 -0400

    fix: lint?

commit 079f0be
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:11:40 2024 -0400

    fix: lint?

commit 71ccfab
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:10:14 2024 -0400

    fix: lint?

commit 11ce351
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:05:16 2024 -0400

    fix: check err value of set

commit f93510b
Author: garrettladley <[email protected]>
Date:   Wed Sep 18 23:00:54 2024 -0400

    feat: kv with state as the key and verifier x challenge as the value

commit 72ef134
Author: aaronkimbrooktec <[email protected]>
Date:   Wed Sep 18 17:33:52 2024 -0400

    feat: spotify oauth

commit 0a567be
Author: Dessy Dusichka <[email protected]>
Date:   Wed Sep 18 20:52:41 2024 -0400

    Create pull_request_template.md

commit 648ad3f
Merge: 37c4fad 7c69270
Author: gaikwadsid <[email protected]>
Date:   Tue Sep 17 22:30:00 2024 -0400

    Merge pull request #16 from GenerateNU/add-schema

    Setting up database tables and seed data

commit 7c69270
Author: ddusichka <[email protected]>
Date:   Tue Sep 17 22:15:37 2024 -0400

    add unique constraint

commit 13c9527
Author: ddusichka <[email protected]>
Date:   Tue Sep 17 15:00:01 2024 -0400

    update ID types and seed data

commit c1feea9
Author: ddusichka <[email protected]>
Date:   Mon Sep 16 22:55:43 2024 -0400

    setting up schema and seed data
  • Loading branch information
adescoteaux1 committed Sep 24, 2024
1 parent e6a8791 commit 6d75f10
Show file tree
Hide file tree
Showing 68 changed files with 4,486 additions and 683 deletions.
6 changes: 5 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ DB_HOST=""
DB_USER=""
DB_PASSWORD=""
DB_PORT=""
DB_NAME="postgres"
DB_NAME=""
SPOTIFY_ID=""
SPOTIFY_SECRET=""
SPOTIFY_REDIRECT_URI=""
BASE_URL=""
17 changes: 17 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--- Provide a general summary of your changes in the Title above -->

## Description
<!--- Describe your changes in detail -->
[Link to Ticket](insert the link to your ticket inside the parenthesis here)

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->

## Checklist:
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] Some tests are included that test my code

## Screenshots:
<!--- If working on a backend ticket, screenshots or a walkthrough of successful API calls are included. -->
<!--- If working on a frontend ticket, screenshots/recording of new screens or functionality are included. -->
22 changes: 11 additions & 11 deletions .github/workflows/frontend-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ jobs:
npm install prettier
npx prettier --check .
frontend-tests:
name: Run frontend-tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# frontend-tests:
# name: Run frontend-tests
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v4

- name: Test Frontend
run: |
cd frontend
npm install
npm run test
# - name: Test Frontend
# run: |
# cd frontend
# npm install
# npm run test
9 changes: 2 additions & 7 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"platnm/internal/config"
"platnm/internal/service"
"platnm/internal/storage/postgres"

_ "github.com/lib/pq"
"github.com/sethvargo/go-envconfig"
Expand All @@ -19,13 +18,9 @@ func main() {
log.Fatalln("Error processing .env file: ", err)
}

// Connect to database
conn := postgres.ConnectDatabase(config.DbHost, config.DbUser, config.DbPassword, config.DbName, config.DbPort)
app := service.InitApp(service.Params{Conn: conn})
app := service.InitApp(config)

defer conn.Close()

if err := app.Listen(":8080"); err != nil {
if err := app.Listen(":" + config.Application.Port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
4 changes: 4 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ go 1.22.6

require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/storage/memory v1.3.4
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.7.0
github.com/lib/pq v1.10.9
github.com/zmb3/spotify/v2 v2.4.2
golang.org/x/oauth2 v0.23.0
)

require (
Expand Down
402 changes: 402 additions & 0 deletions backend/go.sum

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions backend/internal/config/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

type Application struct {
Port string `env:"PORT, default=8080"` // the port for the server to listen on
LogLevel string `env:"LOG_LEVEL, default=INFO"` // the level of event to log
}
11 changes: 3 additions & 8 deletions backend/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package config

type Config struct {
DbHost string `env:"DB_HOST, required"` // the database host to connect to
DbPort string `env:"DB_PORT, required"` // the database port to connect to
DbUser string `env:"DB_USER, required"` // the user to connect to the database with
DbPassword string `env:"DB_PASSWORD, required"` // the password to connect to the database with
DbName string `env:"DB_NAME, required"` // the name of the database to connect to

Port string `env:"PORT, default=8080"` // the port for the server to listen on
LogLevel string `env:"LOG_LEVEL, default=INFO"` // the level of event to log
Application Application
DB DB
Spotify Spotify
}
15 changes: 15 additions & 0 deletions backend/internal/config/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

import "fmt"

type DB struct {
Host string `env:"DB_HOST, required"` // the database host to connect to
Port string `env:"DB_PORT, required"` // the database port to connect to
User string `env:"DB_USER, required"` // the user to connect to the database with
Password string `env:"DB_PASSWORD, required"` // the password to connect to the database with
Name string `env:"DB_NAME, required"` // the name of the database to connect to
}

func (db *DB) Connection() string {
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=require statement_cache_mode=describe pgbouncer=true", db.Host, db.User, db.Password, db.Name, db.Port)
}
5 changes: 5 additions & 0 deletions backend/internal/config/spotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

type Spotify struct {
RedirectURI string `env:"SPOTIFY_REDIRECT_URI, required"`
}
7 changes: 7 additions & 0 deletions backend/internal/constants/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constants

import "time"

const (
SessionDuration = 5 * time.Minute
)
6 changes: 6 additions & 0 deletions backend/internal/constants/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constants

const (
HeaderSession = "X-Session"
HeaderRedirect = "X-Redirect"
)
22 changes: 22 additions & 0 deletions backend/internal/errs/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package errs

import (
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
)

func IsUniqueViolation(err error, constraintName string) bool {
if pgErr, ok := err.(*pgconn.PgError); ok {
return pgErr.Code == pgerrcode.UniqueViolation && pgErr.ConstraintName == constraintName
}

return false
}

func IsForeignKeyViolation(err error, constraintName string) bool {
if pgErr, ok := err.(*pgconn.PgError); ok {
return pgErr.Code == pgerrcode.ForeignKeyViolation && pgErr.ConstraintName == constraintName
}

return false
}
67 changes: 67 additions & 0 deletions backend/internal/errs/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package errs

import (
"errors"
"fmt"
"log/slog"
"net/http"

"github.com/gofiber/fiber/v2"
)

type HTTPError struct {
Code int `json:"code"`
Message any `json:"message"`
}

func (e HTTPError) Error() string {
return fmt.Sprintf("http error: %d %v", e.Code, e.Message)
}

func NewHTTPError(code int, err error) HTTPError {
return HTTPError{
Code: code,
Message: err.Error(),
}
}

func BadRequest(msg string) HTTPError {
return NewHTTPError(http.StatusBadRequest, errors.New(msg))
}

func Unauthorized() HTTPError {
return NewHTTPError(http.StatusUnauthorized, errors.New("unauthorized"))
}

func NotFound(title string, withKey string, withValue any) HTTPError {
return NewHTTPError(http.StatusNotFound, fmt.Errorf("%s with %s='%v' not found", title, withKey, withValue))
}

func Conflict(title string, withKey string, withValue any) HTTPError {
return NewHTTPError(http.StatusConflict, fmt.Errorf("conflict: %s with %s='%s' already exists", title, withKey, withValue))
}

func InvalidRequestData(errors map[string]string) HTTPError {
return HTTPError{
Code: http.StatusUnprocessableEntity,
Message: errors,
}
}

func InternalServerError() HTTPError {
return NewHTTPError(http.StatusInternalServerError, errors.New("internal server error"))
}

func ErrorHandler(c *fiber.Ctx, err error) error {
var httpErr HTTPError

if castedErr, ok := err.(HTTPError); ok {
httpErr = castedErr
} else {
httpErr = InternalServerError()
}

slog.Error("HTTP API error", "err", err.Error(), "method", c.Method(), "path", c.Path())

return c.Status(httpErr.Code).JSON(httpErr)
}
17 changes: 13 additions & 4 deletions backend/internal/models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ package models
import "time"

type Review struct {
ID int `json:"id"`
UserID string `json:"user_id"`
MediaType string `json:"media_type"`
MediaID string `json:"media_id"`
Rating string `json:"rating"`
Desc *string `json:"desc,omitempty"`
MediaType MediaType `json:"media_type"`
MediaID int `json:"media_id"`
Rating int `json:"rating"`
Comment string `json:"comment"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// mediatype can be either track or album
type MediaType string

const (
Track MediaType = "track"
Album MediaType = "album"
)
46 changes: 46 additions & 0 deletions backend/internal/service/handler/oauth/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package oauth

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)

const valueKey = "value"

type SessionValue struct {
State string
Verifier string
}

type SessionValueStore struct {
*session.Store
}

func NewSessionValueStore(config session.Config) *SessionValueStore {
store := &SessionValueStore{
session.New(config),
}

store.RegisterType(SessionValue{})
return store
}

func (s *SessionValueStore) SessionSetValue(c *fiber.Ctx, value SessionValue) error {
sess, err := s.Get(c)
if err != nil {
return err
}
sess.Set(valueKey, value)
if err := sess.Save(); err != nil {
return err
}
return nil
}

func (s *SessionValueStore) SessionGetValue(c *fiber.Ctx) (SessionValue, error) {
sess, err := s.Get(c)
if err != nil {
return SessionValue{}, err
}
return sess.Get(valueKey).(SessionValue), nil
}
34 changes: 34 additions & 0 deletions backend/internal/service/handler/oauth/spotify/begin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spotify

import (
"net/http"
"platnm/internal/constants"
"platnm/internal/service/handler/oauth"

"github.com/gofiber/fiber/v2"
"golang.org/x/oauth2"
)

func (h *Handler) Begin(c *fiber.Ctx) error {
var (
verifier = oauth2.GenerateVerifier()
challenge = oauth2.S256ChallengeFromVerifier(verifier)
)

state, err := oauth.GenerateState()
if err != nil {
return err
}

url := h.authenticator.AuthURL(state,
oauth2.SetAuthURLParam("code_challenge_method", "S256"),
oauth2.SetAuthURLParam("code_challenge", challenge),
)

if err := h.store.SessionSetValue(c, oauth.SessionValue{State: state, Verifier: verifier}); err != nil {
return err
}

c.Set(constants.HeaderRedirect, url)
return c.SendStatus(http.StatusFound)
}
34 changes: 34 additions & 0 deletions backend/internal/service/handler/oauth/spotify/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spotify

import (
"log/slog"
"net/http"
"platnm/internal/constants"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"golang.org/x/oauth2"
)

func (h *Handler) Callback(c *fiber.Ctx) error {
v, err := h.store.SessionGetValue(c)
if err != nil {
return err
}

req, err := adaptor.ConvertRequest(c, false)
if err != nil {
return err
}

token, err := h.authenticator.Token(c.Context(), v.State, req, oauth2.SetAuthURLParam("code_verifier", v.Verifier))
if err != nil {
return err
}

slog.Info("Access token:", "token", token.AccessToken)
slog.Info("Refresh token:", "token", token.RefreshToken)

c.Set(constants.HeaderRedirect, "http://127.0.0.1:3000")
return c.SendStatus(http.StatusFound)
}
Loading

0 comments on commit 6d75f10

Please sign in to comment.