event api v1

This commit is contained in:
gom-by
2025-01-27 11:37:13 +01:00
parent c202764966
commit 18dd8fa1bb
4 changed files with 45 additions and 96 deletions

View File

@@ -1,4 +1,3 @@
package api package api
import ( import (
@@ -26,8 +25,6 @@ func HandleDeleteEvent(w http.ResponseWriter, r *http.Request) {
} }
log.Println(res) log.Println(res)
// TODO sql request to remove content
core.JSONSuccess{ core.JSONSuccess{
Status: core.Success, Status: core.Success,
Message: "Event deleted.", Message: "Event deleted.",

View File

@@ -8,31 +8,20 @@ import (
) )
func HandleGetEvent(w http.ResponseWriter, r *http.Request) { func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") event_uuid := r.PathValue("event_uuid")
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 var event core.Event
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background()) _, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
if err != nil { if err != nil {
core.JSONError{ core.JSONError{
Status: core.Error, Status: core.Error,
Message: err.Error(), Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable) }.Respond(w, http.StatusInternalServerError)
return return
} }
core.JSONSuccess{ core.JSONSuccess{
Status: core.Success, Status: core.Success,
Message: "Event Returned", Message: "Event successfully sent",
Data: event, Data: event,
}.Respond(w, http.StatusOK) }.Respond(w, http.StatusOK)
return return

View File

@@ -3,30 +3,19 @@ package api
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"io"
"log"
"net/http" "net/http"
"time"
core "fr.latosa-escrima/api/core" core "fr.latosa-escrima/api/core"
) )
func HandleCreateEvent(w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
core.JSONError{ core.JSONError{
Status: core.Error, Status: core.Error,
Message: err.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 return
} }
@@ -40,7 +29,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
core.JSONSuccess{ core.JSONSuccess{
Status: core.Success, Status: core.Success,
Message: "Event inserted", Message: "Event created",
Data: event, Data: event,
}.Respond(w, http.StatusCreated) }.Respond(w, http.StatusCreated)
} }

View File

@@ -1,91 +1,65 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "log"
"io"
"net/http" "net/http"
"fr.latosa-escrima/api/core" "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) { func HandleUpdateEvent(w http.ResponseWriter, r *http.Request) {
// var updateArgs UpdateEventArgs var event core.Event
body, err := io.ReadAll(r.Body) err := json.NewDecoder(r.Body).Decode(&event)
if err != nil { if err != nil {
core.JSONError{ core.JSONError{
Status: core.Error, Status: core.Error,
Message: err.Error(), Message: err.Error(),
}.Respond(w, http.StatusInternalServerError) }.Respond(w, http.StatusBadRequest)
return return
} }
fmt.Println("update and event here. Body : ", body)
var event core.Event event_uuid := r.PathValue("event_uuid")
err = json.Unmarshal(body, &event) event.EventID, err = uuid.FromBytes([]byte(event_uuid))
if err != nil { if err != nil {
core.JSONError{ core.JSONError{
Status: core.Error, Status: core.Error,
Message: err.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) }.Respond(w, http.StatusInternalServerError)
return 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{ core.JSONSuccess{
Status: core.Success, Status: core.Success,
Message: "User updated.", Message: "Event updated",
Data: "ok", Data: event,
}.Respond(w, http.StatusOK) }.Respond(w, http.StatusOK)
} }