Refactor channel service, extract find-by-id code

This commit is contained in:
Denis Arh
2019-09-09 13:04:12 +02:00
parent 904b38c3e3
commit 15c003fe3c
+16 -8
View File
@@ -103,10 +103,7 @@ func (svc *channel) With(ctx context.Context) ChannelService {
}
func (svc *channel) FindByID(ID uint64) (ch *types.Channel, err error) {
ch, err = svc.channel.FindByID(ID)
if err != nil {
return
} else if err = svc.preloadExtras(types.ChannelSet{ch}); err != nil {
if ch, err = svc.findByID(ID); err != nil {
return
}
@@ -117,6 +114,17 @@ func (svc *channel) FindByID(ID uint64) (ch *types.Channel, err error) {
return
}
func (svc *channel) findByID(ID uint64) (ch *types.Channel, err error) {
ch, err = svc.channel.FindByID(ID)
if err != nil {
return
} else if err = svc.preloadExtras(types.ChannelSet{ch}); err != nil {
return
}
return
}
func (svc *channel) Find(filter *types.ChannelFilter) (cc types.ChannelSet, err error) {
filter.CurrentUserID = auth.GetIdentityFromContext(svc.ctx).Identity()
@@ -467,7 +475,7 @@ func (svc *channel) Delete(ID uint64) (ch *types.Channel, err error) {
return ch, svc.db.Transaction(func() (err error) {
var userID = repository.Identity(svc.ctx)
if ch, err = svc.FindByID(ID); err != nil {
if ch, err = svc.findByID(ID); err != nil {
return
}
@@ -505,7 +513,7 @@ func (svc *channel) Undelete(ID uint64) (ch *types.Channel, err error) {
return ch, svc.db.Transaction(func() (err error) {
var userID = repository.Identity(svc.ctx)
if ch, err = svc.FindByID(ID); err != nil {
if ch, err = svc.findByID(ID); err != nil {
return
}
@@ -574,7 +582,7 @@ func (svc *channel) Archive(ID uint64) (ch *types.Channel, err error) {
return ch, svc.db.Transaction(func() (err error) {
var userID = repository.Identity(svc.ctx)
if ch, err = svc.FindByID(ID); err != nil {
if ch, err = svc.findByID(ID); err != nil {
return
}
@@ -608,7 +616,7 @@ func (svc *channel) Unarchive(ID uint64) (ch *types.Channel, err error) {
return ch, svc.db.Transaction(func() (err error) {
var userID = repository.Identity(svc.ctx)
if ch, err = svc.FindByID(ID); err != nil {
if ch, err = svc.findByID(ID); err != nil {
return
}