Skip to content

Commit

Permalink
Improved paginator and delete checks
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Nov 3, 2024
1 parent 84b1694 commit 30584bb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
3 changes: 0 additions & 3 deletions internal/webserver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func SetupControllers(cfg Config, db *gorm.DB, metadataReaders map[string]metada

homeCfg := home.Config{
LibraryPath: cfg.LibraryPath,
HomeDir: cfg.HomeDir,
CoverMaxWidth: cfg.CoverMaxWidth,
Hostname: cfg.Hostname,
Port: cfg.Port,
}

return Controllers{
Expand Down
10 changes: 7 additions & 3 deletions internal/webserver/controller/document/delete.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package document

import (
"errors"
"log"
"os"
"path/filepath"

"github.com/gofiber/fiber/v2"
Expand All @@ -14,20 +16,22 @@ func (d *Controller) Delete(c *fiber.Ctx) error {
}

fullPath := filepath.Join(d.config.LibraryPath, document.ID)
if _, err := d.appFs.Stat(fullPath); err != nil {
if _, err := d.appFs.Stat(fullPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("error checking file %s for removal: %s\n", fullPath, err.Error())
return fiber.ErrBadRequest
}

if err := d.idx.RemoveFile(fullPath); err != nil {
log.Println(err)
return fiber.ErrInternalServerError
}

if err := d.appFs.Remove(fullPath); err != nil {
log.Printf("error removing file %s", fullPath)
log.Printf("error removing file %s\n", fullPath)
}

if err := d.hlRepository.RemoveDocument(document.ID); err != nil {
log.Printf("error removing file %s from highlights", document.ID)
log.Printf("error removing file %s from highlights\n", document.ID)
}

return nil
Expand Down
10 changes: 3 additions & 7 deletions internal/webserver/controller/home/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/svera/coreander/v4/internal/result"
)

const relatedDocuments = 4
const highlightsAmount = 6

type Sender interface {
SendDocument(address string, libraryPath string, fileName string) error
Expand All @@ -23,12 +23,8 @@ type highlightsRepository interface {
}

type Config struct {
LibraryPath string
HomeDir string
CoverMaxWidth int
Hostname string
Port int
UploadDocumentMaxSize int
LibraryPath string
CoverMaxWidth int
}

type Controller struct {
Expand Down
7 changes: 6 additions & 1 deletion internal/webserver/controller/home/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package home

import (
"log"

"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v4/internal/index"
"github.com/svera/coreander/v4/internal/webserver/infrastructure"
Expand All @@ -20,16 +22,19 @@ func (d *Controller) Index(c *fiber.Ctx) error {

count, err := d.idx.Count()
if err != nil {
log.Println(err)
return fiber.ErrInternalServerError
}

docsSortedByHighlightedDate, err := d.hlRepository.Highlights(int(session.ID), 0, 6)
docsSortedByHighlightedDate, err := d.hlRepository.Highlights(int(session.ID), 0, highlightsAmount)
if err != nil {
log.Println(err)
return fiber.ErrInternalServerError
}

docs, err := d.idx.Documents(docsSortedByHighlightedDate.Hits())
if err != nil {
log.Println(err)
return fiber.ErrInternalServerError
}

Expand Down
12 changes: 9 additions & 3 deletions internal/webserver/view/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ func Pagination[T any](size int, results result.Paginated[T], params map[string]
end = results.TotalPages()
}
for i := start; i <= end; i++ {
if params == nil {
params = make(map[string]string, 1)
}
params["page"] = fmt.Sprintf("%d", i)
p := Page{
Link: fmt.Sprintf("?%s&page=%d", ToQueryString(params), i),
Link: fmt.Sprintf("?%s", ToQueryString(params)),
}
if i == results.Page() {
p.IsCurrent = true
if i > 1 {
nav.PreviousLink = fmt.Sprintf("?%s&page=%d", ToQueryString(params), i-1)
params["page"] = fmt.Sprintf("%d", i-1)
nav.PreviousLink = fmt.Sprintf("?%s", ToQueryString(params))
}
if i < results.TotalPages() {
nav.NextLink = fmt.Sprintf("?%s&page=%d", ToQueryString(params), i+1)
params["page"] = fmt.Sprintf("%d", i+1)
nav.NextLink = fmt.Sprintf("?%s", ToQueryString(params))
}
}
nav.Pages[i] = p
Expand Down

0 comments on commit 30584bb

Please sign in to comment.