Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 fix main bugs #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix main bugs
  • Loading branch information
matheuspolitano committed Dec 24, 2024
commit 6c29e2f3001fd025143c433777c0a128700416de
7 changes: 4 additions & 3 deletions review/cmd/coupon_service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ var (

func main() {
svc := service.New(repo)
本 := api.New(cfg.API, svc)
本.Start()
fmt.Println(cfg)
server := api.New(cfg.API, svc)
server.Start()
fmt.Println("Starting Coupon service server")
<-time.After(1 * time.Hour * 24 * 365)
fmt.Println("Coupon service server alive for a year, closing")
.Close()
server.Close()
}
28 changes: 9 additions & 19 deletions review/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type Service interface {
ApplyCoupon(entity.Basket, string) (*entity.Basket, error)
CreateCoupon(int, string, int) any
GetCoupons([]string) ([]entity.Coupon, error)
CreateCoupon(int, string, int) (string, error)
GetCoupons(...string) ([]entity.Coupon, error)
}

type Config struct {
Expand All @@ -30,30 +30,20 @@ type API struct {
}

func New[T Service](cfg Config, svc T) API {
gin.SetMode(gin.ReleaseMode)
r := new(gin.Engine)
r = gin.New()
r.Use(gin.Recovery())

r := gin.Default()
return API{
MUX: r,
CFG: cfg,
svc: svc,
}.withServer()
}.withServer().withRoutes()
}

func (a API) withServer() API {

ch := make(chan API)
go func() {
a.srv = &http.Server{
Addr: fmt.Sprintf(":%d", a.CFG.Port),
Handler: a.MUX,
}
ch <- a
}()

return <-ch
a.srv = &http.Server{
Addr: fmt.Sprintf(":%d", a.CFG.Port),
Handler: a.MUX,
}
return a
}

func (a API) withRoutes() API {
Expand Down
13 changes: 7 additions & 6 deletions review/internal/api/coupon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
. "coupon_service/internal/api/entity"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)
Expand All @@ -25,19 +26,19 @@ func (a *API) Create(c *gin.Context) {
if err := c.ShouldBindJSON(&apiReq); err != nil {
return
}
err := a.svc.CreateCoupon(apiReq.Discount, apiReq.Code, apiReq.MinBasketValue)
id, err := a.svc.CreateCoupon(apiReq.Discount, apiReq.Code, apiReq.MinBasketValue)
if err != nil {
return
}
c.Status(http.StatusOK)
c.Writer.Write([]byte(id))
}

func (a *API) Get(c *gin.Context) {
apiReq := CouponRequest{}
if err := c.ShouldBindJSON(&apiReq); err != nil {
return
}
coupons, err := a.svc.GetCoupons(apiReq.Codes)
codes := c.Query("codes")
codeList := strings.Split(codes, ",")

coupons, err := a.svc.GetCoupons(codeList...)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions review/internal/api/entity/Coupons.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package entity

type Coupon struct {
Discount int
Code string
MinBasketValue int
Discount int `json:"discount"`
Code string `json:"code"`
MinBasketValue int `json:"min_basket_value"`
}
4 changes: 3 additions & 1 deletion review/internal/repository/memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type Repository struct {
}

func New() *Repository {
return &Repository{}
return &Repository{
entries: make(map[string]entity.Coupon),
}
}

func (r *Repository) FindByCode(code string) (*entity.Coupon, error) {
Expand Down
10 changes: 5 additions & 5 deletions review/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s Service) ApplyCoupon(basket Basket, code string) (b *Basket, e error) {
return nil, fmt.Errorf("Tried to apply discount to negative value")
}

func (s Service) CreateCoupon(discount int, code string, minBasketValue int) any {
func (s Service) CreateCoupon(discount int, code string, minBasketValue int) (string, error) {
coupon := Coupon{
Discount: discount,
Code: code,
Expand All @@ -49,14 +49,14 @@ func (s Service) CreateCoupon(discount int, code string, minBasketValue int) any
}

if err := s.repo.Save(coupon); err != nil {
return err
return "", err
}
return nil
return coupon.ID, nil
}

func (s Service) GetCoupons(codes []string) ([]Coupon, error) {
func (s Service) GetCoupons(codes ...string) ([]Coupon, error) {
coupons := make([]Coupon, 0, len(codes))
var e error = nil
var e error

for idx, code := range codes {
coupon, err := s.repo.FindByCode(code)
Expand Down