-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic support for flexible cards
- Loading branch information
1 parent
d298c3d
commit ced30a3
Showing
10 changed files
with
421 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
// }) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.