Files
server/database/application.go
Jannis Mattheis c912bb8cba Delete Clients/Apps/Messages when deleting a user
sqlite3 doesn't support adding a foreign key via gorm.DB#AddForeignKey
this would mean, that we have some hacky solutions for having foreign
keys for sqlite and the other databases. Therefore manually deleting
the referencing models seems to be the best solution. We already have
interfaces for database capsuling, so no api must be adjusted.
2018-04-01 12:26:06 +02:00

49 lines
1.3 KiB
Go

package database
import (
"github.com/gotify/server/model"
)
// GetApplicationByToken returns the application for the given token or nil.
func (d *GormDatabase) GetApplicationByToken(token string) *model.Application {
app := new(model.Application)
d.DB.Where("token = ?", token).Find(app)
if app.Token == token {
return app
}
return nil
}
// GetApplicationByID returns the application for the given id or nil.
func (d *GormDatabase) GetApplicationByID(id uint) *model.Application {
app := new(model.Application)
d.DB.Where("id = ?", id).Find(app)
if app.ID == id {
return app
}
return nil
}
// CreateApplication creates an application.
func (d *GormDatabase) CreateApplication(application *model.Application) error {
return d.DB.Create(application).Error
}
// DeleteApplicationByID deletes an application by its id.
func (d *GormDatabase) DeleteApplicationByID(id uint) error {
d.DeleteMessagesByApplication(id)
return d.DB.Where("id = ?", id).Delete(&model.Application{}).Error
}
// GetApplicationsByUser returns all applications from a user.
func (d *GormDatabase) GetApplicationsByUser(userID uint) []*model.Application {
var apps []*model.Application
d.DB.Where("user_id = ?", userID).Find(&apps)
return apps
}
// UpdateApplication updates an application.
func (d *GormDatabase) UpdateApplication(app *model.Application) {
d.DB.Save(app)
}