diff --git a/api/event/join.go b/api/event/join.go new file mode 100644 index 0000000..5f11fc7 --- /dev/null +++ b/api/event/join.go @@ -0,0 +1,70 @@ +package event + +import ( + "nixcn-cms/internal/exception" + "nixcn-cms/service/service_event" + "nixcn-cms/utils" + + "github.com/gin-gonic/gin" +) + +// Join handles the request for a user to join a specific event. +// +// @Summary Join an Event +// @Description Allows an authenticated user to join an event by providing the event ID. The user's role and state are initialized by the service. +// @Tags Event +// @Accept json +// @Produce json +// @Param request body service_event.EventJoinData true "Event Join Details (UserId and EventId are required)" +// @Success 200 {object} utils.RespStatus{data=nil} "Successfully joined the event" +// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input or UUID Parse Failed" +// @Failure 403 {object} utils.RespStatus{data=nil} "Unauthorized / Missing User ID" +// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error / Database Error" +// @Security ApiKeyAuth +// @Router /event/join [post] +func (self *EventHandler) Join(c *gin.Context) { + userIdOrig, ok := c.Get("user_id") + if !ok { + errorCode := new(exception.Builder). + SetStatus(exception.StatusUser). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorMissingUserId). + Throw(c). + String() + utils.HttpResponse(c, 403, errorCode) + return + } + + var joinData service_event.EventJoinData + if err := c.ShouldBindJSON(&joinData); err != nil { + errorCode := new(exception.Builder). + SetStatus(exception.StatusUser). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorInvalidInput). + SetError(err). + Throw(c). + String() + utils.HttpResponse(c, 400, errorCode) + return + } + + joinData.UserId = userIdOrig.(string) + + payload := &service_event.EventJoinPayload{ + Context: c, + Data: &joinData, + } + + result := self.svc.JoinEvent(payload) + + if result.Common.Exception.Original != exception.CommonSuccess { + utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String()) + return + } + + utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String()) +} diff --git a/cmd/gen_exception/definitions/endpoint.yaml b/cmd/gen_exception/definitions/endpoint.yaml index 34324dc..17f00b9 100644 --- a/cmd/gen_exception/definitions/endpoint.yaml +++ b/cmd/gen_exception/definitions/endpoint.yaml @@ -13,6 +13,7 @@ endpoint: checkin_query: "03" checkin_submit: "04" list: "05" + join: "06" user: service: info: "01" diff --git a/docs/docs.go b/docs/docs.go index 1ea97f3..6ee14d4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -909,6 +909,111 @@ const docTemplate = `{ } } }, + "/event/join": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Allows an authenticated user to join an event by providing the event ID. The user's role and state are initialized by the service.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Event" + ], + "summary": "Join an Event", + "parameters": [ + { + "description": "Event Join Details (UserId and EventId are required)", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/service_event.EventJoinData" + } + } + ], + "responses": { + "200": { + "description": "Successfully joined the event", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "400": { + "description": "Invalid Input or UUID Parse Failed", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "403": { + "description": "Unauthorized / Missing User ID", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error / Database Error", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + } + } + } + }, "/event/list": { "get": { "security": [ @@ -1611,6 +1716,23 @@ const docTemplate = `{ } } }, + "service_event.EventJoinData": { + "type": "object", + "properties": { + "event_id": { + "type": "string" + }, + "role": { + "type": "string" + }, + "state": { + "type": "string" + }, + "user_id": { + "type": "string" + } + } + }, "service_user.UserInfoData": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 496b8bb..c6226eb 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -907,6 +907,111 @@ } } }, + "/event/join": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Allows an authenticated user to join an event by providing the event ID. The user's role and state are initialized by the service.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Event" + ], + "summary": "Join an Event", + "parameters": [ + { + "description": "Event Join Details (UserId and EventId are required)", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/service_event.EventJoinData" + } + } + ], + "responses": { + "200": { + "description": "Successfully joined the event", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "400": { + "description": "Invalid Input or UUID Parse Failed", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "403": { + "description": "Unauthorized / Missing User ID", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error / Database Error", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/utils.RespStatus" + }, + { + "type": "object", + "properties": { + "data": { + "type": "object" + } + } + } + ] + } + } + } + } + }, "/event/list": { "get": { "security": [ @@ -1609,6 +1714,23 @@ } } }, + "service_event.EventJoinData": { + "type": "object", + "properties": { + "event_id": { + "type": "string" + }, + "role": { + "type": "string" + }, + "state": { + "type": "string" + }, + "user_id": { + "type": "string" + } + } + }, "service_user.UserInfoData": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d00646b..31dd3f3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -100,6 +100,17 @@ definitions: checkin_code: type: string type: object + service_event.EventJoinData: + properties: + event_id: + type: string + role: + type: string + state: + type: string + user_id: + type: string + type: object service_user.UserInfoData: properties: allow_public: @@ -636,6 +647,63 @@ paths: summary: Get Event Information tags: - Event + /event/join: + post: + consumes: + - application/json + description: Allows an authenticated user to join an event by providing the + event ID. The user's role and state are initialized by the service. + parameters: + - description: Event Join Details (UserId and EventId are required) + in: body + name: request + required: true + schema: + $ref: '#/definitions/service_event.EventJoinData' + produces: + - application/json + responses: + "200": + description: Successfully joined the event + schema: + allOf: + - $ref: '#/definitions/utils.RespStatus' + - properties: + data: + type: object + type: object + "400": + description: Invalid Input or UUID Parse Failed + schema: + allOf: + - $ref: '#/definitions/utils.RespStatus' + - properties: + data: + type: object + type: object + "403": + description: Unauthorized / Missing User ID + schema: + allOf: + - $ref: '#/definitions/utils.RespStatus' + - properties: + data: + type: object + type: object + "500": + description: Internal Server Error / Database Error + schema: + allOf: + - $ref: '#/definitions/utils.RespStatus' + - properties: + data: + type: object + type: object + security: + - ApiKeyAuth: [] + summary: Join an Event + tags: + - Event /event/list: get: consumes: diff --git a/service/service_event/join_event.go b/service/service_event/join_event.go index bd22a4c..832f682 100644 --- a/service/service_event/join_event.go +++ b/service/service_event/join_event.go @@ -1,11 +1,24 @@ package service_event -import "nixcn-cms/service/shared" +import ( + "context" + "nixcn-cms/data" + "nixcn-cms/internal/exception" + "nixcn-cms/service/shared" + + "github.com/google/uuid" +) type EventJoinData struct { + EventId string `json:"event_id"` + UserId string `json:"user_id"` + Role string `json:"role"` + State string `json:"state"` } type EventJoinPayload struct { + Context context.Context + Data *EventJoinData } type EventJoinResult struct { @@ -13,5 +26,93 @@ type EventJoinResult struct { } func (self *EventServiceImpl) JoinEvent(payload *EventJoinPayload) (result *EventJoinResult) { + var err error + + attendenceData := new(data.Attendance) + + eventId, err := uuid.Parse(payload.Data.EventId) + if err != nil { + exception := new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorUuidParseFailed). + SetError(err). + Throw(payload.Context) + + result = &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 400, + Exception: exception, + }, + } + + return + } + + userId, err := uuid.Parse(payload.Data.UserId) + if err != nil { + exception := new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorUuidParseFailed). + SetError(err). + Throw(payload.Context) + + result = &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 400, + Exception: exception, + }, + } + + return + } + + attendenceData.SetEventId(eventId) + attendenceData.SetUserId(userId) + attendenceData.SetRole("notmal") + attendenceData.SetState("success") + + err = attendenceData.Create(payload.Context) + if err != nil { + exception := new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorDatabase). + SetError(err). + Throw(payload.Context) + + result = &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 500, + Exception: exception, + }, + } + + return + } + + exception := new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonSuccess). + SetError(nil). + Throw(payload.Context) + + result = &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 200, + Exception: exception, + }, + } + return }