Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding basic support for flexible cards #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions admin-app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
r.POST("/images", postImageHandler(app_settings, database))
// r.DELETE("/images", deleteImageHandler(&database))

// Card related endpoints
r.POST("/card", postCardHandler(database))

return r
}
169 changes: 169 additions & 0 deletions admin-app/cards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package admin_app

import (
"encoding/json"
"net/http"

"github.com/fossoreslp/go-uuid-v4"
"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/database"
"github.com/rs/zerolog/log"
)

type AddCardRequest struct {
ImageLocation string `json:"image_location"`
JsonData string `json:"json_data"`
SchemaName string `json:"json_schema"`
}

// func getCardHandler(database database.Database) func(*gin.Context) {
// return func(c *gin.Context) {
// // localhost:8080/post/{id}
// var post_binding PostBinding
// if err := c.ShouldBindUri(&post_binding); err != nil {
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "could not get post id",
// "msg": err.Error(),
// })
// return
// }

// post_id, err := strconv.Atoi(post_binding.Id)
// if err != nil {
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "invalid post id type",
// "msg": err.Error(),
// })
// return
// }

// post, err := database.GetPost(post_id)
// if err != nil {
// log.Warn().Msgf("could not get post from DB: %v", err)
// c.JSON(http.StatusNotFound, gin.H{
// "error": "post id not found",
// "msg": err.Error(),
// })
// return
// }

// c.JSON(http.StatusOK, gin.H{
// "id": post.Id,
// "title": post.Title,
// "excerpt": post.Excerpt,
// "content": post.Content,
// })
// }
// }

func postCardHandler(database database.Database) func(*gin.Context) {
return func(c *gin.Context) {
var add_card_request AddCardRequest
decoder := json.NewDecoder(c.Request.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&add_card_request)

if err != nil {
log.Warn().Msgf("invalid post card request: %v", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request body",
"msg": err.Error(),
})
return
}

card_uuid, err := uuid.New()
if err != nil {
log.Error().Msgf("could not create the UUID: %v", err)
return
}

err = database.AddCard(
card_uuid.String(),
add_card_request.ImageLocation,
add_card_request.JsonData,
add_card_request.SchemaName,
)
if err != nil {
log.Error().Msgf("failed to add post: %v", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "could not add post",
"msg": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"id": card_uuid.String(),
})
}
}

// func putCardHandler(database database.Database) func(*gin.Context) {
// return func(c *gin.Context) {
// var change_post_request ChangePostRequest
// decoder := json.NewDecoder(c.Request.Body)
// decoder.DisallowUnknownFields()

// err := decoder.Decode(&change_post_request)
// if err != nil {
// log.Warn().Msgf("could not get post from DB: %v", err)
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "invalid request body",
// "msg": err.Error(),
// })
// return
// }

// err = database.ChangePost(
// change_post_request.Id,
// change_post_request.Title,
// change_post_request.Excerpt,
// change_post_request.Content,
// )
// if err != nil {
// log.Error().Msgf("failed to change post: %v", err)
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "could not change post",
// "msg": err.Error(),
// })
// return
// }

// c.JSON(http.StatusOK, gin.H{
// "id": change_post_request.Id,
// })
// }
// }

// func deleteCardHandler(database database.Database) func(*gin.Context) {
// return func(c *gin.Context) {
// var delete_post_request DeletePostRequest
// decoder := json.NewDecoder(c.Request.Body)
// decoder.DisallowUnknownFields()

// err := decoder.Decode(&delete_post_request)
// if err != nil {
// log.Warn().Msgf("could not delete post: %v", err)
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "invalid request body",
// "msg": err.Error(),
// })
// return
// }

// err = database.DeletePost(delete_post_request.Id)
// if err != nil {
// log.Error().Msgf("failed to delete post: %v", err)
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "could not delete post",
// "msg": err.Error(),
// })
// return
// }

