Started to work the shortcodes
This commit is contained in:
@@ -3,6 +3,7 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -137,14 +138,43 @@ type WebsiteSettings struct {
|
||||
}
|
||||
|
||||
type Media struct {
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Author *User `bun:"rel:belongs-to,join:author_id=user_id" json:"author,omitempty"`
|
||||
Type string `bun:"media_type" json:"type"` // Image, Video, GIF etc. Add support for PDFs?
|
||||
Alt string `bun:"media_alt" json:"alt"`
|
||||
Path string `bun:"media_path" json:"path"`
|
||||
Size int64 `bun:"media_size" json:"size"`
|
||||
URL string `bun:"-" json:"url"`
|
||||
bun.BaseModel `bun:"table:media"`
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Author *User `bun:"rel:belongs-to,join:author_id=user_id" json:"author,omitempty"`
|
||||
Type string `bun:"media_type" json:"type"` // Image, Video, GIF etc. Add support for PDFs?
|
||||
Alt string `bun:"media_alt" json:"alt"`
|
||||
Path string `bun:"media_path" json:"path"`
|
||||
Size int64 `bun:"media_size" json:"size"`
|
||||
URL string `bun:"-" json:"url"`
|
||||
}
|
||||
|
||||
type ShortcodeType string
|
||||
|
||||
const (
|
||||
ShortcodeMedia ShortcodeType = "media"
|
||||
ShortcodeValue ShortcodeType = "value"
|
||||
)
|
||||
|
||||
type Shortcode struct {
|
||||
bun.BaseModel `bun:"table:shortcodes,alias:sc"`
|
||||
|
||||
ID int64 `bun:"id,pk,autoincrement" json:"id"` // Primary key
|
||||
Code string `bun:"code,notnull,unique" json:"code"` // The shortcode value
|
||||
Type ShortcodeType `bun:"shortcode_type,notnull" json:"type"`
|
||||
Value *string `bun:"value" json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `bun:"media_id,type:uuid" json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
Media *Media `bun:"rel:belongs-to,join:media_id=id" json:"media,omitempty"` // Relation to Media
|
||||
}
|
||||
|
||||
func (s *Shortcode) Validate() error {
|
||||
if s.Value != nil && s.MediaID != nil {
|
||||
return errors.New("both value and media_id cannot be set at the same time")
|
||||
}
|
||||
if s.Value == nil && s.MediaID == nil {
|
||||
return errors.New("either value or media_id must be set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
@@ -164,6 +194,7 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Media)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
28
backend/api/delete_shortcode.go
Normal file
28
backend/api/delete_shortcode.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDeleteShortcode(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)
|
||||
}
|
||||
31
backend/api/get_shortcode.go
Normal file
31
backend/api/get_shortcode.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetShortcode(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/get_shortcodes.go
Normal file
26
backend/api/get_shortcodes.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleGetShortcodes(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)
|
||||
}
|
||||
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)
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@@ -11,26 +10,15 @@ import (
|
||||
)
|
||||
|
||||
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "The body of your message is invalid.",
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("body : ", body )
|
||||
var user core.User
|
||||
err = json.Unmarshal(body, &user)
|
||||
err := json.NewDecoder(r.Body).Decode(&user)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "It seems your body in invalid JSON.",
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("User : ", user)
|
||||
|
||||
res, err := user.Insert(context.Background())
|
||||
|
||||
71
backend/api/update_shortcode.go
Normal file
71
backend/api/update_shortcode.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UpdateShortcodeArgs struct {
|
||||
Code *string `json:"code"` // The shortcode value
|
||||
Type *core.ShortcodeType `json:"type"`
|
||||
Value *string `json:"value"`
|
||||
MediaID *uuid.UUID `json:"media_id"` // Nullable reference to another table's ID
|
||||
}
|
||||
|
||||
func HandleUpdateShortcode(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("code = ?", 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)
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -24,15 +23,7 @@ type UpdateUserArgs struct {
|
||||
|
||||
func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
var updateArgs UpdateUserArgs
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(body, &updateArgs)
|
||||
err := json.NewDecoder(r.Body).Decode(&updateArgs)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
|
||||
Reference in New Issue
Block a user