mirror of
https://github.com/usememos/memos.git
synced 2025-02-14 10:20:49 +01:00
refactor: store
This commit is contained in:
parent
5fc0fb24f4
commit
fbf4afff8e
@ -45,11 +45,3 @@ type MemoFind struct {
|
||||
type MemoDelete struct {
|
||||
ID *int `json:"id"`
|
||||
}
|
||||
|
||||
type MemoService interface {
|
||||
CreateMemo(create *MemoCreate) (*Memo, error)
|
||||
PatchMemo(patch *MemoPatch) (*Memo, error)
|
||||
FindMemoList(find *MemoFind) ([]*Memo, error)
|
||||
FindMemo(find *MemoFind) (*Memo, error)
|
||||
DeleteMemo(delete *MemoDelete) error
|
||||
}
|
||||
|
@ -39,10 +39,3 @@ type ResourceFind struct {
|
||||
type ResourceDelete struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
type ResourceService interface {
|
||||
CreateResource(create *ResourceCreate) (*Resource, error)
|
||||
FindResourceList(find *ResourceFind) ([]*Resource, error)
|
||||
FindResource(find *ResourceFind) (*Resource, error)
|
||||
DeleteResource(delete *ResourceDelete) error
|
||||
}
|
||||
|
@ -47,11 +47,3 @@ type ShortcutFind struct {
|
||||
type ShortcutDelete struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
type ShortcutService interface {
|
||||
CreateShortcut(create *ShortcutCreate) (*Shortcut, error)
|
||||
PatchShortcut(patch *ShortcutPatch) (*Shortcut, error)
|
||||
FindShortcutList(find *ShortcutFind) ([]*Shortcut, error)
|
||||
FindShortcut(find *ShortcutFind) (*Shortcut, error)
|
||||
DeleteShortcut(delete *ShortcutDelete) error
|
||||
}
|
||||
|
@ -55,9 +55,3 @@ type UserFind struct {
|
||||
Name *string `json:"name"`
|
||||
OpenID *string
|
||||
}
|
||||
|
||||
type UserService interface {
|
||||
CreateUser(create *UserCreate) (*User, error)
|
||||
PatchUser(patch *UserPatch) (*User, error)
|
||||
FindUser(find *UserFind) (*User, error)
|
||||
}
|
||||
|
@ -27,11 +27,8 @@ func (m *Main) Run() error {
|
||||
|
||||
s := server.NewServer(m.profile)
|
||||
|
||||
s.ShortcutService = store.NewShortcutService(db)
|
||||
s.MemoService = store.NewMemoService(db)
|
||||
s.UserService = store.NewUserService(db)
|
||||
s.ShortcutService = store.NewShortcutService(db)
|
||||
s.ResourceService = store.NewResourceService(db)
|
||||
storeInstance := store.New(db)
|
||||
s.Store = storeInstance
|
||||
|
||||
m.server = s
|
||||
|
||||
|
@ -21,7 +21,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
Email: &login.Email,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", login.Email)).SetInternal(err)
|
||||
}
|
||||
@ -63,7 +63,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
|
||||
ownerUserFind := api.UserFind{
|
||||
Role: &ownerUserType,
|
||||
}
|
||||
ownerUser, err := s.UserService.FindUser(&ownerUserFind)
|
||||
ownerUser, err := s.Store.FindUser(&ownerUserFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find owner user").SetInternal(err)
|
||||
}
|
||||
@ -91,7 +91,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
Email: &signup.Email,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", signup.Email)).SetInternal(err)
|
||||
}
|
||||
@ -111,7 +111,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
|
||||
PasswordHash: string(passwordHash),
|
||||
OpenID: common.GenUUID(),
|
||||
}
|
||||
user, err = s.UserService.CreateUser(userCreate)
|
||||
user, err = s.Store.CreateUser(userCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func removeUserSession(c echo.Context) error {
|
||||
}
|
||||
|
||||
// Use session to store user.id.
|
||||
func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.HandlerFunc {
|
||||
func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// Skips auth
|
||||
if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping", "/api/status") {
|
||||
@ -79,7 +79,7 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
|
||||
userFind := &api.UserFind{
|
||||
ID: &userID,
|
||||
}
|
||||
user, err := us.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
|
||||
}
|
||||
|
||||
memo, err := s.MemoService.CreateMemo(memoCreate)
|
||||
memo, err := s.Store.CreateMemo(memoCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
||||
}
|
||||
@ -47,7 +47,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
|
||||
}
|
||||
|
||||
memo, err := s.MemoService.PatchMemo(memoPatch)
|
||||
memo, err := s.Store.PatchMemo(memoPatch)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
|
||||
}
|
||||
@ -70,7 +70,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
memoFind.RowStatus = &rowStatus
|
||||
}
|
||||
|
||||
list, err := s.MemoService.FindMemoList(memoFind)
|
||||
list, err := s.Store.FindMemoList(memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
||||
}
|
||||
@ -92,7 +92,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
memoFind := &api.MemoFind{
|
||||
ID: &memoID,
|
||||
}
|
||||
memo, err := s.MemoService.FindMemo(memoFind)
|
||||
memo, err := s.Store.FindMemo(memoFind)
|
||||
if err != nil {
|
||||
if common.ErrorCode(err) == common.NotFound {
|
||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
||||
@ -119,7 +119,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
ID: &memoID,
|
||||
}
|
||||
|
||||
err = s.MemoService.DeleteMemo(memoDelete)
|
||||
err = s.Store.DeleteMemo(memoDelete)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
||||
CreatorID: userID,
|
||||
}
|
||||
|
||||
resource, err := s.ResourceService.CreateResource(resourceCreate)
|
||||
resource, err := s.Store.CreateResource(resourceCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
||||
}
|
||||
@ -65,7 +65,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
||||
resourceFind := &api.ResourceFind{
|
||||
CreatorID: &userID,
|
||||
}
|
||||
list, err := s.ResourceService.FindResourceList(resourceFind)
|
||||
list, err := s.Store.FindResourceList(resourceFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
||||
}
|
||||
@ -87,7 +87,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
|
||||
resourceDelete := &api.ResourceDelete{
|
||||
ID: resourceID,
|
||||
}
|
||||
if err := s.ResourceService.DeleteResource(resourceDelete); err != nil {
|
||||
if err := s.Store.DeleteResource(resourceDelete); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,8 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"memos/api"
|
||||
"memos/common"
|
||||
"memos/store"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/securecookie"
|
||||
@ -18,10 +18,7 @@ type Server struct {
|
||||
|
||||
Profile *common.Profile
|
||||
|
||||
UserService api.UserService
|
||||
MemoService api.MemoService
|
||||
ShortcutService api.ShortcutService
|
||||
ResourceService api.ResourceService
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
func NewServer(profile *common.Profile) *Server {
|
||||
@ -65,7 +62,7 @@ func NewServer(profile *common.Profile) *Server {
|
||||
|
||||
apiGroup := e.Group("/api")
|
||||
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return BasicAuthMiddleware(s.UserService, next)
|
||||
return BasicAuthMiddleware(s, next)
|
||||
})
|
||||
s.registerSystemRoutes(apiGroup)
|
||||
s.registerAuthRoutes(apiGroup)
|
||||
|
@ -20,7 +20,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
|
||||
}
|
||||
|
||||
shortcut, err := s.ShortcutService.CreateShortcut(shortcutCreate)
|
||||
shortcut, err := s.Store.CreateShortcut(shortcutCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err)
|
||||
}
|
||||
@ -46,7 +46,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
|
||||
}
|
||||
|
||||
shortcut, err := s.ShortcutService.PatchShortcut(shortcutPatch)
|
||||
shortcut, err := s.Store.PatchShortcut(shortcutPatch)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
|
||||
}
|
||||
@ -64,7 +64,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
shortcutFind := &api.ShortcutFind{
|
||||
CreatorID: &userID,
|
||||
}
|
||||
list, err := s.ShortcutService.FindShortcutList(shortcutFind)
|
||||
list, err := s.Store.FindShortcutList(shortcutFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err)
|
||||
}
|
||||
@ -86,7 +86,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
shortcutFind := &api.ShortcutFind{
|
||||
ID: &shortcutID,
|
||||
}
|
||||
shortcut, err := s.ShortcutService.FindShortcut(shortcutFind)
|
||||
shortcut, err := s.Store.FindShortcut(shortcutFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err)
|
||||
}
|
||||
@ -108,7 +108,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
|
||||
shortcutDelete := &api.ShortcutDelete{
|
||||
ID: shortcutID,
|
||||
}
|
||||
if err := s.ShortcutService.DeleteShortcut(shortcutDelete); err != nil {
|
||||
if err := s.Store.DeleteShortcut(shortcutDelete); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
|
||||
ownerUserFind := api.UserFind{
|
||||
Role: &ownerUserType,
|
||||
}
|
||||
ownerUser, err := s.UserService.FindUser(&ownerUserFind)
|
||||
ownerUser, err := s.Store.FindUser(&ownerUserFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find owner user").SetInternal(err)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
ID: &userID,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch user").SetInternal(err)
|
||||
}
|
||||
@ -49,7 +49,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
||||
userFind := api.UserFind{
|
||||
Email: userPatch.Email,
|
||||
}
|
||||
user, err := s.UserService.FindUser(&userFind)
|
||||
user, err := s.Store.FindUser(&userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by email %s", *userPatch.Email)).SetInternal(err)
|
||||
}
|
||||
@ -73,7 +73,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
||||
userPatch.OpenID = &openID
|
||||
}
|
||||
|
||||
user, err := s.UserService.PatchUser(userPatch)
|
||||
user, err := s.Store.PatchUser(userPatch)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
OpenID: &openID,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
|
||||
}
|
||||
@ -37,7 +37,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err)
|
||||
}
|
||||
|
||||
memo, err := s.MemoService.CreateMemo(memoCreate)
|
||||
memo, err := s.Store.CreateMemo(memoCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
||||
}
|
||||
@ -56,7 +56,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
OpenID: &openID,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
|
||||
}
|
||||
@ -72,7 +72,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
memoFind.RowStatus = &rowStatus
|
||||
}
|
||||
|
||||
list, err := s.MemoService.FindMemoList(memoFind)
|
||||
list, err := s.Store.FindMemoList(memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
||||
}
|
||||
@ -91,7 +91,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
userFind := &api.UserFind{
|
||||
OpenID: &openID,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
user, err := s.Store.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
|
||||
}
|
||||
@ -130,7 +130,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
CreatorID: user.ID,
|
||||
}
|
||||
|
||||
resource, err := s.ResourceService.CreateResource(resourceCreate)
|
||||
resource, err := s.Store.CreateResource(resourceCreate)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)
|
||||
}
|
||||
@ -156,7 +156,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
|
||||
Filename: &filename,
|
||||
}
|
||||
|
||||
resource, err := s.ResourceService.FindResource(resourceFind)
|
||||
resource, err := s.Store.FindResource(resourceFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
|
||||
}
|
||||
|
@ -7,15 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MemoService struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewMemoService(db *DB) *MemoService {
|
||||
return &MemoService{db: db}
|
||||
}
|
||||
|
||||
func (s *MemoService) CreateMemo(create *api.MemoCreate) (*api.Memo, error) {
|
||||
func (s *Store) CreateMemo(create *api.MemoCreate) (*api.Memo, error) {
|
||||
memo, err := createMemo(s.db, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -24,7 +16,7 @@ func (s *MemoService) CreateMemo(create *api.MemoCreate) (*api.Memo, error) {
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (s *MemoService) PatchMemo(patch *api.MemoPatch) (*api.Memo, error) {
|
||||
func (s *Store) PatchMemo(patch *api.MemoPatch) (*api.Memo, error) {
|
||||
memo, err := patchMemo(s.db, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,7 +25,7 @@ func (s *MemoService) PatchMemo(patch *api.MemoPatch) (*api.Memo, error) {
|
||||
return memo, nil
|
||||
}
|
||||
|
||||
func (s *MemoService) FindMemoList(find *api.MemoFind) ([]*api.Memo, error) {
|
||||
func (s *Store) FindMemoList(find *api.MemoFind) ([]*api.Memo, error) {
|
||||
list, err := findMemoList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -42,7 +34,7 @@ func (s *MemoService) FindMemoList(find *api.MemoFind) ([]*api.Memo, error) {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *MemoService) FindMemo(find *api.MemoFind) (*api.Memo, error) {
|
||||
func (s *Store) FindMemo(find *api.MemoFind) (*api.Memo, error) {
|
||||
list, err := findMemoList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -55,7 +47,7 @@ func (s *MemoService) FindMemo(find *api.MemoFind) (*api.Memo, error) {
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (s *MemoService) DeleteMemo(delete *api.MemoDelete) error {
|
||||
func (s *Store) DeleteMemo(delete *api.MemoDelete) error {
|
||||
err := deleteMemo(s.db, delete)
|
||||
if err != nil {
|
||||
return FormatError(err)
|
||||
|
@ -7,15 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ResourceService struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewResourceService(db *DB) *ResourceService {
|
||||
return &ResourceService{db: db}
|
||||
}
|
||||
|
||||
func (s *ResourceService) CreateResource(create *api.ResourceCreate) (*api.Resource, error) {
|
||||
func (s *Store) CreateResource(create *api.ResourceCreate) (*api.Resource, error) {
|
||||
resource, err := createResource(s.db, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -24,7 +16,7 @@ func (s *ResourceService) CreateResource(create *api.ResourceCreate) (*api.Resou
|
||||
return resource, nil
|
||||
}
|
||||
|
||||
func (s *ResourceService) FindResourceList(find *api.ResourceFind) ([]*api.Resource, error) {
|
||||
func (s *Store) FindResourceList(find *api.ResourceFind) ([]*api.Resource, error) {
|
||||
list, err := findResourceList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,7 +25,7 @@ func (s *ResourceService) FindResourceList(find *api.ResourceFind) ([]*api.Resou
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *ResourceService) FindResource(find *api.ResourceFind) (*api.Resource, error) {
|
||||
func (s *Store) FindResource(find *api.ResourceFind) (*api.Resource, error) {
|
||||
list, err := findResourceList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -46,7 +38,7 @@ func (s *ResourceService) FindResource(find *api.ResourceFind) (*api.Resource, e
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (s *ResourceService) DeleteResource(delete *api.ResourceDelete) error {
|
||||
func (s *Store) DeleteResource(delete *api.ResourceDelete) error {
|
||||
err := deleteResource(s.db, delete)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -7,15 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ShortcutService struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewShortcutService(db *DB) *ShortcutService {
|
||||
return &ShortcutService{db: db}
|
||||
}
|
||||
|
||||
func (s *ShortcutService) CreateShortcut(create *api.ShortcutCreate) (*api.Shortcut, error) {
|
||||
func (s *Store) CreateShortcut(create *api.ShortcutCreate) (*api.Shortcut, error) {
|
||||
shortcut, err := createShortcut(s.db, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -24,7 +16,7 @@ func (s *ShortcutService) CreateShortcut(create *api.ShortcutCreate) (*api.Short
|
||||
return shortcut, nil
|
||||
}
|
||||
|
||||
func (s *ShortcutService) PatchShortcut(patch *api.ShortcutPatch) (*api.Shortcut, error) {
|
||||
func (s *Store) PatchShortcut(patch *api.ShortcutPatch) (*api.Shortcut, error) {
|
||||
shortcut, err := patchShortcut(s.db, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,7 +25,7 @@ func (s *ShortcutService) PatchShortcut(patch *api.ShortcutPatch) (*api.Shortcut
|
||||
return shortcut, nil
|
||||
}
|
||||
|
||||
func (s *ShortcutService) FindShortcutList(find *api.ShortcutFind) ([]*api.Shortcut, error) {
|
||||
func (s *Store) FindShortcutList(find *api.ShortcutFind) ([]*api.Shortcut, error) {
|
||||
list, err := findShortcutList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -42,7 +34,7 @@ func (s *ShortcutService) FindShortcutList(find *api.ShortcutFind) ([]*api.Short
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *ShortcutService) FindShortcut(find *api.ShortcutFind) (*api.Shortcut, error) {
|
||||
func (s *Store) FindShortcut(find *api.ShortcutFind) (*api.Shortcut, error) {
|
||||
list, err := findShortcutList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -55,7 +47,7 @@ func (s *ShortcutService) FindShortcut(find *api.ShortcutFind) (*api.Shortcut, e
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (s *ShortcutService) DeleteShortcut(delete *api.ShortcutDelete) error {
|
||||
func (s *Store) DeleteShortcut(delete *api.ShortcutDelete) error {
|
||||
err := deleteShortcut(s.db, delete)
|
||||
if err != nil {
|
||||
return FormatError(err)
|
||||
|
13
store/store.go
Normal file
13
store/store.go
Normal file
@ -0,0 +1,13 @@
|
||||
package store
|
||||
|
||||
// Store provides database access to all raw objects
|
||||
type Store struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
// New creates a new instance of Store
|
||||
func New(db *DB) *Store {
|
||||
return &Store{
|
||||
db: db,
|
||||
}
|
||||
}
|
@ -7,15 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewUserService(db *DB) *UserService {
|
||||
return &UserService{db: db}
|
||||
}
|
||||
|
||||
func (s *UserService) CreateUser(create *api.UserCreate) (*api.User, error) {
|
||||
func (s *Store) CreateUser(create *api.UserCreate) (*api.User, error) {
|
||||
user, err := createUser(s.db, create)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -24,7 +16,7 @@ func (s *UserService) CreateUser(create *api.UserCreate) (*api.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *UserService) PatchUser(patch *api.UserPatch) (*api.User, error) {
|
||||
func (s *Store) PatchUser(patch *api.UserPatch) (*api.User, error) {
|
||||
user, err := patchUser(s.db, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,7 +25,7 @@ func (s *UserService) PatchUser(patch *api.UserPatch) (*api.User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *UserService) FindUser(find *api.UserFind) (*api.User, error) {
|
||||
func (s *Store) FindUser(find *api.UserFind) (*api.User, error) {
|
||||
list, err := findUserList(s.db, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
x
Reference in New Issue
Block a user