Reorganized API + added db migrations
Read the README file for more informations
This commit is contained in:
28
backend/api/shortcodes/delete.go
Normal file
28
backend/api/shortcodes/delete.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*core.Shortcode)(nil)).
|
||||
Where("code = ?", code).
|
||||
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 deleted",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
44
backend/api/shortcodes/new.go
Normal file
44
backend/api/shortcodes/new.go
Normal 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)
|
||||
|
||||
}
|
||||
31
backend/api/shortcodes/shortcode.go
Normal file
31
backend/api/shortcodes/shortcode.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
var shortcode core.Shortcode
|
||||
err := core.DB.NewSelect().
|
||||
Model(&shortcode).
|
||||
Where("code = ?", code).
|
||||
Limit(1).
|
||||
Scan(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 found",
|
||||
Data: shortcode,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
26
backend/api/shortcodes/shortcodes.go
Normal file
26
backend/api/shortcodes/shortcodes.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleShortcodes(w http.ResponseWriter, r *http.Request) {
|
||||
var shortcodes []core.Shortcode
|
||||
err := core.DB.NewSelect().Model(&shortcodes).Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Shortcodes retrieved.",
|
||||
Data: shortcodes,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
72
backend/api/shortcodes/update.go
Normal file
72
backend/api/shortcodes/update.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package shortcodes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UpdateShortcodeArgs struct {
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
Code *string `json:"code,omitempty"` // The shortcode value
|
||||
Type *core.ShortcodeType `bun:"shortcode_type" json:"type,omitempty"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
}
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var updateArgs UpdateShortcodeArgs
|
||||
err := json.NewDecoder(r.Body).Decode(&updateArgs)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var shortcode core.Shortcode
|
||||
updateQuery := core.DB.NewUpdate().Model(&shortcode)
|
||||
val := reflect.ValueOf(updateArgs)
|
||||
typ := reflect.TypeOf(updateArgs)
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
|
||||
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() {
|
||||
updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
code := r.PathValue("shortcode")
|
||||
_, err = updateQuery.
|
||||
Where("id = ? OR code = ?", updateArgs.ID, code).
|
||||
Returning("*").
|
||||
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 updated.",
|
||||
Data: shortcode,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user