Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegsuus committed Jun 20, 2024
1 parent 0280266 commit 26b0df5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"news-api/internal/app"
"news-api/internal/config"
"news-api/internal/database"
"news-api/internal/migrations"
)

func main() {
cfg := config.GetConfig()
db := database.NewDB(cfg)
migrations.Migrations(cfg, db)
reformDB := database.CreateReformDB(db)

app := &app.App{
Config: cfg,
DB: db,
DB: reformDB,
Fiber: fiber.New(),
}

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pressly/goose/v3 v3.21.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand All @@ -40,6 +43,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
Expand All @@ -70,6 +72,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.21.1 h1:5SSAKKWej8LVVzNLuT6KIvP1eFDuPvxa+B6H0w78buQ=
github.com/pressly/goose/v3 v3.21.1/go.mod h1:sqthmzV8PitchEkjecFJII//l43dLOCzfWh8pHEe+vE=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand All @@ -78,6 +82,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
Expand Down Expand Up @@ -118,6 +124,8 @@ golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCR
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
13 changes: 8 additions & 5 deletions internal/database/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DB struct {
DB *reform.DB
}

func NewDB(cfg *config.Config) *DB {
func NewDB(cfg *config.Config) *sql.DB {
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
cfg.Database.Host, cfg.Database.Port, cfg.Database.User, cfg.Database.Password, cfg.Database.DBName)

Expand All @@ -24,6 +24,10 @@ func NewDB(cfg *config.Config) *DB {
log.Fatalf("Failed to open database: %v", err)
}

return sqlDB
}

func CreateReformDB(sqlDB *sql.DB) *DB {
db := reform.NewDB(sqlDB, postgresql.Dialect, nil)
return &DB{DB: db}
}
Expand All @@ -44,15 +48,14 @@ func (db *DB) GetAllNews(limit, offset int) ([]models.News, error) {
newsList := make([]models.News, len(structs))
for i, s := range structs {
if news, ok := s.(*models.News); ok {
var categoryIDs []int64
categoryIDs := []int64{}
query := fmt.Sprintf("WHERE news_id = %d", news.ID)
newsCategoryStructs, err := db.DB.SelectAllFrom(models.NewsCategoryTable, query)
if err != nil {

fmt.Println("newcat =", newsCategoryStructs)
log.Printf("Error selecting categories for news ID %d: %v", news.ID, err)

continue
}

for _, ns := range newsCategoryStructs {
if nc, ok := ns.(*models.NewsCategory); ok {
categoryIDs = append(categoryIDs, nc.CategoryID)
Expand Down
26 changes: 26 additions & 0 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package migrations

import (
"database/sql"
"embed"
"github.com/pressly/goose/v3"
"log"
"news-api/internal/config"
)

//go:embed migrations/*.sql
var embedMigrations embed.FS

func Migrations(cfg *config.Config, db *sql.DB) {
goose.SetBaseFS(embedMigrations)

if err := goose.SetDialect("postgres"); err != nil {
log.Fatalf("Failed to set goose dialect: %v", err)
}

if err := goose.Up(db, "migrations"); err != nil {
log.Fatalf("Failed to apply migrations: %v", err)
}

log.Println("Success migrations")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS News (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS NewsCategories (
news_id INT REFERENCES News(id),
category_id INT,
PRIMARY KEY (news_id, category_id)
);

CREATE TABLE IF NOT EXISTS Categories (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

-- +goose StatementEnd


-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS NewsCategories;
DROP TABLE IF EXISTS Categories;
DROP TABLE IF EXISTS News;
-- +goose StatementEnd
2 changes: 1 addition & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type News struct {
//reform:newscategories
type NewsCategory struct {
NewsID int64 `reform:"news_id,pk" json:"news_id"`
CategoryID int64 `reform:"category_id " json:"category_id"`
CategoryID int64 `reform:"category_id" json:"category_id"`
}

//go:generate reform
Expand Down
4 changes: 2 additions & 2 deletions internal/models/models_reform.go

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

0 comments on commit 26b0df5

Please sign in to comment.