diff --git a/cmd/main.go b/cmd/main.go index 97a559e..ebdf8b7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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" @@ -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) diff --git a/docker-compose.qa.template.yml b/docker-compose.qa.template.yml index 97f2871..14e1759 100644 --- a/docker-compose.qa.template.yml +++ b/docker-compose.qa.template.yml @@ -23,8 +23,8 @@ services: IMG_MAX_FILE_SIZE_MB: 1 IMG_CROP_WIDTH: 500 IMG_CROP_HEIGHT: 500 - REG_CHECKIN_START: 2024-07-21T07:00:00Z - REG_RPKM_START: 2024-07-20T20:00:00Z + REG_CHECKIN_START: "2024-07-21T07:00:00Z" + REG_RPKM_START: "2024-07-20T20:00:00Z" SERVICE_AUTH: auth:3002 SERVICE_BACKEND: backend:3003 SERVICE_CHECKIN: checkin:3004 diff --git a/docs/docs.go b/docs/docs.go index 90ecf4f..3036ae3 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -326,7 +326,7 @@ const docTemplate = `{ "201": { "description": "Created", "schema": { - "$ref": "#/definitions/dto.CountResponse" + "$ref": "#/definitions/dto.CreateCountRequest" } }, "400": { @@ -1477,14 +1477,6 @@ const docTemplate = `{ } } }, - "dto.CountResponse": { - "type": "object", - "properties": { - "success": { - "type": "boolean" - } - } - }, "dto.CreateCheckInRequest": { "type": "object", "properties": { @@ -1513,6 +1505,14 @@ const docTemplate = `{ } } }, + "dto.CreateCountRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, "dto.CreateSelectionRequest": { "type": "object", "required": [ diff --git a/docs/swagger.json b/docs/swagger.json index 3b2478d..c572745 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -320,7 +320,7 @@ "201": { "description": "Created", "schema": { - "$ref": "#/definitions/dto.CountResponse" + "$ref": "#/definitions/dto.CreateCountRequest" } }, "400": { @@ -1471,14 +1471,6 @@ } } }, - "dto.CountResponse": { - "type": "object", - "properties": { - "success": { - "type": "boolean" - } - } - }, "dto.CreateCheckInRequest": { "type": "object", "properties": { @@ -1507,6 +1499,14 @@ } } }, + "dto.CreateCountRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, "dto.CreateSelectionRequest": { "type": "object", "required": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8731b14..e09ab00 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: @@ -67,6 +62,11 @@ definitions: lastname: type: string type: object + dto.CreateCountRequest: + properties: + name: + type: string + type: object dto.CreateSelectionRequest: properties: baan_id: @@ -621,7 +621,7 @@ paths: "201": description: Created schema: - $ref: '#/definitions/dto.CountResponse' + $ref: '#/definitions/dto.CreateCountRequest' "400": description: Bad Request schema: diff --git a/go.mod b/go.mod index 00c88d4..5392ea6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 397409e..a2de470 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/count/count.handler.go b/internal/count/count.handler.go index e247684..5ecb1eb 100644 --- a/internal/count/count.handler.go +++ b/internal/count/count.handler.go @@ -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, } @@ -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) { @@ -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) } diff --git a/internal/count/count.service.go b/internal/count/count.service.go new file mode 100644 index 0000000..60788e1 --- /dev/null +++ b/internal/count/count.service.go @@ -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 +} diff --git a/internal/dto/count.dto.go b/internal/dto/count.dto.go index f51c0ba..c48dc7b 100644 --- a/internal/dto/count.dto.go +++ b/internal/dto/count.dto.go @@ -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"` }