generated from yandex-praktikum/go-musthave-diploma-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Пешков Дмитрий
committed
Sep 15, 2024
1 parent
faa82a7
commit 5b7c173
Showing
7 changed files
with
194 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/spqrcor/gofermart/internal/config" | ||
"github.com/spqrcor/gofermart/internal/db" | ||
"github.com/spqrcor/gofermart/internal/logger" | ||
"github.com/spqrcor/gofermart/internal/server" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/workers" | ||
) | ||
|
||
func main() { | ||
mainCtx := context.Background() | ||
config.Init() | ||
logger.Init() | ||
db.Init() | ||
|
||
server.Start() | ||
userService := services.NewUserService() | ||
orderService := services.NewOrderService() | ||
withdrawalService := services.NewWithdrawalService() | ||
|
||
orderWorker := workers.NewOrderWorker(mainCtx, orderService) | ||
orderWorker.Run() | ||
|
||
server.Start(userService, orderService, withdrawalService) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"github.com/spqrcor/gofermart/internal/config" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func SendOrder(OrderNum string) error { | ||
url := config.Cfg.AccrualSystemAddress + "/api/orders" | ||
resp, err := http.Post(url, "application/json", bytes.NewReader([]byte(`{"order":`+OrderNum+`,"goods":[{"description":"Чайник Bork","price":7000}]}`))) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusAccepted { | ||
return errors.New("Error " + resp.Status) | ||
} | ||
return nil | ||
} | ||
|
||
func CheckOrder(OrderNum string) (services.OrderFromAccrual, error) { | ||
data := services.OrderFromAccrual{} | ||
resp, err := http.Get(config.Cfg.AccrualSystemAddress + "/api/orders/" + OrderNum) | ||
if err != nil { | ||
return data, nil | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return data, errors.New("Error " + resp.Status) | ||
} | ||
|
||
bodyBytes, _ := io.ReadAll(resp.Body) | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
if err = json.Unmarshal(bodyBytes, &data); err != nil { | ||
return data, err | ||
} | ||
return data, nil | ||
} | ||
|
||
func SetReward() error { | ||
url := config.Cfg.AccrualSystemAddress + "/api/goods" | ||
resp, err := http.Post(url, "application/json", bytes.NewReader([]byte(`{"match":"Bork","reward":10,"reward_type":"%"}`))) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return errors.New("Error " + resp.Status) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package workers | ||
|
||
import ( | ||
"context" | ||
"github.com/spqrcor/gofermart/internal/client" | ||
"github.com/spqrcor/gofermart/internal/logger" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"time" | ||
) | ||
|
||
type OrderWorker struct { | ||
ctx context.Context | ||
orderService services.OrderRepository | ||
} | ||
|
||
func NewOrderWorker(ctx context.Context, orderService services.OrderRepository) *OrderWorker { | ||
return &OrderWorker{ctx: ctx, orderService: orderService} | ||
} | ||
|
||
func (w *OrderWorker) Send(orderNum string) error { | ||
if err := client.SendOrder(orderNum); err != nil { | ||
return err | ||
} | ||
data := services.OrderFromAccrual{Order: orderNum, Status: "PROCESSING"} | ||
if err := w.orderService.ChangeStatus(w.ctx, data); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (w *OrderWorker) Run() { | ||
//_ = client.SetReward() | ||
go w.doInterval() | ||
} | ||
|
||
func (w *OrderWorker) doInterval() { | ||
for range time.Tick(time.Second * 5) { | ||
logger.Log.Info("execute 5s") | ||
|
||
orders, _ := w.orderService.GetUnComplete(w.ctx) | ||
for _, order := range orders { | ||
logger.Log.Info("before check") | ||
result, err := client.CheckOrder(order) | ||
if err != nil { | ||
logger.Log.Info(err.Error()) | ||
|
||
} else { | ||
err = w.orderService.ChangeStatus(w.ctx, result) | ||
if err != nil { | ||
logger.Log.Info(err.Error()) | ||
|
||
} | ||
logger.Log.Info("after check") | ||
} | ||
} | ||
} | ||
} |