-
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.
feat: update repository and domain models
- Loading branch information
Showing
10 changed files
with
330 additions
and
227 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,76 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
_ "github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/joho/godotenv" | ||
"github.com/kyong0612/my-go-clean-architecture/internal/repository" | ||
"github.com/kyong0612/my-go-clean-architecture/internal/rest" | ||
"github.com/kyong0612/my-go-clean-architecture/internal/rest/middleware" | ||
"github.com/kyong0612/my-go-clean-architecture/usecase/article" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// const ( | ||
// defaultTimeout = 30 | ||
// defaultAddress = ":9090" | ||
// ) | ||
const ( | ||
defaultTimeout = 30 | ||
defaultAddress = ":9090" | ||
) | ||
|
||
// func init() { | ||
// err := godotenv.Load() | ||
// if err != nil { | ||
// log.Fatal("Error loading .env file") | ||
// } | ||
// } | ||
func init() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Println("Hello World") | ||
// //prepare database | ||
// dbHost := os.Getenv("DATABASE_HOST") | ||
// dbPort := os.Getenv("DATABASE_PORT") | ||
// dbUser := os.Getenv("DATABASE_USER") | ||
// dbPass := os.Getenv("DATABASE_PASS") | ||
// dbName := os.Getenv("DATABASE_NAME") | ||
// connection := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPass, dbHost, dbPort, dbName) | ||
// val := url.Values{} | ||
// val.Add("parseTime", "1") | ||
// val.Add("loc", "Asia/Jakarta") | ||
// dsn := fmt.Sprintf("%s?%s", connection, val.Encode()) | ||
// dbConn, err := sql.Open(`rdb`, dsn) | ||
// if err != nil { | ||
// log.Fatal("failed to open connection to database", err) | ||
// } | ||
// err = dbConn.Ping() | ||
// if err != nil { | ||
// log.Fatal("failed to ping database ", err) | ||
// } | ||
//prepare database | ||
dbHost := os.Getenv("DATABASE_HOST") | ||
dbPort := os.Getenv("DATABASE_PORT") | ||
dbUser := os.Getenv("DATABASE_USER") | ||
dbPass := os.Getenv("DATABASE_PASS") | ||
dbName := os.Getenv("DATABASE_NAME") | ||
connURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=verify-full", dbUser, dbPass, dbHost, dbPort, dbName) | ||
|
||
dbConn, err := pgx.Connect(context.TODO(), connURL) | ||
if err != nil { | ||
log.Fatal("failed to open connection to database", err) | ||
} | ||
defer dbConn.Close(context.TODO()) | ||
|
||
// defer func() { | ||
// err := dbConn.Close() | ||
// if err != nil { | ||
// log.Fatal("got error when closing the DB connection", err) | ||
// } | ||
// }() | ||
// // prepare echo | ||
// prepare echo | ||
|
||
// e := echo.New() | ||
// e.Use(middleware.CORS) | ||
// timeoutStr := os.Getenv("CONTEXT_TIMEOUT") | ||
// timeout, err := strconv.Atoi(timeoutStr) | ||
// if err != nil { | ||
// log.Println("failed to parse timeout, using default timeout") | ||
// timeout = defaultTimeout | ||
// } | ||
// timeoutContext := time.Duration(timeout) * time.Second | ||
// e.Use(middleware.SetRequestContextWithTimeout(timeoutContext)) | ||
e := echo.New() | ||
e.Use(middleware.CORS) | ||
timeoutStr := os.Getenv("CONTEXT_TIMEOUT") | ||
timeout, err := strconv.Atoi(timeoutStr) | ||
if err != nil { | ||
log.Println("failed to parse timeout, using default timeout") | ||
timeout = defaultTimeout | ||
} | ||
timeoutContext := time.Duration(timeout) * time.Second | ||
e.Use(middleware.SetRequestContextWithTimeout(timeoutContext)) | ||
|
||
// // Prepare Repository | ||
// authorRepo := rdbRepo.NewAuthorRepository(dbConn) | ||
// articleRepo := rdbRepo.NewArticleRepository(dbConn) | ||
// Prepare Repository | ||
repo := repository.New(dbConn) | ||
|
||
// // Build service Layer | ||
// svc := article.NewService(articleRepo, authorRepo) | ||
// rest.NewArticleHandler(e, svc) | ||
// Build service Layer | ||
svc := article.NewService(repo) | ||
rest.NewArticleHandler(e, svc) | ||
|
||
// // Start Server | ||
// address := os.Getenv("SERVER_ADDRESS") | ||
// if address == "" { | ||
// address = defaultAddress | ||
// } | ||
// log.Fatal(e.Start(os.Getenv("SERVER_ADDRESS"))) //nolint | ||
// Start Server | ||
address := os.Getenv("SERVER_ADDRESS") | ||
if address == "" { | ||
address = defaultAddress | ||
} | ||
log.Fatal(e.Start(os.Getenv("SERVER_ADDRESS"))) //nolint | ||
} |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package domain | ||
|
||
import "time" | ||
|
||
// Author representing the Author data struct. | ||
type Author struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
CreatedAt string `json:"created_at"` | ||
UpdatedAt string `json:"updated_at"` | ||
ID int32 `json:"id"` | ||
Name string `json:"name"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} |
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,49 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/kyong0612/my-go-clean-architecture/domain" | ||
"github.com/kyong0612/my-go-clean-architecture/internal/repository/postgres" | ||
) | ||
|
||
func articleToModel(article postgres.Article, author postgres.Author) domain.Article { | ||
return domain.Article{ | ||
ID: article.ID, | ||
Title: article.Title, | ||
Content: article.Content, | ||
Author: authorToModel(author), | ||
CreatedAt: article.CreatedAt.Time, | ||
UpdatedAt: article.UpdatedAt.Time, | ||
} | ||
} | ||
|
||
func (r *Repository) FetchArticles(ctx context.Context, cursor string, num int32) (res []domain.Article, nextCursor string, err error) { | ||
// TODO: implement the code | ||
return nil, "", nil | ||
} | ||
|
||
func (r *Repository) GetArticleByID(ctx context.Context, id int32) (domain.Article, error) { | ||
// TODO: implement the code | ||
return domain.Article{}, nil | ||
} | ||
|
||
func (r *Repository) GetArticleByTitle(ctx context.Context, title string) (domain.Article, error) { | ||
// TODO: implement the code | ||
return domain.Article{}, nil | ||
} | ||
|
||
func (r *Repository) UpdateArticle(ctx context.Context, ar *domain.Article) error { | ||
// TODO: implement the code | ||
return nil | ||
} | ||
|
||
func (r *Repository) StoreArticle(ctx context.Context, a *domain.Article) error { | ||
// TODO: implement the code | ||
return nil | ||
} | ||
|
||
func (r *Repository) DeleteArticle(ctx context.Context, id int32) error { | ||
// TODO: implement the code | ||
return 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,25 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/kyong0612/my-go-clean-architecture/domain" | ||
"github.com/kyong0612/my-go-clean-architecture/internal/repository/postgres" | ||
) | ||
|
||
func authorToModel(author postgres.Author) domain.Author { | ||
return domain.Author{ | ||
ID: author.ID, | ||
Name: author.Name, | ||
CreatedAt: author.CreatedAt.Time, | ||
UpdatedAt: author.UpdatedAt.Time, | ||
} | ||
} | ||
|
||
func (r *Repository) GetAuthorByID(ctx context.Context, id int32) (domain.Author, error) { | ||
author, err := r.db.GetAuthorByID(ctx, id) | ||
if err != nil { | ||
return domain.Author{}, err | ||
} | ||
return authorToModel(author), 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,13 @@ | ||
package repository | ||
|
||
import "github.com/kyong0612/my-go-clean-architecture/internal/repository/postgres" | ||
|
||
type Repository struct { | ||
db *postgres.Queries | ||
} | ||
|
||
func New(db postgres.DBTX) *Repository { | ||
return &Repository{ | ||
db: postgres.New(db), | ||
} | ||
} |
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
Oops, something went wrong.