Skip to content

Commit

Permalink
feat: new font and many other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thexyno committed Oct 4, 2023
1 parent cad7cfe commit 60878a0
Show file tree
Hide file tree
Showing 30 changed files with 497 additions and 65 deletions.
31 changes: 21 additions & 10 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand Down Expand Up @@ -89,13 +89,24 @@ var addCmd = &cobra.Command{
}
server.RenderSimple([]byte(mdRest)) // Panics when md is broken

created := time.Now()
draft := true
post, err := dbc.Post(header.Id)
log.Infof("Post %#v", post)
if err == nil && post.Id != "" {
log.Infof("Post %s already exists", header.Id)
created = post.Created
draft = post.Draft
}

if err := dbc.Add(db.Post{
Title: header.Title,
Id: db.PostId(header.Id),
Content: mdRest,
Created: header.Created,
Updated: header.Updated,
Created: created,
Updated: time.Now(),
Tags: header.Tags,
Draft: draft,
}); err != nil {
log.Error(err)
return err
Expand Down
69 changes: 69 additions & 0 deletions cmd/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2022 Philipp Hochkamp <git@phochkamp.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/thexyno/xynoblog/db"
)

// / publishCmd represents the publish command
var publishCmd = &cobra.Command{
Use: "publish",
Short: "Publishes posts (sets draft to false)",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
dbc := db.NewDb(viper.GetString(dbURIKey))
dbc.Seed()
for _, arg := range args {
if err := dbc.Publish(arg); err != nil {
log.Error(err)
}
}

},
}

func init() {
rootCmd.AddCommand(publishCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
63 changes: 63 additions & 0 deletions cmd/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright © 2022 Philipp Hochkamp <git@phochkamp.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/thexyno/xynoblog/db"
)

var seedCmd = &cobra.Command{
Use: "seed",
Short: "Update database schema",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
dbc := db.NewDb(viper.GetString(dbURIKey))
dbc.Seed()
log.Info("Seeded database")
},
}

