Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 909 Update initial data #918

Merged
merged 4 commits into from Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
138 changes: 138 additions & 0 deletions internal/migrations/init.go
Expand Up @@ -23,6 +23,10 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/apache/incubator-answer/internal/base/data"
"github.com/apache/incubator-answer/internal/repo/unique"
"github.com/apache/incubator-answer/internal/schema"
"github.com/segmentfault/pacman/log"

Expand Down Expand Up @@ -73,6 +77,7 @@ func (m *Mentor) InitDB() error {
m.do("init site info user config", m.initSiteInfoUsersConfig)
m.do("init site info privilege rank", m.initSiteInfoPrivilegeRank)
m.do("init site info write", m.initSiteInfoWrite)
m.do("init default content", m.initDefaultContent)
return m.err
}

Expand Down Expand Up @@ -255,3 +260,136 @@ func (m *Mentor) initSiteInfoWrite() {
Status: 1,
})
}

func (m *Mentor) initDefaultContent() {
uniqueIDRepo := unique.NewUniqueIDRepo(&data.Data{DB: m.engine})
now := time.Now()

tagId, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Tag{}.TableName())
if err != nil {
m.err = err
return
}

q1Id, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Question{}.TableName())
if err != nil {
m.err = err
return
}

a1Id, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Answer{}.TableName())
if err != nil {
m.err = err
return
}

q2Id, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Question{}.TableName())
if err != nil {
m.err = err
return
}

a2Id, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Answer{}.TableName())
if err != nil {
m.err = err
return
}

tag := entity.Tag{
ID: tagId,
SlugName: "support",
DisplayName: "support",
OriginalText: "For general support questions.",
ParsedText: "<p>For general support questions.</p>",
UserID: "1",
QuestionCount: 1,
Status: entity.TagStatusAvailable,
}

q1 := &entity.Question{
ID: q1Id,
CreatedAt: now,
UserID: "1",
Title: "What is a tag?",
OriginalText: "When asking a question, we need to choose tags. What are tags and why should I use them?",
ParsedText: "<p>When asking a question, we need to choose tags. What are tags and why should I use them?</p>",
Show: entity.QuestionShow,
Status: entity.QuestionStatusAvailable,
AnswerCount: 1,
}

a1 := &entity.Answer{
ID: a1Id,
CreatedAt: now,
QuestionID: q1Id,
UserID: "1",
OriginalText: "Tags help to organize content and make searching easier. It helps your question get more attention from people interested in that tag. Tags also send notifications. If you are interested in some topic, follow that tag to get updates.",
ParsedText: "<p>Tags help to organize content and make searching easier. It helps your question get more attention from people interested in that tag. Tags also send notifications. If you are interested in some topic, follow that tag to get updates.</p>",
Status: entity.AnswerStatusAvailable,
}

q2 := &entity.Question{
ID: q2Id,
CreatedAt: now,
UserID: "1",
Title: "What is reputation and how do I earn them?",
OriginalText: "I see that each user has reputation points, What is it and how do I earn them?",
ParsedText: "<p>I see that each user has reputation points, What is it and how do I earn them?</p>",
Show: entity.QuestionShow,
Status: entity.QuestionStatusAvailable,
AnswerCount: 1,
}

a2 := &entity.Answer{
ID: a2Id,
CreatedAt: now,
QuestionID: q2Id,
UserID: "1",
OriginalText: "Your reputation points show how much the community values your knowledge. You earn points when someone find your question or answer helpful. You also get points when the person who asked the question thinks you did a good job and accepts your answer.",
ParsedText: "<p>Your reputation points show how much the community values your knowledge. You earn points when someone find your question or answer helpful. You also get points when the person who asked the question thinks you did a good job and accepts your answer.</p>",
Status: entity.AnswerStatusAvailable,
}

_, m.err = m.engine.Context(m.ctx).Insert(tag)
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(q1)
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(a1)
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(entity.TagRel{
ObjectID: q1.ID,
TagID: tag.ID,
Status: entity.TagRelStatusAvailable,
})
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(q2)
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(a2)
if m.err != nil {
return
}

_, m.err = m.engine.Context(m.ctx).Insert(entity.TagRel{
ObjectID: q2.ID,
TagID: tag.ID,
Status: entity.TagRelStatusAvailable,
})
if m.err != nil {
return
}
}
14 changes: 7 additions & 7 deletions internal/repo/repo_test/tag_rel_repo_test.go
Expand Up @@ -36,13 +36,13 @@ var (
tagRelOnce sync.Once
testTagRelList = []*entity.TagRel{
{
ObjectID: "10010000000000001",
TagID: "10030000000000001",
ObjectID: "10010000000000101",
TagID: "10030000000000101",
Status: entity.TagRelStatusAvailable,
},
{
ObjectID: "10010000000000002",
TagID: "10030000000000002",
ObjectID: "10010000000000202",
TagID: "10030000000000202",
Status: entity.TagRelStatusAvailable,
},
}
Expand All @@ -68,7 +68,7 @@ func Test_tagListRepo_BatchGetObjectTagRelList(t *testing.T) {
func Test_tagListRepo_CountTagRelByTagID(t *testing.T) {
tagRelOnce.Do(addTagRelList)
tagRelRepo := tag.NewTagRelRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
count, err := tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000001")
count, err := tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000101")
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_tagListRepo_GetObjectTagRelWithoutStatus(t *testing.T) {
err = tagRelRepo.RemoveTagRelListByIDs(context.TODO(), ids)
assert.NoError(t, err)

count, err := tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000001")
count, err := tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000101")
assert.NoError(t, err)
assert.Equal(t, int64(0), count)

Expand All @@ -107,7 +107,7 @@ func Test_tagListRepo_GetObjectTagRelWithoutStatus(t *testing.T) {
err = tagRelRepo.EnableTagRelByIDs(context.TODO(), ids)
assert.NoError(t, err)

count, err = tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000001")
count, err = tagRelRepo.CountTagRelByTagID(context.TODO(), "10030000000000101")
assert.NoError(t, err)
assert.Equal(t, int64(1), count)
}