Skip to content

Commit

Permalink
Merge pull request #13 from jason810496/main
Browse files Browse the repository at this point in the history
add: add post router for user unsubscribe event
  • Loading branch information
peterxcli authored Feb 18, 2024
2 parents f4dfed7 + 3664dbf commit 3999871
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 25 deletions.
23 changes: 0 additions & 23 deletions docs/.swagger-codegen-ignore

This file was deleted.

1 change: 0 additions & 1 deletion docs/.swagger-codegen/VERSION

This file was deleted.

Empty file removed docs/README.md
Empty file.
43 changes: 43 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
43 changes: 43 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
27 changes: 27 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions pkg/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/router/user_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 3999871

Please sign in to comment.