Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] v0.3.3

## 12 Feb 2025

### Changed

- Changed config file path for tests
- Config new field for fighters data path

## 11 Feb 2025

### Add

- CreateFightResult method

### Changed

- SearchEvents method
- SetFightResult method now called SetFightIsDone
- FightResultRequest and corresponding protobuf model
- DB init files
- Swagger doc

## 10 Feb 2025

### Added
Expand Down
4 changes: 4 additions & 0 deletions api/pickfighter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ message FightResultRequest {
int32 fightId = 1;
int32 winnerId = 2;
bool notContest = 3;
bool isCanceled = 4;
bool isDraw = 5;
string method = 6;
int32 round = 7;
}

message FightResultResponse {
Expand Down
3 changes: 3 additions & 0 deletions documentation/openapi/yaml/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ components:
cont_contest:
type: boolean
example: false
is_draw:
type: boolean
example: false
AddResultResponse:
type: object
properties:
Expand Down
8 changes: 4 additions & 4 deletions events/internal/controller/event/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"time"

"github.com/DoRightt/pickfighter-server/events/pkg/model"
eventmodel "github.com/DoRightt/pickfighter-server/events/pkg/model"
"github.com/DoRightt/pickfighter-server/events/pkg/version"
"github.com/DoRightt/pickfighter-server/pkg/pgxs"
Expand All @@ -27,7 +26,8 @@ type eventRepository interface {
TxCreateBet(ctx context.Context, tx pgx.Tx, req *eventmodel.Bet) (int32, error)
SearchBetsCount(ctx context.Context, userId int32) (int32, error)
SearchBets(ctx context.Context, userId int32) ([]*eventmodel.Bet, error)
SetFightResult(ctx context.Context, tx pgx.Tx, fr *eventmodel.FightResultRequest) error
CreateFightResult(ctx context.Context, tx pgx.Tx, req *eventmodel.FightResultRequest) error
SetFightIsDone(ctx context.Context, tx pgx.Tx, fight_id int) error
GetEventId(ctx context.Context, tx pgx.Tx, fightId int32) (int32, error)
GetUndoneFightsCount(ctx context.Context, tx pgx.Tx, eventId int32) (int, error)
SetEventDone(ctx context.Context, tx pgx.Tx, eventId int32) error
Expand All @@ -48,8 +48,8 @@ func New(repo eventRepository) *Controller {
// HealthCheck returns the current health status of the application.
// It includes information such as the app version, start time, uptime,
// and a message indicating the application's health.
func (c *Controller) HealthCheck() *model.HealthStatus {
return &model.HealthStatus{
func (c *Controller) HealthCheck() *eventmodel.HealthStatus {
return &eventmodel.HealthStatus{
AppDevVersion: version.DevVersion,
AppName: version.Name,
Timestamp: time.Now().Format(time.RFC1123),
Expand Down
1 change: 1 addition & 0 deletions events/internal/controller/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *Controller) GetEvents(ctx context.Context) (*model.EventsResponse, erro

return nil, intErr
}

if count == 0 {
intErr := internalErr.NewDefault(internalErr.EventsNoRows, 902)
return nil, intErr
Expand Down
2 changes: 0 additions & 2 deletions events/internal/controller/event/event_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ func (c *Controller) handleEventCreation(ctx context.Context, tx pgx.Tx, req *ev
EventId: eventId,
FighterRedId: f.FighterRedId,
FighterBlueId: f.FighterBlueId,
IsDone: false,
IsCanceled: false,
}

if err := c.repo.TxCreateEventFight(ctx, tx, fight); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions events/internal/controller/event/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *Controller) SetFightResult(ctx context.Context, req *model.FightResultR
return 0, intErr
}

err = c.repo.SetFightResult(ctx, tx, req)
err = c.repo.CreateFightResult(ctx, tx, req)
if err != nil {
if txErr := tx.Rollback(ctx); txErr != nil {
logs.Errorf("Unable to rollback transaction: %s", txErr)
Expand All @@ -28,12 +28,21 @@ func (c *Controller) SetFightResult(ctx context.Context, req *model.FightResultR
return 0, intErr
}

err = c.repo.SetFightIsDone(ctx, tx, int(req.FightId))
if err != nil {
if txErr := tx.Rollback(ctx); txErr != nil {
logs.Errorf("Unable to rollback transaction: %s", txErr)
}
intErr := internalErr.New(internalErr.EventsFightResult, err, 905)
return 0, intErr
}

err = c.checkEventIsDone(ctx, tx, req.FightId)
if err != nil {
if txErr := tx.Rollback(ctx); txErr != nil {
logs.Errorf("Unable to rollback transaction: %s", txErr)
}
intErr := internalErr.New(internalErr.EventIsDone, err, 905)
intErr := internalErr.New(internalErr.EventIsDone, err, 906)
return 0, intErr
}

Expand Down
10 changes: 6 additions & 4 deletions events/internal/repository/psql/db_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ func (r *Repository) SearchEvents(ctx context.Context) ([]*eventmodel.Event, err
)
SELECT
e.event_id, e.name, e.is_done AS is_event_done,
f.fight_id, f.is_done AS is_fight_done, f.not_contest,
f.created_at, f.fight_date, f.result,
f.fighter_red_id, f.fighter_blue_id
f.fight_id, f.is_done AS is_fight_done, f.created_at, f.fight_date,
f.fighter_red_id, f.fighter_blue_id, f.is_canceled
r.not_contest, r.is_draw
FROM
filtered_events e
LEFT JOIN
events.fights f ON e.event_id = f.event_id`
events.fights f ON e.event_id = f.event_id
LEFT JOIN
events.fight_results r ON f.fight_id = r.fight_id`

var events []*eventmodel.Event

Expand Down
41 changes: 33 additions & 8 deletions events/internal/repository/psql/db_fights.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
// It returns an error if the insertion fails.
func (r *Repository) TxCreateEventFight(ctx context.Context, tx pgx.Tx, f eventmodel.Fight) error {
q := `INSERT INTO
events.fights(event_id, fighter_red_id, fighter_blue_id, is_done, not_contest)
VALUES ($1, $2, $3, $4, $5)`
events.fights(event_id, fighter_red_id, fighter_blue_id)
VALUES ($1, $2, $3)`

args := []any{
f.EventId, f.FighterRedId, f.FighterBlueId, f.IsDone, f.NotContest,
f.EventId, f.FighterRedId, f.FighterBlueId,
}

if tx != nil {
Expand All @@ -32,16 +32,41 @@ func (r *Repository) TxCreateEventFight(ctx context.Context, tx pgx.Tx, f eventm
return nil
}

// SetFightResult updates the result of a fight in the 'fights' table.
// CreateFightResult creates result row in fight_results table.
// It takes a context, a transaction, and a FightResultRequest.
// It returns an error if the update fails.
func (r *Repository) SetFightResult(ctx context.Context, tx pgx.Tx, req *eventmodel.FightResultRequest) error {
func (r *Repository) CreateFightResult(ctx context.Context, tx pgx.Tx, req *eventmodel.FightResultRequest) error {
q := `INSERT INTO events.fight_results
(fight_id, winner_id, not_contest, is_draw)
VALUES ($1, $2, $3, $4);`

args := []any{
req.FightId, req.WinnerId, req.NotContest, req.IsDraw,
}

if tx != nil {
if _, err := tx.Exec(ctx, q, args...); err != nil {
return r.DebugLogSqlErr(q, err)
}
} else {
if _, err := r.GetPool().Exec(ctx, q, args...); err != nil {
return r.DebugLogSqlErr(q, err)
}
}

return nil
}

// SetFightResult set is_done field as true of a fight in the 'fights' table.
// It takes a context, a transaction, and a fight id.
// It returns an error if the update fails.
func (r *Repository) SetFightIsDone(ctx context.Context, tx pgx.Tx, fightId int) error {
q := `UPDATE events.fights
SET result = $1, not_contest = $2, is_done = true
WHERE fight_id = $3;`
SET is_done = true
WHERE fight_id = $1;`

args := []any{
req.WinnerId, req.NotContest, req.FightId,
fightId,
}

if tx != nil {
Expand Down
12 changes: 8 additions & 4 deletions events/migrations/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func InitEventsSchema(ctx context.Context, r *psql.Repository) error {
queries := [][]string{
{
// Queries to create Schema
`CREATE SCHEMA IF NOT EXISTS auth;`,
`CREATE SCHEMA IF NOT EXISTS events;`,
},
{
// Queries to create tables
Expand All @@ -34,13 +34,18 @@ func InitEventsSchema(ctx context.Context, r *psql.Repository) error {
);`,
`CREATE TABLE IF NOT EXISTS events.events (
event_id integer NOT NULL,
name character varying(255) NOT NULL
name character varying(255) NOT NULL,
is_done boolean DEFAULT false
);`,
`CREATE TABLE IF NOT EXISTS events.fight_results (
result_id integer NOT NULL,
fight_id integer,
winner_id integer,
not_contest boolean DEFAULT false
not_contest boolean DEFAULT false,
is_draw boolean DEFAULT false,
round smallint,
method VARCHAR(50),
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);`,
`CREATE TABLE IF NOT EXISTS events.fights (
fight_id integer NOT NULL,
Expand All @@ -50,7 +55,6 @@ func InitEventsSchema(ctx context.Context, r *psql.Repository) error {
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
fight_date timestamp without time zone,
is_canceled boolean DEFAULT false,
result integer DEFAULT '-1'::integer,
event_id integer
);`,
},
Expand Down
10 changes: 7 additions & 3 deletions events/pkg/model/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ type FullEventResponse struct {

// FightResultRequest represents a request for fight result with fight id, winner id and not contest flag.
type FightResultRequest struct {
FightId int32 `json:"fight_id"`
WinnerId int32 `json:"winner_id"`
NotContest bool `json:"not_contest"`
FightId int32 `json:"fight_id"`
WinnerId int32 `json:"winner_id"`
IsCanceled bool `json:"is_canceled"`
IsDraw bool `json:"is_draw"`
NotContest bool `json:"not_contest"`
Method string `json:"method"`
Round int `json:"round"`
}
9 changes: 8 additions & 1 deletion events/pkg/model/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func FightsFromProto(p []*gen.Fight) []Fight {
FighterBlueId: v.FighterBlueId,
IsDone: v.IsDone,
IsCanceled: v.IsCanceled,
Result: v.Result,
CreatedAt: v.CreatedAt,
FightDate: int(v.FightDate),
}
Expand Down Expand Up @@ -141,6 +140,10 @@ func FightResultFromProto(p *gen.FightResultRequest) *FightResultRequest {
FightId: p.FightId,
WinnerId: p.WinnerId,
NotContest: p.NotContest,
IsCanceled: p.IsCanceled,
IsDraw: p.IsDraw,
Round: int(p.Round),
Method: p.Method,
}
}

Expand All @@ -149,6 +152,10 @@ func FightResultToProto(req *FightResultRequest) *gen.FightResultRequest {
FightId: req.FightId,
WinnerId: req.WinnerId,
NotContest: req.NotContest,
IsCanceled: req.IsCanceled,
IsDraw: req.IsDraw,
Round: int32(req.Round),
Method: req.Method,
}
}

Expand Down
3 changes: 2 additions & 1 deletion fighters/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ func initTestConfig() {
env := os.Getenv("APP_ENV")

if env == "local" {
viper.SetConfigName("config")
viper.AddConfigPath("../configs")
viper.SetConfigName("config.dev")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s\n", err)
}
Expand Down
1 change: 1 addition & 0 deletions fighters/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func setConfigDefaults() {
viper.SetDefault("app.name", version.Name)
viper.SetDefault("app.version", version.DevVersion)
viper.SetDefault("app.run_date", time.Unix(version.RunDate, 0).Format(time.RFC1123))
viper.SetDefault("app.fighters_path", "../data/fighters.json")

// http server
viper.SetDefault("http.addr", "127.0.0.1")
Expand Down
4 changes: 2 additions & 2 deletions fighters/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/spf13/viper"
)

// ReadFighterData reads fighter data from a JSON file and returns a slice of model.Fighter.
// The file path is set to "../../scraper/collection/fighters.json".
func ReadFighterData() ([]model.Fighter, error) {
// TODO: tricky path
filePath := "./data/fighters.json"
filePath := viper.GetString("app.fighters_path")

jsonData, err := os.ReadFile(filePath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion fighters/internal/repository/psql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func initTestConfig() {
env := os.Getenv("APP_ENV")

if env == "local" {
viper.SetConfigName("config")
viper.SetConfigName("config.dev")
viper.AddConfigPath("../../../configs")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s\n", err)
Expand Down
Loading
Loading