Skip to content
Merged
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
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ func Load(filename string, envFilename string) (*Config, error) {
func Get() *Config {
return config
}

// ConfigPaths holds the paths for config and env files
type ConfigPaths struct {
ConfigFile string
EnvFile string
}

// NewConfig creates a new config instance for FX dependency injection
func NewConfig(paths ConfigPaths) (*Config, error) {
return Load(paths.ConfigFile, paths.EnvFile)
}
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
go.uber.org/fx v1.24.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -22,15 +23,14 @@ require (
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/dig v1.19.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.2.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bytedance/sonic v1.13.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sessions v1.0.4
github.com/gin-contrib/sse v1.0.0 // indirect
Expand Down Expand Up @@ -69,5 +69,4 @@ require (
golang.org/x/text v0.24.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
85 changes: 28 additions & 57 deletions go.sum

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions internal/repository/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
package repository

import (
"api/config"
"api/types"
"context"
"database/sql"
"errors"
"fmt"
"os"
"time"

"api/config"
"api/types"

"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/fx"
)

var _ Repository = (*SQLite)(nil)

type SQLite struct {
conn *sql.DB
conn *sql.DB
config *config.Config
}

type SQLiteParams struct {
fx.In
Config *config.Config
}

var (
ErrRepoConflict = errors.New("conflict")
ErrRepoNotFound = errors.New("not found")
)

func NewSQLite() (*SQLite, error) {
conf := config.Get()
func NewSQLite(p SQLiteParams) (Repository, error) {
db, err := sql.Open(
"sqlite3",
fmt.Sprintf("%s%s?_foreign_keys=on&cache=%s", conf.Database.Type, conf.Database.Address, conf.Database.Cache),
fmt.Sprintf("%s%s?_foreign_keys=on&cache=%s", p.Config.Database.Type, p.Config.Database.Address, p.Config.Database.Cache),
)
if err != nil {
return nil, err
}

// Set the maximum number of open connections
db.SetMaxOpenConns(conf.Database.MaxConn)
db.SetMaxOpenConns(p.Config.Database.MaxConn)

// Ping to check if the database connection is established
err = db.Ping()
Expand All @@ -45,10 +52,11 @@ func NewSQLite() (*SQLite, error) {
}

repo := &SQLite{
conn: db,
conn: db,
config: p.Config,
}

err = repo.migrate(conf.Database.Schema)
err = repo.migrate(p.Config.Database.Schema)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -475,7 +483,6 @@ func (s *SQLite) AddVoteToAnswer(ctx context.Context, id uuid.UUID, voteRequest
voteRequest.ID,
sessionID,
)

if err != nil {
return err
}
Expand All @@ -494,7 +501,6 @@ func (s *SQLite) RemoveVoteFromAnswer(ctx context.Context, voteRequest *types.An
voteRequest.ID,
sessionID,
)

if err != nil {
return err
}
Expand All @@ -508,7 +514,6 @@ func (s *SQLite) RemoveVoteFromAnswer(ctx context.Context, voteRequest *types.An
}

func (s *SQLite) refreshAnswerData(ctx context.Context, answer *types.Answer) error {

sqlQuery := `SELECT a.id, a.text, a.position, a.question_id, COUNT(av.id) as votes
FROM answers a LEFT JOIN answer_votes av ON a.id = av.answer_id
WHERE a.id = $1
Expand All @@ -521,5 +526,4 @@ func (s *SQLite) refreshAnswerData(ctx context.Context, answer *types.Answer) er
&answer.QuestionID,
&answer.Votes,
)

}
61 changes: 43 additions & 18 deletions internal/repository/sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package repository

import (
"api/config"
"api/types"
"context"
"database/sql"
"os"
"testing"
"time"

"api/config"
"api/types"

"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,11 +63,14 @@ func createGenericQuestion(db *SQLite, retro *types.Retrospective) (*types.Quest
}

func TestCreateRetrospective(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

id, err := uuid.NewV7()
assert.Nilf(t, err, "error generating UUID")
Expand All @@ -93,11 +97,14 @@ func TestCreateRetrospective(t *testing.T) {
}

func TestUpdateRetrospective(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

retro, err := createGenericRetrospective(db)
assert.Nilf(t, err, "error creating retrospective")
Expand All @@ -122,11 +129,14 @@ func TestUpdateRetrospective(t *testing.T) {
}

func TestDeleteRetrospective(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

retro, err := createGenericRetrospective(db)
retro.Questions = []types.Question{}
Expand All @@ -149,11 +159,14 @@ func TestDeleteRetrospective(t *testing.T) {
}

func TestGetRetrospective(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

id, err := uuid.NewV7()
assert.Nilf(t, err, "error generating UUID")
Expand Down Expand Up @@ -221,11 +234,14 @@ func TestGetRetrospective(t *testing.T) {
}

func TestGetAllRetrospective(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

id, err := uuid.NewV7()
assert.Nilf(t, err, "error generating UUID")
Expand Down Expand Up @@ -280,11 +296,14 @@ func TestGetAllRetrospective(t *testing.T) {
}

func TestCreateQuestion(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

retro, err := createGenericRetrospective(db)
assert.Nilf(t, err, "error creating retrospective")
Expand Down Expand Up @@ -312,11 +331,14 @@ func TestCreateQuestion(t *testing.T) {
}

func TestUpdateQuestion(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

retro, err := createGenericRetrospective(db)
assert.Nilf(t, err, "error creating retrospective")
Expand Down Expand Up @@ -345,11 +367,14 @@ func TestUpdateQuestion(t *testing.T) {
}

func TestDeleteQuestion(t *testing.T) {
_, err := config.Load("config/config_test.yaml", "config/test.env")
conf, err := config.Load("config/config_test.yaml", "config/test.env")
assert.Nilf(t, err, "error loading config")

db, err := NewSQLite()
repo, err := NewSQLite(SQLiteParams{
Config: conf,
})
assert.Nilf(t, err, "error connecting to database")
db := repo.(*SQLite)

retro, err := createGenericRetrospective(db)
assert.Nilf(t, err, "error creating retrospective")
Expand Down
22 changes: 14 additions & 8 deletions internal/repository/websocket.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package repository

import (
"api/types"
"context"
"errors"
"fmt"
Expand All @@ -11,8 +10,11 @@ import (
"net/http"
"time"

"api/types"

"github.com/google/uuid"
"github.com/gorilla/websocket"
"go.uber.org/fx"
)

var _ WebSocketRepository = (*WebSocket)(nil)
Expand All @@ -21,6 +23,10 @@ type WebSocket struct {
connections map[uuid.UUID][]*websocket.Conn
}

type WebSocketParams struct {
fx.In
}

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Expand All @@ -29,6 +35,13 @@ var upgrader = websocket.Upgrader{
},
}

func NewWebSocket(p WebSocketParams) WebSocketRepository {
connections := make(map[uuid.UUID][]*websocket.Conn)
return &WebSocket{
connections: connections,
}
}

// AddConnection implements WebSocketRepository.
func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
retrospectiveID, ok := ctx.Value("retrospective_id").(uuid.UUID)
Expand Down Expand Up @@ -86,13 +99,6 @@ func (*WebSocket) GetRetrospective(ctx context.Context, id uuid.UUID) (*types.Re
panic("unimplemented")
}

func NewWebSocket() (*WebSocket, error) {
connections := make(map[uuid.UUID][]*websocket.Conn)
return &WebSocket{
connections: connections,
}, nil
}

func (w *WebSocket) sendMessageToRetro(ctx context.Context, message types.WebSocketMessage, retrospectiveID *uuid.UUID) error {
if retrospectiveID == nil {
id, ok := ctx.Value("retrospective_id").(uuid.UUID)
Expand Down
Loading