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

feat: endpoint to serve readable content #885

Merged
merged 11 commits into from
Apr 20, 2024
33 changes: 33 additions & 0 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ const docTemplate = `{
}
}
},
"/api/v1/bookmarks/id/readable": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Auth"
],
"summary": "Get readable version of bookmark.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api_v1.contentResponseMessage"
}
},
"401": {
"description": "Token not provided/invalid"
}
}
}
},
"/api/v1/tags": {
"get": {
"produces": [
Expand Down Expand Up @@ -206,6 +228,17 @@ const docTemplate = `{
}
},
"definitions": {
"api_v1.contentResponseMessage": {
"type": "object",
"properties": {
"content": {
"type": "string"
},
"html": {
"type": "string"
}
}
},
"api_v1.loginRequestPayload": {
"type": "object",
"required": [
Expand Down
33 changes: 33 additions & 0 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@
}
}
},
"/api/v1/bookmarks/id/readable": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Auth"
],
"summary": "Get readable version of bookmark.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api_v1.contentResponseMessage"
}
},
"401": {
"description": "Token not provided/invalid"
}
}
}
},
"/api/v1/tags": {
"get": {
"produces": [
Expand Down Expand Up @@ -195,6 +217,17 @@
}
},
"definitions": {
"api_v1.contentResponseMessage": {
"type": "object",
"properties": {
"content": {
"type": "string"
},
"html": {
"type": "string"
}
}
},
"api_v1.loginRequestPayload": {
"type": "object",
"required": [
Expand Down
21 changes: 21 additions & 0 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
definitions:
api_v1.contentResponseMessage:
properties:
content:
type: string
html:
type: string
type: object
api_v1.loginRequestPayload:
properties:
password:
Expand Down Expand Up @@ -218,6 +225,20 @@ paths:
summary: Update Cache and Ebook on server.
tags:
- Auth
/api/v1/bookmarks/id/readable:
get:
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/api_v1.contentResponseMessage'
"401":
description: Token not provided/invalid
summary: Get readable version of bookmark.
tags:
- Auth
/api/v1/tags:
get:
produces:
Expand Down
58 changes: 58 additions & 0 deletions internal/http/routes/api/v1/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
func (r *BookmarksAPIRoutes) Setup(g *gin.RouterGroup) model.Routes {
g.Use(middleware.AuthenticationRequired())
g.PUT("/cache", r.updateCache)
g.GET("/:id/readable", r.bookmarkReadable)
return r
}

Expand Down Expand Up @@ -57,6 +58,63 @@
return nil
}

type contentResponseMessage struct {
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
Content string `json:"content"`
Html string `json:"html"`
}

func (r *BookmarksAPIRoutes) getBookmark(c *context.Context) (*model.BookmarkDTO, error) {
bookmarkIDParam, present := c.Params.Get("id")
if !present {
response.SendError(c.Context, http.StatusBadRequest, "Invalid bookmark ID")
return nil, model.ErrBookmarkInvalidID

Check warning on line 70 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L66-L70

Added lines #L66 - L70 were not covered by tests
}

bookmarkID, err := strconv.Atoi(bookmarkIDParam)
if err != nil {
r.logger.WithError(err).Error("error parsing bookmark ID parameter")
response.SendInternalServerError(c.Context)
return nil, err

Check warning on line 77 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L73-L77

Added lines #L73 - L77 were not covered by tests
}

if bookmarkID == 0 {
response.SendError(c.Context, http.StatusNotFound, nil)
return nil, model.ErrBookmarkNotFound

Check warning on line 82 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L80-L82

Added lines #L80 - L82 were not covered by tests
}

bookmark, err := r.deps.Domains.Bookmarks.GetBookmark(c.Context, model.DBID(bookmarkID))
if err != nil {
response.SendError(c.Context, http.StatusNotFound, nil)
return nil, model.ErrBookmarkNotFound

Check warning on line 88 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L85-L88

Added lines #L85 - L88 were not covered by tests
}

return bookmark, nil

Check warning on line 91 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L91

Added line #L91 was not covered by tests
}
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved

// Bookmark Readable godoc
//
// @Summary Get readable version of bookmark.
// @Tags Auth
// @securityDefinitions.apikey ApiKeyAuth
// @Produce json
// @Success 200 {object} contentResponseMessage
// @Failure 401 {object} nil "Token not provided/invalid"
// @Router /api/v1/bookmarks/id/readable [get]
func (r *BookmarksAPIRoutes) bookmarkReadable(c *gin.Context) {
ctx := context.NewContextFromGin(c)

Check warning on line 104 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L103-L104

Added lines #L103 - L104 were not covered by tests

bookmark, err := r.getBookmark(ctx)
if err != nil {
return

Check warning on line 108 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L106-L108

Added lines #L106 - L108 were not covered by tests
}
responseMessage := contentResponseMessage{
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
Content: bookmark.Content,
Html: bookmark.HTML,

Check warning on line 112 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L110-L112

Added lines #L110 - L112 were not covered by tests
}

response.Send(c, 200, responseMessage)

Check warning on line 115 in internal/http/routes/api/v1/bookmarks.go

View check run for this annotation

Codecov / codecov/patch

internal/http/routes/api/v1/bookmarks.go#L115

Added line #L115 was not covered by tests
}

// updateCache godoc
//
// @Summary Update Cache and Ebook on server.
Expand Down
Loading