47 lines
972 B
Go
47 lines
972 B
Go
package locations
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"fr.latosa-escrima/core"
|
|
"fr.latosa-escrima/core/models"
|
|
"fr.latosa-escrima/utils"
|
|
)
|
|
|
|
func HandleLocations(w http.ResponseWriter, r *http.Request) {
|
|
var locations []*models.Location
|
|
if err := core.DB.
|
|
NewSelect().
|
|
Model(&locations).
|
|
Scan(context.Background()); err != nil {
|
|
fmt.Println("Error")
|
|
core.JSONError{
|
|
Status: core.Error,
|
|
Message: err.Error(),
|
|
}.Respond(w, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
locations = utils.Map(locations, func(l *models.Location) *models.Location {
|
|
var events []models.Event
|
|
err := core.DB.
|
|
NewSelect().
|
|
Model(&events).
|
|
Where("location = ? || ', ' || ? || ', ' || ?", l.Street, l.City, l.PostalCode).
|
|
Scan(context.Background())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
l.Events = &events
|
|
return l
|
|
})
|
|
|
|
core.JSONSuccess{
|
|
Status: core.Success,
|
|
Message: "Locations retrieved.",
|
|
Data: locations,
|
|
}.Respond(w, http.StatusOK)
|
|
}
|