Skip to content

Commit

Permalink
feat(vote): refactor vote engine init process
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Dec 13, 2023
1 parent ad110a4 commit c3c7a3d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 77 deletions.
39 changes: 0 additions & 39 deletions dashboard/src/pages/business/workplace/mock/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +0,0 @@
import Mock from 'mockjs';
import setupMock from '@/utils/setupMock';

setupMock({
setup: () => {
// Mock.mock(new RegExp('/admin/v1/businesses'), () => {
// return [
// {
// title: '评论点赞',
// business_id: 'COMMENT_LIKE',
// type: 'SIMPLE_VOTE',
// },
// {
// title: '评论点踩',
// business_id: 'COMMENT_UNLIKE',
// type: 'SIMPLE_VOTE',
// },
// ];
// });

Mock.mock(new RegExp('/admin/v1/vote_system'), () => {
return [
{
id: 'SIMPLE_VOTE',
feature: ['vote', 'unvote','vote_count'],
qps: 100,
},
];
});

// create and update
// Mock.mock(new RegExp('/admin/v1/business'), () => {
// return {
// code: 0,
// };
// });

},
});
15 changes: 15 additions & 0 deletions internal/application/domain/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain

const (
SimpleVote string = "SIMPLE_VOTE"

MiddleVote string = "MIDDLE_VOTE"
)

type Feature struct {
Features []string
}

type Require struct {
Database map[string]int
}
2 changes: 2 additions & 0 deletions internal/application/domain/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ var (
ErrUserHasVoted = errors.New("user has voted")

ErrUserNotVoted = errors.New("user has not voted")

ErrVoteEngineNotSupport = errors.New("vote engine not support")
)
22 changes: 11 additions & 11 deletions internal/application/server/middle_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ import (
"github.com/CorrectRoadH/Likit/internal/application/domain"
)

