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
|
secret-santa
|
||||||
|
*.db
|
|
@ -1,4 +1,4 @@
|
||||||
development:
|
development:
|
||||||
dialect: sqlite3
|
dialect: sqlite3
|
||||||
datasource: test.db
|
datasource: test.db
|
||||||
dir: migrations/
|
dir: sql/migrations/
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
package queries
|
package queries
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int64
|
ID int64 `json:"id"`
|
||||||
FirstName string
|
FirstName string `json:"first_name"`
|
||||||
LastName string
|
LastName string `json:"last_name"`
|
||||||
Email string
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ INSERT INTO users (id, email, first_name, last_name) VALUES (NULL, ?, ?, ?)
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateUserParams struct {
|
type CreateUserParams struct {
|
||||||
Email string
|
Email string `json:"email"`
|
||||||
FirstName string
|
FirstName string `json:"first_name"`
|
||||||
LastName string
|
LastName string `json:"last_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/queries"
|
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/queries"
|
||||||
|
"git.katuwoss.dev/JustScreaMy/secret-santa/internal/types"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "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() {
|
func main() {
|
||||||
ctx := context.Background()
|
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 {
|
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{
|
log.Fatal(http.ListenAndServe(":8080", router))
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
-- +migrate Up
|
-- +migrate Up
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id INT PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
first_name VARCHAR(64) NOT NULL,
|
first_name VARCHAR(64) NOT NULL,
|
||||||
last_name VARCHAR(64) NOT NULL,
|
last_name VARCHAR(64) NOT NULL,
|
||||||
email VARCHAR(128) NOT NULL
|
email VARCHAR(128) NOT NULL
|
|
@ -1,9 +1,10 @@
|
||||||
version: "2"
|
version: "2"
|
||||||
sql:
|
sql:
|
||||||
- engine: sqlite
|
- engine: sqlite
|
||||||
queries: queries/
|
queries: sql/queries/
|
||||||
schema: migrations/
|
schema: sql/migrations/
|
||||||
gen:
|
gen:
|
||||||
go:
|
go:
|
||||||
package: "queries"
|
package: "queries"
|
||||||
out: "internal/queries"
|
out: "internal/queries"
|
||||||
|
emit_json_tags: true
|
Loading…
Reference in a new issue