feat: default tag

This commit is contained in:
Damillora 2021-05-11 18:00:05 +07:00
parent ee9d2c80bf
commit 44eebcedfb
5 changed files with 25 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"github.com/Damillora/Shioriko/pkg/config" "github.com/Damillora/Shioriko/pkg/config"
"github.com/google/uuid"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
@ -29,6 +30,16 @@ func Initialize() {
}) })
} }
var tagme Tag
result = db.Where("name = ?", "tagme").First(&tagme)
if result.Error != nil {
db.Create(&Tag{
ID: uuid.NewString(),
Name: "tagme",
TagType: general,
})
}
if err != nil { if err != nil {
log.Fatal("Unable to connect to database" + err.Error()) log.Fatal("Unable to connect to database" + err.Error())
} }

View File

@ -29,10 +29,10 @@ type TagUpdateModel struct {
type PostCreateModel struct { type PostCreateModel struct {
BlobID string `json:"blob_id" validate:"required"` BlobID string `json:"blob_id" validate:"required"`
SourceURL string `json:"source_url"` SourceURL string `json:"source_url"`
Tags []string `json:"tags" validate:"required"` Tags []string `json:"tags"`
} }
type PostUpdateModel struct { type PostUpdateModel struct {
SourceURL string `json:"source_url"` SourceURL string `json:"source_url"`
Tags []string `json:"tags" validate:"required"` Tags []string `json:"tags"`
} }

View File

@ -41,7 +41,9 @@ func CreatePost(userID string, model models.PostCreateModel) (*database.Post, er
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(tags) == 0 {
database.DB.Where("name = ?", "tagme").Find(&tags)
}
post := database.Post{ post := database.Post{
ID: uuid.NewString(), ID: uuid.NewString(),
UserID: userID, UserID: userID,
@ -61,6 +63,9 @@ func UpdatePost(id string, model models.PostUpdateModel) (*database.Post, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(tags) == 0 {
database.DB.Where("name = ?", "tagme").Find(&tags)
}
var post database.Post var post database.Post
result := database.DB.Preload("Tags").Where("id = ?", id).First(&post) result := database.DB.Preload("Tags").Where("id = ?", id).First(&post)

View File

@ -62,8 +62,6 @@
{#each post.tags as tag (tag)} {#each post.tags as tag (tag)}
<TagLink {tag} /> <TagLink {tag} />
{/each} {/each}
{:else}
None
{/if} {/if}
</p> </p>
</div> </div>

View File

@ -121,7 +121,7 @@
<TagLink {tag} /> <TagLink {tag} />
{/each} {/each}
{:else} {:else}
None <TagLink tag="tagme" />
{/if} {/if}
</div> </div>
</div> </div>