Merge remote-tracking branch 'origin/dev/guerby' into dev/cedric
This commit is contained in:
33
backend/api/delete_event.go
Normal file
33
backend/api/delete_event.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteEvent(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("event_uuid")
|
||||
var event core.Event
|
||||
res, err := core.DB.NewDelete().
|
||||
Model(&event).
|
||||
Where("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)
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method not Allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
event_uuid := r.PathValue("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.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Event Returned",
|
||||
Data: event,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
49
backend/api/get_event.go
Normal file
49
backend/api/get_event.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetEvent(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 HangleGetEvents(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
|
||||
}
|
||||
|
||||
@@ -2,50 +2,34 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"io"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
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.StatusNoContent)
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var event core.Event
|
||||
if err = json.Unmarshal(body, &event); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Event inserted",
|
||||
Message: "Event created",
|
||||
Data: event,
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
|
||||
65
backend/api/update_event.go
Normal file
65
backend/api/update_event.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdateEvent(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.FromBytes([]byte(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)
|
||||
}
|
||||
|
||||
@@ -105,11 +105,24 @@ func main() {
|
||||
"/users/{user_uuid}/update": {
|
||||
Handler: api.HandleUpdateUser,
|
||||
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
||||
// "/users/{user_uuid}/events": {Handler: nil, Middleware: nil},
|
||||
// "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil},
|
||||
// "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil},
|
||||
// "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil},
|
||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil},
|
||||
"/events": {
|
||||
Handler: api.HangleGetEvents,
|
||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||
"/events/new": {
|
||||
Handler: api.HandleCreateEvent,
|
||||
Middlewares: []core.Middleware{api.Methods("POST")}},
|
||||
"/events/{event_uuid}": {
|
||||
Handler: api.HandleGetEvent,
|
||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||
"/events/{event_uuid}/delete": {
|
||||
Handler: api.HandleDeleteEvent,
|
||||
Middlewares: []core.Middleware{api.Methods("DELETE")}},
|
||||
"/events/{event_uuid}/update": {
|
||||
Handler: api.HandleUpdateEvent,
|
||||
Middlewares: []core.Middleware{api.Methods("PATCH")}},
|
||||
"/blogs/new": {
|
||||
Handler: api.HandleCreateBlog,
|
||||
Middlewares: []core.Middleware{api.Methods(("POST"))}},
|
||||
"/blogs/{uuid}": {
|
||||
Handler: api.HandleGetBlog,
|
||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||
|
||||
Reference in New Issue
Block a user