Skip to content

Commit

Permalink
feat (SRS-11) : update proto, docs & implement API update user shorte…
Browse files Browse the repository at this point in the history
…ners
  • Loading branch information
PickHD committed May 17, 2023
1 parent f9e9d68 commit 7846bbd
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 32 deletions.
5 changes: 5 additions & 0 deletions user/api/v1/proto/shortener/shortener.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ message CreateShortenerMessage {
message UpdateVisitorCountMessage {
string short_url=1;
}

message UpdateShortenerMessage {
string id =1;
string full_url=2;
}
1 change: 1 addition & 0 deletions user/cmd/v1/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AMQP_SERVER_URL=amqp://guest:guest@amqp:5672/
AMQP_QUEUE_CREATE_SHORTENER=create-shortener-queue
AMQP_QUEUE_UPDATE_VISITOR_COUNT=update-visitor-count-queue
AMQP_QUEUE_UPLOAD_AVATAR=upload-avatar-queue
AMQP_QUEUE_UPDATE_SHORTENER=update-shortener-queue

JWT_SECRET=secret
JWT_EXPIRE=7
Expand Down
69 changes: 67 additions & 2 deletions user/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 67 additions & 2 deletions user/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.GenerateShortUserRequest"
"$ref": "#/definitions/model.ShortUserRequest"
}
}
],
Expand All @@ -230,6 +230,71 @@
}
}
},
"/short/{id}": {
"put": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Update Users Short URL",
"parameters": [
{
"type": "string",
"description": "id short urls",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Authorization Bearer \u003cPlace Access Token Here\u003e",
"name": "Authorization",
"in": "header",
"required": true
},
{
"description": "update short user",
"name": "short",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.ShortUserRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/helper.BaseResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/helper.BaseResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/helper.BaseResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/helper.BaseResponse"
}
}
}
}
},
"/upload/avatar": {
"post": {
"consumes": [
Expand Down Expand Up @@ -317,7 +382,7 @@
}
}
},
"model.GenerateShortUserRequest": {
"model.ShortUserRequest": {
"type": "object",
"properties": {
"full_url": {
Expand Down
47 changes: 45 additions & 2 deletions user/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ definitions:
full_name:
type: string
type: object
model.GenerateShortUserRequest:
model.ShortUserRequest:
properties:
full_url:
type: string
Expand Down Expand Up @@ -143,6 +143,49 @@ paths:
summary: Update Users Profile
tags:
- User
/short/{id}:
put:
consumes:
- application/json
parameters:
- description: id short urls
in: path
name: id
required: true
type: string
- description: Authorization Bearer <Place Access Token Here>
in: header
name: Authorization
required: true
type: string
- description: update short user
in: body
name: short
required: true
schema:
$ref: '#/definitions/model.ShortUserRequest'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/helper.BaseResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/helper.BaseResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/helper.BaseResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/helper.BaseResponse'
summary: Update Users Short URL
tags:
- User
/short/generate:
post:
consumes:
Expand All @@ -158,7 +201,7 @@ paths:
name: short
required: true
schema:
$ref: '#/definitions/model.GenerateShortUserRequest'
$ref: '#/definitions/model.ShortUserRequest'
produces:
- application/json
responses:
Expand Down
2 changes: 2 additions & 0 deletions user/internal/v1/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type (
QueueCreateShortener string
QueueUpdateVisitor string
QueueUploadAvatar string
QueueUpdateShortener string
}

Secret struct {
Expand Down Expand Up @@ -82,6 +83,7 @@ func loadConfiguration() *Configuration {
QueueCreateShortener: helper.GetEnvString("AMQP_QUEUE_CREATE_SHORTENER"),
QueueUpdateVisitor: helper.GetEnvString("AMQP_QUEUE_UPDATE_VISITOR"),
QueueUploadAvatar: helper.GetEnvString("AMQP_QUEUE_UPLOAD_AVATAR"),
QueueUpdateShortener: helper.GetEnvString("AMQP_QUEUE_UPDATE_SHORTENER"),
},
Secret: &Secret{
JWTSecret: helper.GetEnvString("JWT_SECRET"),
Expand Down
42 changes: 40 additions & 2 deletions user/internal/v1/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
GenerateShort(ctx *fiber.Ctx) error
EditProfile(ctx *fiber.Ctx) error
UploadAvatar(ctx *fiber.Ctx) error
UpdateShort(ctx *fiber.Ctx) error
}

// UserControllerImpl is an app user struct that consists of all the dependencies needed for user controller
Expand Down Expand Up @@ -112,7 +113,7 @@ func (uc *UserControllerImpl) Dashboard(ctx *fiber.Ctx) error {
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization Bearer <Place Access Token Here>"
// @Param short body model.GenerateShortUserRequest true "generate short user"
// @Param short body model.ShortUserRequest true "generate short user"
// @Success 201 {object} helper.BaseResponse
// @Failure 400 {object} helper.BaseResponse
// @Failure 500 {object} helper.BaseResponse
Expand All @@ -122,7 +123,7 @@ func (uc *UserControllerImpl) GenerateShort(ctx *fiber.Ctx) error {
_, span := tr.Start(uc.Context, "Start GenerateShort")
defer span.End()

var req model.GenerateShortUserRequest
var req model.ShortUserRequest

data := ctx.Locals(model.KeyJWTValidAccess)
extData, err := middleware.Extract(data)
Expand Down Expand Up @@ -211,3 +212,40 @@ func (uc *UserControllerImpl) UploadAvatar(ctx *fiber.Ctx) error {

return helper.NewResponses[any](ctx, fiber.StatusOK, "Success upload avatar users", resp, nil, nil)
}

// Check godoc
// @Summary Update Users Short URL
// @Tags User
// @Accept json
// @Produce json
// @Param id path string true "id short urls"
// @Param Authorization header string true "Authorization Bearer <Place Access Token Here>"
// @Param short body model.ShortUserRequest true "update short user"
// @Success 200 {object} helper.BaseResponse
// @Failure 400 {object} helper.BaseResponse
// @Failure 404 {object} helper.BaseResponse
// @Failure 500 {object} helper.BaseResponse
// @Router /short/{id} [put]
func (uc *UserControllerImpl) UpdateShort(ctx *fiber.Ctx) error {
tr := uc.Tracer.Tracer("User-UpdateShort Controller")
_, span := tr.Start(uc.Context, "Start UpdateShort")
defer span.End()

var req model.ShortUserRequest

if err := ctx.BodyParser(&req); err != nil {
return helper.NewResponses[any](ctx, fiber.StatusBadRequest, err.Error(), nil, err, nil)
}

shortID := ctx.Params("id", "")
if shortID == "" {
return helper.NewResponses[any](ctx, fiber.StatusBadRequest, "id required", model.NewError(model.Validation, "ID Required"), nil, nil)
}

newShort, err := uc.UserSvc.UpdateUserShorts(shortID, &req)
if err != nil {
return helper.NewResponses[any](ctx, fiber.StatusInternalServerError, err.Error(), nil, err, nil)
}

return helper.NewResponses[any](ctx, fiber.StatusOK, "Success update Short URL's", newShort, nil, nil)
}
2 changes: 2 additions & 0 deletions user/internal/v1/infrastructure/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func setupRouter(app *application.App) {
v1.Post("/short/generate", middleware.ValidateJWTMiddleware, dep.UserController.GenerateShort)

v1.Post("/upload/avatar", middleware.ValidateJWTMiddleware, dep.UserController.UploadAvatar)

v1.Put("/short/:id", middleware.ValidateJWTMiddleware, dep.UserController.UpdateShort)
}

// handler for route not found
Expand Down
8 changes: 4 additions & 4 deletions user/internal/v1/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type (
Visited int64 `json:"visited"`
}

// GenerateShortUserRequest consist request data generate short users
GenerateShortUserRequest struct {
// ShortUserRequest consist request data generate/update short users
ShortUserRequest struct {
FullURL string `json:"full_url"`
}

// GenerateShortUserResponse consist response data when success generate short users
GenerateShortUserResponse struct {
// ShortUserResponse consist response data when success generate/update short users
ShortUserResponse struct {
ShortURL string `json:"short_url"`
}

Expand Down
Loading

0 comments on commit 7846bbd

Please sign in to comment.