kinda working

This commit is contained in:
wagslane
2023-05-30 21:28:23 -06:00
parent 7d1852d380
commit 80ec635705
91 changed files with 13103 additions and 2 deletions

23
internal/auth/auth.go Normal file
View File

@@ -0,0 +1,23 @@
package auth
import (
"errors"
"net/http"
"strings"
)
var ErrNoAuthHeaderIncluded = errors.New("no authorization header included")
// GetAPIKey -
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
splitAuth := strings.Split(authHeader, " ")
if len(splitAuth) < 2 || splitAuth[0] != "ApiKey" {
return "", errors.New("malformed authorization header")
}
return splitAuth[1], nil
}

31
internal/database/db.go Normal file
View 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,
}
}

View 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
}

View 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
}

View 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
}