Nach Suchbegriff aus Datenbank lesen implementiert, außerdem structs mit new() instanziiert

This commit is contained in:
Jason Streifling 2023-10-07 16:58:34 +02:00
parent f570950425
commit 9acc6711fc
3 changed files with 34 additions and 20 deletions

View File

@ -22,7 +22,7 @@ func main() {
mux := http.NewServeMux()
cb := make(chan *types.Briefing)
db, err := db.OpenDB("sicherheitsunterweisung")
db, err := db.Open("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
}

View File

@ -37,27 +37,27 @@ func getCredentials() (string, string, error) {
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
}
func OpenDB(dbName string) (*DB, error) {
var db DB
func Open(dbName string) (*DB, error) {
var err error
db := new(DB)
cfg := mysql.NewConfig()
cfg.DBName = dbName
cfg.User, cfg.Passwd, err = getCredentials()
if err != nil {
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
return nil, fmt.Errorf("Open: getCredentials(): %v\n", err)
}
db.Name = dbName
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
return nil, fmt.Errorf("Open: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err)
return nil, fmt.Errorf("Open: db.Ping(): %v\n", err)
}
return &db, nil
return db, nil
}
func (db *DB) WriteBriefing(b *types.Briefing) error {
@ -87,23 +87,37 @@ func (db *DB) WriteBriefing(b *types.Briefing) error {
}
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
var b types.Briefing
var bs []types.Briefing
bs := make([]types.Briefing, 0)
rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" +
" instructor_first LIKE " + name + " OR instructor_last LIKE " + name +
" participant_first LIKE " + name + " participant_last LIKE " + name)
rows, err := db.Query("SELECT *"+
" FROM "+db.Name+
" WHERE instructor_first LIKE ?"+
" OR instructor_last LIKE ?"+
" OR participant_first LIKE ?"+
" OR participant_last LIKE ?",
"%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%")
if err != nil {
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" +
" instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" +
" participant_first LIKE \"+name+\" participant_last LIKE \"+name)")
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+
" FROM \"+db.Name+"+
" WHERE instructor_first LIKE ?"+
" OR instructor_last LIKE ?"+
" OR participant_first LIKE ?"+
" OR participant_last LIKE ?\"): %v\n", err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company); err != nil {
return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company)")
b := new(types.Briefing)
p := new(types.Participant)
if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State,
&b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil {
return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+
" &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+
" &p.LastName, &p.Company): %v\n", err)
}
bs = append(bs, b)
b.Participants = append(b.Participants, *p)
bs = append(bs, *b)
}
return &bs, nil

View File

@ -23,7 +23,7 @@ func AddParticipant(i *int64) http.HandlerFunc {
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var b types.Briefing
b := new(types.Briefing)
b.FirstName = r.PostFormValue("instructor-first")
b.LastName = r.PostFormValue("instructor-last")
@ -44,6 +44,6 @@ func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
}
log.Println(b)
ch <- &b
ch <- b
}
}