Created
April 23, 2024 06:57
-
-
Save pingkunga/a82456733388b3a2cdae81b00dfaf5b0 to your computer and use it in GitHub Desktop.
Golang Controller / Service / Repository with Postgres
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
package main | |
import ( | |
"net/http" | |
"github.com/labstack/echo/v4" | |
) | |
type Handler struct { | |
service *Service | |
} | |
func NewHandler(service *Service) *Handler { | |
return &Handler{service: service} | |
} | |
func (h *Handler) GetUser(c echo.Context) error { | |
id := c.Param("id") | |
user, err := h.service.GetUser(id) | |
if err != nil { | |
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | |
} | |
return c.JSON(http.StatusOK, user) | |
} |
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
package main | |
import ( | |
"net/http" | |
"github.com/labstack/echo/v4" | |
"github.com/labstack/echo/v4/middleware" | |
) | |
func main() { | |
e := echo.New() | |
e.Use(middleware.Logger()) | |
e.Use(middleware.Recover()) | |
db := NewPostgresDB() // replace with your actual DB initialization | |
repo := NewRepository(db) | |
service := NewService(repo) | |
handler := NewHandler(service) | |
e.GET("/users/:id", handler.GetUser) | |
e.Start(":8080") | |
} |
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
package main | |
type User struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
} |
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
package main | |
import ( | |
"database/sql" | |
) | |
type Repository struct { | |
db *sql.DB | |
} | |
func NewRepository(db *sql.DB) *Repository { | |
return &Repository{db: db} | |
} | |
func (r *Repository) GetUser(id string) (*User, error) { | |
// implement your SQL query to fetch user by id from Postgres | |
} |
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
package main | |
type Service struct { | |
repo *Repository | |
} | |
func NewService(repo *Repository) *Service { | |
return &Service{repo: repo} | |
} | |
func (s *Service) GetUser(id string) (*User, error) { | |
return s.repo.GetUser(id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment