From 18dd8fa1bb12ac07077e4243849fa46d8ddd1586 Mon Sep 17 00:00:00 2001 From: gom-by Date: Mon, 27 Jan 2025 11:37:13 +0100 Subject: [PATCH] event api v1 --- backend/api/delete_event.go | 3 - backend/api/{event.go => get_event.go} | 17 +---- backend/api/new_event.go | 21 ++---- backend/api/update_event.go | 100 +++++++++---------------- 4 files changed, 45 insertions(+), 96 deletions(-) rename backend/api/{event.go => get_event.go} (59%) diff --git a/backend/api/delete_event.go b/backend/api/delete_event.go index ae3ded7..586eac4 100644 --- a/backend/api/delete_event.go +++ b/backend/api/delete_event.go @@ -1,4 +1,3 @@ - package api import ( @@ -26,8 +25,6 @@ func HandleDeleteEvent(w http.ResponseWriter, r *http.Request) { } log.Println(res) - // TODO sql request to remove content - core.JSONSuccess{ Status: core.Success, Message: "Event deleted.", diff --git a/backend/api/event.go b/backend/api/get_event.go similarity index 59% rename from backend/api/event.go rename to backend/api/get_event.go index 20e3869..dc50e4b 100644 --- a/backend/api/event.go +++ b/backend/api/get_event.go @@ -8,31 +8,20 @@ import ( ) 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") - + 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.StatusNotAcceptable) + }.Respond(w, http.StatusInternalServerError) return } core.JSONSuccess{ Status: core.Success, - Message: "Event Returned", + Message: "Event successfully sent", Data: event, }.Respond(w, http.StatusOK) return diff --git a/backend/api/new_event.go b/backend/api/new_event.go index dfd0abb..6abba18 100644 --- a/backend/api/new_event.go +++ b/backend/api/new_event.go @@ -3,33 +3,22 @@ package api import ( "context" "encoding/json" - "io" - "log" "net/http" - "time" core "fr.latosa-escrima/api/core" ) func HandleCreateEvent(w http.ResponseWriter, r *http.Request) { - 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 } - log.Println(time.Now()) - 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{ @@ -40,7 +29,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) { core.JSONSuccess{ Status: core.Success, - Message: "Event inserted", + Message: "Event created", Data: event, }.Respond(w, http.StatusCreated) } diff --git a/backend/api/update_event.go b/backend/api/update_event.go index 6a3e2d6..ed143ae 100644 --- a/backend/api/update_event.go +++ b/backend/api/update_event.go @@ -1,91 +1,65 @@ package api import ( + "context" "encoding/json" - "fmt" - "io" + "log" "net/http" "fr.latosa-escrima/api/core" + "github.com/google/uuid" ) -type UpdateEventArgs struct { - FirstName *string `json:"firstname,omitempty"` - LastName *string `json:"lastname,omitempty"` - Email *string `json:"email,omitempty"` - Password *string `json:"password,omitempty"` - Phone *string `json:"phone,omitempty"` - Role *core.Role `json:"role,omitempty"` -} - func HandleUpdateEvent(w http.ResponseWriter, r *http.Request) { - // var updateArgs UpdateEventArgs - 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.StatusInternalServerError) + }.Respond(w, http.StatusBadRequest) return } - fmt.Println("update and event here. Body : ", body) - var event core.Event - err = json.Unmarshal(body, &event) + + 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 } - // var event core.Event - // updateQuery := core.DB.NewUpdate().Model(&event) - // - // val := reflect.ValueOf(updateArgs) - // typ := reflect.TypeOf(updateArgs) - // - // for i := 0; i < val.NumField(); i++ { - // field := val.Field(i) - // fieldname := typ.Field(i).Name - // - // tag := typ.Field(i).Tag.Get("bun") - // if tag == "" { - // tag = typ.Field(i).Tag.Get("json") - // } - // - // // Only add fields that are non-nil and non-zero - // if field.IsValid() && !field.IsNil() && !field.IsZero() { - // if fieldname == "Password" { - // updateQuery.Set(fmt.Sprintf("%s = crypt(?, gen_salt('bf'))", strings.Split(tag, ",")[0]), field.Interface()) - // } else { - // updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface()) - // } - // } - // } - // - // // Always update the `updated_at` field - // updateQuery.Set("updated_at = ?", time.Now()) - // - // uuid := r.PathValue("user_uuid") - // _, err = updateQuery. - // Where("user_id = ?", uuid). - // Returning("*"). - // Exec(context.Background()) - // - // if err != nil { - // core.JSONError{ - // Status: core.Error, - // Message: err.Error(), - // }.Respond(w, http.StatusInternalServerError) - // return - // } - // - // user.Password = "" - // core.JSONSuccess{ Status: core.Success, - Message: "User updated.", - Data: "ok", + Message: "Event updated", + Data: event, }.Respond(w, http.StatusOK) } +