add not found error

add checks for if something actually exists in db
This commit is contained in:
mStar aka a person 2024-01-29 17:11:09 +00:00
parent 77b275130b
commit 5593efee37
3 changed files with 20 additions and 0 deletions

View file

@ -18,6 +18,11 @@ type Follow struct {
} }
func (s *Storage) GetFollowersFor(url string) ([]Follow, error) { func (s *Storage) GetFollowersFor(url string) ([]Follow, error) {
var pHit int64
s.Db.Where("url = ?", url).Count(&pHit)
if pHit <= 0 {
return nil, ErrNotFound
}
var followed *Person = &Person{} var followed *Person = &Person{}
s.Db.Table("people").First(&followed, "url = ?", url) s.Db.Table("people").First(&followed, "url = ?", url)
var f []Follow = make([]Follow, 0) var f []Follow = make([]Follow, 0)

View file

@ -57,3 +57,15 @@ func (pdb *Person) IntoAPPerson() (*astreams.Person, error) {
actor.PreferredUsername = pdb.Name actor.PreferredUsername = pdb.Name
return nil, errors.New("unimplemented") return nil, errors.New("unimplemented")
} }
// Gets a person from the db by name
func (s *Storage) GetPersonByName(name string) (*Person, error) {
var nrFound int64
s.Db.Table("people").Where("name = ?", name).Count(&nrFound)
if nrFound <= 0 {
return nil, ErrNotFound
}
var p Person
s.Db.Table("people").Where("name = ?", name).First(&p)
return &p, nil
}

View file

@ -11,6 +11,7 @@
package storage package storage
import ( import (
"errors"
"fmt" "fmt"
"gitlab.com/mstarongitlab/linstrom/config" "gitlab.com/mstarongitlab/linstrom/config"
@ -18,6 +19,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
var ErrNotFound = errors.New("entry not found")
type Storage struct { type Storage struct {
Db *gorm.DB Db *gorm.DB
} }