Skip to content

Commit

Permalink
Merge pull request #5 from esh2n/feat/bo
Browse files Browse the repository at this point in the history
Feat/bo
  • Loading branch information
esh2n authored Jul 3, 2023
2 parents 58c8b77 + eb3833a commit 8d1c0e6
Show file tree
Hide file tree
Showing 57 changed files with 5,311 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: gen-proto

BIN := $(abspath ./bin)
TAG := v1.0.0
TAG := v1.1.1

go-build:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o grapevineer-grpc cmd/grapevineer-grpc/main.go cmd/grapevineer-grpc/wire_gen.go
Expand Down
50 changes: 40 additions & 10 deletions application/grapevineer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ type GrapevineerService interface {
UpdatePlayer(ctx context.Context, req *grapevineer.UpdatePlayerRequest) (*grapevineer.UpdatePlayerResponse, error)
GetPlayerInfo(ctx context.Context, req *grapevineer.GetPlayerInfoRequest) (*grapevineer.GetPlayerInfoResponse, error)

// bo
SetBoScript(ctx context.Context, req *grapevineer.SetBoScriptRequest) (*grapevineer.SetBoScriptResponse, error)
GetBoScriptRandomly(ctx context.Context, req *grapevineer.GetBoScriptRandomlyRequest) (*grapevineer.GetBoScriptRandomlyResponse, error)

// voicevox
GetWavFromText(ctx context.Context, req *grapevineer.GetWavFromTextRequest) (*grapevineer.GetWavFromTextResponse, error)
}

type grapevineerService struct {
cfg config.Config
db *database.Database
repository repository.Repository
cfg config.Config
db *database.Database
playerRepository repository.PlayerRepository
boRepository repository.BoRepository
}

func NewGrapevineerService(cfg config.Config, db *database.Database, repository repository.Repository) GrapevineerService {
func NewGrapevineerService(cfg config.Config, db *database.Database, playerRepository repository.PlayerRepository, boRepository repository.BoRepository) GrapevineerService {
return &grapevineerService{
cfg: cfg,
db: db,
repository: repository,
cfg: cfg,
db: db,
playerRepository: playerRepository,
boRepository: boRepository,
}
}

Expand Down Expand Up @@ -319,7 +325,7 @@ func (s *grapevineerService) SetPlayer(ctx context.Context, req *grapevineer.Set
Name: req.Name,
Region: req.Region,
}
if _, err := s.repository.CreatePlayer(ctx, s.db, player); err != nil {
if _, err := s.playerRepository.CreatePlayer(ctx, s.db, player); err != nil {
return nil, status.Errorf(codes.Internal, "failed to create player: %v", err)
}
return &grapevineer.SetPlayerResponse{
Expand All @@ -328,7 +334,7 @@ func (s *grapevineerService) SetPlayer(ctx context.Context, req *grapevineer.Set
}

func (s *grapevineerService) GetAllPlayers(ctx context.Context, req *grapevineer.GetAllPlayersRequest) (*grapevineer.GetAllPlayersResponse, error) {
players, err := s.repository.FindAll(ctx, s.db)
players, err := s.playerRepository.FindAll(ctx, s.db)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get all players: %v", err)
}
Expand Down Expand Up @@ -359,7 +365,7 @@ func (s *grapevineerService) UpdatePlayer(ctx context.Context, req *grapevineer.
Name: req.Name,
Region: req.Region,
}
if _, err := s.repository.UpdatePlayer(ctx, s.db, player); err != nil {
if _, err := s.playerRepository.UpdatePlayer(ctx, s.db, player); err != nil {
return nil, status.Errorf(codes.Internal, "failed to update player: %v", err)
}
return &grapevineer.UpdatePlayerResponse{
Expand Down Expand Up @@ -526,3 +532,27 @@ func (s *grapevineerService) GetWavFromText(ctx context.Context, req *grapevinee

return response, nil
}

func (s *grapevineerService) SetBoScript(ctx context.Context, req *grapevineer.SetBoScriptRequest) (*grapevineer.SetBoScriptResponse, error) {
bo := &model.Bo{
ID: uuid.New().String(),
Script: req.Script,
}
if _, err := s.boRepository.CreateBo(ctx, s.db, bo); err != nil {
return nil, status.Errorf(codes.Internal, "failed to create bo: %v", err)
}
return &grapevineer.SetBoScriptResponse{
Status: 200,
}, nil
}

func (s *grapevineerService) GetBoScriptRandomly(ctx context.Context, req *grapevineer.GetBoScriptRandomlyRequest) (*grapevineer.GetBoScriptRandomlyResponse, error) {
bo, err := s.boRepository.FindOneRandomly(ctx, s.db)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get bo: %v", err)
}
return &grapevineer.GetBoScriptRandomlyResponse{
BoId: bo.ID,
Script: bo.Script,
}, nil
}
5 changes: 3 additions & 2 deletions cmd/grapevineer-grpc/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions domain/model/bo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type Bo struct {
ID string
Script string
}
55 changes: 55 additions & 0 deletions domain/repository/bo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package repository

import (
"context"
"errors"
"esh2n/grapevineer/domain/model"
"esh2n/grapevineer/internal/database"
"math/rand"
"time"
)

type BoRepository interface {
CreateBo(ctx context.Context, db *database.Database, bo *model.Bo) (*model.Bo, error)
FindOneRandomly(ctx context.Context, db *database.Database) (*model.Bo, error)
}

type boRepositoryImpl struct{}

func (r *boRepositoryImpl) CreateBo(ctx context.Context, db *database.Database, bo *model.Bo) (*model.Bo, error) {
q := db.Client.Bo.
Create().
SetID(bo.ID).
SetScript(bo.Script)

x, err := q.Save(ctx)
if err != nil {
return nil, err
}
return &model.Bo{
ID: x.ID,
Script: x.Script,
}, nil
}

func (r *boRepositoryImpl) FindOneRandomly(ctx context.Context, db *database.Database) (*model.Bo, error) {
bos, err := db.Client.Bo.Query().All(ctx)
if err != nil {
return nil, err
}

if len(bos) == 0 {
return nil, errors.New("No records found in the database")
}

rand.Seed(time.Now().UnixNano()) // ensure truly random number
randomIndex := rand.Intn(len(bos))

return &model.Bo{
ID: bos[randomIndex].ID,
Script: bos[randomIndex].Script,
}, nil
}
func NewBoRepository() BoRepository {
return &boRepositoryImpl{}
}
4 changes: 2 additions & 2 deletions domain/repository/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"esh2n/grapevineer/internal/database"
)

type Repository interface {
type PlayerRepository interface {
CreatePlayer(ctx context.Context, db *database.Database, player *model.Player) (*model.Player, error)
DeletePlayer(ctx context.Context, db *database.Database, id string) error
UpdatePlayer(ctx context.Context, db *database.Database, player *model.Player) (*model.Player, error)
Expand Down Expand Up @@ -76,6 +76,6 @@ func (a *playerRepositoryImpl) FindAll(ctx context.Context, db *database.Databas
return result, nil
}

func NewPlayerRepository() Repository {
func NewPlayerRepository() PlayerRepository {
return &playerRepositoryImpl{}
}
1 change: 1 addition & 0 deletions domain/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (

var Set = wire.NewSet(
NewPlayerRepository,
NewBoRepository,
)
101 changes: 101 additions & 0 deletions ent/bo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions ent/bo/bo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8d1c0e6

Please sign in to comment.