Reorganized API + added db migrations

Read the README file for more informations
This commit is contained in:
cdricms
2025-01-28 17:41:05 +01:00
parent 74118a5aa6
commit 501ffaea17
47 changed files with 608 additions and 219 deletions

View File

@@ -0,0 +1,32 @@
package events
import (
"context"
"log"
"net/http"
"fr.latosa-escrima/api/core"
)
func HandleDelete(w http.ResponseWriter, r *http.Request) {
uuid := r.PathValue("event_uuid")
var event core.Event
res, err := core.DB.NewDelete().
Model(&event).
Where("event_id = ?", uuid).
Returning("*").
Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
log.Println(res)
core.JSONSuccess{
Status: core.Success,
Message: "Event deleted.",
}.Respond(w, http.StatusOK)
}

View File

@@ -0,0 +1,48 @@
package events
import (
"context"
"fmt"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleEvent(w http.ResponseWriter, r *http.Request) {
event_uuid := r.PathValue("event_uuid")
var event core.Event
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Event successfully sent",
Data: event,
}.Respond(w, http.StatusOK)
return
}
func HandleEvents(w http.ResponseWriter, r *http.Request) {
var events []core.Event
rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
Data: events,
}.Respond(w, http.StatusOK)
return
}

36
backend/api/events/new.go Normal file
View File

@@ -0,0 +1,36 @@
package events
import (
"context"
"encoding/json"
"net/http"
core "fr.latosa-escrima/api/core"
)
func HandleNew(w http.ResponseWriter, r *http.Request) {
var event core.Event
err := json.NewDecoder(r.Body).Decode(&event)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
return
}
_, err = core.DB.NewInsert().Model(&event).Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Event created",
Data: event,
}.Respond(w, http.StatusCreated)
}

View File

@@ -0,0 +1,64 @@
package events
import (
"context"
"encoding/json"
"log"
"net/http"
"fr.latosa-escrima/api/core"
"github.com/google/uuid"
)
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
var event core.Event
err := json.NewDecoder(r.Body).Decode(&event)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
return
}
event_uuid := r.PathValue("event_uuid")
event.EventID, err = uuid.Parse(event_uuid)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusBadRequest)
return
}
log.Println(event)
// val := reflect.ValueOf(event)
// typ := val.Type()
//
// for i := 0; i < val.NumField(); i++ {
// field := val.Field(i)
// fieldType := typ.Field(i)
// fmt.Printf("Field Name: %s, Field Value: %v\n", fieldType.Name, field.Interface())
// if fiel
// }
_, err = core.DB.NewUpdate().
Model(&event).
OmitZero().
WherePK().
Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: "Event not found.",
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Event updated",
Data: event,
}.Respond(w, http.StatusOK)
}