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 3 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
145 changes: 145 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,143 @@ func (m *Mentor) initSiteInfoWrite() {
Status: 1,
})
}

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

tag_id, err := uniqueIDRepo.GenUniqueIDStr(m.ctx, entity.Tag{}.TableName())
zahash marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
m.err = err
return
}

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

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

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

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

tag := entity.Tag{
ID: tag_id,
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: q1_id,
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>",
Pin: entity.QuestionPin,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The questions don't need to be pinned to the top.

Show: entity.QuestionShow,
Status: entity.QuestionStatusAvailable,
AnswerCount: 1,
AcceptedAnswerID: a1_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer corresponding to the question does not need to be accepted.

}

a1 := &entity.Answer{
ID: a1_id,
CreatedAt: now,
QuestionID: q1_id,
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,
Accepted: schema.AnswerAcceptedEnable,
}

q2 := &entity.Question{
ID: q2_id,
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>",
Pin: entity.QuestionPin,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The questions don't need to be pinned to the top.

Show: entity.QuestionShow,
Status: entity.QuestionStatusAvailable,
AnswerCount: 1,
AcceptedAnswerID: a2_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer corresponding to the question does not need to be accepted.

}

a2 := &entity.Answer{
ID: a2_id,
CreatedAt: now,
QuestionID: q2_id,
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,
Accepted: schema.AnswerAcceptedEnable,
}

_, 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{
CreatedAt: now,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like below, no need to set the time here.

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)
}