mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-05-05 12:22:35 +02:00
identity providers and bearerauth
This commit is contained in:
parent
5f3d4df33a
commit
626b7fa3e9
32 changed files with 1317 additions and 12 deletions
|
@ -38,9 +38,10 @@ import (
|
|||
|
||||
// AppDatabase is the high level interface for the DB
|
||||
type AppDatabase interface {
|
||||
UserExists(uid string) (bool, error)
|
||||
GetUserID(name string) (string, error)
|
||||
SetName(name string) error
|
||||
CreateUser(uid string, name string) error
|
||||
CreateUser(name string) (string, error)
|
||||
FollowUser(uid string, follow string) error
|
||||
UnfollowUser(uid string, unfollow string) error
|
||||
BanUser(uid string, ban string) error
|
||||
|
|
|
@ -1,8 +1,26 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/notherealmarco/WASAPhoto/service/database/db_errors"
|
||||
)
|
||||
|
||||
//Check if user exists and if exists return the user id by username
|
||||
//todo
|
||||
|
||||
// Check if user exists
|
||||
func (db *appdbimpl) UserExists(uid string) (bool, error) {
|
||||
var name string
|
||||
err := db.c.QueryRow(`SELECT "name" FROM "users" WHERE "uid" = ?`, name).Scan(&name)
|
||||
|
||||
if db_errors.EmptySet(err) {
|
||||
return false, nil
|
||||
} else if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Get user id by username
|
||||
func (db *appdbimpl) GetUserID(name string) (string, error) {
|
||||
var uid string
|
||||
|
@ -11,9 +29,13 @@ func (db *appdbimpl) GetUserID(name string) (string, error) {
|
|||
}
|
||||
|
||||
// Create a new user
|
||||
func (db *appdbimpl) CreateUser(uid string, name string) error {
|
||||
_, err := db.c.Exec(`INSERT INTO "users" ("uid", "name") VALUES (?, ?)`, uid, name)
|
||||
return err
|
||||
func (db *appdbimpl) CreateUser(name string) (string, error) {
|
||||
uid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = db.c.Exec(`INSERT INTO "users" ("uid", "name") VALUES (?, ?)`, uid.String(), name)
|
||||
return uid.String(), err
|
||||
}
|
||||
|
||||
// Follow a user
|
||||
|
|
11
service/database/db_errors/db-errors.go
Normal file
11
service/database/db_errors/db-errors.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package db_errors
|
||||
|
||||
import "strings"
|
||||
|
||||
// Returns true if the query result has no rows
|
||||
func EmptySet(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(err.Error(), "no rows in result set")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue