Add getting subscriptions when getting entities

This commit is contained in:
kolaente 2021-02-13 20:53:59 +01:00
parent a7c5f327be
commit 3a33805919
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
8 changed files with 75 additions and 13 deletions

View File

@ -69,7 +69,8 @@ type List struct {
IsFavorite bool `xorm:"default false" json:"is_favorite"`
// The subscription status for the user reading this list. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// Will only returned when retreiving one list.
Subscription *Subscription `xorm:"-" json:"subscription"`
// A timestamp when this list was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
@ -239,6 +240,7 @@ func (l *List) ReadOne(s *xorm.Session, a web.Auth) (err error) {
}
}
l.Subscription, err = getSubscription(s, SubscriptionEntityList, l.ID, a)
return nil
}

View File

@ -215,3 +215,34 @@ func TestList_ReadAll(t *testing.T) {
_ = s.Close()
})
}
func TestList_ReadOne(t *testing.T) {
t.Run("normal", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 1}
l := &List{ID: 1}
can, _, err := l.CanRead(s, u)
assert.NoError(t, err)
assert.True(t, can)
err = l.ReadOne(s, u)
assert.NoError(t, err)
assert.Equal(t, "Test1", l.Title)
})
t.Run("with subscription", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
u := &user.User{ID: 6}
l := &List{ID: 12}
can, _, err := l.CanRead(s, u)
assert.NoError(t, err)
assert.True(t, can)
err = l.ReadOne(s, u)
assert.NoError(t, err)
assert.NotNil(t, l.Subscription)
})
}

View File

@ -51,7 +51,8 @@ type Namespace struct {
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
// The subscription status for the user reading this namespace. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// Will only returned when retreiving one namespace.
Subscription *Subscription `xorm:"-" json:"subscription"`
// A timestamp when this namespace was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
@ -169,6 +170,8 @@ func (n *Namespace) ReadOne(s *xorm.Session, a web.Auth) (err error) {
return err
}
*n = *nn
n.Subscription, err = getSubscription(s, SubscriptionEntityNamespace, n.ID, a)
return
}

View File

@ -75,19 +75,31 @@ func TestNamespace_ReadOne(t *testing.T) {
n := &Namespace{ID: 1}
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
err := n.ReadOne(s, u)
assert.NoError(t, err)
assert.Equal(t, n.Title, "testnamespace")
_ = s.Close()
})
t.Run("nonexistant", func(t *testing.T) {
n := &Namespace{ID: 99999}
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
err := n.ReadOne(s, u)
assert.Error(t, err)
assert.True(t, IsErrNamespaceDoesNotExist(err))
_ = s.Close()
})
t.Run("with subscription", func(t *testing.T) {
n := &Namespace{ID: 1}
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
err := n.ReadOne(s, &user.User{ID: 6})
assert.NoError(t, err)
assert.NotNil(t, n.Subscription)
})
}

View File

@ -87,7 +87,7 @@ func (et SubscriptionEntityType) validate() error {
// It will return the next parent of a subscription. That means for tasks, it will first look for a subscription for
// that task, if there is none it will look for a subscription on the list the task belongs to and if that also
// doesn't exist it will check for a subscription for the namespace the list is belonging to.
func getSubscription(s *xorm.Session, entityType SubscriptionEntityType, a web.Auth, entityID int64) (subscription *Subscription, err error) {
func getSubscription(s *xorm.Session, entityType SubscriptionEntityType, entityID int64, a web.Auth) (subscription *Subscription, err error) {
u, is := a.(*user.User)
if !is {
return

View File

@ -272,7 +272,7 @@ func TestSubscriptionGet(t *testing.T) {
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityNamespace, u, 6)
sub, err := getSubscription(s, SubscriptionEntityNamespace, 6, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(2), sub.ID)
@ -282,7 +282,7 @@ func TestSubscriptionGet(t *testing.T) {
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityList, u, 12)
sub, err := getSubscription(s, SubscriptionEntityList, 12, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(3), sub.ID)
@ -292,7 +292,7 @@ func TestSubscriptionGet(t *testing.T) {
s := db.NewSession()
defer s.Close()
sub, err := getSubscription(s, SubscriptionEntityTask, u, 22)
sub, err := getSubscription(s, SubscriptionEntityTask, 22, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(4), sub.ID)
@ -305,7 +305,7 @@ func TestSubscriptionGet(t *testing.T) {
defer s.Close()
// List 6 belongs to namespace 6 where user 6 has subscribed to
sub, err := getSubscription(s, SubscriptionEntityList, u, 6)
sub, err := getSubscription(s, SubscriptionEntityList, 6, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(2), sub.ID)
@ -316,7 +316,7 @@ func TestSubscriptionGet(t *testing.T) {
defer s.Close()
// Task 20 belongs to list 11 which belongs to namespace 6 where the user has subscribed
sub, err := getSubscription(s, SubscriptionEntityTask, u, 20)
sub, err := getSubscription(s, SubscriptionEntityTask, 20, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(2), sub.ID)
@ -327,7 +327,7 @@ func TestSubscriptionGet(t *testing.T) {
defer s.Close()
// Task 21 belongs to list 12 which the user has subscribed to
sub, err := getSubscription(s, SubscriptionEntityTask, u, 21)
sub, err := getSubscription(s, SubscriptionEntityTask, 21, u)
assert.NoError(t, err)
assert.NotNil(t, sub)
assert.Equal(t, int64(3), sub.ID)
@ -338,7 +338,7 @@ func TestSubscriptionGet(t *testing.T) {
s := db.NewSession()
defer s.Close()
_, err := getSubscription(s, 2342, u, 21)
_, err := getSubscription(s, 2342, 21, u)
assert.Error(t, err)
assert.True(t, IsErrUnknownSubscriptionEntityType(err))
})

View File

@ -92,7 +92,8 @@ type Task struct {
IsFavorite bool `xorm:"default false" json:"is_favorite"`
// The subscription status for the user reading this task. You can only read this property, use the subscription endpoints to modify it.
Subscription *Subscription `xorm:"-" json:"subscription,omitempty"`
// Will only returned when retreiving one task.
Subscription *Subscription `xorm:"-" json:"subscription"`
// A timestamp when this task was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
@ -1244,5 +1245,6 @@ func (t *Task) ReadOne(s *xorm.Session, a web.Auth) (err error) {
*t = *taskMap[t.ID]
t.Subscription, err = getSubscription(s, SubscriptionEntityTask, t.ID, a)
return
}

View File

@ -467,4 +467,16 @@ func TestTask_ReadOne(t *testing.T) {
assert.Error(t, err)
assert.True(t, IsErrTaskDoesNotExist(err))
})
t.Run("with subscription", func(t *testing.T) {
u = &user.User{ID: 6}
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
task := &Task{ID: 12}
err := task.ReadOne(s, u)
assert.NoError(t, err)
assert.NotNil(t, task.Subscription)
})
}