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{})
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) {
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