Skip to content

Commit

Permalink
handle db
Browse files Browse the repository at this point in the history
  • Loading branch information
milwad-dev committed Apr 27, 2024
1 parent 9091e26 commit 4be0665
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module do-it
go 1.22

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
4 changes: 2 additions & 2 deletions internal/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package handlers
import "database/sql"

type DBHandler struct {
db *sql.DB
*sql.DB
}

// NewDBHandler => create a new instance from DBHandler and return it
func NewDBHandler(db *sql.DB) *DBHandler {
return &DBHandler{db: db}
return &DBHandler{db}
}
31 changes: 31 additions & 0 deletions internal/handlers/user_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handlers

import (
"do-it/internal/models"
"do-it/internal/utils"
"log"
"net/http"
)

// GetLatestUsers => get the latest users and return json response
func (db *DBHandler) GetLatestUsers(w http.ResponseWriter, r *http.Request) {
var users []models.User

sql := "SELECT id, name, email, phone, created_at FROM USERS ORDER BY created_at DESC"
rows, err := db.Query(sql)
if err != nil {
log.Fatal(err)
}

for rows.Next() {
var user models.User
err := rows.Scan(&user.ID, &user.Name, &user.Email, &user.Phone, &user.CreatedAt)
if err != nil {
panic(err.Error())
}

users = append(users, user)
}

utils.JsonResponse(w, users)
}
7 changes: 3 additions & 4 deletions internal/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package routers
import (
"do-it/internal/handlers"
"github.com/go-chi/chi/v5"
"net/http"
)

func GetRouter(handler *handlers.DBHandler) *chi.Mux {
router := chi.NewRouter()
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("milwad"))
})

// Users
router.Get("/users", handler.GetLatestUsers)

return router
}
12 changes: 12 additions & 0 deletions internal/utils/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

import (
"encoding/json"
"net/http"
)

// JsonResponse => set json header and return json response
func JsonResponse(w http.ResponseWriter, data any) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"do-it/internal/handlers"
"do-it/internal/routers"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"log"
"net/http"
Expand Down

0 comments on commit 4be0665

Please sign in to comment.