events api file init

This commit is contained in:
gom-by
2025-01-27 10:15:05 +01:00
parent 78ce2b533b
commit c202764966
6 changed files with 226 additions and 40 deletions

View File

@@ -0,0 +1,36 @@
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)
// TODO sql request to remove content
core.JSONSuccess{
Status: core.Success,
Message: "Event deleted.",
}.Respond(w, http.StatusOK)
}

View File

@@ -2,22 +2,16 @@ package api
import (
"context"
"net/http"
"io"
"encoding/json"
"io"
"log"
"net/http"
"time"
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)
if err != nil {
core.JSONError{
@@ -26,6 +20,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
}.Respond(w, http.StatusNoContent)
return
}
log.Println(time.Now())
var event core.Event
if err = json.Unmarshal(body, &event); err != nil {
core.JSONError{
@@ -41,7 +36,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNotAcceptable)
}
}
core.JSONSuccess{
Status: core.Success,

View File

@@ -0,0 +1,91 @@
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"fr.latosa-escrima/api/core"
)
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)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
fmt.Println("update and event here. Body : ", body)
var event core.Event
err = json.Unmarshal(body, &event)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.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",
}.Respond(w, http.StatusOK)
}