bin/models.go

50 lines
1005 B
Go
Raw Normal View History

2023-09-10 23:40:19 +02:00
package main
import (
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type File struct {
gorm.Model
PageKey string `gorm:"index:idx_pagekey,unique"`
AdminKey string `gorm:"index:idx_adminkey"`
// The "virtual" filename.
Filename string
// The description of the file submission.
Description string
Language string
// The contents of the file must be valid text.
Data string
}
func databaseMigrations(db *gorm.DB) {
logrus.Infoln("Running database migrations.")
err := db.AutoMigrate(&File{})
if err != nil {
logrus.WithError(err).Fatalln("Failed to run database migration.")
}
logrus.Infoln("Migrations ran successfully.")
}
func createNewBin(text, name, description, language, key, adminKey string) (File, error) {
bin := File{
Filename: name,
Description: description,
Language: language,
PageKey: key,
AdminKey: adminKey,
Data: text,
}
result := db.Create(&bin)
if result.Error != nil {
return File{}, result.Error
}
return bin, nil
}