From a3cf4f17f857101094a653cfb4802e02c5fde45b Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Tue, 22 Nov 2022 22:47:17 +0100 Subject: [PATCH] Solved some todo(s) --- service/api/authorization/auth-manager.go | 2 +- service/api/reqcontext/context-auth.go | 19 +++++++++++++++++++ service/api/reqcontext/request-context.go | 17 ----------------- service/database/db-stream.go | 3 +-- service/database/db-users.go | 16 ++++++---------- 5 files changed, 27 insertions(+), 30 deletions(-) create mode 100644 service/api/reqcontext/context-auth.go diff --git a/service/api/authorization/auth-manager.go b/service/api/authorization/auth-manager.go index 3434bd3..325ba18 100644 --- a/service/api/authorization/auth-manager.go +++ b/service/api/authorization/auth-manager.go @@ -14,7 +14,7 @@ func BuildAuth(header string) (reqcontext.Authorization, error) { auth, err := BuildBearer(header) if err != nil { if err.Error() == "invalid authorization header" { - return nil, errors.New("method not supported") // todo: better error description + return nil, errors.New("authentication method not supported") } return nil, err } diff --git a/service/api/reqcontext/context-auth.go b/service/api/reqcontext/context-auth.go new file mode 100644 index 0000000..a8dad60 --- /dev/null +++ b/service/api/reqcontext/context-auth.go @@ -0,0 +1,19 @@ +package reqcontext + +import "github.com/notherealmarco/WASAPhoto/service/database" + +type AuthStatus int + +const ( + AUTHORIZED = 0 + UNAUTHORIZED = 1 + FORBIDDEN = 2 + USER_NOT_FOUND = 3 +) + +type Authorization interface { + GetType() string + GetUserID() string + Authorized(db database.AppDatabase) (AuthStatus, error) + UserAuthorized(db database.AppDatabase, uid string) (AuthStatus, error) +} diff --git a/service/api/reqcontext/request-context.go b/service/api/reqcontext/request-context.go index c44ab2a..ecb3720 100644 --- a/service/api/reqcontext/request-context.go +++ b/service/api/reqcontext/request-context.go @@ -8,19 +8,9 @@ package reqcontext import ( "github.com/gofrs/uuid" - "github.com/notherealmarco/WASAPhoto/service/database" "github.com/sirupsen/logrus" ) -type AuthStatus int - -const ( - AUTHORIZED = 0 - UNAUTHORIZED = 1 - FORBIDDEN = 2 - USER_NOT_FOUND = 3 -) // todo: here? - // RequestContext is the context of the request, for request-dependent parameters type RequestContext struct { // ReqUUID is the request unique ID @@ -31,10 +21,3 @@ type RequestContext struct { Auth Authorization } - -type Authorization interface { - GetType() string - GetUserID() string - Authorized(db database.AppDatabase) (AuthStatus, error) - UserAuthorized(db database.AppDatabase, uid string) (AuthStatus, error) -} diff --git a/service/database/db-stream.go b/service/database/db-stream.go index ce54c52..c5542e9 100644 --- a/service/database/db-stream.go +++ b/service/database/db-stream.go @@ -5,10 +5,9 @@ import ( ) // Get user stream -// todo implement stidx + offset func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error) { - // Get photos + // Get photos from the database rows, err := db.c.Query(`SELECT "p"."user", "p"."id", "p"."date", ( SELECT COUNT(*) AS "likes" FROM "likes" AS "l" diff --git a/service/database/db-users.go b/service/database/db-users.go index c05b561..e528c3f 100644 --- a/service/database/db-users.go +++ b/service/database/db-users.go @@ -8,20 +8,16 @@ import ( "github.com/notherealmarco/WASAPhoto/service/structures" ) -//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) { //todo: refactor code - var name string - err := db.c.QueryRow(`SELECT "name" FROM "users" WHERE "uid" = ?`, uid).Scan(&name) +func (db *appdbimpl) UserExists(uid string) (bool, error) { - if db_errors.EmptySet(err) { - return false, nil - } else if err != nil { + var cnt int + err := db.c.QueryRow(`SELECT COUNT(*) FROM "users" WHERE "uid" = ?`, uid).Scan(&cnt) + + if err != nil { return false, err } - return true, nil + return cnt > 0, nil } // Get user id by username