Skip to content

Commit

Permalink
feat: create token schema; apns development mode for dev
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Aug 1, 2023
1 parent 3b6986f commit ada824c
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 56 deletions.
30 changes: 20 additions & 10 deletions apis/token/apis.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package token

import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/opentreehole/go-common"
"gorm.io/gorm"

. "notification/models"
"time"
)

// ListTokens
Expand All @@ -29,16 +31,17 @@ func ListTokens(c *fiber.Ctx) (err error) {
return c.JSON(tokens)
}

// AddToken
// CreateToken
// @Summary Add Token of a User
// @Tags Token
// @Produce application/json
// @Param json body models.PushToken true "json"
// @Param json body CreateTokenRequest true "json"
// @Router /users/push-tokens [post]
// @Router /users/push-tokens [put]
// @Success 200 {object} PushToken
func AddToken(c *fiber.Ctx) (err error) {
var token PushToken
err = common.ValidateBody(c, &token)
func CreateToken(c *fiber.Ctx) (err error) {
var body CreateTokenRequest
err = common.ValidateBody(c, &body)
if err != nil {
return err
}
Expand All @@ -47,17 +50,24 @@ func AddToken(c *fiber.Ctx) (err error) {
if !ok {
return common.Unauthorized()
}
token.UserID = userID

token := PushToken{
UserID: userID,
Service: NewPushService(body.Service),
DeviceID: body.DeviceID,
Token: body.Token,
PackageName: body.PackageName,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

err = DB.Transaction(func(tx *gorm.DB) (err error) {
// remove all device_id duplicates
err = tx.Where("device_id = ?", token.DeviceID).Delete(&PushToken{}).Error
if err != nil {
return err
}

token.CreatedAt = time.Now()
token.UpdatedAt = time.Now()

// create or update new token
return tx.Save(&token).Error
})
Expand Down
5 changes: 3 additions & 2 deletions apis/token/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package token

import (
"context"

"github.com/gofiber/fiber/v2"
)

func RegisterRoutes(app fiber.Router) {
app.Get("/users/push-tokens", ListTokens)
app.Post("/users/push-tokens", AddToken)
app.Put("/users/push-tokens", AddToken)
app.Post("/users/push-tokens", CreateToken)
app.Put("/users/push-tokens", CreateToken)
app.Delete("/users/push-tokens", DeleteToken)
app.Delete("/users/push-tokens/_all", DeleteAllTokens)
}
Expand Down
8 changes: 8 additions & 0 deletions apis/token/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package token

type CreateTokenRequest struct {
DeviceID string `json:"device_id" validate:"max=64"`
Service string `json:"service" validate:"required,oneof=apns fcm mipush"`
Token string `json:"token" validate:"max=64"`
PackageName string `json:"package_name"`
}
78 changes: 63 additions & 15 deletions docs/docs.go

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

78 changes: 63 additions & 15 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
}
}
},
"post": {
"put": {
"produces": [
"application/json"
],
Expand All @@ -112,10 +112,38 @@
"name": "json",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/token.CreateTokenRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.PushToken"
}
}
}
},
"post": {
"produces": [
"application/json"
],
"tags": [
"Token"
],
"summary": "Add Token of a User",
"parameters": [
{
"description": "json",
"name": "json",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/token.CreateTokenRequest"
}
}
],
"responses": {
"200": {
Expand Down Expand Up @@ -216,23 +244,11 @@
}
}
},
"models.PushService": {
"type": "integer",
"enum": [
0,
1,
2
],
"x-enum-varnames": [
"ServiceAPNS",
"ServiceFCM",
"ServiceMipush"
]
},
"models.PushToken": {
"type": "object",
"required": [
"device_id",
"service",
"token"
],
"properties": {
Expand All @@ -247,7 +263,12 @@
"type": "string"
},
"service": {
"$ref": "#/definitions/models.PushService"
"type": "string",
"enum": [
"apns",
"fcm",
"mipush"
]
},
"token": {
"type": "string",
Expand All @@ -262,6 +283,33 @@
}
}
},
"token.CreateTokenRequest": {
"type": "object",
"required": [
"service"
],
"properties": {
"device_id": {
"type": "string",
"maxLength": 64
},
"package_name": {
"type": "string"
},
"service": {
"type": "string",
"enum": [
"apns",
"fcm",
"mipush"
]
},
"token": {
"type": "string",
"maxLength": 64
}
}
},
"token.DeleteModel": {
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit ada824c

Please sign in to comment.