assign trainers to trainings
This commit is contained in:
parent
097ecd6dfc
commit
b5ec4a9f17
2 changed files with 96 additions and 3 deletions
45
main.go
45
main.go
|
|
@ -322,7 +322,22 @@ func trainingListHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "training_list.html", trainings)
|
trainers, err := fetchTrainers()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching trainers: %v", err)
|
||||||
|
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := struct {
|
||||||
|
Trainings []Training
|
||||||
|
Trainers []Trainer
|
||||||
|
}{
|
||||||
|
Trainings: trainings,
|
||||||
|
Trainers: trainers,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tmpl.ExecuteTemplate(w, "training_list.html", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error in templating page training_list.html: %v", err)
|
log.Printf("Error in templating page training_list.html: %v", err)
|
||||||
http.Error(w, fmt.Sprintf("Error in templating page %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Error in templating page %v", err), http.StatusInternalServerError)
|
||||||
|
|
@ -608,11 +623,39 @@ func addTrainingHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assignTrainerHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("Handling trainer assignment to training")
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
trainingID := r.FormValue("training_id")
|
||||||
|
trainerID := r.FormValue("trainer_id")
|
||||||
|
|
||||||
|
if trainingID == "" || trainerID == "" {
|
||||||
|
log.Println("Missing training ID or trainer ID")
|
||||||
|
http.Error(w, "Training ID and Trainer ID are required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := db.Exec("INSERT OR IGNORE INTO training_trainers (training_id, trainer_id) VALUES (?, ?)", trainingID, trainerID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error assigning trainer to training: %v", err)
|
||||||
|
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Trainer assigned to training successfully")
|
||||||
|
http.Redirect(w, r, "/trainings", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
func initServerHandlers() {
|
func initServerHandlers() {
|
||||||
http.Handle("/", redirectIfNoAdmin(http.HandlerFunc(homeHandler)))
|
http.Handle("/", redirectIfNoAdmin(http.HandlerFunc(homeHandler)))
|
||||||
|
|
||||||
http.Handle("/trainings", redirectIfNoAdmin(http.HandlerFunc(trainingListHandler)))
|
http.Handle("/trainings", redirectIfNoAdmin(http.HandlerFunc(trainingListHandler)))
|
||||||
http.Handle("/trainings/add", redirectIfNoAdmin(http.HandlerFunc(addTrainingHandler)))
|
http.Handle("/trainings/add", redirectIfNoAdmin(http.HandlerFunc(addTrainingHandler)))
|
||||||
|
http.Handle("/trainings/assign_trainer", redirectIfNoAdmin(http.HandlerFunc(assignTrainerHandler)))
|
||||||
|
|
||||||
http.Handle("/trainers", redirectIfNoAdmin(http.HandlerFunc(trainerListHandler)))
|
http.Handle("/trainers", redirectIfNoAdmin(http.HandlerFunc(trainerListHandler)))
|
||||||
http.Handle("/trainers/add", redirectIfNoAdmin(http.HandlerFunc(addTrainerHandler)))
|
http.Handle("/trainers/add", redirectIfNoAdmin(http.HandlerFunc(addTrainerHandler)))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<p><a href="/trainings/add"><button>Add New Training</button></a></p>
|
<p><a href="/trainings/add"><button>Add New Training</button></a></p>
|
||||||
|
|
||||||
<h1>Trainings</h1>
|
<h1>Trainings</h1>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Title</th>
|
<th>Title</th>
|
||||||
|
|
@ -12,10 +12,11 @@
|
||||||
<th>End</th>
|
<th>End</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
<th>Trainers</th>
|
<th>Trainers</th>
|
||||||
|
<th>Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{{range .}}
|
{{range .Trainings}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{.Title}}</td>
|
<td>{{.Title}}</td>
|
||||||
<td>{{.TrainingType}}</td>
|
<td>{{.TrainingType}}</td>
|
||||||
|
|
@ -32,10 +33,59 @@
|
||||||
No trainers assigned
|
No trainers assigned
|
||||||
{{end}}
|
{{end}}
|
||||||
</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<button onclick="openAssignTrainerModal({{.ID}})">Assign Trainer</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
<!-- Assign Trainer Modal -->
|
||||||
|
<div id="assignTrainerModal" style="display:none; position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); background:white; padding:20px; border:1px solid black;">
|
||||||
|
<h3>Assign Trainer</h3>
|
||||||
|
<form id="assignTrainerForm" >
|
||||||
|
<input type="hidden" id="training_id" name="training_id">
|
||||||
|
<label for="trainer">Select Trainer:</label>
|
||||||
|
<select id="trainer_id" name="trainer_id">
|
||||||
|
{{range .Trainers}}
|
||||||
|
<option value="{{.ID}}">{{.Name}} ({{.Email}})</option>
|
||||||
|
{{end}}
|
||||||
|
</select>
|
||||||
|
<button type="submit">Assign</button>
|
||||||
|
<button type="button" onclick="closeAssignTrainerModal()">Cancel</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function openAssignTrainerModal(trainingId) {
|
||||||
|
document.getElementById('training_id').value = trainingId;
|
||||||
|
document.getElementById('assignTrainerModal').style.display = 'block';
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeAssignTrainerModal() {
|
||||||
|
document.getElementById('assignTrainerModal').style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('assignTrainerForm').addEventListener('submit', function(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const formData = new FormData(this); // Create FormData from the form element
|
||||||
|
console.log(formData);
|
||||||
|
|
||||||
|
fetch('/trainings/assign_trainer', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData // Send as form data
|
||||||
|
}).then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
alert('Failed to assign trainer');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{{template "epilogue.html" }}
|
{{template "epilogue.html" }}
|
||||||
Loading…
Add table
Reference in a new issue