kinda working
This commit is contained in:
31
internal/database/db.go
Normal file
31
internal/database/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.18.0
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
25
internal/database/models.go
Normal file
25
internal/database/models.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.18.0
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Note struct {
|
||||
ID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Note string
|
||||
UserID string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Name string
|
||||
ApiKey string
|
||||
}
|
||||
113
internal/database/notes.sql.go
Normal file
113
internal/database/notes.sql.go
Normal file
@@ -0,0 +1,113 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.18.0
|
||||
// source: notes.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createNote = `-- name: CreateNote :exec
|
||||
INSERT INTO notes (id, created_at, updated_at, note, user_id)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
type CreateNoteParams struct {
|
||||
ID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Note string
|
||||
UserID string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateNote(ctx context.Context, arg CreateNoteParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createNote,
|
||||
arg.ID,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
arg.Note,
|
||||
arg.UserID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteNote = `-- name: DeleteNote :exec
|
||||
|
||||
DELETE FROM notes WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteNote(ctx context.Context, id string) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteNote, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getNote = `-- name: GetNote :one
|
||||
|
||||
SELECT id, created_at, updated_at, note, user_id FROM notes WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetNote(ctx context.Context, id string) (Note, error) {
|
||||
row := q.db.QueryRowContext(ctx, getNote, id)
|
||||
var i Note
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Note,
|
||||
&i.UserID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getNotesForUser = `-- name: GetNotesForUser :many
|
||||
|
||||
SELECT id, created_at, updated_at, note, user_id FROM notes WHERE user_id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetNotesForUser(ctx context.Context, userID string) ([]Note, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getNotesForUser, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Note
|
||||
for rows.Next() {
|
||||
var i Note
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Note,
|
||||
&i.UserID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateNote = `-- name: UpdateNote :exec
|
||||
|
||||
UPDATE notes SET updated_at = ?, note = ? WHERE id = ?
|
||||
`
|
||||
|
||||
type UpdateNoteParams struct {
|
||||
UpdatedAt time.Time
|
||||
Note string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateNote(ctx context.Context, arg UpdateNoteParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateNote, arg.UpdatedAt, arg.Note, arg.ID)
|
||||
return err
|
||||
}
|
||||
59
internal/database/users.sql.go
Normal file
59
internal/database/users.sql.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.18.0
|
||||
// source: users.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :exec
|
||||
INSERT INTO users (id, created_at, updated_at, name, api_key)
|
||||
VALUES (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
ID string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Name string
|
||||
ApiKey string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createUser,
|
||||
arg.ID,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
arg.Name,
|
||||
arg.ApiKey,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const getUserByAPIKey = `-- name: GetUserByAPIKey :one
|
||||
|
||||
SELECT id, created_at, updated_at, name, api_key FROM users WHERE api_key = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByAPIKey(ctx context.Context, apiKey string) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserByAPIKey, apiKey)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Name,
|
||||
&i.ApiKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user