refactor: store

This commit is contained in:
boojack
2022-05-16 07:37:23 +08:00
parent 5fc0fb24f4
commit fbf4afff8e
19 changed files with 65 additions and 119 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}