Skip to content

Commit

Permalink
+ save order, get orders
Browse files Browse the repository at this point in the history
  • Loading branch information
lammer90 committed Dec 19, 2023
1 parent 55c199b commit 0be8e6a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/gophermart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ func InitDB(driverName, dataSource string) *sql.DB {
}

func buildSession() *sessions.CookieStore {
key := []byte("super-secret-key")
key := []byte("abc123")
return sessions.NewCookieStore(key)
}
7 changes: 7 additions & 0 deletions internal/repository/orderstorage/orderstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package orderstorage
import (
"context"
"database/sql"
"errors"
"github.com/lammer90/gofermart/internal/dto/order"
)

Expand Down Expand Up @@ -38,6 +39,9 @@ func (d dbOrderStorage) FindByUser(login string) ([]order.Order, error) {
o.login = $1
`, login)

if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,6 +74,9 @@ func (d dbOrderStorage) FindByNumber(number string) (*order.Order, error) {
WHERE
o.order_number = $1
`, number)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/repository/userstorage/userstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package userstorage
import (
"context"
"database/sql"
"errors"
)

type dbUserStorage struct {
Expand Down Expand Up @@ -35,7 +36,7 @@ func (d *dbUserStorage) Find(login string) (string, error) {

var authHash string
err := row.Scan(&authHash)
if err != nil {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", err
}
return authHash, nil
Expand Down
3 changes: 2 additions & 1 deletion internal/services/authservice/authservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package authservice

import (
"crypto/sha256"
"encoding/base64"
"github.com/golang-jwt/jwt/v4"
"github.com/lammer90/gofermart/internal/repository/userstorage"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (a *authenticationServiceImpl) ToLoginUser(login, password string) (token s
func buildHash(login string, password string) string {
src := []byte(login + ":" + password)
newHashByte := sha256.Sum256(src)
return string(newHashByte[:])
return base64.StdEncoding.EncodeToString(newHashByte[:])
}

func buildJWTString(login, privateKey string) (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/services/orderservice/orderservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (o orderServiceImpl) FindAll(login string) ([]order.OrderResponse, error) {
if err != nil {
return nil, err
}
if orders == nil {
return nil, nil
}
for _, ord := range orders {
resp := order.OrderResponse{
ord.Number,
Expand Down
2 changes: 1 addition & 1 deletion internal/web/handlers/orderhandler/orderhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o orderHandler) FindAll(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusInternalServerError)
return
}
if len(orders) == 0 {
if orders == nil || len(orders) == 0 {
res.WriteHeader(http.StatusNoContent)
return
}
Expand Down
8 changes: 1 addition & 7 deletions internal/web/middleware/authfilter/authfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ func New(authenticationService authservice.AuthenticationService, cookieStore *s
return
}

session, err := cookieStore.Get(r, "Authorization")
if err != nil {
logger.Log.Error("Error get session", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
session, _ := cookieStore.Get(r, "Authorization")
session.Values["login"] = login
session.Save(r, w)
}

next.ServeHTTP(w, r)
Expand Down

0 comments on commit 0be8e6a

Please sign in to comment.