// c.JSON(http.StatusOK, gin.H{
// "id": delete_post_request.Id,
// })
// }
// }
72 changes: 72 additions & 0 deletions cmd/urchin/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/matheusgomes28/urchin/app"
"github.com/matheusgomes28/urchin/common"

"github.com/stretchr/testify/assert"
)

type DatabaseMock struct{}

func (db DatabaseMock) GetPosts() ([]common.Post, error) {
return []common.Post{
{
Title: "TestPost",
Content: "TestContent",
Excerpt: "TestExcerpt",
Id: 0,
},
}, nil
}

func (db DatabaseMock) GetPost(post_id int) (common.Post, error) {
return common.Post{}, fmt.Errorf("not implemented")
}

func (db DatabaseMock) AddPost(title string, excerpt string, content string) (int, error) {
return 0, fmt.Errorf("not implemented")
}

func (db DatabaseMock) ChangePost(id int, title string, excerpt string, content string) error {
return nil
}

func (db DatabaseMock) DeletePost(id int) error {
return fmt.Errorf("not implemented")
}

func (db DatabaseMock) AddImage(string, string, string) error {
return fmt.Errorf("not implemented")
}

func (db DatabaseMock) AddCard(string, string, string, string) error {
return fmt.Errorf("not implemented")
}

func TestIndexPing(t *testing.T) {
app_settings := common.AppSettings{
DatabaseAddress: "localhost",
DatabasePort: 3006,
DatabaseUser: "root",
DatabasePassword: "root",
DatabaseName: "urchin",
WebserverPort: 8080,
}

database_mock := DatabaseMock{}

r := app.SetupRoutes(app_settings, database_mock)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "TestPost")
assert.Contains(t, w.Body.String(), "TestExcerpt")
}
35 changes: 26 additions & 9 deletions common/app_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/BurntSushi/toml"
)

type CardSchema struct {
Name string `toml:"schema_name"`
}

type AppSettings struct {
DatabaseAddress string `toml:"database_address"`
DatabasePort int `toml:"database_port"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
WebserverPort int `toml:"webserver_port"`
ImageDirectory string `toml:"image_dir"`
DatabaseAddress string `toml:"database_address"`
DatabasePort int `toml:"database_port"`
DatabaseUser string `toml:"database_user"`
DatabasePassword string `toml:"database_password"`
DatabaseName string `toml:"database_name"`
WebserverPort int `toml:"webserver_port"`
ImageDirectory string `toml:"image_dir"`
CardSchemas []CardSchema `toml:"card_schema"`
}

func LoadSettings() (AppSettings, error) {
Expand Down Expand Up @@ -62,7 +68,7 @@ func LoadSettings() (AppSettings, error) {

image_directory := os.Getenv("URCHIN_IMAGE_DIRECTORY")
if len(image_directory) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_IMAGE_DIRECTORY is not defined\n")
return AppSettings{}, fmt.Errorf("URCHIN_IMAGE_DIRECTORY is not defined")
}

return AppSettings{
Expand All @@ -78,10 +84,21 @@ func LoadSettings() (AppSettings, error) {

func ReadConfigToml(filepath string) (AppSettings, error) {
var config AppSettings
_, err := toml.DecodeFile(filepath, &config)
metadata, err := toml.DecodeFile(filepath, &config)
if err != nil {
return AppSettings{}, err
}

if undecoded_keys := metadata.Undecoded(); len(undecoded_keys) > 0 {
err := fmt.Errorf("could not decode keys: ")

for _, key := range undecoded_keys {
metadata.Keys()
err = fmt.Errorf("%v %v,", err, strings.Join(key, "."))
}

return AppSettings{}, err
}

return config, nil
}
7 changes: 7 additions & 0 deletions common/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ type Post struct {
Excerpt string
Id int
}

type Card struct {
Uuid string
ImageLocation string
JsonData string
SchemaName string
}
Loading
Loading