Remove unused

This commit is contained in:
Sung Won Cho 2022-04-18 09:54:48 +10:00
commit b4126aa82b
2 changed files with 5 additions and 160 deletions

View file

@ -9,7 +9,6 @@ import (
"github.com/dnote/dnote/pkg/server/database"
"github.com/dnote/dnote/pkg/server/helpers"
"github.com/dnote/dnote/pkg/server/presenters"
"github.com/dnote/dnote/pkg/server/views"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
@ -18,17 +17,13 @@ import (
// It panics if the necessary templates are not parsed.
func NewBooks(app *app.App) *Books {
return &Books{
IndexView: views.NewView(app, views.Config{Title: "", Layout: "base", HeaderTemplate: "navbar"}, "books/index"),
ShowView: views.NewView(app, views.Config{Title: "", Layout: "base", HeaderTemplate: "navbar"}, "books/show"),
app: app,
app: app,
}
}
// Books is a user controller.
type Books struct {
IndexView *views.View
ShowView *views.View
app *app.App
app *app.App
}
func (b *Books) getBooks(r *http.Request) ([]database.Book, error) {
@ -66,23 +61,6 @@ func (b *Books) getBooks(r *http.Request) ([]database.Book, error) {
return books, nil
}
// Index handles GET /
func (b *Books) Index(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
result, err := b.getBooks(r)
if err != nil {
handleHTMLError(w, r, err, "getting books", b.IndexView, vd)
return
}
vd.Yield = map[string]interface{}{
"Books": result,
}
b.IndexView.Render(w, r, &vd, http.StatusOK)
}
// V3Index gets books
func (b *Books) V3Index(w http.ResponseWriter, r *http.Request) {
result, err := b.getBooks(r)
@ -171,19 +149,6 @@ func (b *Books) create(r *http.Request) (database.Book, error) {
return book, nil
}
// Create creates a book
func (b *Books) Create(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
_, err := b.create(r)
if err != nil {
handleHTMLError(w, r, err, "creating a books", b.IndexView, vd)
return
}
http.Redirect(w, r, "/books", http.StatusCreated)
}
// createBookResp is the response from create book api
type createBookResp struct {
Book presenters.Book `json:"book"`
@ -248,19 +213,6 @@ func (b *Books) update(r *http.Request) (database.Book, error) {
return book, nil
}
// Update updates a book
func (b *Books) Update(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
_, err := b.update(r)
if err != nil {
handleHTMLError(w, r, err, "creating a books", b.IndexView, vd)
return
}
http.Redirect(w, r, "/books", http.StatusOK)
}
// V3Update updates a book
func (b *Books) V3Update(w http.ResponseWriter, r *http.Request) {
book, err := b.update(r)
@ -317,19 +269,6 @@ func (b *Books) del(r *http.Request) (database.Book, error) {
return book, nil
}
// Delete updates a book
func (b *Books) Delete(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
_, err := b.del(r)
if err != nil {
handleHTMLError(w, r, err, "creating a books", b.IndexView, vd)
return
}
http.Redirect(w, r, "/", http.StatusOK)
}
// deleteBookResp is the response from create book api
type deleteBookResp struct {
Status int `json:"status"`
@ -338,11 +277,9 @@ type deleteBookResp struct {
// Delete updates a book
func (b *Books) V3Delete(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
book, err := b.del(r)
if err != nil {
handleHTMLError(w, r, err, "creating a books", b.IndexView, vd)
handleJSONError(w, err, "creating a books")
return
}

View file

@ -1,9 +1,6 @@
package controllers
import (
"bytes"
"fmt"
"html/template"
"math"
"net/http"
"net/url"
@ -17,19 +14,15 @@ import (
"github.com/dnote/dnote/pkg/server/database"
"github.com/dnote/dnote/pkg/server/operations"
"github.com/dnote/dnote/pkg/server/presenters"
"github.com/dnote/dnote/pkg/server/views"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/yuin/goldmark"
)
// NewNotes creates a new Notes controller.
// It panics if the necessary templates are not parsed.
func NewNotes(app *app.App) *Notes {
return &Notes{
IndexView: views.NewView(app, views.Config{Title: "", Layout: "base", HeaderTemplate: "navbar"}, "notes/index"),
ShowView: views.NewView(app, views.Config{Title: "", Layout: "base", HeaderTemplate: "navbar"}, "notes/show"),
app: app,
app: app,
}
}
@ -37,9 +30,7 @@ var notesPerPage = 30
// Notes is a user controller.
type Notes struct {
IndexView *views.View
ShowView *views.View
app *app.App
app *app.App
}
// escapeSearchQuery escapes the query for full text search
@ -204,29 +195,6 @@ func getMaxPage(page, total int) int {
return int(math.Ceil(tmp))
}
// Index handles GET /
func (n *Notes) Index(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
res, p, err := n.getNotes(r)
if err != nil {
handleHTMLError(w, r, err, "getting notes", n.IndexView, vd)
return
}
noteGroups := groupNotes(res.Notes)
vd.Yield = map[string]interface{}{
"NoteGroups": noteGroups,
"CurrentPage": p.Page,
"MaxPage": getMaxPage(p.Page, res.Total),
"HasNext": p.Page*notesPerPage < res.Total,
"HasPrev": p.Page > 1,
}
n.IndexView.Render(w, r, &vd, http.StatusOK)
}
// GetNotesResponse is a reponse by getNotesHandler
type GetNotesResponse struct {
Notes []presenters.Note `json:"notes"`
@ -264,27 +232,6 @@ func (n *Notes) getNote(r *http.Request) (database.Note, error) {
return note, nil
}
// Show shows note
func (n *Notes) Show(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
note, err := n.getNote(r)
if err != nil {
handleHTMLError(w, r, err, "getting notes", n.ShowView, vd)
return
}
var buf bytes.Buffer
goldmark.Convert([]byte(note.Body), &buf)
vd.Yield = map[string]interface{}{
"Note": note,
"Content": template.HTML(buf.String()),
}
n.ShowView.Render(w, r, &vd, http.StatusOK)
}
// V3Show is api for show
func (n *Notes) V3Show(w http.ResponseWriter, r *http.Request) {
note, err := n.getNote(r)
@ -389,32 +336,6 @@ func (n *Notes) V3Create(w http.ResponseWriter, r *http.Request) {
})
}
// Create creates note
func (n *Notes) Create(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
note, err := n.create(r)
if err != nil {
handleHTMLError(w, r, err, "creating note", n.IndexView, vd)
return
}
http.Redirect(w, r, fmt.Sprintf("/notes/%s", note.UUID), http.StatusCreated)
}
// Delete deletes note
func (n *Notes) Delete(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
_, err := n.del(r)
if err != nil {
handleHTMLError(w, r, err, "getting notes", n.IndexView, vd)
return
}
http.Redirect(w, r, "/notes", http.StatusOK)
}
type DeleteNoteResp struct {
Status int `json:"status"`
Result presenters.Note `json:"result"`
@ -518,19 +439,6 @@ func (n *Notes) V3Update(w http.ResponseWriter, r *http.Request) {
})
}
// Update updates a note
func (n *Notes) Update(w http.ResponseWriter, r *http.Request) {
vd := views.Data{}
note, err := n.update(r)
if err != nil {
handleHTMLError(w, r, err, "updating note", n.IndexView, vd)
return
}
http.Redirect(w, r, fmt.Sprintf("/notes/%s", note.UUID), http.StatusOK)
}
// IndexOptions is a handler for OPTIONS endpoint for notes
func (n *Notes) IndexOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "POST")