forked from robherley/snips.sh
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(robherley#97): implement list index and rename
list index implemented, however with unhelpful names it would be nice to see more pleasant name options. so ability to rename and specify name has been added too
- Loading branch information
Showing
16 changed files
with
615 additions
and
134 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
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,56 @@ | ||
package api_v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/robherley/snips.sh/internal/config" | ||
"github.com/robherley/snips.sh/internal/db" | ||
"github.com/robherley/snips.sh/internal/logger" | ||
) | ||
|
||
// GetSnips | ||
// Retrieve a list of snips on the server, paginated, in date created DESC by | ||
// default. | ||
// Ideal utilisation, when trying to view all snips, is to iterate through pages | ||
// until you reach a payload size of 0. | ||
func GetSnips(database db.DB) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
log := logger.From(r.Context()) | ||
|
||
page := 0 | ||
if r.URL.Query().Has("page") { | ||
strPage := r.URL.Query().Get("page") | ||
p, err := strconv.Atoi(strPage) | ||
if err == nil { | ||
page = p | ||
} | ||
} | ||
|
||
files, err := database.FindFiles(r.Context(), page) | ||
if err != nil { | ||
log.Error().Err(err).Msg("unable to render template") | ||
http.Error(w, "internal error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
filesMarshalled, err := json.Marshal(files) | ||
if err != nil { | ||
log.Error().Err(err).Msg("unable to render template") | ||
http.Error(w, "internal error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Write(filesMarshalled) | ||
} | ||
} | ||
|
||
func ApiHandler(cfg *config.Config, database db.DB) *chi.Mux { | ||
apiRouter := chi.NewMux() | ||
|
||
apiRouter.Get("/snips", GetSnips(database)) | ||
|
||
return apiRouter | ||
} |
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ var ( | |
|
||
jsFiles = []string{ | ||
"snips.js", | ||
"list.js", | ||
} | ||
) | ||
|
||
|
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
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.