This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
swb6-it-sec/Lab01/app/database/database.go
2022-04-04 10:16:29 +02:00

43 lines
1 KiB
Go

package database
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// GCC Toolchain for Windows: https://jmeubank.github.io/tdm-gcc/
func migrateInitial(orm *gorm.DB) {
err := orm.AutoMigrate(&User{})
if err != nil {
fmt.Println(fmt.Errorf("failed to migrate User"))
}
}
func (db *Database) Initialize() {
orm, err := gorm.Open(sqlite.Open(db.Location), &gorm.Config{})
if err != nil {
fmt.Println(fmt.Errorf("failed to connect Database"))
}
migrateInitial(orm)
db.ORM = orm
}
func (db *Database) CreateUser(username string, password string) error {
user := User{Username: username, Password: password}
result := db.ORM.Create(&user)
return result.Error
}
func (db *Database) LoginUser(username string, password string) (bool, error) {
user := User{Username: username, Password: password}
result := db.ORM.Where("username = ? AND password = ?", username, password).Find(&user)
if result.Error != nil {
return false, result.Error
}
if result.RowsAffected == 1 {
return true, nil
}
return false, nil
}