Locations added

This commit is contained in:
cdricms
2025-03-10 16:25:12 +01:00
parent 7cb633b4c6
commit 4cf85981eb
32 changed files with 1504 additions and 227 deletions

View File

@@ -3,7 +3,6 @@ package events
import (
"context"
"encoding/json"
"log"
"net/http"
"fr.latosa-escrima/core"
@@ -32,8 +31,6 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
return
}
log.Println(event)
_, err = core.DB.NewUpdate().
Model(&event).
OmitZero().

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View File

@@ -0,0 +1,30 @@
package api
import (
"fr.latosa-escrima/api/locations"
"fr.latosa-escrima/core"
)
var LocationsRoutes = map[string]core.Handler{
"GET /locations/all": {
Handler: locations.HandleLocations,
Middlewares: []core.Middleware{Methods("GET")},
},
"/locations/new": {
Handler: locations.HandleNew,
Middlewares: []core.Middleware{Methods(("POST")),
HasPermissions("locations", "insert"), AuthJWT}},
"GET /locations": {
Handler: locations.HandleLocation,
Middlewares: []core.Middleware{Methods("GET")}},
"DELETE /locations": {
Handler: locations.HandleDelete,
Middlewares: []core.Middleware{Methods("DELETE"),
HasPermissions("locations", "delete"), AuthJWT},
},
"PATCH /locations": {
Handler: locations.HandleUpdate,
Middlewares: []core.Middleware{Methods("PATCH"),
HasPermissions("blogs", "update"), AuthJWT},
},
}

View File

@@ -0,0 +1,30 @@
package migrations
import (
"context"
"fmt"
"fr.latosa-escrima/core/models"
"github.com/uptrace/bun"
)
func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
fmt.Print(" [up migration] ")
_, err := db.
NewAddColumn().
Model((*models.Event)(nil)).
ColumnExpr("description TEXT").
Exec(ctx)
_, err = db.
NewAddColumn().
Model((*models.Event)(nil)).
ColumnExpr("location TEXT").
Exec(ctx)
return err
}, func(ctx context.Context, db *bun.DB) error {
fmt.Print(" [down migration] ")
return nil
})
}

View File

@@ -17,5 +17,7 @@ type Event struct {
ScheduleEnd time.Time `bun:"schedule_end,notnull" json:"end"`
FullDay bool `bun:"full_day,notnull,default:false" json:"fullDay"`
IsVisible bool `bun:"is_visible,notnull,default:true" json:"isVisible"`
Description *string `bun:"description" json:"description,omitempty"`
Location *string `bun:"location" json:"location,omitempty"`
Rrule string `bun:"rrule" json:"rrule"`
}

View File

@@ -0,0 +1,16 @@
package models
import "github.com/uptrace/bun"
type Location struct {
bun.BaseModel `bun:"table:locations"`
ID int `bun:"id,pk,autoincrement" json:"id"`
Street string `bun:"street,notnull,unique:location" json:"street"`
City string `bun:"city,notnull,unique:location" json:"city"`
PostalCode string `bun:"postal_code,notnull,unique:location" json:"postalCode"`
Latitude *float64 `bun:"latitude" json:"latitude,omitempty"`
Longitude *float64 `bun:"longitude" json:"longitude,omitempty"`
Events *[]Event `bun:"-" json:"events,omitempty"`
}

View File

@@ -10,7 +10,7 @@ import (
type Permissions []models.Permission
func GetAllPermissions() Permissions {
resources := []string{"users", "roles", "media", "events", "permissions", "shortcodes", "blogs"}
resources := []string{"users", "roles", "media", "events", "permissions", "shortcodes", "blogs", "locations"}
var perms Permissions
for _, resource := range resources {
perms = append(perms, Permissions{

View File

@@ -72,6 +72,9 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
_, err = db.NewCreateTable().
Model((*m.UserToRole)(nil)).IfNotExists().Exec(ctx)
_, err = db.NewCreateTable().
Model((*m.Location)(nil)).IfNotExists().Exec(ctx)
if err != nil {
return nil, err
}

View File

@@ -81,7 +81,8 @@ func main() {
api.MediaRoutes,
api.PermissionsRoutes,
api.RolesRoutes,
api.ShortcodesRoutes)
api.ShortcodesRoutes,
api.LocationsRoutes)
core.HandleRoutes(mux, routes)
fmt.Printf("Serving on port %s\n", port)