Add list API endpoints

This commit is contained in:
Kevin Kandlbinder 2022-03-01 13:39:46 +01:00
parent 1e071ffed4
commit c572bbce85
5 changed files with 275 additions and 2 deletions

View file

@ -91,8 +91,6 @@ func GetEntries(first int64, cursor *primitive.ObjectID) ([]*model.DBEntry, erro
return nil, err
}
log.Println("DBG1")
return object, nil
}
@ -144,6 +142,57 @@ func GetListByID(id primitive.ObjectID) (*model.DBHashList, error) {
return &object, nil
}
func GetListByName(name string) (*model.DBHashList, error) {
db := DbClient.Database(viper.GetString("bot.mongo.database"))
res := db.Collection(viper.GetString("bot.mongo.collection.lists")).FindOne(context.TODO(), bson.D{{"name", name}})
if res.Err() != nil {
return nil, res.Err()
}
object := model.DBHashList{}
err := res.Decode(&object)
if err != nil {
return nil, err
}
return &object, nil
}
func GetLists(first int64, cursor *primitive.ObjectID) ([]*model.DBHashList, error) {
db := DbClient.Database(viper.GetString("bot.mongo.database"))
opts := options.FindOptions{
Limit: &first,
}
filter := bson.M{}
if cursor != nil {
filter = bson.M{
"_id": bson.M{
"$gt": *cursor,
},
}
log.Println(filter)
}
res, err := db.Collection(viper.GetString("bot.mongo.collection.lists")).Find(context.TODO(), filter, &opts)
if err != nil {
return nil, res.Err()
}
var object []*model.DBHashList
err = res.All(context.TODO(), &object)
if err != nil {
return nil, err
}
return object, nil
}
func SaveUser(user *model.DBUser) error {
db := DbClient.Database(viper.GetString("bot.mongo.database"))

View file

@ -4,6 +4,7 @@ import "go.mongodb.org/mongo-driver/bson/primitive"
type DBHashList struct {
ID primitive.ObjectID `bson:"_id" json:"id"`
Name string `bson:"name" json:"name"`
Tags []string `bson:"tags" json:"tags"` // Tags of this list for discovery, and sorting
Comments []*DBComment `bson:"comments" json:"comments"` // Comments regarding this list
Maintainers []*primitive.ObjectID `bson:"maintainers" json:"maintainers"` // Maintainers contains references to the users who may edit this list

View file

@ -12,6 +12,8 @@ type DBUser struct {
Username string `bson:"username" json:"username"` // Username is the username the user has
HashedPassword string `bson:"password" json:"password"` // HashedPassword contains the bcrypt-ed password
Admin *bool `bson:"admin,omitempty" json:"admin,omitempty"` // If set to true this user will have all privileges
MatrixLinks []*string `bson:"matrix_links" json:"matrix_links"` // MatrixLinks is the matrix-users this user has verified ownership over
PendingMatrixLinks []*string `bson:"pending_matrix_links" json:"pending_matrix_links"` // PendingMatrixLinks is the matrix-users pending verification