storage: add GetSourceType
This commit is contained in:
parent
20d4299239
commit
0625d8f5ec
|
@ -52,7 +52,7 @@ func init() {
|
|||
|
||||
router.GET("/api/sources/:sid", apiAuthHandler(sourceHandler(getSource)))
|
||||
router.PUT("/api/sources/:sid", apiAuthHandler(sourceHandler(updateSource)))
|
||||
router.DELETE("/api/sources/:sid", apiAuthHandler(sourceHandler(deleteSource)))
|
||||
router.DELETE("/api/sources/:sid", apiAuthHandler(sourceTypeHandler(deleteSource)))
|
||||
|
||||
router.GET("/api/sources/:sid/domains", apiAuthHandler(sourceHandler(getDomainsHostedBySource)))
|
||||
}
|
||||
|
@ -73,6 +73,18 @@ func getSources(_ *config.Options, u *happydns.User, p httprouter.Params, body i
|
|||
}
|
||||
}
|
||||
|
||||
func sourceTypeHandler(f func(*config.Options, *happydns.SourceType, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
|
||||
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
|
||||
if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if srcType, err := storage.MainStore.GetSourceType(u, sid); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else {
|
||||
return f(opts, srcType, u, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sourceHandler(f func(*config.Options, *happydns.SourceCombined, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
|
||||
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
|
||||
if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil {
|
||||
|
@ -167,8 +179,8 @@ func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.Use
|
|||
}
|
||||
}
|
||||
|
||||
func deleteSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response {
|
||||
if err := storage.MainStore.DeleteSource(&s.SourceType); err != nil {
|
||||
func deleteSource(_ *config.Options, st *happydns.SourceType, u *happydns.User, body io.Reader) Response {
|
||||
if err := storage.MainStore.DeleteSource(st); err != nil {
|
||||
return APIErrorResponse{
|
||||
err: err,
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ type Storage interface {
|
|||
|
||||
GetSourceTypes(u *happydns.User) ([]happydns.SourceType, error)
|
||||
GetSource(u *happydns.User, id int64) (*happydns.SourceCombined, error)
|
||||
GetSourceType(u *happydns.User, id int64) (*happydns.SourceType, error)
|
||||
CreateSource(u *happydns.User, s happydns.Source, comment string) (*happydns.SourceCombined, error)
|
||||
UpdateSource(s *happydns.SourceCombined) error
|
||||
UpdateSourceOwner(s *happydns.SourceCombined, newOwner *happydns.User) error
|
||||
|
|
|
@ -70,6 +70,27 @@ func (s *LevelDBStorage) GetSourceTypes(u *happydns.User) (srcs []happydns.Sourc
|
|||
return
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) GetSourceType(u *happydns.User, id int64) (srcType *happydns.SourceType, err error) {
|
||||
var v []byte
|
||||
v, err = s.db.Get([]byte(fmt.Sprintf("source-%d", id)), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
srcType = new(happydns.SourceType)
|
||||
err = decodeData(v, &srcType)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if srcType.OwnerId != u.Id {
|
||||
srcType = nil
|
||||
err = leveldb.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) GetSource(u *happydns.User, id int64) (src *happydns.SourceCombined, err error) {
|
||||
var v []byte
|
||||
v, err = s.db.Get([]byte(fmt.Sprintf("source-%d", id)), nil)
|
||||
|
|
Loading…
Reference in New Issue