From d3f9fbe9522fa91165ef64c7016e0451cf07f5f1 Mon Sep 17 00:00:00 2001 From: Eugene Toropov Date: Wed, 7 Oct 2020 20:51:56 +0300 Subject: [PATCH] Renamed db packahe to pg (preparing to add mysql package) --- .idea/sqldialects.xml | 2 +- cmd/api/env.sh | 2 +- cmd/api/main.go | 10 +++++----- {db => pg}/migrations/20200629153445_init.down.sql | 0 {db => pg}/migrations/20200629153445_init.up.sql | 0 {db => pg}/migrations/20201003153445_init.down.sql | 0 {db => pg}/migrations/20201003153445_init.up.sql | 0 {db => pg}/pg.go | 2 +- ...on => REST API Example.postman_collection.json} | 2 +- .../REST API Server Local.postman_environment.json | 14 ++++++++++++++ repository/pg/file.go | 14 +++++++------- repository/pg/user.go | 14 +++++++------- 12 files changed, 37 insertions(+), 23 deletions(-) rename {db => pg}/migrations/20200629153445_init.down.sql (100%) rename {db => pg}/migrations/20200629153445_init.up.sql (100%) rename {db => pg}/migrations/20201003153445_init.down.sql (100%) rename {db => pg}/migrations/20201003153445_init.up.sql (100%) rename {db => pg}/pg.go (98%) rename postman/{Simple Web Server.postman_collection.json => REST API Example.postman_collection.json} (98%) create mode 100644 postman/REST API Server Local.postman_environment.json diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 2914123..dc576be 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/cmd/api/env.sh b/cmd/api/env.sh index 577d915..bdec63e 100755 --- a/cmd/api/env.sh +++ b/cmd/api/env.sh @@ -7,7 +7,7 @@ export GC_BUCKET=my-cool-bucket # Local postgres export PG_URL=postgres://postgres:postgres@localhost/test?sslmode=disable -export PG_MIGRATIONS_PATH=file://../../db//migrations +export PG_MIGRATIONS_PATH=file://../../pg/migrations # Logger settings export LOG_LEVEL=debug \ No newline at end of file diff --git a/cmd/api/main.go b/cmd/api/main.go index bf990c0..f24f69e 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -12,11 +12,11 @@ import ( "github.com/evt/rest-api-example/config" "github.com/evt/rest-api-example/controller" - "github.com/evt/rest-api-example/db" libError "github.com/evt/rest-api-example/lib/error" "github.com/evt/rest-api-example/lib/validator" "github.com/evt/rest-api-example/logger" - "github.com/evt/rest-api-example/repository/pg" + "github.com/evt/rest-api-example/pg" + pgrepo "github.com/evt/rest-api-example/repository/pg" "github.com/evt/rest-api-example/service/web" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -45,7 +45,7 @@ func run() error { l := logger.Get() // connect to Postgres - pgDB, err := db.Dial(cfg) + pgDB, err := pg.Dial(cfg) if err != nil { log.Fatal(err) } @@ -63,8 +63,8 @@ func run() error { } // Init repositories - userRepo := pg.NewUserRepo(pgDB) - fileRepo := pg.NewFileRepo(pgDB) + userRepo := pgrepo.NewUserRepo(pgDB) + fileRepo := pgrepo.NewFileRepo(pgDB) fileContentRepo := gcloudRepo.NewFileRepo(cloudStorage, cfg.GCBucket) // Init services diff --git a/db/migrations/20200629153445_init.down.sql b/pg/migrations/20200629153445_init.down.sql similarity index 100% rename from db/migrations/20200629153445_init.down.sql rename to pg/migrations/20200629153445_init.down.sql diff --git a/db/migrations/20200629153445_init.up.sql b/pg/migrations/20200629153445_init.up.sql similarity index 100% rename from db/migrations/20200629153445_init.up.sql rename to pg/migrations/20200629153445_init.up.sql diff --git a/db/migrations/20201003153445_init.down.sql b/pg/migrations/20201003153445_init.down.sql similarity index 100% rename from db/migrations/20201003153445_init.down.sql rename to pg/migrations/20201003153445_init.down.sql diff --git a/db/migrations/20201003153445_init.up.sql b/pg/migrations/20201003153445_init.up.sql similarity index 100% rename from db/migrations/20201003153445_init.up.sql rename to pg/migrations/20201003153445_init.up.sql diff --git a/db/pg.go b/pg/pg.go similarity index 98% rename from db/pg.go rename to pg/pg.go index 24f50dc..180637c 100644 --- a/db/pg.go +++ b/pg/pg.go @@ -1,4 +1,4 @@ -package db +package pg import ( "time" diff --git a/postman/Simple Web Server.postman_collection.json b/postman/REST API Example.postman_collection.json similarity index 98% rename from postman/Simple Web Server.postman_collection.json rename to postman/REST API Example.postman_collection.json index 43f98ad..ebf8ecb 100644 --- a/postman/Simple Web Server.postman_collection.json +++ b/postman/REST API Example.postman_collection.json @@ -1,7 +1,7 @@ { "info": { "_postman_id": "8e87341e-2892-494b-a0e9-8ff700847a2e", - "name": "Simple Web Server", + "name": "REST API Example", "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" }, "item": [ diff --git a/postman/REST API Server Local.postman_environment.json b/postman/REST API Server Local.postman_environment.json new file mode 100644 index 0000000..675c221 --- /dev/null +++ b/postman/REST API Server Local.postman_environment.json @@ -0,0 +1,14 @@ +{ + "id": "33494e3e-f94d-41f2-9035-566955b2ffd2", + "name": "REST API Server Local", + "values": [ + { + "key": "rest_api_addr", + "value": "http://127.0.0.1:8080", + "enabled": true + } + ], + "_postman_variable_scope": "environment", + "_postman_exported_at": "2020-10-07T17:49:31.940Z", + "_postman_exported_using": "Postman/7.33.1" +} \ No newline at end of file diff --git a/repository/pg/file.go b/repository/pg/file.go index 3901a88..1dbd108 100644 --- a/repository/pg/file.go +++ b/repository/pg/file.go @@ -5,18 +5,18 @@ import ( "github.com/google/uuid" - "github.com/evt/rest-api-example/db" "github.com/evt/rest-api-example/model" - "github.com/go-pg/pg/v10" + "github.com/evt/rest-api-example/pg" + gopg "github.com/go-pg/pg/v10" ) // FilePgRepo ... type FilePgRepo struct { - db *db.PgDB + db *pg.PgDB } // NewFileRepo ... -func NewFileRepo(db *db.PgDB) *FilePgRepo { +func NewFileRepo(db *pg.PgDB) *FilePgRepo { return &FilePgRepo{db: db} } @@ -27,7 +27,7 @@ func (repo *FilePgRepo) Get(ctx context.Context, id uuid.UUID) (*model.DBFile, e Where("id = ?", id). Select() if err != nil { - if err == pg.ErrNoRows { //not found + if err == gopg.ErrNoRows { //not found return nil, nil } return nil, err @@ -53,7 +53,7 @@ func (repo *FilePgRepo) Update(ctx context.Context, user *model.DBFile) (*model. Returning("*"). Update() if err != nil { - if err == pg.ErrNoRows { //not found + if err == gopg.ErrNoRows { //not found return nil, nil } return nil, err @@ -68,7 +68,7 @@ func (repo *FilePgRepo) Delete(ctx context.Context, id uuid.UUID) error { Where("id = ?", id). Delete() if err != nil { - if err == pg.ErrNoRows { + if err == gopg.ErrNoRows { return nil } return err diff --git a/repository/pg/user.go b/repository/pg/user.go index 7366676..01a7cf8 100644 --- a/repository/pg/user.go +++ b/repository/pg/user.go @@ -5,18 +5,18 @@ import ( "github.com/google/uuid" - "github.com/evt/rest-api-example/db" "github.com/evt/rest-api-example/model" - "github.com/go-pg/pg/v10" + "github.com/evt/rest-api-example/pg" + gopg "github.com/go-pg/pg/v10" ) // UserPgRepo ... type UserPgRepo struct { - db *db.PgDB + db *pg.PgDB } // NewUserRepo ... -func NewUserRepo(db *db.PgDB) *UserPgRepo { +func NewUserRepo(db *pg.PgDB) *UserPgRepo { return &UserPgRepo{db: db} } @@ -27,7 +27,7 @@ func (repo *UserPgRepo) GetUser(ctx context.Context, id uuid.UUID) (*model.DBUse Where("id = ?", id). Select() if err != nil { - if err == pg.ErrNoRows { //not found + if err == gopg.ErrNoRows { //not found return nil, nil } return nil, err @@ -53,7 +53,7 @@ func (repo *UserPgRepo) UpdateUser(ctx context.Context, user *model.DBUser) (*mo Returning("*"). Update() if err != nil { - if err == pg.ErrNoRows { //not found + if err == gopg.ErrNoRows { //not found return nil, nil } return nil, err @@ -68,7 +68,7 @@ func (repo *UserPgRepo) DeleteUser(ctx context.Context, id uuid.UUID) error { Where("id = ?", id). Delete() if err != nil { - if err == pg.ErrNoRows { + if err == gopg.ErrNoRows { return nil } return err