Add get followers

This commit is contained in:
Marco Realacci 2022-11-18 18:58:12 +01:00
parent b89296c249
commit e8047c77a0
10 changed files with 162 additions and 17 deletions

View file

@ -34,6 +34,8 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/notherealmarco/WASAPhoto/service/structures"
)
// AppDatabase is the high level interface for the DB
@ -42,6 +44,7 @@ type AppDatabase interface {
GetUserID(name string) (string, error)
UpdateUsername(uid, name string) error
CreateUser(name string) (string, error)
GetUserFollowers(uid string) ([]structures.UIDName, error)
FollowUser(uid string, follow string) error
UnfollowUser(uid string, unfollow string) error
BanUser(uid string, ban string) error

View file

@ -3,6 +3,7 @@ package database
import (
"github.com/gofrs/uuid"
"github.com/notherealmarco/WASAPhoto/service/database/db_errors"
"github.com/notherealmarco/WASAPhoto/service/structures"
)
//Check if user exists and if exists return the user id by username
@ -44,6 +45,28 @@ func (db *appdbimpl) UpdateUsername(uid string, name string) error {
return err
}
// Get user followers
func (db *appdbimpl) GetUserFollowers(uid string) ([]structures.UIDName, error) {
rows, err := db.c.Query(`SELECT "follower", "user.name" FROM "follows", "users"
WHERE "follows.follower" = "users.uid"
AND "followed" = ?`, uid)
if err != nil {
return nil, err
}
var followers []structures.UIDName = make([]structures.UIDName, 0)
for rows.Next() {
var uid string
var name string
err = rows.Scan(&uid, &name)
if err != nil {
return nil, err
}
followers = append(followers, structures.UIDName{UID: uid, Name: name})
}
return followers, nil
}
// Follow a user
func (db *appdbimpl) FollowUser(uid string, follow string) error {
_, err := db.c.Exec(`INSERT INTO "follows" ("follower", "followed") VALUES (?, ?)`, uid, follow)