Skip to content

Commit

Permalink
fix review part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Пешков Дмитрий committed Sep 17, 2024
1 parent f4b8fbc commit 09da209
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 350 deletions.
17 changes: 10 additions & 7 deletions cmd/gophermart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ import (
func main() {
mainCtx := context.Background()
config.Init()
logger.Init()
db.Init()
loggerRes := logger.NewLogger()
dbRes := db.NewDB(loggerRes)

orderQueue := make(chan string)
defer close(orderQueue)

userService := services.NewUserService()
orderService := services.NewOrderService(orderQueue)
withdrawalService := services.NewWithdrawalService()
userService := services.NewUserService(dbRes)
orderService := services.NewOrderService(orderQueue, dbRes, loggerRes)
withdrawalService := services.NewWithdrawalService(dbRes, loggerRes)

orderWorker := workers.NewOrderWorker(mainCtx, orderService, orderQueue)
orderWorker := workers.NewOrderWorker(mainCtx, orderService, orderQueue, loggerRes)
orderWorker.Run()

server.Start(userService, orderService, withdrawalService)
appServer := server.NewServer(userService, orderService, withdrawalService, loggerRes)
if err := appServer.Start(); err != nil {
loggerRes.Fatal(err.Error())
}
}
5 changes: 1 addition & 4 deletions internal/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"github.com/spqrcor/gofermart/internal/logger"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -62,9 +61,7 @@ func GetUserIDFromCookie(tokenString string) (uuid.UUID, error) {

func SetCookie(rw http.ResponseWriter, UserID uuid.UUID) {
cookie, err := CreateCookie(UserID)
if err != nil {
logger.Log.Error(err.Error())
} else {
if err == nil {
http.SetCookie(rw, &cookie)
}
}
7 changes: 6 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)

type Config struct {
RunAddr string `env:"RUN_ADDRESS"`
AccrualSystemAddress string `env:"ACCRUAL_SYSTEM_ADDRESS"`
LogLevel zapcore.Level `env:"LOG_LEVEL"`
DatabaseURI string `env:"DATABASE_URI"`
QueryTimeOut time.Duration `env:"QUERY_TIME_OUT"`
WorkerCount int `env:"WORKER_COUNT"`
}

var Cfg = Config{
RunAddr: "localhost:8080",
LogLevel: zap.InfoLevel,
AccrualSystemAddress: "",
DatabaseURI: "",
DatabaseURI: "postgres://postgres:Sp123456@localhost:5432/gofermart?sslmode=disable",
QueryTimeOut: 3,
WorkerCount: 3,
}

func Init() {
Expand Down
26 changes: 15 additions & 11 deletions internal/db/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@ package db
import (
"database/sql"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/spqrcor/gofermart/internal/config"
"github.com/spqrcor/gofermart/internal/logger"
"go.uber.org/zap"
)

var Source *sql.DB

func connect() (*sql.DB, error) {
func connect(logger *zap.Logger) (*sql.DB, error) {
db, err := sql.Open("pgx", config.Cfg.DatabaseURI)
if err != nil {
logger.Log.Fatal(err.Error())
logger.Fatal(err.Error())
return nil, err
}
if err := db.Ping(); err != nil {
logger.Log.Error(err.Error())
logger.Error(err.Error())
return nil, err
}
return db, nil
}

func Init() {
res, err := connect()
func NewDB(logger *zap.Logger) *sql.DB {
res, err := connect(logger)
if err != nil {
logger.Log.Fatal(err.Error())
logger.Fatal(err.Error())
}
if err := goose.SetDialect("postgres"); err != nil {
logger.Fatal(err.Error())
}
if err := goose.Up(res, "internal/migrations"); err != nil {
logger.Fatal(err.Error())
}
Migrate(res)
Source = res
return res
}
17 changes: 0 additions & 17 deletions internal/db/migrator.go

This file was deleted.

34 changes: 34 additions & 0 deletions internal/handlers/add_orders.go
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)
}
}
32 changes: 32 additions & 0 deletions internal/handlers/add_withdrawals.go
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)
}
}
25 changes: 25 additions & 0 deletions internal/handlers/get_balance.go
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
}
}
}
29 changes: 29 additions & 0 deletions internal/handlers/get_orders.go
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
}
}
}
29 changes: 29 additions & 0 deletions internal/handlers/get_withdrawals.go
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
}
}
}
Loading

0 comments on commit 09da209

Please sign in to comment.