Locations added
This commit is contained in:
46
backend/api/locations/locations.go
Normal file
46
backend/api/locations/locations.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user