chore(subscription): return subscription entity type using json Marshaler
(cherry picked from commit b60efbd259466637cd9331e5a2101eced07808e8)
This commit is contained in:
parent
313b99e296
commit
0a29a88a26
@ -17,6 +17,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -37,6 +38,56 @@ const (
|
|||||||
SubscriptionEntityTask
|
SubscriptionEntityTask
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (st *SubscriptionEntityType) UnmarshalJSON(bytes []byte) error {
|
||||||
|
var value string
|
||||||
|
err := json.Unmarshal(bytes, &value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch value {
|
||||||
|
case "project":
|
||||||
|
*st = SubscriptionEntityProject
|
||||||
|
case "task":
|
||||||
|
*st = SubscriptionEntityTask
|
||||||
|
default:
|
||||||
|
return &ErrUnknownSubscriptionEntityType{EntityType: *st}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st SubscriptionEntityType) MarshalJSON() ([]byte, error) {
|
||||||
|
switch st {
|
||||||
|
case SubscriptionEntityProject:
|
||||||
|
return []byte(`"project"`), nil
|
||||||
|
case SubscriptionEntityTask:
|
||||||
|
return []byte(`"task"`), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(`nil`), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEntityTypeFromString(entityType string) SubscriptionEntityType {
|
||||||
|
switch entityType {
|
||||||
|
case entityProject:
|
||||||
|
return SubscriptionEntityProject
|
||||||
|
case entityTask:
|
||||||
|
return SubscriptionEntityTask
|
||||||
|
}
|
||||||
|
|
||||||
|
return SubscriptionEntityUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st SubscriptionEntityType) validate() error {
|
||||||
|
if st == SubscriptionEntityProject ||
|
||||||
|
st == SubscriptionEntityTask {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ErrUnknownSubscriptionEntityType{EntityType: st}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
entityProject = `project`
|
entityProject = `project`
|
||||||
entityTask = `task`
|
entityTask = `task`
|
||||||
@ -47,8 +98,8 @@ type Subscription struct {
|
|||||||
// The numeric ID of the subscription
|
// The numeric ID of the subscription
|
||||||
ID int64 `xorm:"autoincr not null unique pk" json:"id"`
|
ID int64 `xorm:"autoincr not null unique pk" json:"id"`
|
||||||
|
|
||||||
EntityType SubscriptionEntityType `xorm:"index not null" json:"-"`
|
EntityType SubscriptionEntityType `xorm:"index not null" json:"entity"`
|
||||||
Entity string `xorm:"-" json:"entity" param:"entity"`
|
Entity string `xorm:"-" json:"-" param:"entity"`
|
||||||
// The id of the entity to subscribe to.
|
// The id of the entity to subscribe to.
|
||||||
EntityID int64 `xorm:"bigint index not null" json:"entity_id" param:"entityID"`
|
EntityID int64 `xorm:"bigint index not null" json:"entity_id" param:"entityID"`
|
||||||
|
|
||||||
@ -78,38 +129,6 @@ func (sb *Subscription) TableName() string {
|
|||||||
return "subscriptions"
|
return "subscriptions"
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEntityTypeFromString(entityType string) SubscriptionEntityType {
|
|
||||||
switch entityType {
|
|
||||||
case entityProject:
|
|
||||||
return SubscriptionEntityProject
|
|
||||||
case entityTask:
|
|
||||||
return SubscriptionEntityTask
|
|
||||||
}
|
|
||||||
|
|
||||||
return SubscriptionEntityUnknown
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns a human-readable string of an entity
|
|
||||||
func (et SubscriptionEntityType) String() string {
|
|
||||||
switch et {
|
|
||||||
case SubscriptionEntityProject:
|
|
||||||
return entityProject
|
|
||||||
case SubscriptionEntityTask:
|
|
||||||
return entityTask
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (et SubscriptionEntityType) validate() error {
|
|
||||||
if et == SubscriptionEntityProject ||
|
|
||||||
et == SubscriptionEntityTask {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ErrUnknownSubscriptionEntityType{EntityType: et}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create subscribes the current user to an entity
|
// Create subscribes the current user to an entity
|
||||||
// @Summary Subscribes the current user to an entity.
|
// @Summary Subscribes the current user to an entity.
|
||||||
// @Description Subscribes the current user to an entity.
|
// @Description Subscribes the current user to an entity.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user