Skip to content

Commit 9118bdb

Browse files
author
Moby Polo
committed
feature(gophermart): fix conflicts
2 parents 5b12e10 + c3ad18c commit 9118bdb

File tree

9 files changed

+56
-33
lines changed

9 files changed

+56
-33
lines changed

internal/accrual/handlers/rewards.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"errors"
55
"gorm.io/gorm"
66
"net/http"
7+
"slices"
78
"ya41-56/internal/accrual/models"
89
"ya41-56/internal/shared/httputil"
10+
models2 "ya41-56/internal/shared/models"
911
"ya41-56/internal/shared/repositories"
1012
"ya41-56/internal/shared/response"
1113
)
@@ -21,9 +23,9 @@ func NewRewardsHandler(repo repositories.Repository[models.RewardMechanic]) *Rew
2123
}
2224

2325
type CreateMechanicRequest struct {
24-
Match string `json:"match"`
25-
Reward float32 `json:"reward"`
26-
RewardType string `json:"reward_type"`
26+
Match string `json:"match"`
27+
Reward float32 `json:"reward"`
28+
RewardType models2.RewardType `json:"reward_type"`
2729
}
2830

2931
func (h *RewardsHandler) CreateRewards(w http.ResponseWriter, r *http.Request) {
@@ -49,7 +51,13 @@ func (h *RewardsHandler) CreateRewards(w http.ResponseWriter, r *http.Request) {
4951
return
5052
}
5153

52-
if req.RewardType != "%" && req.RewardType != "pt" {
54+
if !slices.Contains(
55+
[]models2.RewardType{
56+
models2.RewardTypePercent,
57+
models2.RewardTypePoints,
58+
},
59+
req.RewardType,
60+
) {
5361
response.Error(w, http.StatusBadRequest, "invalid reward type")
5462
return
5563
}

internal/accrual/models/reward.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package models
22

3-
import "time"
3+
import (
4+
"time"
5+
"ya41-56/internal/shared/models"
6+
)
47

58
type RewardMechanic struct {
6-
ID uint `gorm:"primaryKey"`
7-
Match string `gorm:"type:text;not null"`
8-
Reward float32 `gorm:"not null"`
9-
RewardType string `gorm:"type:varchar(10);not null"` // 'pt', '%'
9+
ID uint `gorm:"primaryKey"`
10+
Match string `gorm:"type:text;not null"`
11+
Reward float32 `gorm:"not null"`
12+
RewardType models.RewardType `gorm:"type:varchar(10);not null"` // 'pt', '%'
1013

1114
CreatedAt time.Time
1215
UpdatedAt time.Time

internal/accrual/services/accrualCounter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"math"
55
"strings"
66
"ya41-56/internal/accrual/models"
7+
sharedModels "ya41-56/internal/shared/models"
78
)
89

910
type AccrualService interface {
@@ -24,9 +25,9 @@ func (h *AccrualCounter) CalculateAccrual(goods []models.Good, mechanics []model
2425
for _, m := range mechanics {
2526
if strings.Contains(strings.ToLower(g.Description), strings.ToLower(m.Match)) {
2627
switch m.RewardType {
27-
case "%":
28+
case sharedModels.RewardTypePercent:
2829
total += float64(g.Price) * float64(m.Reward) / 100
29-
case "pt":
30+
case sharedModels.RewardTypePoints:
3031
total += float64(m.Reward)
3132
}
3233
}

internal/gophermart/handlers/balance.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"ya41-56/internal/gophermart/customerror"
77
"ya41-56/internal/gophermart/models"
88
"ya41-56/internal/shared/contextutil"
9+
"ya41-56/internal/shared/customstrings"
910
"ya41-56/internal/shared/httputil"
1011
"ya41-56/internal/shared/luhn"
1112
"ya41-56/internal/shared/repositories"
@@ -53,13 +54,13 @@ func (h *BalanceHandler) Withdraw(w http.ResponseWriter, r *http.Request) {
5354

5455
// TODO: Begin the transaction
5556

56-
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
57+
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
5758
if err != nil {
5859
response.Error(w, http.StatusInternalServerError, err.Error())
5960
return
6061
}
6162

62-
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
63+
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
6364
if err != nil {
6465
response.Error(w, http.StatusInternalServerError, err.Error())
6566
return
@@ -89,7 +90,7 @@ func (h *BalanceHandler) Withdraw(w http.ResponseWriter, r *http.Request) {
8990
}
9091

9192
withdrawal := &models.Withdrawal{
92-
UserID: parseID(userIDStr),
93+
UserID: customstrings.ParseID(userIDStr),
9394
Order: number,
9495
Value: float32(req.Sum),
9596
}

internal/gophermart/handlers/handlers.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/gophermart/handlers/orders.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package handlers
22

33
import (
44
"errors"
5+
"gorm.io/gorm"
56
"io"
67
"net/http"
78
"strings"
89
"ya41-56/internal/gophermart/models"
910
"ya41-56/internal/gophermart/worker"
1011
"ya41-56/internal/shared/contextutil"
12+
"ya41-56/internal/shared/customstrings"
1113
"ya41-56/internal/shared/luhn"
1214
"ya41-56/internal/shared/repositories"
1315
"ya41-56/internal/shared/response"
14-
15-
"gorm.io/gorm"
1616
)
1717

1818
type OrdersHandler struct {
@@ -52,7 +52,7 @@ func (h *OrdersHandler) Upload(w http.ResponseWriter, r *http.Request) {
5252
}
5353
}
5454
if existed.ID > 0 {
55-
if existed.UserID == parseID(userIDStr) {
55+
if existed.UserID == customstrings.ParseID(userIDStr) {
5656
response.Error(w, http.StatusOK, "order already exists")
5757
return
5858
}
@@ -61,7 +61,7 @@ func (h *OrdersHandler) Upload(w http.ResponseWriter, r *http.Request) {
6161
}
6262

6363
order := &models.Order{
64-
UserID: parseID(userIDStr),
64+
UserID: customstrings.ParseID(userIDStr),
6565
Number: number,
6666
Status: models.OrderStatusNew,
6767
}
@@ -83,7 +83,7 @@ func (h *OrdersHandler) List(w http.ResponseWriter, r *http.Request) {
8383
return
8484
}
8585

86-
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
86+
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
8787
if err != nil {
8888
response.Error(w, http.StatusInternalServerError, err.Error())
8989
return

internal/gophermart/handlers/users.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"ya41-56/internal/gophermart/models"
99
"ya41-56/internal/gophermart/services"
1010
"ya41-56/internal/shared/contextutil"
11+
"ya41-56/internal/shared/customstrings"
1112
"ya41-56/internal/shared/httputil"
1213
"ya41-56/internal/shared/logger"
1314
"ya41-56/internal/shared/repositories"
@@ -106,13 +107,13 @@ func (h *UsersHandler) Balance(w http.ResponseWriter, r *http.Request) {
106107
return
107108
}
108109

109-
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
110+
orders, err := h.Orders.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
110111
if err != nil {
111112
response.Error(w, http.StatusInternalServerError, err.Error())
112113
return
113114
}
114115

115-
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
116+
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
116117
if err != nil {
117118
response.Error(w, http.StatusInternalServerError, err.Error())
118119
return
@@ -154,7 +155,7 @@ func (h *UsersHandler) Withdrawals(w http.ResponseWriter, r *http.Request) {
154155
}
155156

156157
// SELECT * FROM withdrawals WHERE user_id = ... ORDER BY created_at ASC
157-
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", parseID(userIDStr))
158+
withdrawals, err := h.Withdrawal.FindManyByField(r.Context(), "user_id", customstrings.ParseID(userIDStr))
158159
if err != nil {
159160
response.Error(w, http.StatusInternalServerError, err.Error())
160161
return
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package customstrings
2+
3+
import "fmt"
4+
5+
func ParseID(id string) uint {
6+
var uid uint
7+
_, err := fmt.Sscanf(id, "%d", &uid)
8+
if err != nil {
9+
return 0
10+
}
11+
return uid
12+
}

internal/shared/models/RewardType.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models
2+
3+
type RewardType string
4+
5+
const (
6+
RewardTypePercent RewardType = "%"
7+
RewardTypePoints RewardType = "pt"
8+
)

0 commit comments

Comments
 (0)