Модели amqp событий перенесены непосредственно в компонент amqp worker

This commit is contained in:
ErickSkrauch 2017-09-03 21:41:40 +03:00
parent 76a3f3ad26
commit 9cb6502f9c
3 changed files with 28 additions and 29 deletions

View File

@ -1,20 +0,0 @@
package model
type UsernameChanged struct {
AccountId int `json:"accountId"`
OldUsername string `json:"oldUsername"`
NewUsername string `json:"newUsername"`
}
type SkinChanged struct {
AccountId int `json:"userId"`
Uuid string `json:"uuid"`
SkinId int `json:"skinId"`
OldSkinId int `json:"oldSkinId"`
Hash string `json:"hash"`
Is1_8 bool `json:"is1_8"`
IsSlim bool `json:"isSlim"`
Url string `json:"url"`
MojangTextures string `json:"mojangTextures"`
MojangSignature string `json:"mojangSignature"`
}

View File

@ -30,7 +30,7 @@ func TestServices_HandleChangeUsername(t *testing.T) {
skinRepo.EXPECT().Save(resultModel) skinRepo.EXPECT().Save(resultModel)
wd.EXPECT().IncCounter("worker.change_username", int64(1)) wd.EXPECT().IncCounter("worker.change_username", int64(1))
assert.True(services.HandleChangeUsername(&model.UsernameChanged{ assert.True(services.HandleChangeUsername(&UsernameChanged{
AccountId: 1, AccountId: 1,
OldUsername: "mock_user", OldUsername: "mock_user",
NewUsername: "new_username", NewUsername: "new_username",
@ -42,7 +42,7 @@ func TestServices_HandleChangeUsername(t *testing.T) {
wd.EXPECT().IncCounter("worker.change_username", int64(1)) wd.EXPECT().IncCounter("worker.change_username", int64(1))
wd.EXPECT().IncCounter("worker.change_username_empty_old_username", int64(1)) wd.EXPECT().IncCounter("worker.change_username_empty_old_username", int64(1))
assert.True(services.HandleChangeUsername(&model.UsernameChanged{ assert.True(services.HandleChangeUsername(&UsernameChanged{
AccountId: 1, AccountId: 1,
OldUsername: "", OldUsername: "",
NewUsername: "new_mock", NewUsername: "new_mock",
@ -55,7 +55,7 @@ func TestServices_HandleChangeUsername(t *testing.T) {
wd.EXPECT().IncCounter("worker.change_username_id_not_found", int64(1)) wd.EXPECT().IncCounter("worker.change_username_id_not_found", int64(1))
wd.EXPECT().Info("Cannot find user id :accountId. Trying to search.", gomock.Any()) wd.EXPECT().Info("Cannot find user id :accountId. Trying to search.", gomock.Any())
assert.True(services.HandleChangeUsername(&model.UsernameChanged{ assert.True(services.HandleChangeUsername(&UsernameChanged{
AccountId: 1, AccountId: 1,
OldUsername: "mock_user", OldUsername: "mock_user",
NewUsername: "new_mock2", NewUsername: "new_mock2",
@ -69,7 +69,7 @@ func TestServices_HandleChangeUsername(t *testing.T) {
wd.EXPECT().Info("Cannot find user id :accountId. Trying to search.", gomock.Any()) wd.EXPECT().Info("Cannot find user id :accountId. Trying to search.", gomock.Any())
wd.EXPECT().Error("Unknown error when requesting a skin from the repository: :err", gomock.Any()) wd.EXPECT().Error("Unknown error when requesting a skin from the repository: :err", gomock.Any())
assert.True(services.HandleChangeUsername(&model.UsernameChanged{ assert.True(services.HandleChangeUsername(&UsernameChanged{
AccountId: 1, AccountId: 1,
OldUsername: "mock_user", OldUsername: "mock_user",
NewUsername: "new_mock2", NewUsername: "new_mock2",
@ -84,7 +84,7 @@ func TestServices_HandleSkinChanged(t *testing.T) {
services, skinRepo, accountsAPI, wd := setupMocks(ctrl) services, skinRepo, accountsAPI, wd := setupMocks(ctrl)
event := &model.SkinChanged{ event := &SkinChanged{
AccountId: 1, AccountId: 1,
Uuid: "cdb907ce-84f4-4c38-801d-1e287dca2623", Uuid: "cdb907ce-84f4-4c38-801d-1e287dca2623",
SkinId: 2, SkinId: 2,

View File

@ -19,6 +19,25 @@ type Services struct {
Logger wd.Watchdog Logger wd.Watchdog
} }
type UsernameChanged struct {
AccountId int `json:"accountId"`
OldUsername string `json:"oldUsername"`
NewUsername string `json:"newUsername"`
}
type SkinChanged struct {
AccountId int `json:"userId"`
Uuid string `json:"uuid"`
SkinId int `json:"skinId"`
OldSkinId int `json:"oldSkinId"`
Hash string `json:"hash"`
Is1_8 bool `json:"is1_8"`
IsSlim bool `json:"isSlim"`
Url string `json:"url"`
MojangTextures string `json:"mojangTextures"`
MojangSignature string `json:"mojangSignature"`
}
const exchangeName string = "events" const exchangeName string = "events"
const queueName string = "skinsystem-accounts-events" const queueName string = "skinsystem-accounts-events"
@ -44,11 +63,11 @@ func (service *Services) HandleDelivery(delivery *amqp.Delivery) {
var result bool = true var result bool = true
switch delivery.RoutingKey { switch delivery.RoutingKey {
case "accounts.username-changed": case "accounts.username-changed":
var event *model.UsernameChanged var event *UsernameChanged
json.Unmarshal(delivery.Body, &event) json.Unmarshal(delivery.Body, &event)
result = service.HandleChangeUsername(event) result = service.HandleChangeUsername(event)
case "accounts.skin-changed": case "accounts.skin-changed":
var event *model.SkinChanged var event *SkinChanged
json.Unmarshal(delivery.Body, &event) json.Unmarshal(delivery.Body, &event)
result = service.HandleSkinChanged(event) result = service.HandleSkinChanged(event)
default: default:
@ -64,7 +83,7 @@ func (service *Services) HandleDelivery(delivery *amqp.Delivery) {
} }
} }
func (service *Services) HandleChangeUsername(event *model.UsernameChanged) bool { func (service *Services) HandleChangeUsername(event *UsernameChanged) bool {
service.Logger.IncCounter("worker.change_username", 1) service.Logger.IncCounter("worker.change_username", 1)
if event.OldUsername == "" { if event.OldUsername == "" {
service.Logger.IncCounter("worker.change_username_empty_old_username", 1) service.Logger.IncCounter("worker.change_username_empty_old_username", 1)
@ -98,7 +117,7 @@ func (service *Services) HandleChangeUsername(event *model.UsernameChanged) bool
} }
// TODO: возможно стоит добавить проверку на совпадение id аккаунтов // TODO: возможно стоит добавить проверку на совпадение id аккаунтов
func (service *Services) HandleSkinChanged(event *model.SkinChanged) bool { func (service *Services) HandleSkinChanged(event *SkinChanged) bool {
service.Logger.IncCounter("worker.skin_changed", 1) service.Logger.IncCounter("worker.skin_changed", 1)
var record *model.Skin var record *model.Skin
record, err := service.SkinsRepo.FindByUserId(event.AccountId) record, err := service.SkinsRepo.FindByUserId(event.AccountId)