-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
430 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
DATABASE_URL= | ||
KAKAO_REST_API_KEY= | ||
KAKAO_REDIRECT_URI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY, | ||
email VARCHAR(255) NOT NULL, | ||
password VARCHAR(255) NOT NULL, | ||
nickname VARCHAR(20) NOT NULL, | ||
fullname VARCHAR(20) NOT NULL, | ||
fb_provider_type VARCHAR(50), | ||
fb_uid VARCHAR(255), | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP | ||
); | ||
|
||
ALTER TABLE | ||
users | ||
ADD | ||
CONSTRAINT users_email_uix UNIQUE (email); | ||
|
||
ALTER TABLE | ||
users | ||
ADD | ||
CONSTRAINT users_fb_uid_uix UNIQUE (fb_uid); | ||
|
||
CREATE INDEX users_email_idx ON users (email); | ||
|
||
CREATE INDEX users_fb_uid_idx ON users (fb_uid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: "3.7" | ||
|
||
services: | ||
db_test: | ||
image: postgres:11.5-alpine | ||
ports: | ||
- "5455:5432" | ||
environment: | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_DB=pets_next_door_api_test | ||
volumes: | ||
- pg_pets_next_door_api_db_test:/var/lib/postgresql/data | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready -U postgres"] | ||
interval: 5s | ||
timeout: 5s | ||
retries: 5 | ||
|
||
migrate: | ||
image: migrate/migrate:v4.7.0 | ||
depends_on: | ||
db_test: | ||
condition: service_healthy | ||
volumes: | ||
- ./db/migrations:/db/migrations | ||
command: | ||
[ "-path", "/db/migrations", "-database", "postgresql://postgres:postgres@db_test:5432/pets_next_door_api_test?sslmode=disable", "up" ] | ||
restart: on-failure | ||
|
||
volumes: | ||
pg_pets_next_door_api_db_test: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
_ "github.com/golang-migrate/migrate/v4/database/postgres" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
) | ||
|
||
func Open(databaseURL string) (*DB, error) { | ||
db, err := sql.Open("postgres", databaseURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DB{DB: db, databaseURL: databaseURL}, nil | ||
} | ||
|
||
func (db *DB) Close() error { | ||
return db.DB.Close() | ||
} | ||
|
||
func (db *DB) Migrate() { | ||
|
||
m, err := migrate.New( | ||
"file://db/migrations", | ||
db.databaseURL, | ||
) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := m.Up(); err != nil && err != migrate.ErrNoChange { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func (db *DB) Begin() (*Tx, error) { | ||
tx, err := db.DB.Begin() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Tx{tx}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package database | ||
|
||
import "database/sql" | ||
|
||
type DB struct { | ||
DB *sql.DB | ||
databaseURL string | ||
} | ||
|
||
type Tx struct { | ||
*sql.Tx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package database | ||
|
||
import "github.com/pet-sitter/pets-next-door-api/internal/models" | ||
|
||
func (tx *Tx) CreateUser(user *models.User) (*models.User, error) { | ||
err := tx.QueryRow(` | ||
INSERT INTO | ||
users | ||
( | ||
email, | ||
nickname, | ||
fullname, | ||
password, | ||
fb_provider_type, | ||
fb_uid, | ||
created_at, | ||
updated_at | ||
) | ||
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW()) | ||
RETURNING id, created_at, updated_at | ||
`, | ||
user.Email, | ||
user.Nickname, | ||
user.Fullname, | ||
user.Password, | ||
user.FirebaseProviderType, | ||
user.FirebaseUID, | ||
).Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
func (tx *Tx) FindUserByEmail(email string) (*models.User, error) { | ||
user := &models.User{} | ||
|
||
err := tx.QueryRow(` | ||
SELECT | ||
id, | ||
email, | ||
nickname, | ||
fullname, | ||
fb_provider_type, | ||
fb_uid, | ||
created_at, | ||
updated_at | ||
FROM | ||
users | ||
WHERE | ||
email = $1 | ||
`, | ||
email, | ||
).Scan( | ||
&user.ID, | ||
&user.Email, | ||
&user.Nickname, | ||
&user.Fullname, | ||
&user.FirebaseProviderType, | ||
&user.FirebaseUID, | ||
&user.CreatedAt, | ||
&user.UpdatedAt, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
func (tx *Tx) FindUserByUID(uid string) (*models.User, error) { | ||
user := &models.User{} | ||
|
||
err := tx.QueryRow(` | ||
SELECT | ||
id, | ||
email, | ||
nickname, | ||
fullname, | ||
fb_provider_type, | ||
fb_uid, | ||
created_at, | ||
updated_at | ||
FROM | ||
users | ||
WHERE | ||
fb_uid = $1 | ||
`, | ||
uid, | ||
).Scan( | ||
&user.ID, | ||
&user.Email, | ||
&user.Nickname, | ||
&user.Fullname, | ||
&user.FirebaseProviderType, | ||
&user.FirebaseUID, | ||
&user.CreatedAt, | ||
&user.UpdatedAt, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
func (tx *Tx) UpdateUserByUID(uid string, nickname string) (*models.User, error) { | ||
user := &models.User{} | ||
|
||
err := tx.QueryRow(` | ||
UPDATE | ||
users | ||
SET | ||
nickname = $1, | ||
updated_at = NOW() | ||
WHERE | ||
fb_uid = $2 | ||
RETURNING id, created_at, updated_at | ||
`, | ||
nickname, | ||
uid, | ||
).Scan( | ||
&user.ID, | ||
&user.CreatedAt, | ||
&user.UpdatedAt, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
type User struct { | ||
ID int `field:"id"` | ||
Email string `field:"email"` | ||
Password string `field:"password"` | ||
Nickname string `field:"nickname"` | ||
Fullname string `field:"fullname"` | ||
FirebaseProviderType string `field:"fb_provider_type"` | ||
FirebaseUID string `field:"fb_uid"` | ||
CreatedAt string `field:"created_at"` | ||
UpdatedAt string `field:"updated_at"` | ||
DeletedAt string `field:"deleted_at"` | ||
} |
Oops, something went wrong.