Skip to content

Commit

Permalink
refact: init refactoring project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hiago-balbino committed Sep 26, 2023
1 parent 96ae48a commit 7f2f201
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 101 deletions.
15 changes: 0 additions & 15 deletions cmd/api.go

This file was deleted.

12 changes: 12 additions & 0 deletions cmd/luck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hiago-balbino/random-luck/config"
handler "github.com/hiago-balbino/random-luck/internal/handler/http"
)

func main() {
config.InitConfigurations()
server := handler.NewServer(handler.API)
server.Start()
}
20 changes: 0 additions & 20 deletions cmd/root.go

This file was deleted.

15 changes: 0 additions & 15 deletions cmd/web.go

This file was deleted.

12 changes: 12 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hiago-balbino/random-luck/config"
handler "github.com/hiago-balbino/random-luck/internal/handler/http"
)

func main() {
config.InitConfigurations()
server := handler.NewServer(handler.WEB)
server.Start()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configuration
package config

import "github.com/spf13/viper"

Expand Down
13 changes: 0 additions & 13 deletions internal/core/service/game_randomizer.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/core/domain/game.go → internal/game/game.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain
package game

// Game is a struct to represent the randomly created game model.
type Game struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package service
package game

import (
"context"
"crypto/rand"
"math/big"
"sort"

"github.com/hiago-balbino/random-luck/internal/core/domain"
"github.com/hiago-balbino/random-luck/internal/core/errors"
"github.com/hiago-balbino/random-luck/internal/pkg/apperrors"
"github.com/hiago-balbino/random-luck/internal/pkg/logger"
"go.uber.org/zap/zapcore"
)

// GameRandomizer is an interface that handles functions to randomize data to create games.
type GameRandomizer interface {
// Randomize is an function for randomizing luck numbers to create games.
Randomize(ctx context.Context, amountOfGames, amountOfNumbersPerGame int) ([]Game, error)
}

const (
minAmountOfGamesAllowed = 1
minAmountOfNumbersPerGame = 6
Expand All @@ -31,14 +35,14 @@ func NewGameRandomizer() GameRandomize {
}

// Randomize is an function for randomizing luck numbers to create games.
func (g GameRandomize) Randomize(_ context.Context, amountOfGames, amountOfNumbersPerGame int) ([]domain.Game, error) {
func (g GameRandomize) Randomize(_ context.Context, amountOfGames, amountOfNumbersPerGame int) ([]Game, error) {
if err := g.validateParameters(amountOfGames, amountOfNumbersPerGame); err != nil {
log.Error("validate parameters", zapcore.Field{Type: zapcore.StringType, String: err.Error()})
log.Error("validate parameters", logger.FieldError(err))

return nil, err
}

games := make([]domain.Game, 0)
games := make([]Game, 0)
for i := minAmountOfGamesAllowed; i <= amountOfGames; i++ {
numbers := make([]int, 0)

Expand All @@ -48,7 +52,7 @@ func (g GameRandomize) Randomize(_ context.Context, amountOfGames, amountOfNumbe
}

sort.Ints(numbers)
games = append(games, domain.Game{ID: i, Numbers: numbers})
games = append(games, Game{ID: i, Numbers: numbers})
}

return games, nil
Expand All @@ -58,11 +62,11 @@ func (g GameRandomize) Randomize(_ context.Context, amountOfGames, amountOfNumbe
func (g GameRandomize) validateParameters(amountOfGames, amountOfNumbersPerGame int) error {
switch {
case amountOfGames < minAmountOfGamesAllowed:
return errors.ErrMinAmountOfGames
return apperrors.ErrMinAmountOfGames
case amountOfNumbersPerGame < minAmountOfNumbersPerGame:
return errors.ErrMinAmountOfNumbersPerGame
return apperrors.ErrMinAmountOfNumbersPerGame
case amountOfNumbersPerGame > maxAmountOfNumbersPerGame:
return errors.ErrMaxAmountOfNumbersPerGame
return apperrors.ErrMaxAmountOfNumbersPerGame
default:
return nil
}
Expand All @@ -72,7 +76,7 @@ func (g GameRandomize) validateParameters(amountOfGames, amountOfNumbersPerGame
func (g GameRandomize) generateNewNumber(numbersAlreadyGenerated []int) int {
randomNumber, err := rand.Int(rand.Reader, big.NewInt(maxNumberPerGame-minNumberPerGame))
if err != nil {
log.Error("generate new randomNumber", zapcore.Field{Type: zapcore.StringType, String: err.Error()})
log.Error("generate new randomNumber", logger.FieldError(err))

return g.generateNewNumber(numbersAlreadyGenerated)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package service
package game

import (
"context"
"testing"

"github.com/hiago-balbino/random-luck/internal/core/domain"
"github.com/hiago-balbino/random-luck/internal/core/errors"
"github.com/hiago-balbino/random-luck/internal/pkg/apperrors"
"github.com/stretchr/testify/assert"
)

Expand All @@ -26,7 +25,7 @@ func TestRandomize(t *testing.T) {
games, err := randomizer.Randomize(ctx, amountOfGames, amountOfNumbersPerGame)

assert.Empty(t, games)
assert.EqualError(t, err, errors.ErrMinAmountOfGames.Error())
assert.EqualError(t, err, apperrors.ErrMinAmountOfGames.Error())
},
},
{
Expand All @@ -39,7 +38,7 @@ func TestRandomize(t *testing.T) {
games, err := randomizer.Randomize(ctx, amountOfGames, amountOfNumbersPerGame)

assert.Empty(t, games)
assert.EqualError(t, err, errors.ErrMinAmountOfNumbersPerGame.Error())
assert.EqualError(t, err, apperrors.ErrMinAmountOfNumbersPerGame.Error())
},
},
{
Expand All @@ -52,7 +51,7 @@ func TestRandomize(t *testing.T) {
games, err := randomizer.Randomize(ctx, amountOfGames, amountOfNumbersPerGame)

assert.Empty(t, games)
assert.EqualError(t, err, errors.ErrMaxAmountOfNumbersPerGame.Error())
assert.EqualError(t, err, apperrors.ErrMaxAmountOfNumbersPerGame.Error())
},
},
{
Expand Down Expand Up @@ -90,7 +89,7 @@ func TestRandomize(t *testing.T) {
}
}

func assertGameNumbers(t *testing.T, games []domain.Game) {
func assertGameNumbers(t *testing.T, games []Game) {
for _, game := range games {
for _, number := range game.Numbers {
if number < minNumberPerGame || number > maxNumberPerGame {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errors
package apperrors

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ func getLogLevel() zapcore.Level {
return zap.ErrorLevel
}
}

// FieldError is a function to return a zapcore.Field from an error.
func FieldError(err error) zapcore.Field {
return zapcore.Field{Type: zapcore.StringType, String: err.Error()}
}
16 changes: 0 additions & 16 deletions main.go

This file was deleted.

0 comments on commit 7f2f201

Please sign in to comment.