Skip to content

Commit

Permalink
City, orders, jobs linked to game to be registered with the right bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaysoro committed Nov 29, 2024
1 parent 6abf804 commit d4f206e
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 44 deletions.
18 changes: 11 additions & 7 deletions models/entities/books.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package entities

import amqp "github.com/kaellybot/kaelly-amqp"

type JobBook struct {
UserID string `gorm:"primaryKey;type:varchar(100);"`
JobID string `gorm:"primaryKey;type:varchar(100);"`
ServerID string `gorm:"primaryKey;type:varchar(100);"`
UserID string `gorm:"primaryKey;type:varchar(100);"`
JobID string `gorm:"primaryKey;type:varchar(100);"`
ServerID string `gorm:"primaryKey;type:varchar(100);"`
Game amqp.Game `gorm:"primaryKey"`
Level int64
Job Job `gorm:"foreignKey:JobID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Server Server `gorm:"foreignKey:ServerID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

type AlignmentBook struct {
UserID string `gorm:"primaryKey;type:varchar(100);"`
CityID string `gorm:"primaryKey;type:varchar(100);"`
OrderID string `gorm:"primaryKey;type:varchar(100);"`
ServerID string `gorm:"primaryKey;type:varchar(100);"`
UserID string `gorm:"primaryKey;type:varchar(100);"`
CityID string `gorm:"primaryKey;type:varchar(100);"`
OrderID string `gorm:"primaryKey;type:varchar(100);"`
ServerID string `gorm:"primaryKey;type:varchar(100);"`
Game amqp.Game `gorm:"primaryKey"`
Level int64
City City `gorm:"foreignKey:CityID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Order Order `gorm:"foreignKey:OrderID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Expand Down
5 changes: 4 additions & 1 deletion models/entities/cities.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package entities

import amqp "github.com/kaellybot/kaelly-amqp"

type City struct {
ID string `gorm:"primaryKey;type:varchar(100)"`
ID string `gorm:"primaryKey;type:varchar(100)"`
Game amqp.Game `gorm:"primaryKey"`
}
5 changes: 4 additions & 1 deletion models/entities/jobs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package entities

import amqp "github.com/kaellybot/kaelly-amqp"

type Job struct {
ID string `gorm:"primaryKey;type:varchar(100)"`
ID string `gorm:"primaryKey;type:varchar(100)"`
Game amqp.Game `gorm:"primaryKey"`
}
5 changes: 4 additions & 1 deletion models/entities/orders.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package entities

import amqp "github.com/kaellybot/kaelly-amqp"

type Order struct {
ID string `gorm:"primaryKey;type:varchar(100)"`
ID string `gorm:"primaryKey;type:varchar(100)"`
Game amqp.Game `gorm:"primaryKey"`
}
9 changes: 5 additions & 4 deletions repositories/alignments/alignments.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alignments

import (
amqp "github.com/kaellybot/kaelly-amqp"
"github.com/kaellybot/kaelly-books/models/entities"
"github.com/kaellybot/kaelly-books/utils/databases"
)
Expand All @@ -10,11 +11,11 @@ func New(db databases.MySQLConnection) *Impl {
}

func (repo *Impl) GetBooks(cityID, orderID, serverID string, userIDs []string,
offset, limit int) ([]entities.AlignmentBook, int64, error) {
game amqp.Game, offset, limit int) ([]entities.AlignmentBook, int64, error) {
var total int64
var alignBooks []entities.AlignmentBook
baseQuery := repo.db.GetDB().
Where("server_id = ? AND user_id IN (?)", serverID, userIDs)
Where("server_id = ? AND game = ? AND user_id IN (?)", serverID, game, userIDs)

if len(cityID) > 0 {
baseQuery = baseQuery.Where("city_id = ?", cityID)
Expand All @@ -36,10 +37,10 @@ func (repo *Impl) GetBooks(cityID, orderID, serverID string, userIDs []string,
Find(&alignBooks).Error
}

func (repo *Impl) GetUserBook(userID, serverID string) ([]entities.AlignmentBook, error) {
func (repo *Impl) GetUserBook(userID, serverID string, game amqp.Game) ([]entities.AlignmentBook, error) {
var alignBooks []entities.AlignmentBook
return alignBooks, repo.db.GetDB().
Where("user_id = ? AND server_id = ?", userID, serverID).
Where("user_id = ? AND server_id = ? AND game = ?", userID, serverID, game).
Find(&alignBooks).Error
}

Expand Down
5 changes: 3 additions & 2 deletions repositories/alignments/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package alignments

import (
amqp "github.com/kaellybot/kaelly-amqp"
"github.com/kaellybot/kaelly-books/models/entities"
"github.com/kaellybot/kaelly-books/utils/databases"
)

type Repository interface {
GetBooks(cityID, orderID, serverID string, userIDs []string,
offset, limit int) ([]entities.AlignmentBook, int64, error)
GetUserBook(userID, serverID string) ([]entities.AlignmentBook, error)
game amqp.Game, offset, limit int) ([]entities.AlignmentBook, int64, error)
GetUserBook(userID, serverID string, game amqp.Game) ([]entities.AlignmentBook, error)
SaveUserBook(alignBook entities.AlignmentBook) error
DeleteUserBook(alignBook entities.AlignmentBook) error
}
Expand Down
9 changes: 5 additions & 4 deletions repositories/jobs/jobs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jobs

import (
amqp "github.com/kaellybot/kaelly-amqp"
"github.com/kaellybot/kaelly-books/models/entities"
"github.com/kaellybot/kaelly-books/utils/databases"
)
Expand All @@ -10,11 +11,11 @@ func New(db databases.MySQLConnection) *Impl {
}

func (repo *Impl) GetBooks(jobID, serverID string, userIDs []string,
offset, limit int) ([]entities.JobBook, int64, error) {
game amqp.Game, offset, limit int) ([]entities.JobBook, int64, error) {
var total int64
var jobBooks []entities.JobBook
baseQuery := repo.db.GetDB().
Where("job_id = ? AND server_id = ? AND user_id IN (?)", jobID, serverID, userIDs)
Where("job_id = ? AND server_id = ? AND game = ? AND user_id IN (?)", jobID, serverID, game, userIDs)

if errTotal := baseQuery.Model(&entities.JobBook{}).
Count(&total).Error; errTotal != nil {
Expand All @@ -28,10 +29,10 @@ func (repo *Impl) GetBooks(jobID, serverID string, userIDs []string,
Find(&jobBooks).Error
}

func (repo *Impl) GetUserBook(userID, serverID string) ([]entities.JobBook, error) {
func (repo *Impl) GetUserBook(userID, serverID string, game amqp.Game) ([]entities.JobBook, error) {
var jobBooks []entities.JobBook
return jobBooks, repo.db.GetDB().
Where("user_id = ? AND server_id = ?", userID, serverID).
Where("user_id = ? AND server_id = ? AND game = ?", userID, serverID, game).
Find(&jobBooks).Error
}

Expand Down
5 changes: 3 additions & 2 deletions repositories/jobs/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package jobs

import (
amqp "github.com/kaellybot/kaelly-amqp"
"github.com/kaellybot/kaelly-books/models/entities"
"github.com/kaellybot/kaelly-books/utils/databases"
)

type Repository interface {
GetBooks(jobID, serverID string, userIDs []string,
offset, limit int) ([]entities.JobBook, int64, error)
GetUserBook(userID, serverID string) ([]entities.JobBook, error)
game amqp.Game, offset, limit int) ([]entities.JobBook, int64, error)
GetUserBook(userID, serverID string, game amqp.Game) ([]entities.JobBook, error)
SaveUserBook(jobBook entities.JobBook) error
DeleteUserBook(jobBook entities.JobBook) error
}
Expand Down
4 changes: 2 additions & 2 deletions services/alignments/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (service *Impl) GetBookRequest(ctx amqp.Context, request *amqp.AlignGetBookRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidAlignGetRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_ALIGN_GET_BOOK_ANSWER, lg)
return
Expand All @@ -22,7 +22,7 @@ func (service *Impl) GetBookRequest(ctx amqp.Context, request *amqp.AlignGetBook
Msgf("Get align books request received")

books, total, err := service.alignBookRepo.GetBooks(request.GetCityId(), request.GetOrderId(),
request.GetServerId(), request.GetUserIds(), int(request.GetOffset()), int(request.GetSize()))
request.GetServerId(), request.GetUserIds(), game, int(request.GetOffset()), int(request.GetSize()))
if err != nil {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_ALIGN_GET_BOOK_ANSWER, lg)
return
Expand Down
3 changes: 2 additions & 1 deletion services/alignments/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (service *Impl) SetRequest(ctx amqp.Context, request *amqp.AlignSetRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidAlignSetRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_ALIGN_SET_ANSWER, lg)
return
Expand All @@ -28,6 +28,7 @@ func (service *Impl) SetRequest(ctx amqp.Context, request *amqp.AlignSetRequest,
CityID: request.CityId,
OrderID: request.OrderId,
ServerID: request.ServerId,
Game: game,
Level: request.Level,
}

Expand Down
6 changes: 3 additions & 3 deletions services/alignments/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type Service interface {
GetBookRequest(ctx amqp.Context, request *amqp.AlignGetBookRequest, lg amqp.Language)
SetRequest(ctx amqp.Context, request *amqp.AlignSetRequest, lg amqp.Language)
UserRequest(ctx amqp.Context, request *amqp.AlignGetUserRequest, lg amqp.Language)
GetBookRequest(ctx amqp.Context, request *amqp.AlignGetBookRequest, game amqp.Game, lg amqp.Language)
SetRequest(ctx amqp.Context, request *amqp.AlignSetRequest, game amqp.Game, lg amqp.Language)
UserRequest(ctx amqp.Context, request *amqp.AlignGetUserRequest, game amqp.Game, lg amqp.Language)
}

type Impl struct {
Expand Down
4 changes: 2 additions & 2 deletions services/alignments/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (service *Impl) UserRequest(ctx amqp.Context, request *amqp.AlignGetUserRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidAlignGetUserRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_ALIGN_GET_USER_ANSWER, lg)
return
Expand All @@ -20,7 +20,7 @@ func (service *Impl) UserRequest(ctx amqp.Context, request *amqp.AlignGetUserReq
Str(constants.LogServerID, request.ServerId).
Msgf("Get job user request received")

books, err := service.alignBookRepo.GetUserBook(request.UserId, request.ServerId)
books, err := service.alignBookRepo.GetUserBook(request.UserId, request.ServerId, game)
if err != nil {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_ALIGN_GET_USER_ANSWER, lg)
return
Expand Down
12 changes: 6 additions & 6 deletions services/books/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func (service *Impl) consume(ctx amqp.Context, message *amqp.RabbitMQMessage) {
//exhaustive:ignore Don't need to be exhaustive here since they will be handled by default case
switch message.Type {
case amqp.RabbitMQMessage_JOB_GET_BOOK_REQUEST:
service.jobService.GetBookRequest(ctx, message.JobGetBookRequest, message.Language)
service.jobService.GetBookRequest(ctx, message.JobGetBookRequest, message.Game, message.Language)
case amqp.RabbitMQMessage_JOB_GET_USER_REQUEST:
service.jobService.UserRequest(ctx, message.JobGetUserRequest, message.Language)
service.jobService.UserRequest(ctx, message.JobGetUserRequest, message.Game, message.Language)
case amqp.RabbitMQMessage_JOB_SET_REQUEST:
service.jobService.SetRequest(ctx, message.JobSetRequest, message.Language)
service.jobService.SetRequest(ctx, message.JobSetRequest, message.Game, message.Language)
case amqp.RabbitMQMessage_ALIGN_GET_BOOK_REQUEST:
service.alignService.GetBookRequest(ctx, message.AlignGetBookRequest, message.Language)
service.alignService.GetBookRequest(ctx, message.AlignGetBookRequest, message.Game, message.Language)
case amqp.RabbitMQMessage_ALIGN_GET_USER_REQUEST:
service.alignService.UserRequest(ctx, message.AlignGetUserRequest, message.Language)
service.alignService.UserRequest(ctx, message.AlignGetUserRequest, message.Game, message.Language)
case amqp.RabbitMQMessage_ALIGN_SET_REQUEST:
service.alignService.SetRequest(ctx, message.AlignSetRequest, message.Language)
service.alignService.SetRequest(ctx, message.AlignSetRequest, message.Game, message.Language)
default:
log.Warn().
Str(constants.LogCorrelationID, ctx.CorrelationID).
Expand Down
4 changes: 2 additions & 2 deletions services/jobs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (service *Impl) GetBookRequest(ctx amqp.Context, request *amqp.JobGetBookRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidJobGetRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_JOB_GET_BOOK_ANSWER, lg)
return
Expand All @@ -21,7 +21,7 @@ func (service *Impl) GetBookRequest(ctx amqp.Context, request *amqp.JobGetBookRe
Msgf("Get job books request received")

books, total, err := service.jobBookRepo.GetBooks(request.GetJobId(), request.GetServerId(),
request.GetUserIds(), int(request.GetOffset()), int(request.GetSize()))
request.GetUserIds(), game, int(request.GetOffset()), int(request.GetSize()))
if err != nil {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_JOB_GET_BOOK_ANSWER, lg)
return
Expand Down
3 changes: 2 additions & 1 deletion services/jobs/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (service *Impl) SetRequest(ctx amqp.Context, request *amqp.JobSetRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidJobSetRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_JOB_SET_ANSWER, lg)
return
Expand All @@ -26,6 +26,7 @@ func (service *Impl) SetRequest(ctx amqp.Context, request *amqp.JobSetRequest,
UserID: request.UserId,
JobID: request.JobId,
ServerID: request.ServerId,
Game: game,
Level: request.Level,
}

Expand Down
6 changes: 3 additions & 3 deletions services/jobs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type Service interface {
GetBookRequest(ctx amqp.Context, request *amqp.JobGetBookRequest, lg amqp.Language)
SetRequest(ctx amqp.Context, request *amqp.JobSetRequest, lg amqp.Language)
UserRequest(ctx amqp.Context, request *amqp.JobGetUserRequest, lg amqp.Language)
GetBookRequest(ctx amqp.Context, request *amqp.JobGetBookRequest, game amqp.Game, lg amqp.Language)
SetRequest(ctx amqp.Context, request *amqp.JobSetRequest, game amqp.Game, lg amqp.Language)
UserRequest(ctx amqp.Context, request *amqp.JobGetUserRequest, game amqp.Game, lg amqp.Language)
}

type Impl struct {
Expand Down
4 changes: 2 additions & 2 deletions services/jobs/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (service *Impl) UserRequest(ctx amqp.Context, request *amqp.JobGetUserRequest,
lg amqp.Language) {
game amqp.Game, lg amqp.Language) {
if !isValidJobGetUserRequest(request) {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_JOB_GET_USER_ANSWER, lg)
return
Expand All @@ -20,7 +20,7 @@ func (service *Impl) UserRequest(ctx amqp.Context, request *amqp.JobGetUserReque
Str(constants.LogServerID, request.ServerId).
Msgf("Get job user request received")

books, err := service.jobBookRepo.GetUserBook(request.UserId, request.ServerId)
books, err := service.jobBookRepo.GetUserBook(request.UserId, request.ServerId, game)
if err != nil {
replies.FailedAnswer(ctx, service.broker, amqp.RabbitMQMessage_JOB_GET_USER_ANSWER, lg)
return
Expand Down

0 comments on commit d4f206e

Please sign in to comment.