func init() {
rootCmd.AddCommand(seedCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
49 changes: 43 additions & 6 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package db
import (
"context"
"database/sql"
"embed"
_ "embed"
"errors"
"math"
"net/http"
"regexp"
"time"

_ "github.com/mattn/go-sqlite3"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"github.com/thexyno/xynoblog/dbconn"
)
Expand All @@ -35,12 +38,14 @@ type Post struct {
Updated time.Time
Tags []string
TimeToRead time.Duration
Draft bool
}
type PostNoContent struct {
Id PostId
Title string
Created time.Time
Updated time.Time
Draft bool
}

func (post *Post) ToIdUpdated() IdUpdated {
Expand All @@ -49,8 +54,8 @@ func (post *Post) ToIdUpdated() IdUpdated {

var ErrNotFound error = errors.New("not found")

//go:embed schema.sql
var ddl string
//go:embed migrations
var ddl embed.FS

func NewDb(uri string) DbConn {
log.Infof("dbpath is %v", uri)
Expand All @@ -66,10 +71,15 @@ func (conn *DbConn) Close() error {
}

func (conn *DbConn) Seed() error {
// create tables
if _, err := conn.db.ExecContext(conn.ctx, ddl); err != nil {
httpFs := http.FS(ddl)
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: httpFs,
}
n, err := migrate.Exec(conn.db, "sqlite3", migrationSource, migrate.Up)
if err != nil {
return err
}
log.Infof("Applied %d migrations!", n)
return nil
}

Expand Down Expand Up @@ -121,6 +131,7 @@ func (conn *DbConn) insertPost(post Post) error {
Content: post.Content,
CreatedAt: post.Created,
UpdatedAt: post.Created,
Draft: post.Draft,
},
)
if err != nil {
Expand Down Expand Up @@ -166,7 +177,7 @@ func wordCount(value string) int {
return len(results)
}

/// returns post with id = id
// / returns post with id = id
func (conn *DbConn) Post(id string) (Post, error) {
post, err := conn.queries.GetPost(conn.ctx, id)
if err != nil {
Expand All @@ -185,10 +196,11 @@ func (conn *DbConn) Post(id string) (Post, error) {
post.UpdatedAt,
tags,
duration,
post.Draft,
}, nil
}

/// returns post with id = id
// / returns posts limited by limit and offset order by updated desc
func (conn *DbConn) Posts(limit int64, offset int64) ([]Post, error) {
posts, err := conn.queries.GetPosts(conn.ctx, dbconn.GetPostsParams{Limit: limit, Offset: offset})
if err != nil {
Expand All @@ -205,15 +217,39 @@ func (conn *DbConn) Posts(limit int64, offset int64) ([]Post, error) {
post.UpdatedAt,
[]string{},
0,
false,
}
}
return toReturn, nil
}

// / returns shortPosts with tag = tag
func (conn *DbConn) ShortPostsByTag(tag string, limit, offset int64) ([]PostNoContent, error) {
posts, err := conn.queries.GetPostsByTagNoContent(conn.ctx, dbconn.GetPostsByTagNoContentParams{Tag: tag, Limit: limit, Offset: offset})
if err != nil {
return []PostNoContent{}, err
}
var toReturn = make([]PostNoContent, len(posts))
for i, post := range posts {
toReturn[i] = PostNoContent{
PostId(post.PostID),
post.Title,
post.CreatedAt,
post.UpdatedAt,
false,
}
}
return toReturn, nil
}

func (conn *DbConn) PostIds() ([]dbconn.GetPostIdsRow, error) {
return conn.queries.GetPostIds(conn.ctx)
}

func (conn *DbConn) Publish(id string) error {
return conn.queries.PublishPost(conn.ctx, id)
}

// returns posts with only textLength chars of post text and no duration
// limit = -1 returns all
func (conn *DbConn) ShortPosts(limit int64, skip int64) ([]PostNoContent, error) {
Expand All @@ -229,6 +265,7 @@ func (conn *DbConn) ShortPosts(limit int64, skip int64) ([]PostNoContent, error)
post.Title,
post.CreatedAt,
post.UpdatedAt,
false,
}
}
return toReturn, nil
Expand Down
5 changes: 5 additions & 0 deletions db/schema.sql → db/migrations/1_initial.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- +migrate Up
CREATE TABLE posts (post_id text primary key not null,
title text not null,
content text not null,
Expand All @@ -11,3 +12,7 @@ CREATE TABLE post_tags (post_id text not null,
REFERENCES posts (post_id),
PRIMARY KEY (post_id, tag) -- it's all a primary key
);

-- +migrate Down
DROP TABLE post_tags;
DROP TABLE posts;
6 changes: 6 additions & 0 deletions db/migrations/2_draft.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- +migrate Up
ALTER TABLE posts ADD COLUMN draft BOOLEAN NOT NULL DEFAULT TRUE;
UPDATE posts SET draft = FALSE;

-- +migrate Down
ALTER TABLE posts DROP COLUMN draft;
15 changes: 15 additions & 0 deletions db/migrations/3_constraints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
PRAGMA foreign_keys = ON;
CREATE TEMPORARY TABLE post_tags_backup AS SELECT * FROM post_tags;
DROP TABLE post_tags;
CREATE TABLE post_tags (
post_id TEXT NOT NULL,
tag TEXT NOT NULL,
FOREIGN KEY (post_id) REFERENCES posts (post_id) ON DELETE CASCADE,
PRIMARY KEY (post_id, tag)
);
INSERT INTO post_tags (post_id, tag) SELECT post_id, tag FROM post_tags_backup;
DROP TABLE post_tags_backup;

-- +migrate Down

20 changes: 13 additions & 7 deletions db/query.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
-- name: GetPostsNoContent :many
select post_id, title, created_at, updated_at from posts order by created_at desc limit ? offset ?;
select post_id, title, created_at, updated_at from posts where draft = FALSE order by created_at desc limit ? offset ?;

-- name: GetPosts :many
select post_id, title, content, created_at, updated_at from posts order by created_at desc limit ? offset ?;
select post_id, title, content, created_at, updated_at from posts where draft = FALSE order by created_at desc limit ? offset ?;
-- name: GetPostsWithTags :many
select posts.post_id, title, content, created_at, updated_at, tag from posts join post_tags on posts.post_id = post_tags.post_id group by posts.post_id order by created_at desc limit ? offset ?;
select posts.post_id, title, content, created_at, updated_at, tag from posts join post_tags on posts.post_id = post_tags.post_id where draft = FALSE group by posts.post_id order by created_at desc limit ? offset ?;
-- name: GetPostsByTag :many
select posts.post_id, title, content, created_at, updated_at, tag from posts join post_tags on posts.post_id = post_tags.post_id where tag = ? and draft = FALSE order by created_at desc limit ? offset ?;
-- name: GetPostsByTagNoContent :many
select posts.post_id, title, created_at, updated_at, tag from posts join post_tags on posts.post_id = post_tags.post_id where tag = ? and draft = FALSE order by created_at desc limit ? offset ?;

-- name: GetPostIds :many
select post_id, updated_at from posts;
select post_id, updated_at from posts where draft = FALSE;

-- name: GetPost :one
select post_id, title, content, created_at, updated_at from posts where post_id = ?;
select post_id, title, content, created_at, updated_at, draft from posts where post_id = ?;

-- name: GetTags :many
select tag from post_tags where post_id = ?;

-- name: AddPost :one
insert into posts (post_id,title,content,created_at,updated_at) values (?,?,?,?,?) returning *;
insert into posts (post_id,title,content,created_at,updated_at,draft) values (?,?,?,?,?,?) returning *;
-- name: UpdatePost :exec
update posts set post_id = ?,title = ?,content = ?,created_at = ?,updated_at = ? where post_id = ?;
update posts set post_id = ?,title = ?,content = ?,created_at = ?,updated_at = ?, draft = ? where post_id = ?;
-- name: AddTag :one
insert into post_tags (post_id, tag) values (?,?) returning *;
-- name: DeleteTags :exec
delete from post_tags where post_id = ?;
-- name: PublishPost :exec
update posts set draft = FALSE, created_at = datetime(), updated_at = datetime() where post_id = ?;
Loading

0 comments on commit 60878a0

Please sign in to comment.