blog handlers mock-up

This commit is contained in:
gom-by
2025-01-15 13:35:30 +01:00
parent 683a8c3133
commit e62dfd8c40
4 changed files with 113 additions and 9 deletions

View File

View File

View File

@@ -5,7 +5,9 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
"encoding/json"
"strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"context" "context"
@@ -18,6 +20,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>") fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>")
} }
type ErrorResponse struct {
ErrorCode string `json:"errorCode"`
Message string `json:"message"`
}
var DB *bun.DB var DB *bun.DB
type DSN struct { type DSN struct {
@@ -35,7 +42,9 @@ func (dsn *DSN) ToString() string {
func handlerCreateUser(w http.ResponseWriter, r *http.Request) { func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
// return an empty json w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "Resource created successfully"}`))
} }
user := &User{ user := &User{
@@ -54,6 +63,100 @@ func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
fmt.Println("User inserted successfully") fmt.Println("User inserted successfully")
} }
func handlerCreateBlog(w http.ResponseWriter, r *http.Request) {
emptyObject := make(map[string]interface{})
emptyJSON, json_err := json.Marshal(emptyObject)
if json_err != nil {
fmt.Println("Couldn't create the json object")
w.Write(emptyJSON)
}
if r.Method != http.MethodPost {
return string(emptyJSON)
}
err := r.ParseForm()
if err != nil {
return string(emptyJSON)
}
user := &Blog{
BaseModel: bun.BaseModel{},
BlogID: [16]byte{},
Slug: "",
Content: "",
Label: "",
AuthorID: [16]byte{},
Published: time.Time{},
Summary: "",
Image: "",
Href: "",
Author: &User{},
}
_, err_db := DB.NewInsert().Model(user).Exec(context.Background())
if err_db != nil {
log.Fatal(err)
}
fmt.Println("User inserted successfully")
return string(emptyJSON)
}
func handlerGetBlog(w http.ResponseWriter, r *http.Request) (string) {
log.Println("ok")
emptyObject := make(map[string]interface{})
emptyJSON, json_err := json.Marshal(emptyObject)
if json_err != nil {
fmt.Println("Couldn't create the json object")
return string(emptyJSON)
}
if r.Method != http.MethodGet {
http.Error(w, "Wrong method", http.StatusMethodNotAllowed)
return string(emptyJSON)
}
if !strings.HasPrefix(r.URL.Path, "/blog/") {
http.Redirect(w, r, "/blog", http.StatusFound)
return string(emptyJSON)
}
blog_uuid := strings.TrimPrefix(r.URL.Path, "/blog/")
if blog_uuid == "" {
http.Error(w, "Slug is required", http.StatusBadRequest)
return string(emptyJSON)
}
blog := &Blog{
BaseModel: bun.BaseModel{},
BlogID: [16]byte{},
Slug: "",
Content: "",
Label: "",
AuthorID: [16]byte{},
Published: time.Time{},
Summary: "",
Image: "",
Href: "",
Author: &User{},
}
ctx := context.Background()
err_db := DB.NewSelect().Model(blog).Where("uuid = ", blog_uuid).Scan(ctx)
if err_db != nil {
log.Fatal(err_db)
http.Error(w, "Can't use select", http.StatusNotFound)
return string(emptyJSON)
}
fmt.Println("Blog inserted successfully")
return string(emptyJSON)
}
func main() { func main() {
err := godotenv.Load() err := godotenv.Load()
if err != nil { if err != nil {
@@ -81,6 +184,7 @@ func main() {
http.HandleFunc("/user/new", handlerCreateUser) http.HandleFunc("/user/new", handlerCreateUser)
http.HandleFunc("/users/login", HandleLogin) http.HandleFunc("/users/login", HandleLogin)
http.HandleFunc("/blog/", handlerGetBlog)
fmt.Printf("Serving on port %s\n", port) fmt.Printf("Serving on port %s\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil) err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)

View File

@@ -24,12 +24,12 @@ export default function RootLayout({
children: React.ReactNode; children: React.ReactNode;
}>) { }>) {
return ( return (
<SWRConfig //<SWRConfig
value={{ // value={{
fetcher: (url: string) => fetch(url).then((res) => res.json()), // fetcher: (url: string) => fetch(url).then((res) => res.json()),
revalidateOnFocus: false, // revalidateOnFocus: false,
}} // }}
> //>
<html lang="fr"> <html lang="fr">
<body <body
className={`${geistSans.variable} ${geistMono.variable} antialiased`} className={`${geistSans.variable} ${geistMono.variable} antialiased`}
@@ -37,6 +37,6 @@ export default function RootLayout({
{children} {children}
</body> </body>
</html> </html>
</SWRConfig> //</SWRConfig>
); );
} }