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 17, 2024
1 parent
f4b8fbc
commit 09da209
Showing
23 changed files
with
416 additions
and
350 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
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 was deleted.
Oops, something went wrong.
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,34 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/utils" | ||
"net/http" | ||
) | ||
|
||
func AddOrdersHandler(o services.OrderRepository) http.HandlerFunc { | ||
return func(res http.ResponseWriter, req *http.Request) { | ||
orderNum, err := utils.FromPostPlain(req) | ||
if err != nil { | ||
res.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = o.Add(req.Context(), orderNum) | ||
if errors.Is(err, services.ErrOrderInvalidFormat) { | ||
http.Error(res, err.Error(), http.StatusUnprocessableEntity) | ||
return | ||
} else if errors.Is(err, services.ErrOrderAnotherUserExists) { | ||
res.WriteHeader(http.StatusConflict) | ||
return | ||
} else if errors.Is(err, services.ErrOrderUserExists) { | ||
res.WriteHeader(http.StatusOK) | ||
return | ||
} else if err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
res.WriteHeader(http.StatusAccepted) | ||
} | ||
} |
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,32 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/utils" | ||
"net/http" | ||
) | ||
|
||
func AddWithdrawalHandler(w services.WithdrawalRepository) http.HandlerFunc { | ||
return func(res http.ResponseWriter, req *http.Request) { | ||
var input services.InputWithdrawal | ||
if err := utils.FromPostJSON(req, &input); err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
err := w.Add(req.Context(), input) | ||
if errors.Is(err, services.ErrOrderInvalidFormat) { | ||
http.Error(res, err.Error(), http.StatusUnprocessableEntity) | ||
return | ||
} else if errors.Is(err, services.ErrBalance) { | ||
http.Error(res, err.Error(), http.StatusPaymentRequired) | ||
return | ||
} else if err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
res.WriteHeader(http.StatusOK) | ||
} | ||
} |
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,25 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"net/http" | ||
) | ||
|
||
func GetBalanceHandler(w services.WithdrawalRepository) http.HandlerFunc { | ||
return func(res http.ResponseWriter, req *http.Request) { | ||
balance, err := w.GetBalance(req.Context()) | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
res.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
res.WriteHeader(http.StatusOK) | ||
enc := json.NewEncoder(res) | ||
if err := enc.Encode(balance); err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
} |
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,29 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"net/http" | ||
) | ||
|
||
func GetOrdersHandler(o services.OrderRepository) http.HandlerFunc { | ||
return func(res http.ResponseWriter, req *http.Request) { | ||
orders, err := o.GetAll(req.Context()) | ||
if errors.Is(err, services.ErrOrdersNotFound) { | ||
res.WriteHeader(http.StatusNoContent) | ||
return | ||
} else if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
res.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
res.WriteHeader(http.StatusOK) | ||
enc := json.NewEncoder(res) | ||
if err := enc.Encode(orders); err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
} |
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,29 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"net/http" | ||
) | ||
|
||
func GetWithdrawalsHandler(w services.WithdrawalRepository) http.HandlerFunc { | ||
return func(res http.ResponseWriter, req *http.Request) { | ||
withdrawals, err := w.GetAll(req.Context()) | ||
if errors.Is(err, services.ErrWithdrawNotFound) { | ||
res.WriteHeader(http.StatusNoContent) | ||
return | ||
} else if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
res.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
res.WriteHeader(http.StatusOK) | ||
enc := json.NewEncoder(res) | ||
if err := enc.Encode(withdrawals); err != nil { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.