Skip to content

Commit

Permalink
🍉 Add: bo
Browse files Browse the repository at this point in the history
  • Loading branch information
esh2n committed Jul 3, 2023
1 parent 7572e3d commit eb3833a
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 111 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
46 changes: 36 additions & 10 deletions application/grapevineer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ type GrapevineerService interface {
}

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 @@ -323,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 @@ -332,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 @@ -363,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 @@ -530,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,
)
24 changes: 5 additions & 19 deletions gen/dart/v1/bo.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@ import 'package:protobuf/protobuf.dart' as $pb;

class SetBoScriptRequest extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'SetBoScriptRequest', createEmptyInstance: create)
..aOS(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'boId')
..aOS(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'script')
..aOS(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'script')
..hasRequiredFields = false
;

SetBoScriptRequest._() : super();
factory SetBoScriptRequest({
$core.String? boId,
$core.String? script,
}) {
final _result = create();
if (boId != null) {
_result.boId = boId;
}
if (script != null) {
_result.script = script;
}
Expand Down Expand Up @@ -52,22 +47,13 @@ class SetBoScriptRequest extends $pb.GeneratedMessage {
static SetBoScriptRequest? _defaultInstance;

@$pb.TagNumber(1)
$core.String get boId => $_getSZ(0);
$core.String get script => $_getSZ(0);
@$pb.TagNumber(1)
set boId($core.String v) { $_setString(0, v); }
set script($core.String v) { $_setString(0, v); }
@$pb.TagNumber(1)
$core.bool hasBoId() => $_has(0);
$core.bool hasScript() => $_has(0);
@$pb.TagNumber(1)
void clearBoId() => clearField(1);

@$pb.TagNumber(2)
$core.String get script => $_getSZ(1);
@$pb.TagNumber(2)
set script($core.String v) { $_setString(1, v); }
@$pb.TagNumber(2)
$core.bool hasScript() => $_has(1);
@$pb.TagNumber(2)
void clearScript() => clearField(2);
void clearScript() => clearField(1);
}

class SetBoScriptResponse extends $pb.GeneratedMessage {
Expand Down
5 changes: 2 additions & 3 deletions gen/dart/v1/bo.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import 'dart:typed_data' as $typed_data;
const SetBoScriptRequest$json = const {
'1': 'SetBoScriptRequest',
'2': const [
const {'1': 'bo_id', '3': 1, '4': 1, '5': 9, '10': 'boId'},
const {'1': 'script', '3': 2, '4': 1, '5': 9, '10': 'script'},
const {'1': 'script', '3': 1, '4': 1, '5': 9, '10': 'script'},
],
};

/// Descriptor for `SetBoScriptRequest`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List setBoScriptRequestDescriptor = $convert.base64Decode('ChJTZXRCb1NjcmlwdFJlcXVlc3QSEwoFYm9faWQYASABKAlSBGJvSWQSFgoGc2NyaXB0GAIgASgJUgZzY3JpcHQ=');
final $typed_data.Uint8List setBoScriptRequestDescriptor = $convert.base64Decode('ChJTZXRCb1NjcmlwdFJlcXVlc3QSFgoGc2NyaXB0GAEgASgJUgZzY3JpcHQ=');
@$core.Deprecated('Use setBoScriptResponseDescriptor instead')
const SetBoScriptResponse$json = const {
'1': 'SetBoScriptResponse',
Expand Down
44 changes: 17 additions & 27 deletions gen/go/v1/bo.pb.go

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

6 changes: 0 additions & 6 deletions gen/openapi/api_definition.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
}
},
"parameters": [
{
"name": "boId",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "script",
"in": "query",
Expand Down
4 changes: 0 additions & 4 deletions gen/ts/v1/bo_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import * as jspb from "google-protobuf";

export class SetBoScriptRequest extends jspb.Message {
getBoId(): string;
setBoId(value: string): void;

getScript(): string;
setScript(value: string): void;

Expand All @@ -22,7 +19,6 @@ export class SetBoScriptRequest extends jspb.Message {

export namespace SetBoScriptRequest {
export type AsObject = {
boId: string,
script: string,
}
}
Expand Down
Loading

0 comments on commit eb3833a

Please sign in to comment.