something is cooking
This commit is contained in:
parent
3baf2f97b5
commit
a9623fa7be
9 changed files with 56 additions and 32 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
secret-santa
|
||||
*.db
|
|
@ -1,4 +1,4 @@
|
|||
development:
|
||||
dialect: sqlite3
|
||||
datasource: test.db
|
||||
dir: migrations/
|
||||
dir: sql/migrations/
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
package queries
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
FirstName string
|
||||
LastName string
|
||||
Email string
|
||||
ID int64 `json:"id"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
|
|
@ -14,9 +14,9 @@ INSERT INTO users (id, email, first_name, last_name) VALUES (NULL, ?, ?, ?)
|
|||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Email string
|
||||
FirstName string
|
||||
LastName string
|
||||
Email string `json:"email"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
|
|
9
internal/types/response.go
Normal file
9
internal/types/response.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package types
|
||||
|
||||
type JsonResponse[T any] struct {
|
||||
Data T `json:"data"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
53
main.go
53
main.go
|
@ -3,36 +3,49 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/queries"
|
||||
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/types"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var dbConnection *sql.DB
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
dbConnection, err = sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Database sucessfully connected")
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
dbConnection, err := sql.Open("sqlite3", "./test.db")
|
||||
log.Println("Created database connection")
|
||||
|
||||
router := http.NewServeMux()
|
||||
|
||||
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
query := queries.New(dbConnection)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
users, err := query.GetUsers(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
w.WriteHeader(404)
|
||||
log.Printf("Failed to get all users: %s\n", err.Error())
|
||||
json.NewEncoder(w).Encode(types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
db := queries.New(dbConnection)
|
||||
if len(users) == 0 {
|
||||
users = make([]queries.User, 0)
|
||||
}
|
||||
json.NewEncoder(w).Encode(types.JsonResponse[[]queries.User]{Data: users})
|
||||
})
|
||||
|
||||
if err := db.CreateUser(ctx, queries.CreateUserParams{
|
||||
Email: "jakub.kropacek@olc.cz",
|
||||
FirstName: "Jakub",
|
||||
LastName: "Kropáček",
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
users, err := db.GetUsers(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
fmt.Println(user)
|
||||
}
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
-- +migrate Up
|
||||
CREATE TABLE users (
|
||||
id INT PRIMARY KEY,
|
||||
id INTEGER PRIMARY KEY,
|
||||
first_name VARCHAR(64) NOT NULL,
|
||||
last_name VARCHAR(64) NOT NULL,
|
||||
email VARCHAR(128) NOT NULL
|
|
@ -1,9 +1,10 @@
|
|||
version: "2"
|
||||
sql:
|
||||
- engine: sqlite
|
||||
queries: queries/
|
||||
schema: migrations/
|
||||
queries: sql/queries/
|
||||
schema: sql/migrations/
|
||||
gen:
|
||||
go:
|
||||
package: "queries"
|
||||
out: "internal/queries"
|
||||
emit_json_tags: true
|
Loading…
Reference in a new issue