Started to work the shortcodes
This commit is contained in:
44
backend/api/new_shortcode.go
Normal file
44
backend/api/new_shortcode.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateShortcode(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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user