Skip to content

Commit

Permalink
refactor: raw struct for store
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed May 19, 2022
1 parent 0b50122 commit bc22f69
Show file tree
Hide file tree
Showing 22 changed files with 687 additions and 188 deletions.
21 changes: 21 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

// RowStatus is the status for a row.
type RowStatus string

const (
// Normal is the status for a normal row.
Normal RowStatus = "NORMAL"
// Archived is the status for an archived row.
Archived RowStatus = "ARCHIVED"
)

func (e RowStatus) String() string {
switch e {
case Normal:
return "NORMAL"
case Archived:
return "ARCHIVED"
}
return ""
}
20 changes: 12 additions & 8 deletions api/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ type Memo struct {
ID int `json:"id"`

// Standard fields
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`
RowStatus string `json:"rowStatus"`
RowStatus RowStatus `json:"rowStatus"`
CreatorID int `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`

// Domain specific fields
Content string `json:"content"`
CreatorID int `json:"creatorId"`
Content string `json:"content"`
Pinned bool `json:"pinned"`
}

type MemoCreate struct {
Expand All @@ -27,7 +28,7 @@ type MemoPatch struct {
ID int

// Standard fields
RowStatus *string `json:"rowStatus"`
RowStatus *RowStatus `json:"rowStatus"`

// Domain specific fields
Content *string `json:"content"`
Expand All @@ -37,8 +38,11 @@ type MemoFind struct {
ID *int `json:"id"`

// Standard fields
CreatorID *int `json:"creatorId"`
RowStatus *string `json:"rowStatus"`
RowStatus *RowStatus `json:"rowStatus"`
CreatorID *int `json:"creatorId"`

// Domain specific fields
Pinned *bool
}

type MemoDelete struct {
Expand Down
21 changes: 21 additions & 0 deletions api/memo_organizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

type MemoOrganizer struct {
ID int

// Domain specific fields
MemoID int
UserID int
Pinned bool
}

type MemoOrganizerFind struct {
MemoID int
UserID int
}

type MemoOrganizerUpsert struct {
MemoID int
UserID int
Pinned bool `json:"pinned"`
}
10 changes: 5 additions & 5 deletions api/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ type Shortcut struct {
ID int `json:"id"`

// Standard fields
CreatorID int
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`
RowStatus string `json:"rowStatus"`
RowStatus RowStatus `json:"rowStatus"`
CreatorID int `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`

// Domain specific fields
Title string `json:"title"`
Expand All @@ -27,7 +27,7 @@ type ShortcutPatch struct {
ID int

// Standard fields
RowStatus *string `json:"rowStatus"`
RowStatus *RowStatus `json:"rowStatus"`

// Domain specific fields
Title *string `json:"title"`
Expand Down
21 changes: 19 additions & 2 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ const (
NormalUser Role = "USER"
)

func (e Role) String() string {
switch e {
case Owner:
return "OWNER"
case NormalUser:
return "USER"
}
return "USER"
}

type User struct {
ID int `json:"id"`

// Standard fields
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`
RowStatus RowStatus `json:"rowStatus"`
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`

// Domain specific fields
Email string `json:"email"`
Expand All @@ -38,6 +49,9 @@ type UserCreate struct {
type UserPatch struct {
ID int

// Standard fields
RowStatus *RowStatus `json:"rowStatus"`

// Domain specific fields
Email *string `json:"email"`
Name *string `json:"name"`
Expand All @@ -50,6 +64,9 @@ type UserPatch struct {
type UserFind struct {
ID *int `json:"id"`

// Standard fields
RowStatus *RowStatus `json:"rowStatus"`

// Domain specific fields
Email *string `json:"email"`
Role *Role
Expand Down
2 changes: 2 additions & 0 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
}
if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User not found with email %s", login.Email))
} else if user.RowStatus == api.Archived {
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", login.Email))
}

// Compare the stored hashed password, with the hashed version of the password that was received.
Expand Down
2 changes: 2 additions & 0 deletions server/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
}
if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userID))
} else if user.RowStatus == api.Archived {
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("User has been archived with email %s", user.Email))
}

// Stores userID into context.
Expand Down
47 changes: 46 additions & 1 deletion server/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoFind := &api.MemoFind{
CreatorID: &userID,
}
rowStatus := c.QueryParam("rowStatus")

rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
if rowStatus != "" {
memoFind.RowStatus = &rowStatus
}
pinnedStr := c.QueryParam("pinned")
if pinnedStr != "" {
pinned := pinnedStr == "true"
memoFind.Pinned = &pinned
}

list, err := s.Store.FindMemoList(memoFind)
if err != nil {
Expand All @@ -83,6 +89,45 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil
})

g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
}

userID := c.Get(getUserIDContextKey()).(int)
memoOrganizerUpsert := &api.MemoOrganizerUpsert{
MemoID: memoID,
UserID: userID,
}
if err := json.NewDecoder(c.Request().Body).Decode(memoOrganizerUpsert); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
}

err = s.Store.UpsertMemoOrganizer(memoOrganizerUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
}

memo, err := s.Store.FindMemo(&api.MemoFind{
ID: &memoID,
})
if err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
}

return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
}

c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
}

return nil
})

g.GET("/memo/:memoId", func(c echo.Context) error {
memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil {
Expand Down
63 changes: 50 additions & 13 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"memos/api"
"memos/common"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
Expand Down Expand Up @@ -84,19 +85,6 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
}

if userPatch.Email != nil {
userFind := api.UserFind{
Email: userPatch.Email,
}
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)
}
if user != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("User with email %s existed", *userPatch.Email)).SetInternal(err)
}
}

if userPatch.Password != nil && *userPatch.Password != "" {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
if err != nil {
Expand Down Expand Up @@ -124,4 +112,53 @@ func (s *Server) registerUserRoutes(g *echo.Group) {

return nil
})

g.PATCH("/user/:userId", func(c echo.Context) error {
currentUserID := c.Get(getUserIDContextKey()).(int)
currentUser, err := s.Store.FindUser(&api.UserFind{
ID: &currentUserID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
if currentUser == nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Current session user not found with ID: %d", currentUserID)).SetInternal(err)
} else if currentUser.Role != api.Owner {
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden for current session user").SetInternal(err)
}

userID, err := strconv.Atoi(c.Param("userId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("userId"))).SetInternal(err)
}

userPatch := &api.UserPatch{
ID: userID,
}
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
}

if userPatch.Password != nil && *userPatch.Password != "" {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
}

passwordHashStr := string(passwordHash)
userPatch.PasswordHash = &passwordHashStr
}

user, err := s.Store.PatchUser(userPatch)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
}

c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
}

return nil
})
}
2 changes: 1 addition & 1 deletion server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
memoFind := &api.MemoFind{
CreatorID: &user.ID,
}
rowStatus := c.QueryParam("rowStatus")
rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
if rowStatus != "" {
memoFind.RowStatus = &rowStatus
}
Expand Down
Loading

0 comments on commit bc22f69

Please sign in to comment.