Compare commits
2 commits
ce075d790a
...
1742f65fcd
| Author | SHA1 | Date | |
|---|---|---|---|
| 1742f65fcd | |||
| e3a5f81aed |
4 changed files with 77 additions and 4 deletions
46
main.go
46
main.go
|
|
@ -242,7 +242,7 @@ func addTrainingAreaHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func fetchTrainings() ([]Training, error) {
|
||||
log.Println("Fetching trainings from the database")
|
||||
rows, err := db.Query("SELECT id, title, training_type, mode, start, end, status FROM trainings")
|
||||
rows, err := db.Query("SELECT id, title, training_type, mode, strftime('%Y-%m-%d %H:%M:%S', start), strftime('%Y-%m-%d %H:%M:%S', end), status FROM trainings")
|
||||
if err != nil {
|
||||
log.Printf("Error fetching trainings: %v", err)
|
||||
return nil, err
|
||||
|
|
@ -258,8 +258,8 @@ func fetchTrainings() ([]Training, error) {
|
|||
log.Printf("Error scanning row for training: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
t.Start, _ = time.Parse("2006-01-02 15:04:05", start)
|
||||
t.End, _ = time.Parse("2006-01-02 15:04:05", end)
|
||||
t.Start, _ = time.Parse(time.DateTime, start)
|
||||
t.End, _ = time.Parse(time.DateTime, end)
|
||||
trainings = append(trainings, t)
|
||||
}
|
||||
|
||||
|
|
@ -525,9 +525,49 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
|||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func addTrainingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Handling request to add a new training")
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
title := r.FormValue("title")
|
||||
trainingType := r.FormValue("training_type")
|
||||
mode := r.FormValue("mode")
|
||||
start := r.FormValue("start")
|
||||
end := r.FormValue("end")
|
||||
status := r.FormValue("status")
|
||||
|
||||
if title == "" || trainingType == "" || mode == "" || start == "" || end == "" || status == "" {
|
||||
log.Println("Missing required training fields")
|
||||
http.Error(w, "All fields are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := db.Exec("INSERT INTO trainings (title, training_type, mode, start, end, status) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
title, trainingType, mode, start, end, status)
|
||||
if err != nil {
|
||||
log.Printf("Error adding training: %v", err)
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("New training added successfully")
|
||||
http.Redirect(w, r, "/trainings", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
err := tmpl.ExecuteTemplate(w, "add_training.html", nil)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering add_training.html: %v", err)
|
||||
http.Error(w, "Template error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func initServerHandlers() {
|
||||
http.Handle("/", redirectIfNoAdmin(http.HandlerFunc(homeHandler)))
|
||||
|
||||
http.Handle("/trainings", redirectIfNoAdmin(http.HandlerFunc(trainingListHandler)))
|
||||
http.Handle("/trainings/add", redirectIfNoAdmin(http.HandlerFunc(addTrainingHandler)))
|
||||
|
||||
http.Handle("/trainers", redirectIfNoAdmin(http.HandlerFunc(trainerListHandler)))
|
||||
http.Handle("/trainers/add", redirectIfNoAdmin(http.HandlerFunc(addTrainerHandler)))
|
||||
http.Handle("/training-areas", redirectIfNoAdmin(http.HandlerFunc(trainingAreaListHandler)))
|
||||
|
|
|
|||
27
templates/add_training.html
Normal file
27
templates/add_training.html
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{{ template "preamble.html" }}
|
||||
<h1>Add Training</h1>
|
||||
<form method="post" action="/trainings/add">
|
||||
<label>Title: <input type="text" name="title" required></label><br>
|
||||
<label>Type: <input type="text" name="training_type" required></label><br>
|
||||
<label>Mode:
|
||||
<select name="mode" required>
|
||||
<option value="Online">Online</option>
|
||||
<option value="Offline">Offline</option>
|
||||
<option value="Hybrid">Hybrid</option>
|
||||
</select>
|
||||
</label><br>
|
||||
<label>Start Time: <input type="datetime-local" name="start" required></label><br>
|
||||
<label>End Time: <input type="datetime-local" name="end" required></label><br>
|
||||
<label>Status:
|
||||
<select name="status">
|
||||
<option value="planned">Planned</option>
|
||||
<option value="confirmed">Date Confirmed</option>
|
||||
<option value="ongoing">Ongoing</option>
|
||||
<option value="held">Held</option>
|
||||
<option value="done">Done</option>
|
||||
<option value="cancelled">Cancelled</option>
|
||||
</select>
|
||||
</label><br>
|
||||
<button type="submit">Add Training</button>
|
||||
</form>
|
||||
{{ template "epilogue.html" }}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
{{ template "preamble.html" }}
|
||||
<h1>Training Areas</h1>
|
||||
<a href="/training-areas/add"><button>Add Training Area</button></a>
|
||||
<p>
|
||||
<a href="/training-areas/add"><button> <div class="tile-icon"><i class="fas fa-map-marker-alt"></i></div> Add Training Area</button></a>
|
||||
<a href="/trainers/add"><button><div class="tile-icon"><i class="fas fa-user-plus"></i></div> Add Trainer</button></a>
|
||||
</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
{{ template "preamble.html" }}
|
||||
<p><a href="/trainings/add"><button>Add New Training</button></a></p>
|
||||
|
||||
<h1>Trainings</h1>
|
||||
<table border="1">
|
||||
<tr>
|
||||
|
|
@ -20,4 +22,5 @@
|
|||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
{{template "epilogue.html" }}
|
||||
Loading…
Add table
Reference in a new issue