Locations added
This commit is contained in:
38
backend/api/locations/delete.go
Normal file
38
backend/api/locations/delete.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var location models.Location
|
||||
if err := json.NewDecoder(r.Body).Decode(&location); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := core.DB.
|
||||
NewDelete().
|
||||
Model(&location).
|
||||
WherePK().
|
||||
Exec(context.Background()); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Location deleted.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
56
backend/api/locations/location.go
Normal file
56
backend/api/locations/location.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleLocation(w http.ResponseWriter, r *http.Request) {
|
||||
var location models.Location
|
||||
if err := json.NewDecoder(r.Body).Decode(&location); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err := core.DB.
|
||||
NewSelect().
|
||||
Model(&location).
|
||||
Where("street = ? AND city = ? AND postal_code = ?", location.Street, location.City, location.PostalCode).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var events []models.Event
|
||||
err = core.DB.
|
||||
NewSelect().
|
||||
Model(&events).
|
||||
Where("location = ? || ', ' || ? || ', ' || ?", location.Street, location.City, location.PostalCode).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
location.Events = &events
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Location retrieved.",
|
||||
Data: location,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
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)
|
||||
}
|
||||
38
backend/api/locations/new.go
Normal file
38
backend/api/locations/new.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var location models.Location
|
||||
if err := json.NewDecoder(r.Body).Decode(&location); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := core.DB.
|
||||
NewInsert().
|
||||
Model(&location).
|
||||
Ignore().
|
||||
Exec(context.Background()); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Location created.",
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
41
backend/api/locations/update.go
Normal file
41
backend/api/locations/update.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var location models.Location
|
||||
if err := json.NewDecoder(r.Body).Decode(&location); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := core.DB.
|
||||
NewUpdate().
|
||||
Model(&location).
|
||||
WherePK().
|
||||
OmitZero().
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Location updated",
|
||||
Data: location,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user