2022-03-30 12:39:04 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2022-03-30 13:51:51 +02:00
|
|
|
// GCC Toolchain for Windows: https://jmeubank.github.io/tdm-gcc/
|
|
|
|
|
2022-03-30 12:39:04 +02:00
|
|
|
func migrateInitial(orm *gorm.DB) {
|
|
|
|
err := orm.AutoMigrate(&User{})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(fmt.Errorf("failed to migrate User"))
|
|
|
|
}
|
2022-04-04 12:13:55 +02:00
|
|
|
err = orm.AutoMigrate(&Task{})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(fmt.Errorf("failed to migrate Task"))
|
|
|
|
}
|
2022-03-30 12:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-04-04 10:16:29 +02:00
|
|
|
|
2022-04-04 12:26:36 +02:00
|
|
|
func (db *Database) GetAllTasks() []Task {
|
|
|
|
var tasks []Task
|
|
|
|
db.ORM.Find(&tasks)
|
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
2022-04-06 20:59:10 +02:00
|
|
|
func (db *Database) CreateTask(username string, description string) Task {
|
|
|
|
task := Task{
|
|
|
|
ID: 0,
|
|
|
|
Username: username,
|
|
|
|
Description: description,
|
|
|
|
Done: false,
|
|
|
|
}
|
|
|
|
db.ORM.Create(&task)
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:11:27 +02:00
|
|
|
func (db *Database) DeleteTask(id string) {
|
|
|
|
db.ORM.Delete(&Task{}, id)
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:16:29 +02:00
|
|
|
func (db *Database) CreateUser(username string, password string) error {
|
2022-04-07 15:39:22 +02:00
|
|
|
user := User{Username: username, Password: password, LoggedIn: true}
|
2022-04-04 10:16:29 +02:00
|
|
|
result := db.ORM.Create(&user)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:40:34 +02:00
|
|
|
func (db *Database) LoginUser(username string, password string) bool {
|
2022-04-04 10:16:29 +02:00
|
|
|
user := User{Username: username, Password: password}
|
|
|
|
result := db.ORM.Where("username = ? AND password = ?", username, password).Find(&user)
|
2022-04-04 11:40:34 +02:00
|
|
|
if result.RowsAffected == 1 {
|
|
|
|
db.ORM.Model(&user).Update("logged_in", true)
|
|
|
|
return true
|
2022-04-04 10:16:29 +02:00
|
|
|
}
|
2022-04-04 11:40:34 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) LogoutUser(username string) bool {
|
|
|
|
user := User{Username: username}
|
|
|
|
result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user)
|
|
|
|
if result.RowsAffected == 1 {
|
|
|
|
db.ORM.Model(&user).Update("logged_in", false)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) UserIsLoggedIn(username string) bool {
|
|
|
|
user := User{Username: username}
|
|
|
|
result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user)
|
2022-04-04 10:16:29 +02:00
|
|
|
if result.RowsAffected == 1 {
|
2022-04-04 11:40:34 +02:00
|
|
|
return true
|
2022-04-04 10:16:29 +02:00
|
|
|
}
|
2022-04-04 11:40:34 +02:00
|
|
|
return false
|
2022-04-04 10:16:29 +02:00
|
|
|
}
|