Implement deletion of users

This commit is contained in:
eikendev 2020-07-27 00:53:32 +02:00
parent 18d11677ac
commit f6ca287d0b
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
10 changed files with 112 additions and 35 deletions

View file

@ -18,31 +18,28 @@ func (d *Database) DeleteApplication(application *model.Application) error {
return d.gormdb.Delete(application).Error
}
// UpdateApplication updates an application.
func (d *Database) UpdateApplication(app *model.Application) error {
return d.gormdb.Save(app).Error
}
// GetApplicationByID returns the application for the given ID or nil.
// GetApplicationByID returns the application with the given ID or nil.
func (d *Database) GetApplicationByID(ID uint) (*model.Application, error) {
app := new(model.Application)
var app model.Application
err := d.gormdb.First(&app, ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return app, err
return &app, err
}
// GetApplicationByToken returns the application for the given token or nil.
// GetApplicationByToken returns the application with the given token or nil.
func (d *Database) GetApplicationByToken(token string) (*model.Application, error) {
app := new(model.Application)
err := d.gormdb.Where("token = ?", token).First(app).Error
var app model.Application
err := d.gormdb.Where("token = ?", token).First(&app).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return app, err
return &app, err
}

View file

@ -78,8 +78,9 @@ func (d *Database) Close() {
// Populate fills the database with initial information like the admin user.
func (d *Database) Populate(name, password, matrixID string) error {
user := new(model.User)
query := d.gormdb.Where("name = ?", name).First(user)
var user model.User
query := d.gormdb.Where("name = ?", name).First(&user)
if errors.Is(query.Error, gorm.ErrRecordNotFound) {
user := model.NewUser(name, password, true, matrixID)

View file

@ -13,14 +13,46 @@ func (d *Database) CreateUser(user *model.User) error {
return d.gormdb.Create(user).Error
}
// GetUserByName returns the user by the given name or nil.
func (d *Database) GetUserByName(name string) (*model.User, error) {
user := new(model.User)
err := d.gormdb.Where("name = ?", name).First(user).Error
// DeleteUser deletes a user.
func (d *Database) DeleteUser(user *model.User) error {
if err := d.gormdb.Where("user_id = ?", user.ID).Delete(model.Application{}).Error; err != nil {
return err
}
return d.gormdb.Delete(user).Error
}
// GetUserByID returns the user with the given ID or nil.
func (d *Database) GetUserByID(ID uint) (*model.User, error) {
var user model.User
err := d.gormdb.First(&user, ID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return user, err
return &user, err
}
// GetUserByName returns the user with the given name or nil.
func (d *Database) GetUserByName(name string) (*model.User, error) {
var user model.User
err := d.gormdb.Where("name = ?", name).First(&user).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
return &user, err
}
// GetApplications returns the applications associated with a given user.
func (d *Database) GetApplications(user *model.User) ([]model.Application, error) {
var applications []model.Application
err := d.gormdb.Model(user).Association("Applications").Find(&applications)
return applications, err
}