MySQL-Datenbank hinzugefügt und Abfrage korrigiert

This commit is contained in:
Gonne 2022-09-12 19:42:45 +02:00
parent e9e0cbf382
commit e89429c3c3
5 changed files with 49 additions and 12 deletions

22
main.go
View file

@ -1,14 +1,34 @@
package main package main
import ( import (
"database/sql"
"log"
"log/syslog"
"net/http" "net/http"
"os"
"sprechstundentool/controllers" "sprechstundentool/controllers"
"sprechstundentool/repositories" "sprechstundentool/repositories"
"sprechstundentool/sqldb" "sprechstundentool/sqldb"
"strings"
) )
func main() { func main() {
db := sqldb.ConnectDB() logwriter, e := syslog.New(syslog.LOG_ERR, "sprechstunden")
if e == nil {
log.SetOutput(logwriter)
}
var db *sql.DB
switch os.Getenv("ohtDbType") {
case "mysql":
db = sqldb.ConnectMysql(os.Getenv("ohtDbMysqlConnection"))
default:
if os.Getenv("ohtDbFile") != "" && !strings.Contains(os.Getenv("ohtDbFile"), "/") {
db = sqldb.ConnectSQLite(os.Getenv("ohtDbFile"))
} else {
db = sqldb.ConnectSQLite("sprechstunden.db")
}
}
// Create repos // Create repos
roomRepo := repositories.NewRoomRepo(db) roomRepo := repositories.NewRoomRepo(db)

View file

@ -119,16 +119,15 @@ func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) { func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
var officeHour models.OfficeHour var officeHour models.OfficeHour
var week, day, hour, minute, tutorid int var week, day, hour, minute, tutorId, courseId, roomId int
var roomName, courseName string err := row.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
err := row.Scan(&officeHour.Id, &tutorid, &day, &hour, &minute, &roomName, &courseName, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
if err != nil { if err != nil {
return models.OfficeHour{}, fmt.Errorf("Error getting single officeHours row from database: %s", err.Error()) return models.OfficeHour{}, fmt.Errorf("Error getting single officeHours row from database: %s", err.Error())
} }
officeHour.Date = models.Date{week, day, hour, minute} officeHour.Date = models.Date{week, day, hour, minute}
officeHour.Room, _ = r.roomRepo.FindByName(roomName) officeHour.Room, _ = r.roomRepo.FindById(roomId)
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorid) officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
officeHour.Course, _ = r.courseRepo.FindByName(courseName) officeHour.Course, _ = r.courseRepo.FindById(courseId)
return officeHour, nil return officeHour, nil
} }

View file

@ -6,6 +6,7 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"html/template" "html/template"
"log"
"math/big" "math/big"
"net/smtp" "net/smtp"
"sprechstundentool/models" "sprechstundentool/models"

View file

@ -2,16 +2,31 @@ package sqldb
import ( import (
"database/sql" "database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
// ConnectDB opens a connection to the database func ConnectSQLite(file string) *sql.DB {
func ConnectDB() *sql.DB { db, err := sql.Open("sqlite3", file)
db, err := sql.Open("sqlite3", "sprechstunden.db")
if err != nil { if err != nil {
panic(err.Error()) log.Fatal(err)
} }
return db return db
} }
func ConnectMysql(connection string) *sql.DB {
db, err := sql.Open("mysql", connection)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
// handle error
if err != nil {
log.Fatal(err)
}
return db
}

View file

@ -1,3 +1,4 @@
From: Mathebau Sprechstunden <sprechstunden@mathebau.de>
To: {{.OfficeHour.Tutor.Email}} To: {{.OfficeHour.Tutor.Email}}
Subject: Sprechstunde {{if eq .Action 1}}anlegen{{end}}{{if eq .Action 2}}löschen{{end}} Subject: Sprechstunde {{if eq .Action 1}}anlegen{{end}}{{if eq .Action 2}}löschen{{end}}
@ -7,8 +8,9 @@ mit deiner Emailadresse soll eine Sprechstunde mit folgenden Daten {{if eq .Acti
{{.OfficeHour.Course.Name}} {{.OfficeHour.Course.Name}}
{{DayName .OfficeHour.Date.Day}} {{DayName .OfficeHour.Date.Day}}
{{printf "%02d".OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} Uhr bis {{printf "%02d" .OfficeHour.EndDate.Hour}}:{{printf "%02d" .OfficeHour.EndDate.Minute}} Uhr {{printf "%02d" .OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} Uhr bis {{printf "%02d" .OfficeHour.EndDate.Hour}}:{{printf "%02d" .OfficeHour.EndDate.Minute}} Uhr
{{.OfficeHour.Tutor.Name}} {{.OfficeHour.Tutor.Name}}
{{.OfficeHour.Room.Name}}
Falls dies richtig ist, so bestätige die Sprechstunde durch Abrufen der folgenden URL: Falls dies richtig ist, so bestätige die Sprechstunde durch Abrufen der folgenden URL:
https://sprechstunden.mathebau.de/confirmRequest?code={{.Secret}} https://sprechstunden.mathebau.de/confirmRequest?code={{.Secret}}