Skip to content

Commit

Permalink
Merge pull request #15 from sparkymat/feature/add-deletion
Browse files Browse the repository at this point in the history
Add deletion
  • Loading branch information
sparkymat committed May 8, 2023
2 parents 89c2cfe + b2e3d07 commit ff5dd40
Show file tree
Hide file tree
Showing 43 changed files with 1,043 additions and 410 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ DATABASE_PORT=5432
DATABASE_SSL_MODE=false
DOWNLOAD_PATH=/tmp
MONOLITH_PATH=/usr/bin/monolith
FAKTORY_URL=tcp://localhost:7419
16 changes: 15 additions & 1 deletion archmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package main
//go:generate go run github.com/valyala/quicktemplate/qtc -dir=view

import (
"time"

"github.com/go-co-op/gocron"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/sparkymat/archmark/config"
"github.com/sparkymat/archmark/database"
"github.com/sparkymat/archmark/dbx"
"github.com/sparkymat/archmark/internal/jobs"
"github.com/sparkymat/archmark/internal/route"
)

Expand All @@ -30,9 +34,19 @@ func main() {
panic(err)
}

// Initialize web server
db := dbx.New(dbDriver.DB())

route.Setup(e, appConfig, db)

// Setup scheduler
scheduler := gocron.NewScheduler(time.UTC)

_, err = scheduler.Every(1).Hour().Do(jobs.DeleteExpiredBookmarks(appConfig, db))
if err != nil {
panic(err)
}

scheduler.StartAsync()

e.Logger.Panic(e.Start(":8080"))
}
5 changes: 5 additions & 0 deletions config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type envValues struct {
ReverseProxyAuthentication bool `env:"REVERSE_PROXY_AUTHENTICATION" envDefault:"false"`
ProxyAuthUsernameHeader string `env:"PROXY_AUTH_USERNAME_HEADER" envDefault:"Remote-User"`
ProxyAuthNameHeader string `env:"PROXY_AUTH_NAME_HEADER" envDefault:"Remote-Name"`
DeleteTimerHours int32 `env:"DELETE_TIMER_HOURS" envDefault:"48"`
}

func (s *Service) JWTSecret() string {
Expand Down Expand Up @@ -103,3 +104,7 @@ func (s *Service) ProxyAuthUsernameHeader() string {
func (s *Service) ProxyAuthNameHeader() string {
return s.envValues.ProxyAuthNameHeader
}

func (s *Service) DeleteTimerHours() int32 {
return s.envValues.DeleteTimerHours
}
43 changes: 39 additions & 4 deletions database/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ SELECT u.*
SELECT b.*
FROM bookmarks b
WHERE b.user_id = @user_id::bigint
AND b.deleted_at IS NULL
ORDER BY b.created_at DESC
LIMIT @page_limit::int
OFFSET @page_offset::int;

-- name: CountBookmarksList :one
SELECT COUNT(*)
FROM bookmarks b
WHERE b.user_id = @user_id::bigint;
WHERE b.user_id = @user_id::bigint
AND b.deleted_at IS NULL;

-- name: CreateBookmark :one
INSERT INTO bookmarks (
Expand Down Expand Up @@ -49,22 +51,55 @@ INSERT INTO users (
-- name: SearchBookmarks :many
SELECT b.*
FROM bookmarks b
WHERE b.user_id = @user_id::bigint AND b.html_ts @@ to_tsquery('english', @query::text)
WHERE b.user_id = @user_id::bigint
AND b.html_ts @@ to_tsquery('english', @query::text)
AND b.deleted_at IS NULL
LIMIT @page_limit::int
OFFSET @page_offset::int;

-- name: CountBookmarksSearchResults :one
SELECT COUNT(*)
FROM bookmarks b
WHERE b.user_id = @user_id::bigint AND b.html_ts @@ to_tsquery('english', @query::text);
WHERE b.user_id = @user_id::bigint
AND b.html_ts @@ to_tsquery('english', @query::text)
AND b.deleted_at IS NULL;

-- name: FetchCategories :many
SELECT DISTINCT(b.category)
FROM bookmarks b
WHERE b.category != '' AND b.user_id = @user_id
WHERE b.category != '' AND b.user_id = @user_id AND b.deleted_at IS NULL
ORDER BY b.category ASC;

-- name: UpdateBookmarkCategory :exec
UPDATE bookmarks
SET category = @category::text
WHERE id = @id::bigint;

-- name: FetchArchivedBookmarksList :many
SELECT b.*
FROM bookmarks b
WHERE b.user_id = @user_id::bigint
AND b.deleted_at IS NOT NULL
ORDER BY b.deleted_at DESC
LIMIT @page_limit::int
OFFSET @page_offset::int;

-- name: CountArchivedBookmarksList :one
SELECT COUNT(*)
FROM bookmarks b
WHERE b.user_id = @user_id::bigint
AND b.deleted_at IS NOT NULL;

-- name: ArchiveBookmark :exec
UPDATE bookmarks b
SET deleted_at = now()
WHERE id = @id::bigint;

-- name: UnarchiveBookmark :exec
UPDATE bookmarks b
SET deleted_at = NULL
WHERE id = @id::bigint;

-- name: DeleteBookmarks :exec
DELETE FROM bookmarks b
WHERE b.deleted_at IS NOT NULL AND b.deleted_at < now() - ('1 hour'::interval * @agehours::int);
11 changes: 6 additions & 5 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ CREATE TABLE public.bookmarks (
html text,
file_path text,
status public.bookmark_status DEFAULT 'pending'::public.bookmark_status NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
category text DEFAULT ''::text
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
category text DEFAULT ''::text,
deleted_at timestamp with time zone
);


Expand Down Expand Up @@ -101,8 +102,8 @@ CREATE TABLE public.users (
username text,
name text NOT NULL,
encrypted_password text NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


Expand Down
9 changes: 5 additions & 4 deletions dbx/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 111 additions & 7 deletions dbx/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff5dd40

Please sign in to comment.