From 3664dbf85e2bec323f40c4d892c6da8a275f771d Mon Sep 17 00:00:00 2001 From: jason810496 <810496@email.wlsh.tyc.edu.tw> Date: Sun, 18 Feb 2024 14:40:30 +0800 Subject: [PATCH] add: add post router for user unsubscribe event --- docs/.swagger-codegen-ignore | 23 ----------------- docs/.swagger-codegen/VERSION | 1 - docs/README.md | 0 docs/docs.go | 43 +++++++++++++++++++++++++++++++ docs/swagger.json | 43 +++++++++++++++++++++++++++++++ docs/swagger.yaml | 27 +++++++++++++++++++ pkg/controller/user_controller.go | 37 ++++++++++++++++++++++++++ pkg/middleware/cors.go | 3 ++- pkg/router/user_route.go | 2 ++ 9 files changed, 154 insertions(+), 25 deletions(-) delete mode 100644 docs/.swagger-codegen-ignore delete mode 100644 docs/.swagger-codegen/VERSION delete mode 100644 docs/README.md diff --git a/docs/.swagger-codegen-ignore b/docs/.swagger-codegen-ignore deleted file mode 100644 index c5fa491..0000000 --- a/docs/.swagger-codegen-ignore +++ /dev/null @@ -1,23 +0,0 @@ -# Swagger Codegen Ignore -# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen - -# Use this file to prevent files from being overwritten by the generator. -# The patterns follow closely to .gitignore or .dockerignore. - -# As an example, the C# client generator defines ApiClient.cs. -# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line: -#ApiClient.cs - -# You can match any string of characters against a directory, file or extension with a single asterisk (*): -#foo/*/qux -# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux - -# You can recursively match patterns against a directory, file or extension with a double asterisk (**): -#foo/**/qux -# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux - -# You can also negate patterns with an exclamation (!). -# For example, you can ignore all files in a docs folder with the file extension .md: -#docs/*.md -# Then explicitly reverse the ignore rule for a single file: -#!docs/README.md diff --git a/docs/.swagger-codegen/VERSION b/docs/.swagger-codegen/VERSION deleted file mode 100644 index 97e616f..0000000 --- a/docs/.swagger-codegen/VERSION +++ /dev/null @@ -1 +0,0 @@ -3.0.52 \ No newline at end of file diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/docs/docs.go b/docs/docs.go index 60057d8..6c7db01 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -427,6 +427,49 @@ const docTemplate = `{ } } }, + "/users/events/delete/{event_id}": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Deletes a specific event by its ID for a given user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Delete event", + "parameters": [ + { + "type": "string", + "description": "Event ID", + "name": "event_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Event successfully deleted", + "schema": { + "$ref": "#/definitions/bikefest_pkg_model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/bikefest_pkg_model.Response" + } + } + } + } + }, "/users/events/{event_id}": { "delete": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index a2bfa5d..4458522 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -416,6 +416,49 @@ } } }, + "/users/events/delete/{event_id}": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Deletes a specific event by its ID for a given user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Delete event", + "parameters": [ + { + "type": "string", + "description": "Event ID", + "name": "event_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Event successfully deleted", + "schema": { + "$ref": "#/definitions/bikefest_pkg_model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/bikefest_pkg_model.Response" + } + } + } + } + }, "/users/events/{event_id}": { "delete": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a316e40..3e46d4b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -451,6 +451,33 @@ paths: event notification to user immediately tags: - User + /users/events/delete/{event_id}: + post: + consumes: + - application/json + description: Deletes a specific event by its ID for a given user + parameters: + - description: Event ID + in: path + name: event_id + required: true + type: string + produces: + - application/json + responses: + "200": + description: Event successfully deleted + schema: + $ref: '#/definitions/bikefest_pkg_model.Response' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/bikefest_pkg_model.Response' + security: + - ApiKeyAuth: [] + summary: Delete event + tags: + - User /users/login/{user_id}: get: consumes: diff --git a/pkg/controller/user_controller.go b/pkg/controller/user_controller.go index f9fa335..4941a99 100644 --- a/pkg/controller/user_controller.go +++ b/pkg/controller/user_controller.go @@ -465,6 +465,43 @@ func (ctrl *UserController) UnScribeEvent(c *gin.Context) { }) } + +// UnScribeEvent godoc +// @Summary Delete event +// @Description Deletes a specific event by its ID for a given user +// @Tags User +// @Accept json +// @Produce json +// @Security ApiKeyAuth +// @Param event_id path string true "Event ID" +// @Success 200 {object} model.Response "Event successfully deleted" +// @Failure 500 {object} model.Response "Internal Server Error" +// @Router /users/events/delete/{event_id} [post] +func (ctrl *UserController) UnScribeDeleteEvent(c *gin.Context) { + identity, _ := RetrieveIdentity(c, true) + userID := identity.UserID + eventID := c.Param("event_id") + err := ctrl.userSvc.UnsubscribeEvent(c, userID, eventID) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{ + Msg: err.Error(), + }) + return + } + + err = ctrl.asynqService.DeleteEventNotification(c, userID+eventID) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{ + Msg: err.Error(), + }) + return + } + + c.JSON(200, model.Response{ + Msg: "Delete success", + }) +} + // GetUserSubscribeEvents godoc // @Summary Get User Events // @Description Retrieves a list of events associated with a user diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index 08fc5d0..323ffd7 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -5,7 +5,8 @@ import "github.com/gin-gonic/gin" func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { //TODO: add production frontend url in allow origin - c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173") + origin := c.Request.Header.Get("Origin") + c.Writer.Header().Set("Access-Control-Allow-Origin", origin) c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Content-Range, Accept-Ranges, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, grant_type, code") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE") diff --git a/pkg/router/user_route.go b/pkg/router/user_route.go index 5eae62f..f6f5473 100644 --- a/pkg/router/user_route.go +++ b/pkg/router/user_route.go @@ -22,4 +22,6 @@ func RegisterUserRoutes(app *bootstrap.Application, controller *controller.UserC r.POST("/events", authMiddleware, controller.SubscribeEvent) r.POST("/events/all", authMiddleware, controller.SubscribeAllEvent) r.DELETE("/events/:event_id", authMiddleware, controller.UnScribeEvent) + + r.POST("/events/delete/:event_id", authMiddleware, controller.UnScribeDeleteEvent) }