// MiddleVoteServer is suit for middle vote server
type MiddleVoteServer struct {
// MiddleVoteEngine is suit for middle vote server
type MiddleVoteEngine struct {
}

func (m *MiddleVoteServer) Count(ctx context.Context, businessId string, messageId string) (int64, error) {
func (m *MiddleVoteEngine) Count(ctx context.Context, businessId string, messageId string) (int64, error) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) IsVoted(ctx context.Context, businessId string, messageId string, userId string) (bool, error) {
func (m *MiddleVoteEngine) IsVoted(ctx context.Context, businessId string, messageId string, userId string) (bool, error) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
func (m *MiddleVoteEngine) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
func (m *MiddleVoteEngine) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
// vote to redis
// record vote event to postgres
panic("TODO: Implement")
}

func (m *MiddleVoteServer) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error) {
func (m *MiddleVoteEngine) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) Check(ctx context.Context, config domain.Config) error {
func (m *MiddleVoteEngine) Check(ctx context.Context, config domain.Config) error {
// TODO check the whether have permission to create the table
panic("TODO: Implement")
}

func (m *MiddleVoteServer) Features(ctx context.Context) {
func (m *MiddleVoteEngine) Features(ctx context.Context) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) Init(ctx context.Context) {
func (m *MiddleVoteEngine) Init(ctx context.Context) {
panic("TODO: Implement")
}

func (m *MiddleVoteServer) Requires(ctx context.Context) {
func (m *MiddleVoteEngine) Requires(ctx context.Context) {
panic("TODO: Implement")
}
24 changes: 12 additions & 12 deletions internal/application/server/simple_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import (
"github.com/redis/go-redis/v9"
)

type SimpleVoteServer struct {
type SimpleVoteEngine struct {
rdb *redis.Client
}

func NewSimpleVoteServer(config domain.RedisConfig) in.VoteUseCase {
func NewSimpleVoteEngine(config domain.RedisConfig) in.VoteUseCase {
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port),
Password: config.Password,
DB: 0, // use default DB
})

return &SimpleVoteServer{
return &SimpleVoteEngine{
rdb: rdb,
}

}

func (v *SimpleVoteServer) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
func (v *SimpleVoteEngine) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
keyForCount := fmt.Sprintf("likit:%s:%s:count", businessId, messageId)
keyForVotedUser := fmt.Sprintf("likit:%s:%s:voted_user", businessId, messageId)

Expand All @@ -53,7 +53,7 @@ func (v *SimpleVoteServer) Vote(ctx context.Context, businessId string, messageI
return voteNum, nil
}

func (v *SimpleVoteServer) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
func (v *SimpleVoteEngine) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
keyForCount := fmt.Sprintf("likit:%s:%s:count", businessId, messageId)
keyForVotedUser := fmt.Sprintf("likit:%s:%s:voted_user", businessId, messageId)

Expand All @@ -80,7 +80,7 @@ func (v *SimpleVoteServer) UnVote(ctx context.Context, businessId string, messag
return voteNum, nil
}

func (v *SimpleVoteServer) Count(ctx context.Context, businessId string, messageId string) (int64, error) {
func (v *SimpleVoteEngine) Count(ctx context.Context, businessId string, messageId string) (int64, error) {
val, err := v.rdb.Get(ctx, fmt.Sprintf("likit:%s:%s:count", businessId, messageId)).Int64()
if err == redis.Nil {
return 0, nil
Expand All @@ -92,25 +92,25 @@ func (v *SimpleVoteServer) Count(ctx context.Context, businessId string, message
return val, nil
}

func (v *SimpleVoteServer) IsVoted(ctx context.Context, businessId string, messageId string, userId string) (bool, error) {
func (v *SimpleVoteEngine) IsVoted(ctx context.Context, businessId string, messageId string, userId string) (bool, error) {
val, err := v.rdb.SIsMember(ctx, fmt.Sprintf("likit:%s:%s:voted_user", businessId, messageId), userId).Result()
return val, err
}

func (v *SimpleVoteServer) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error) {
func (v *SimpleVoteEngine) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error) {
members, err := v.rdb.SMembers(ctx, fmt.Sprintf("likit:%s:%s:voted_user", businessId, messageId)).Result()
return members, err
}

func (s *SimpleVoteServer) Check(ctx context.Context, config domain.Config) error {
func (s *SimpleVoteEngine) Check(ctx context.Context, config domain.Config) error {
panic("TODO: Implement")
}

func (s *SimpleVoteServer) Features(ctx context.Context) {
func (s *SimpleVoteEngine) Features(ctx context.Context) {
panic("TODO: Implement")
}

func (s *SimpleVoteServer) Requires(ctx context.Context) {
func (s *SimpleVoteEngine) Requires(ctx context.Context) {
panic("TODO: Implement")
}

Expand All @@ -121,5 +121,5 @@ func NewSimpleVoteSystem(config domain.Config) (in.VoteUseCase, error) {
}

// s := NewSimpleVoteServer()
return NewSimpleVoteServer(redisConfig), nil
return NewSimpleVoteEngine(redisConfig), nil
}
42 changes: 29 additions & 13 deletions internal/application/server/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type VoteServer struct {
businessIdMapVoteSystem map[string]in.VoteUseCase
businessIdMapVoteEngine map[string]in.VoteUseCase
businessIdMapRankEngine map[string]in.RankVoteUseCase
adminUseCase in.AdminUseCase
}

Expand All @@ -23,64 +24,79 @@ func NewVoteServer(adminUseCase in.AdminUseCase) (in.VoteAdminUseCase, error) {

// create all vote system for business
for _, business := range businesses {
simpleVoteSystem, err := NewSimpleVoteSystem(business.Config)
if err != nil {
return nil, err
switch business.Type {
case domain.SimpleVote:
simpleVoteSystem, err := NewSimpleVoteSystem(business.Config)
if err != nil {
return nil, err
}
businessIdMapVoteSystem[business.Id] = simpleVoteSystem
case domain.MiddleVote:

default:
return nil, domain.ErrVoteEngineNotSupport
}
businessIdMapVoteSystem[business.Id] = simpleVoteSystem
}

return &VoteServer{
businessIdMapVoteSystem: businessIdMapVoteSystem,
businessIdMapVoteEngine: businessIdMapVoteSystem,
adminUseCase: adminUseCase,
}, nil
}

func (v *VoteServer) Vote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
voteSystem, ok := v.businessIdMapVoteSystem[businessId]
voteSystem, ok := v.businessIdMapVoteEngine[businessId]
if !ok {
return 0, domain.ErrBusinessNotExist
}
return voteSystem.Vote(ctx, businessId, messageId, userId)
}

func (v *VoteServer) UnVote(ctx context.Context, businessId string, messageId string, userId string) (int64, error) {
voteSystem, ok := v.businessIdMapVoteSystem[businessId]
voteSystem, ok := v.businessIdMapVoteEngine[businessId]
if !ok {
return 0, domain.ErrBusinessNotExist
}
return voteSystem.Vote(ctx, businessId, messageId, userId)
}

func (v *VoteServer) Count(ctx context.Context, businessId string, messageId string) (int64, error) {
voteSystem, ok := v.businessIdMapVoteSystem[businessId]
voteSystem, ok := v.businessIdMapVoteEngine[businessId]
if !ok {
return 0, domain.ErrBusinessNotExist
}
return voteSystem.Count(ctx, businessId, messageId)
}

func (v *VoteServer) IsVoted(ctx context.Context, businessId string, messageId string, userId string) (bool, error) {
voteSystem, ok := v.businessIdMapVoteSystem[businessId]
voteSystem, ok := v.businessIdMapVoteEngine[businessId]
if !ok {
return false, domain.ErrBusinessNotExist
}
return voteSystem.IsVoted(ctx, businessId, messageId, userId)
}

func (v *VoteServer) VotedUsers(ctx context.Context, businessId string, messageId string) ([]string, error) {
voteSystem, ok := v.businessIdMapVoteSystem[businessId]
voteSystem, ok := v.businessIdMapVoteEngine[businessId]
if !ok {
return nil, domain.ErrBusinessNotExist
}
return voteSystem.VotedUsers(ctx, businessId, messageId)
}

func (v *VoteServer) CreateBusiness(ctx context.Context, business domain.Business) error {
simpleVoteSystem, err := NewSimpleVoteSystem(business.Config)
var simpleVoteEngine in.VoteUseCase
var err error
switch business.Type {
case domain.SimpleVote:
simpleVoteEngine, err = NewSimpleVoteSystem(business.Config)
default:
return domain.ErrVoteEngineNotSupport
}

if err != nil {
return err
}
v.businessIdMapVoteSystem[business.Id] = simpleVoteSystem
v.businessIdMapVoteEngine[business.Id] = simpleVoteEngine
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package in

import "context"

type MiddleVoteUseCase interface {
type RankVoteUseCase interface {
Rank(ctx context.Context, businessId string, messageId string)
}
2 changes: 1 addition & 1 deletion test/vote/simple_vote_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("Simple Vote Suite", func() {
var vote_server in.VoteUseCase

BeforeEach(func() {
vote_server = server.NewSimpleVoteServer(
vote_server = server.NewSimpleVoteEngine(
config.TestEnvRedisConfig(),
)
})
Expand Down

0 comments on commit c3c7a3d

Please sign in to comment.