feat: Added chapter 8

This commit is contained in:
2024-10-26 15:03:14 +02:00
parent 61beecd4a0
commit 8b7fdf59b5
13 changed files with 289 additions and 5 deletions

View File

@ -81,3 +81,15 @@ func MakeRefreshToken() (string, error) {
hexData := hex.EncodeToString(buffer)
return hexData, nil
}
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", errors.New("authorization header is not set")
}
if !strings.HasPrefix(authHeader, "ApiKey ") {
return "", errors.New("incorrect authorization type, must be of type ApiKey")
}
apiKey := strings.TrimPrefix(authHeader, "ApiKey ")
return strings.TrimSpace(apiKey), nil
}

View File

@ -41,6 +41,22 @@ func (q *Queries) CreateChirp(ctx context.Context, arg CreateChirpParams) (Chirp
return i, err
}
const deleteChirp = `-- name: DeleteChirp :exec
DELETE FROM chirps
WHERE id = $1
AND user_id = $2
`
type DeleteChirpParams struct {
ID uuid.UUID
UserID uuid.UUID
}
func (q *Queries) DeleteChirp(ctx context.Context, arg DeleteChirpParams) error {
_, err := q.db.ExecContext(ctx, deleteChirp, arg.ID, arg.UserID)
return err
}
const getChirp = `-- name: GetChirp :one
SELECT id, created_at, updated_at, body, user_id FROM chirps
WHERE chirps.id = $1

View File

@ -34,4 +34,5 @@ type User struct {
UpdatedAt time.Time
Email string
HashedPassword string
IsChirpyRed bool
}

View File

@ -0,0 +1,49 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: update_users.sql
package database
import (
"context"
"time"
"github.com/google/uuid"
)
const updateUserCredentials = `-- name: UpdateUserCredentials :one
UPDATE users
SET email = $2,
hashed_password = $3,
updated_at = NOW()
WHERE users.id = $1
RETURNING id, created_at, updated_at, email, is_chirpy_red
`
type UpdateUserCredentialsParams struct {
ID uuid.UUID
Email string
HashedPassword string
}
type UpdateUserCredentialsRow struct {
ID uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
Email string
IsChirpyRed bool
}
func (q *Queries) UpdateUserCredentials(ctx context.Context, arg UpdateUserCredentialsParams) (UpdateUserCredentialsRow, error) {
row := q.db.QueryRowContext(ctx, updateUserCredentials, arg.ID, arg.Email, arg.HashedPassword)
var i UpdateUserCredentialsRow
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Email,
&i.IsChirpyRed,
)
return i, err
}

View File

@ -7,6 +7,8 @@ package database
import (
"context"
"github.com/google/uuid"
)
const createUser = `-- name: CreateUser :one
@ -18,7 +20,7 @@ VALUES (
$1,
$2
)
RETURNING id, created_at, updated_at, email, hashed_password
RETURNING id, created_at, updated_at, email, hashed_password, is_chirpy_red
`
type CreateUserParams struct {
@ -35,13 +37,14 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
&i.UpdatedAt,
&i.Email,
&i.HashedPassword,
&i.IsChirpyRed,
)
return i, err
}
const deleteUser = `-- name: DeleteUser :one
DELETE FROM users
RETURNING id, created_at, updated_at, email, hashed_password
RETURNING id, created_at, updated_at, email, hashed_password, is_chirpy_red
`
func (q *Queries) DeleteUser(ctx context.Context) (User, error) {
@ -53,12 +56,13 @@ func (q *Queries) DeleteUser(ctx context.Context) (User, error) {
&i.UpdatedAt,
&i.Email,
&i.HashedPassword,
&i.IsChirpyRed,
)
return i, err
}
const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, created_at, updated_at, email, hashed_password FROM users
SELECT id, created_at, updated_at, email, hashed_password, is_chirpy_red FROM users
WHERE users.email = $1
`
@ -71,6 +75,18 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error
&i.UpdatedAt,
&i.Email,
&i.HashedPassword,
&i.IsChirpyRed,
)
return i, err
}
const upgradeUser = `-- name: UpgradeUser :exec
UPDATE users
SET is_chirpy_red = true
WHERE id = $1
`
func (q *Queries) UpgradeUser(ctx context.Context, id uuid.UUID) error {
_, err := q.db.ExecContext(ctx, upgradeUser, id)
return err
}