Have got soft and hard deleting
This commit is contained in:
parent
aef3bb6a1e
commit
3ea5823ca0
27 changed files with 588 additions and 55 deletions
|
|
@ -7,12 +7,15 @@ package sqlgen
|
|||
type Post struct {
|
||||
ID int64
|
||||
SiteID int64
|
||||
State int64
|
||||
Guid string
|
||||
Title string
|
||||
Body string
|
||||
Slug string
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
PublishedAt int64
|
||||
DeletedAt int64
|
||||
}
|
||||
|
||||
type PublishTarget struct {
|
||||
|
|
|
|||
|
|
@ -9,46 +9,73 @@ import (
|
|||
"context"
|
||||
)
|
||||
|
||||
const hardDeletePost = `-- name: HardDeletePost :exec
|
||||
DELETE FROM posts WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) HardDeletePost(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, hardDeletePost, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertPost = `-- name: InsertPost :one
|
||||
INSERT INTO posts (
|
||||
site_id,
|
||||
state,
|
||||
guid,
|
||||
title,
|
||||
body,
|
||||
slug,
|
||||
created_at,
|
||||
published_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
updated_at,
|
||||
published_at,
|
||||
deleted_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type InsertPostParams struct {
|
||||
SiteID int64
|
||||
State int64
|
||||
Guid string
|
||||
Title string
|
||||
Body string
|
||||
Slug string
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
PublishedAt int64
|
||||
DeletedAt int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertPost,
|
||||
arg.SiteID,
|
||||
arg.State,
|
||||
arg.Guid,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.Slug,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
arg.PublishedAt,
|
||||
arg.DeletedAt,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const restorePost = `-- name: RestorePost :exec
|
||||
UPDATE posts SET deleted_at = 0 WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) RestorePost(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, restorePost, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const selectPost = `-- name: SelectPost :one
|
||||
SELECT id, site_id, guid, title, body, slug, created_at, published_at FROM posts WHERE id = ? LIMIT 1
|
||||
SELECT id, site_id, state, guid, title, body, slug, created_at, updated_at, published_at, deleted_at FROM posts WHERE id = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) SelectPost(ctx context.Context, id int64) (Post, error) {
|
||||
|
|
@ -57,18 +84,21 @@ func (q *Queries) SelectPost(ctx context.Context, id int64) (Post, error) {
|
|||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SiteID,
|
||||
&i.State,
|
||||
&i.Guid,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Slug,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.DeletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const selectPostByGUID = `-- name: SelectPostByGUID :one
|
||||
SELECT id, site_id, guid, title, body, slug, created_at, published_at FROM posts WHERE guid = ? LIMIT 1
|
||||
SELECT id, site_id, state, guid, title, body, slug, created_at, updated_at, published_at, deleted_at FROM posts WHERE guid = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) SelectPostByGUID(ctx context.Context, guid string) (Post, error) {
|
||||
|
|
@ -77,22 +107,37 @@ func (q *Queries) SelectPostByGUID(ctx context.Context, guid string) (Post, erro
|
|||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SiteID,
|
||||
&i.State,
|
||||
&i.Guid,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Slug,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.DeletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const selectPostsOfSite = `-- name: SelectPostsOfSite :many
|
||||
SELECT id, site_id, guid, title, body, slug, created_at, published_at FROM posts WHERE site_id = ? ORDER BY created_at DESC LIMIT 10
|
||||
SELECT id, site_id, state, guid, title, body, slug, created_at, updated_at, published_at, deleted_at
|
||||
FROM posts
|
||||
WHERE site_id = ? AND (
|
||||
CASE CAST (?2 AS TEXT)
|
||||
WHEN 'deleted' THEN deleted_at > 0
|
||||
ELSE deleted_at = 0
|
||||
END
|
||||
) ORDER BY created_at DESC LIMIT 10
|
||||
`
|
||||
|
||||
func (q *Queries) SelectPostsOfSite(ctx context.Context, siteID int64) ([]Post, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectPostsOfSite, siteID)
|
||||
type SelectPostsOfSiteParams struct {
|
||||
SiteID int64
|
||||
PostFilter string
|
||||
}
|
||||
|
||||
func (q *Queries) SelectPostsOfSite(ctx context.Context, arg SelectPostsOfSiteParams) ([]Post, error) {
|
||||
rows, err := q.db.QueryContext(ctx, selectPostsOfSite, arg.SiteID, arg.PostFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -103,12 +148,15 @@ func (q *Queries) SelectPostsOfSite(ctx context.Context, siteID int64) ([]Post,
|
|||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SiteID,
|
||||
&i.State,
|
||||
&i.Guid,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Slug,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.DeletedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -123,29 +171,52 @@ func (q *Queries) SelectPostsOfSite(ctx context.Context, siteID int64) ([]Post,
|
|||
return items, nil
|
||||
}
|
||||
|
||||
const softDeletePost = `-- name: SoftDeletePost :exec
|
||||
UPDATE posts SET deleted_at = ? WHERE id = ?
|
||||
`
|
||||
|
||||
type SoftDeletePostParams struct {
|
||||
DeletedAt int64
|
||||
ID int64
|
||||
}
|
||||
|
||||
func (q *Queries) SoftDeletePost(ctx context.Context, arg SoftDeletePostParams) error {
|
||||
_, err := q.db.ExecContext(ctx, softDeletePost, arg.DeletedAt, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updatePost = `-- name: UpdatePost :exec
|
||||
UPDATE posts SET
|
||||
title = ?,
|
||||
state = ?,
|
||||
body = ?,
|
||||
slug = ?,
|
||||
published_at = ?
|
||||
updated_at = ?,
|
||||
published_at = ?,
|
||||
deleted_at = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type UpdatePostParams struct {
|
||||
Title string
|
||||
State int64
|
||||
Body string
|
||||
Slug string
|
||||
UpdatedAt int64
|
||||
PublishedAt int64
|
||||
DeletedAt int64
|
||||
ID int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdatePost(ctx context.Context, arg UpdatePostParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updatePost,
|
||||
arg.Title,
|
||||
arg.State,
|
||||
arg.Body,
|
||||
arg.Slug,
|
||||
arg.UpdatedAt,
|
||||
arg.PublishedAt,
|
||||
arg.DeletedAt,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -8,8 +8,16 @@ import (
|
|||
"lmika.dev/lmika/weiro/providers/db/gen/sqlgen"
|
||||
)
|
||||
|
||||
func (db *Provider) SelectPostsOfSite(ctx context.Context, siteID int64) ([]*models.Post, error) {
|
||||
rows, err := db.queries.SelectPostsOfSite(ctx, siteID)
|
||||
func (db *Provider) SelectPostsOfSite(ctx context.Context, siteID int64, showDeleted bool) ([]*models.Post, error) {
|
||||
var filter = ""
|
||||
if showDeleted {
|
||||
filter = "deleted"
|
||||
}
|
||||
|
||||
rows, err := db.queries.SelectPostsOfSite(ctx, sqlgen.SelectPostsOfSiteParams{
|
||||
SiteID: siteID,
|
||||
PostFilter: filter,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -43,12 +51,15 @@ func (db *Provider) SavePost(ctx context.Context, post *models.Post) error {
|
|||
if post.ID == 0 {
|
||||
newID, err := db.queries.InsertPost(ctx, sqlgen.InsertPostParams{
|
||||
SiteID: post.SiteID,
|
||||
State: int64(post.State),
|
||||
Guid: post.GUID,
|
||||
Title: post.Title,
|
||||
Body: post.Body,
|
||||
Slug: post.Slug,
|
||||
CreatedAt: post.CreatedAt.Unix(),
|
||||
PublishedAt: post.PublishedAt.Unix(),
|
||||
CreatedAt: timeToInt(post.CreatedAt),
|
||||
UpdatedAt: timeToInt(post.UpdatedAt),
|
||||
PublishedAt: timeToInt(post.PublishedAt),
|
||||
DeletedAt: timeToInt(post.DeletedAt),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -59,10 +70,13 @@ func (db *Provider) SavePost(ctx context.Context, post *models.Post) error {
|
|||
|
||||
return db.queries.UpdatePost(ctx, sqlgen.UpdatePostParams{
|
||||
ID: post.ID,
|
||||
State: int64(post.State),
|
||||
Title: post.Title,
|
||||
Body: post.Body,
|
||||
Slug: post.Slug,
|
||||
PublishedAt: post.PublishedAt.Unix(),
|
||||
UpdatedAt: timeToInt(post.UpdatedAt),
|
||||
PublishedAt: timeToInt(post.PublishedAt),
|
||||
DeletedAt: timeToInt(post.DeletedAt),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -70,11 +84,21 @@ func dbPostToPost(row sqlgen.Post) *models.Post {
|
|||
return &models.Post{
|
||||
ID: row.ID,
|
||||
SiteID: row.SiteID,
|
||||
State: int(row.State),
|
||||
GUID: row.Guid,
|
||||
Title: row.Title,
|
||||
Body: row.Body,
|
||||
Slug: row.Slug,
|
||||
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
UpdatedAt: time.Unix(row.UpdatedAt, 0).UTC(),
|
||||
PublishedAt: time.Unix(row.PublishedAt, 0).UTC(),
|
||||
DeletedAt: time.Unix(row.DeletedAt, 0).UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
func timeToInt(t time.Time) int64 {
|
||||
if t.IsZero() {
|
||||
return 0
|
||||
}
|
||||
return t.Unix()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package db
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/Southclaws/fault"
|
||||
"lmika.dev/lmika/weiro/providers/db/gen/sqlgen"
|
||||
|
|
@ -38,3 +39,18 @@ func New(dbFile string) (*Provider, error) {
|
|||
func (db *Provider) Close() error {
|
||||
return db.drvr.Close()
|
||||
}
|
||||
|
||||
func (db *Provider) SoftDeletePost(ctx context.Context, postID int64) error {
|
||||
return db.queries.SoftDeletePost(ctx, sqlgen.SoftDeletePostParams{
|
||||
DeletedAt: time.Now().Unix(),
|
||||
ID: postID,
|
||||
})
|
||||
}
|
||||
|
||||
func (db *Provider) HardDeletePost(ctx context.Context, postID int64) error {
|
||||
return db.queries.HardDeletePost(ctx, postID)
|
||||
}
|
||||
|
||||
func (db *Provider) RestorePost(ctx context.Context, postID int64) error {
|
||||
return db.queries.RestorePost(ctx, postID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue