Skip to content

Commit

Permalink
add working with context
Browse files Browse the repository at this point in the history
  • Loading branch information
zelhat committed Jan 11, 2024
1 parent ffa39bd commit 56800de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
5 changes: 3 additions & 2 deletions internal/handler/accrual/accrual.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func SendAccrual(addr string, handler *handler.Handler) {
ctx := context.Background()
client := resty.New()
url := addr + "/api/orders/"
ball := struct {
Expand All @@ -21,7 +22,7 @@ func SendAccrual(addr string, handler *handler.Handler) {
}{}
for {

orders, err := handler.Repository.GetOrders(context.Background())
orders, err := handler.Repository.GetOrders(ctx)
if err != nil {
continue
}
Expand All @@ -37,7 +38,7 @@ func SendAccrual(addr string, handler *handler.Handler) {

if resp.StatusCode() == http.StatusOK && ball.Status == "PROCESSED" {

err := handler.Repository.UpdateOrder(context.Background(), ball.Order, ball.Status, ball.Accrual)
err := handler.Repository.UpdateOrder(ctx, ball.Order, ball.Status, ball.Accrual)
if err != nil {
fmt.Printf("[ERROR] %s\n", err.Error())
continue
Expand Down
15 changes: 7 additions & 8 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
return
}

user, err := h.Repository.AddUser(context.Background(), c.Login, pass)
user, err := h.Repository.AddUser(r.Context(), c.Login, pass)
if err != nil {
var e *pgconn.PgError
if errors.As(err, &e) && e.Code == "23505" {
Expand Down Expand Up @@ -74,7 +73,7 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
return
}

user, err := h.Repository.GetUser(context.Background(), c.Login)
user, err := h.Repository.GetUser(r.Context(), c.Login)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
w.WriteHeader(http.StatusUnauthorized)
Expand Down Expand Up @@ -118,11 +117,11 @@ func (h *Handler) Orders(w http.ResponseWriter, r *http.Request) {

username := r.Header.Get("username")

_, err = h.Repository.AddOrder(context.Background(), username, number)
_, err = h.Repository.AddOrder(r.Context(), username, number)
if err != nil {
var e *pgconn.PgError
if errors.As(err, &e) && e.Code == "23505" {
order, err := h.Repository.GetOrderByNumber(context.Background(), number)
order, err := h.Repository.GetOrderByNumber(r.Context(), number)
if err == nil {
if order.UserLogin == username {
w.WriteHeader(http.StatusOK)
Expand All @@ -142,7 +141,7 @@ func (h *Handler) Orders(w http.ResponseWriter, r *http.Request) {
func (h *Handler) GetOrders(w http.ResponseWriter, r *http.Request) {
username := r.Header.Get("username")

orders, err := h.Repository.GetOrdersByLogin(context.Background(), username)
orders, err := h.Repository.GetOrdersByLogin(r.Context(), username)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down Expand Up @@ -175,7 +174,7 @@ func (h *Handler) Withdraw(w http.ResponseWriter, r *http.Request) {
return
}

err = h.Repository.UpdateWithdraw(context.Background(), username, withdraw.Order, withdraw.Sum)
err = h.Repository.UpdateWithdraw(r.Context(), username, withdraw.Order, withdraw.Sum)
if err != nil {
if err.Error() == "balance error" {
w.WriteHeader(http.StatusPaymentRequired)
Expand All @@ -192,7 +191,7 @@ func (h *Handler) Withdraw(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Balance(w http.ResponseWriter, r *http.Request) {
username := r.Header.Get("username")

user, err := h.Repository.GetBalance(context.Background(), username)
user, err := h.Repository.GetBalance(r.Context(), username)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down

0 comments on commit 56800de

Please sign in to comment.