Skip to content

Commit

Permalink
Merge pull request #42 from isd-sgcu/count-svc
Browse files Browse the repository at this point in the history
Count svc
  • Loading branch information
bookpanda authored Jul 20, 2024
2 parents df659c7 + acc9f7f commit eec4b0c
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 34 deletions.
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/tracer"
authProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/auth/auth/v1"
userProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/auth/user/v1"
countProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/count/v1"
groupProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/group/v1"
pinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/pin/v1"
selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1"
Expand Down Expand Up @@ -143,7 +144,9 @@ func main() {
metricsReg := metrics.NewRegistry(prometheus.NewRegistry(), requestMetrics, countMetrics)
metricsHdr := metrics.NewHandler(metricsReg, logger)

countHdr := count.NewHandler(countMetrics, logger)
countClient := countProto.NewCountServiceClient(backendConn)
countSvc := count.NewService(countClient, logger)
countHdr := count.NewHandler(countSvc, countMetrics, logger)

authMiddleware := middleware.NewAuthMiddleware(authSvc, requestMetrics)

Expand Down
18 changes: 9 additions & 9 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ const docTemplate = `{
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/dto.CountResponse"
"$ref": "#/definitions/dto.CreateCountRequest"
}
},
"400": {
Expand Down Expand Up @@ -1477,14 +1477,6 @@ const docTemplate = `{
}
}
},
"dto.CountResponse": {
"type": "object",
"properties": {
"success": {
"type": "boolean"
}
}
},
"dto.CreateCheckInRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1513,6 +1505,14 @@ const docTemplate = `{
}
}
},
"dto.CreateCountRequest": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"dto.CreateSelectionRequest": {
"type": "object",
"required": [
Expand Down
18 changes: 9 additions & 9 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/dto.CountResponse"
"$ref": "#/definitions/dto.CreateCountRequest"
}
},
"400": {
Expand Down Expand Up @@ -1471,14 +1471,6 @@
}
}
},
"dto.CountResponse": {
"type": "object",
"properties": {
"success": {
"type": "boolean"
}
}
},
"dto.CreateCheckInRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1507,6 +1499,14 @@
}
}
},
"dto.CreateCountRequest": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"dto.CreateSelectionRequest": {
"type": "object",
"required": [
Expand Down
12 changes: 6 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ definitions:
$ref: '#/definitions/dto.BaanCount'
type: array
type: object
dto.CountResponse:
properties:
success:
type: boolean
type: object
dto.CreateCheckInRequest:
properties:
email:
Expand All @@ -67,6 +62,11 @@ definitions:
lastname:
type: string
type: object
dto.CreateCountRequest:
properties:
name:
type: string
type: object
dto.CreateSelectionRequest:
properties:
baan_id:
Expand Down Expand Up @@ -621,7 +621,7 @@ paths:
"201":
description: Created
schema:
$ref: '#/definitions/dto.CountResponse'
$ref: '#/definitions/dto.CreateCountRequest'
"400":
description: Bad Request
schema:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ require (
github.com/go-playground/validator/v10 v10.22.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.5.0
github.com/isd-sgcu/rpkm67-model v0.0.7
github.com/isd-sgcu/rpkm67-go-proto v0.5.3
github.com/isd-sgcu/rpkm67-model v0.2.0
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.19.1
github.com/stretchr/testify v1.9.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ github.com/isd-sgcu/rpkm67-go-proto v0.4.8 h1:tU6nCv4A34guBoDwkZvUzzs6z43NBzgLsS
github.com/isd-sgcu/rpkm67-go-proto v0.4.8/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.5.0 h1:FFYIv/Ejs3fP+AWErtx9ONxbzZLr1y1Ttd7j8xHIJ34=
github.com/isd-sgcu/rpkm67-go-proto v0.5.0/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.5.2 h1:CP9oXIa4MrJZd6ynHkVt18YPGlQU0bkDM1RA1h7PUis=
github.com/isd-sgcu/rpkm67-go-proto v0.5.2/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.5.3 h1:DMxo3vu5OB2RaODWQwIIFRTyPEyTNMvwmfDbVmVnmnM=
github.com/isd-sgcu/rpkm67-go-proto v0.5.3/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-model v0.0.7 h1:3b8gf1Ocg+Ky4xocKtCqVCB3rFDg90IgEXRwNmHt0OE=
github.com/isd-sgcu/rpkm67-model v0.0.7/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/isd-sgcu/rpkm67-model v0.2.0 h1:D2KytmevtV9/3FwfA7FiKo2UKa3jC8knZI97vwRuboA=
github.com/isd-sgcu/rpkm67-model v0.2.0/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down
17 changes: 12 additions & 5 deletions internal/count/count.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type Handler interface {
}

type handlerImpl struct {
svc Service
countMetrics metrics.CountMetrics
log *zap.Logger
}

func NewHandler(countMetrics metrics.CountMetrics, log *zap.Logger) Handler {
func NewHandler(svc Service, countMetrics metrics.CountMetrics, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
countMetrics: countMetrics,
log: log,
}
Expand All @@ -32,7 +34,7 @@ func NewHandler(countMetrics metrics.CountMetrics, log *zap.Logger) Handler {
// @Accept json
// @Produce json
// @Param name path string true "Name of the count metric"
// @Success 201 {object} dto.CountResponse
// @Success 201 {object} dto.CreateCountRequest
// @Failure 400 {object} apperror.AppError
// @Router /count/{name} [post]
func (h *handlerImpl) Count(c context.Ctx) {
Expand All @@ -45,7 +47,12 @@ func (h *handlerImpl) Count(c context.Ctx) {

h.countMetrics.Increment(name)

c.JSON(http.StatusCreated, &dto.CountResponse{
Success: true,
})
res, err := h.svc.Create(&dto.CreateCountRequest{Name: name})
if err != nil {
h.log.Named("Count").Error("Create: failed to create count", zap.Error(err))
c.InternalServerError(err.Error())
return
}

c.JSON(http.StatusCreated, res)
}
48 changes: 48 additions & 0 deletions internal/count/count.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package count

import (
"context"
"time"

"github.com/isd-sgcu/rpkm67-gateway/apperror"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
countProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/count/v1"

"go.uber.org/zap"
)

type Service interface {
Create(req *dto.CreateCountRequest) (*dto.CreateCountResponse, *apperror.AppError)
}

type serviceImpl struct {
client countProto.CountServiceClient
log *zap.Logger
}

func NewService(client countProto.CountServiceClient, log *zap.Logger) Service {
return &serviceImpl{
client: client,
log: log,
}
}

func (s *serviceImpl) Create(req *dto.CreateCountRequest) (*dto.CreateCountResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.Create(ctx, &countProto.CreateCountRequest{
Name: req.Name,
})
if err != nil {
s.log.Named("Create").Error("Create: ", zap.Error(err))
return nil, apperror.HandleServiceError(err)
}

return &dto.CreateCountResponse{
Count: &dto.Count{
ID: res.Count.Id,
Name: res.Count.Name,
},
}, nil
}
13 changes: 11 additions & 2 deletions internal/dto/count.dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package dto

type CountResponse struct {
Success bool `json:"success"`
type Count struct {
ID string `json:"id"`
Name string `json:"name"`
}

type CreateCountRequest struct {
Name string `json:"name"`
}

type CreateCountResponse struct {
Count *Count `json:"count"`
}

0 comments on commit eec4b0c

Please sign in to comment.