Add event join service and api endpoint
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
70
api/event/join.go
Normal file
70
api/event/join.go
Normal file
@@ -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())
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ endpoint:
|
|||||||
checkin_query: "03"
|
checkin_query: "03"
|
||||||
checkin_submit: "04"
|
checkin_submit: "04"
|
||||||
list: "05"
|
list: "05"
|
||||||
|
join: "06"
|
||||||
user:
|
user:
|
||||||
service:
|
service:
|
||||||
info: "01"
|
info: "01"
|
||||||
|
|||||||
122
docs/docs.go
122
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": {
|
"/event/list": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"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": {
|
"service_user.UserInfoData": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|||||||
@@ -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": {
|
"/event/list": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"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": {
|
"service_user.UserInfoData": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|||||||
@@ -100,6 +100,17 @@ definitions:
|
|||||||
checkin_code:
|
checkin_code:
|
||||||
type: string
|
type: string
|
||||||
type: object
|
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:
|
service_user.UserInfoData:
|
||||||
properties:
|
properties:
|
||||||
allow_public:
|
allow_public:
|
||||||
@@ -636,6 +647,63 @@ paths:
|
|||||||
summary: Get Event Information
|
summary: Get Event Information
|
||||||
tags:
|
tags:
|
||||||
- Event
|
- 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:
|
/event/list:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
|||||||
@@ -1,11 +1,24 @@
|
|||||||
package service_event
|
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 {
|
type EventJoinData struct {
|
||||||
|
EventId string `json:"event_id"`
|
||||||
|
UserId string `json:"user_id"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
State string `json:"state"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventJoinPayload struct {
|
type EventJoinPayload struct {
|
||||||
|
Context context.Context
|
||||||
|
Data *EventJoinData
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventJoinResult struct {
|
type EventJoinResult struct {
|
||||||
@@ -13,5 +26,93 @@ type EventJoinResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *EventServiceImpl) JoinEvent(payload *EventJoinPayload) (result *EventJoinResult) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user