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,44 @@
package shortcodes
import (
"context"
"encoding/json"
"net/http"
"fr.latosa-escrima/api/core"
)
func HandleNew(w http.ResponseWriter, r *http.Request) {
var shortcode core.Shortcode
err := json.NewDecoder(r.Body).Decode(&shortcode)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
err = shortcode.Validate()
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
_, err = core.DB.NewInsert().Model(&shortcode).Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "Shortcode inserted.",
}.Respond(w, http.StatusCreated)
}