Nach Suchbegriff aus Datenbank lesen implementiert, außerdem structs mit new() instanziiert
This commit is contained in:
parent
f570950425
commit
9acc6711fc
2
main.go
2
main.go
@ -22,7 +22,7 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
cb := make(chan *types.Briefing)
|
cb := make(chan *types.Briefing)
|
||||||
|
|
||||||
db, err := db.OpenDB("sicherheitsunterweisung")
|
db, err := db.Open("sicherheitsunterweisung")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
@ -37,27 +37,27 @@ func getCredentials() (string, string, error) {
|
|||||||
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
|
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenDB(dbName string) (*DB, error) {
|
func Open(dbName string) (*DB, error) {
|
||||||
var db DB
|
|
||||||
var err error
|
var err error
|
||||||
|
db := new(DB)
|
||||||
|
|
||||||
cfg := mysql.NewConfig()
|
cfg := mysql.NewConfig()
|
||||||
cfg.DBName = dbName
|
cfg.DBName = dbName
|
||||||
cfg.User, cfg.Passwd, err = getCredentials()
|
cfg.User, cfg.Passwd, err = getCredentials()
|
||||||
if err != nil {
|
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.Name = dbName
|
||||||
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
||||||
if err != nil {
|
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 {
|
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 {
|
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) {
|
func (db *DB) ReadByName(name string) (*[]types.Briefing, error) {
|
||||||
var b types.Briefing
|
bs := make([]types.Briefing, 0)
|
||||||
var bs []types.Briefing
|
|
||||||
|
|
||||||
rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" +
|
rows, err := db.Query("SELECT *"+
|
||||||
" instructor_first LIKE " + name + " OR instructor_last LIKE " + name +
|
" FROM "+db.Name+
|
||||||
" participant_first LIKE " + name + " participant_last LIKE " + name)
|
" WHERE instructor_first LIKE ?"+
|
||||||
|
" OR instructor_last LIKE ?"+
|
||||||
|
" OR participant_first LIKE ?"+
|
||||||
|
" OR participant_last LIKE ?",
|
||||||
|
"%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" +
|
return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+
|
||||||
" instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" +
|
" FROM \"+db.Name+"+
|
||||||
" participant_first LIKE \"+name+\" participant_last LIKE \"+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() {
|
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 {
|
b := new(types.Briefing)
|
||||||
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)")
|
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
|
return &bs, nil
|
||||||
|
@ -23,7 +23,7 @@ func AddParticipant(i *int64) http.HandlerFunc {
|
|||||||
|
|
||||||
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
|
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var b types.Briefing
|
b := new(types.Briefing)
|
||||||
|
|
||||||
b.FirstName = r.PostFormValue("instructor-first")
|
b.FirstName = r.PostFormValue("instructor-first")
|
||||||
b.LastName = r.PostFormValue("instructor-last")
|
b.LastName = r.PostFormValue("instructor-last")
|
||||||
@ -44,6 +44,6 @@ func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println(b)
|
log.Println(b)
|
||||||
ch <- &b
|
ch <- b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user