chore: typo `Id` to `ID`

pull/49/head
boojack 3 years ago
parent 13093b6f62
commit d947a512e6

@ -1,23 +1,23 @@
package api package api
type Memo struct { type Memo struct {
Id int `json:"id"` ID int `json:"id"`
CreatedTs int64 `json:"createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"` UpdatedTs int64 `json:"updatedTs"`
RowStatus string `json:"rowStatus"` RowStatus string `json:"rowStatus"`
Content string `json:"content"` Content string `json:"content"`
CreatorId int `json:"creatorId"` CreatorID int `json:"creatorId"`
} }
type MemoCreate struct { type MemoCreate struct {
CreatorId int CreatorID int
Content string `json:"content"` Content string `json:"content"`
} }
type MemoPatch struct { type MemoPatch struct {
Id int ID int
Content *string `json:"content"` Content *string `json:"content"`
RowStatus *string `json:"rowStatus"` RowStatus *string `json:"rowStatus"`
@ -25,13 +25,13 @@ type MemoPatch struct {
} }
type MemoFind struct { type MemoFind struct {
Id *int `json:"id"` ID *int `json:"id"`
CreatorId *int `json:"creatorId"` CreatorID *int `json:"creatorId"`
RowStatus *string `json:"rowStatus"` RowStatus *string `json:"rowStatus"`
} }
type MemoDelete struct { type MemoDelete struct {
Id *int `json:"id"` ID *int `json:"id"`
} }
type MemoService interface { type MemoService interface {

@ -1,7 +1,7 @@
package api package api
type Resource struct { type Resource struct {
Id int `json:"id"` ID int `json:"id"`
CreatedTs int64 `json:"createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"` UpdatedTs int64 `json:"updatedTs"`
@ -10,7 +10,7 @@ type Resource struct {
Type string `json:"type"` Type string `json:"type"`
Size int64 `json:"size"` Size int64 `json:"size"`
CreatorId int `json:"creatorId"` CreatorID int `json:"creatorId"`
} }
type ResourceCreate struct { type ResourceCreate struct {
@ -19,17 +19,17 @@ type ResourceCreate struct {
Type string `json:"type"` Type string `json:"type"`
Size int64 `json:"size"` Size int64 `json:"size"`
CreatorId int CreatorID int
} }
type ResourceFind struct { type ResourceFind struct {
Id *int `json:"id"` ID *int `json:"id"`
CreatorId *int `json:"creatorId"` CreatorID *int `json:"creatorId"`
Filename *string `json:"filename"` Filename *string `json:"filename"`
} }
type ResourceDelete struct { type ResourceDelete struct {
Id int ID int
} }
type ResourceService interface { type ResourceService interface {

@ -1,19 +1,19 @@
package api package api
type Shortcut struct { type Shortcut struct {
Id int `json:"id"` ID int `json:"id"`
CreatedTs int64 `json:"createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"` UpdatedTs int64 `json:"updatedTs"`
Title string `json:"title"` Title string `json:"title"`
Payload string `json:"payload"` Payload string `json:"payload"`
RowStatus string `json:"rowStatus"` RowStatus string `json:"rowStatus"`
CreatorId int CreatorID int
} }
type ShortcutCreate struct { type ShortcutCreate struct {
// Standard fields // Standard fields
CreatorId int CreatorID int
// Domain specific fields // Domain specific fields
Title string `json:"title"` Title string `json:"title"`
@ -21,7 +21,7 @@ type ShortcutCreate struct {
} }
type ShortcutPatch struct { type ShortcutPatch struct {
Id int ID int
Title *string `json:"title"` Title *string `json:"title"`
Payload *string `json:"payload"` Payload *string `json:"payload"`
@ -29,17 +29,17 @@ type ShortcutPatch struct {
} }
type ShortcutFind struct { type ShortcutFind struct {
Id *int ID *int
// Standard fields // Standard fields
CreatorId *int CreatorID *int
// Domain specific fields // Domain specific fields
Title *string `json:"title"` Title *string `json:"title"`
} }
type ShortcutDelete struct { type ShortcutDelete struct {
Id int ID int
} }
type ShortcutService interface { type ShortcutService interface {

@ -1,37 +1,37 @@
package api package api
type User struct { type User struct {
Id int `json:"id"` ID int `json:"id"`
CreatedTs int64 `json:"createdTs"` CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"` UpdatedTs int64 `json:"updatedTs"`
OpenId string `json:"openId"` OpenID string `json:"openId"`
Name string `json:"name"` Name string `json:"name"`
PasswordHash string `json:"-"` PasswordHash string `json:"-"`
} }
type UserCreate struct { type UserCreate struct {
OpenId string OpenID string
Name string Name string
PasswordHash string PasswordHash string
} }
type UserPatch struct { type UserPatch struct {
Id int ID int
OpenId *string OpenID *string
PasswordHash *string PasswordHash *string
Name *string `json:"name"` Name *string `json:"name"`
Password *string `json:"password"` Password *string `json:"password"`
ResetOpenId *bool `json:"resetOpenId"` ResetOpenID *bool `json:"resetOpenId"`
} }
type UserFind struct { type UserFind struct {
Id *int `json:"id"` ID *int `json:"id"`
Name *string `json:"name"` Name *string `json:"name"`
OpenId *string OpenID *string
} }
type UserRenameCheck struct { type UserRenameCheck struct {

@ -91,7 +91,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
userCreate := &api.UserCreate{ userCreate := &api.UserCreate{
Name: signup.Name, Name: signup.Name,
PasswordHash: string(passwordHash), PasswordHash: string(passwordHash),
OpenId: common.GenUUID(), OpenID: common.GenUUID(),
} }
user, err = s.UserService.CreateUser(userCreate) user, err = s.UserService.CreateUser(userCreate)
if err != nil { if err != nil {

@ -13,11 +13,11 @@ import (
) )
var ( var (
userIdContextKey = "user-id" userIDContextKey = "user-id"
) )
func getUserIdContextKey() string { func getUserIDContextKey() string {
return userIdContextKey return userIDContextKey
} }
func setUserSession(c echo.Context, user *api.User) error { func setUserSession(c echo.Context, user *api.User) error {
@ -27,7 +27,7 @@ func setUserSession(c echo.Context, user *api.User) error {
MaxAge: 1000 * 3600 * 24 * 30, MaxAge: 1000 * 3600 * 24 * 30,
HttpOnly: true, HttpOnly: true,
} }
sess.Values[userIdContextKey] = user.Id sess.Values[userIDContextKey] = user.ID
err := sess.Save(c.Request(), c.Response()) err := sess.Save(c.Request(), c.Response())
if err != nil { if err != nil {
return fmt.Errorf("failed to set session, err: %w", err) return fmt.Errorf("failed to set session, err: %w", err)
@ -43,7 +43,7 @@ func removeUserSession(c echo.Context) error {
MaxAge: 0, MaxAge: 0,
HttpOnly: true, HttpOnly: true,
} }
sess.Values[userIdContextKey] = nil sess.Values[userIDContextKey] = nil
err := sess.Save(c.Request(), c.Response()) err := sess.Save(c.Request(), c.Response())
if err != nil { if err != nil {
return fmt.Errorf("failed to set session, err: %w", err) return fmt.Errorf("failed to set session, err: %w", err)
@ -65,30 +65,30 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
return echo.NewHTTPError(http.StatusUnauthorized, "Missing session").SetInternal(err) return echo.NewHTTPError(http.StatusUnauthorized, "Missing session").SetInternal(err)
} }
userIdValue := sess.Values[userIdContextKey] userIDValue := sess.Values[userIDContextKey]
if userIdValue == nil { if userIDValue == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing userId in session") return echo.NewHTTPError(http.StatusUnauthorized, "Missing userID in session")
} }
userId, err := strconv.Atoi(fmt.Sprintf("%v", userIdValue)) userID, err := strconv.Atoi(fmt.Sprintf("%v", userIDValue))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to malformatted user id in the session.").SetInternal(err)
} }
// Even if there is no error, we still need to make sure the user still exists. // Even if there is no error, we still need to make sure the user still exists.
userFind := &api.UserFind{ userFind := &api.UserFind{
Id: &userId, ID: &userID,
} }
user, err := us.FindUser(userFind) user, err := us.FindUser(userFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userId)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userID)).SetInternal(err)
} }
if user == nil { if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userId)) return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Not found user ID: %d", userID))
} }
// Stores userId into context. // Stores userID into context.
c.Set(getUserIdContextKey(), userId) c.Set(getUserIDContextKey(), userID)
return next(c) return next(c)
} }

@ -13,9 +13,9 @@ import (
func (s *Server) registerMemoRoutes(g *echo.Group) { func (s *Server) registerMemoRoutes(g *echo.Group) {
g.POST("/memo", func(c echo.Context) error { g.POST("/memo", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
memoCreate := &api.MemoCreate{ memoCreate := &api.MemoCreate{
CreatorId: userId, CreatorID: userID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
@ -35,13 +35,13 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}) })
g.PATCH("/memo/:memoId", func(c echo.Context) error { g.PATCH("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
} }
memoPatch := &api.MemoPatch{ memoPatch := &api.MemoPatch{
Id: memoId, ID: memoID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
@ -61,9 +61,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}) })
g.GET("/memo", func(c echo.Context) error { g.GET("/memo", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
memoFind := &api.MemoFind{ memoFind := &api.MemoFind{
CreatorId: &userId, CreatorID: &userID,
} }
rowStatus := c.QueryParam("rowStatus") rowStatus := c.QueryParam("rowStatus")
if rowStatus != "" { if rowStatus != "" {
@ -84,21 +84,21 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}) })
g.GET("/memo/:memoId", func(c echo.Context) error { g.GET("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
} }
memoFind := &api.MemoFind{ memoFind := &api.MemoFind{
Id: &memoId, ID: &memoID,
} }
memo, err := s.MemoService.FindMemo(memoFind) memo, err := s.MemoService.FindMemo(memoFind)
if err != nil { if err != nil {
if common.ErrorCode(err) == common.NotFound { 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.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
} }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", 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) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@ -110,18 +110,18 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}) })
g.DELETE("/memo/:memoId", func(c echo.Context) error { g.DELETE("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
} }
memoDelete := &api.MemoDelete{ memoDelete := &api.MemoDelete{
Id: &memoId, ID: &memoID,
} }
err = s.MemoService.DeleteMemo(memoDelete) err = s.MemoService.DeleteMemo(memoDelete)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoId)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
} }
c.JSON(http.StatusOK, true) c.JSON(http.StatusOK, true)

@ -13,7 +13,7 @@ import (
func (s *Server) registerResourceRoutes(g *echo.Group) { func (s *Server) registerResourceRoutes(g *echo.Group) {
g.POST("/resource", func(c echo.Context) error { g.POST("/resource", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
err := c.Request().ParseMultipartForm(5 << 20) err := c.Request().ParseMultipartForm(5 << 20)
if err != nil { if err != nil {
@ -44,7 +44,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
Type: filetype, Type: filetype,
Size: size, Size: size,
Blob: fileBytes, Blob: fileBytes,
CreatorId: userId, CreatorID: userID,
} }
resource, err := s.ResourceService.CreateResource(resourceCreate) resource, err := s.ResourceService.CreateResource(resourceCreate)
@ -54,16 +54,16 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil { if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
} }
return nil return nil
}) })
g.GET("/resource", func(c echo.Context) error { g.GET("/resource", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
CreatorId: &userId, CreatorID: &userID,
} }
list, err := s.ResourceService.FindResourceList(resourceFind) list, err := s.ResourceService.FindResourceList(resourceFind)
if err != nil { if err != nil {
@ -79,13 +79,13 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}) })
g.DELETE("/resource/:resourceId", func(c echo.Context) error { g.DELETE("/resource/:resourceId", func(c echo.Context) error {
resourceId, err := strconv.Atoi(c.Param("resourceId")) resourceID, err := strconv.Atoi(c.Param("resourceId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
} }
resourceDelete := &api.ResourceDelete{ resourceDelete := &api.ResourceDelete{
Id: resourceId, ID: resourceID,
} }
if err := s.ResourceService.DeleteResource(resourceDelete); err != nil { if err := s.ResourceService.DeleteResource(resourceDelete); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete resource").SetInternal(err)

@ -12,9 +12,9 @@ import (
func (s *Server) registerShortcutRoutes(g *echo.Group) { func (s *Server) registerShortcutRoutes(g *echo.Group) {
g.POST("/shortcut", func(c echo.Context) error { g.POST("/shortcut", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
shortcutCreate := &api.ShortcutCreate{ shortcutCreate := &api.ShortcutCreate{
CreatorId: userId, CreatorID: userID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
@ -34,13 +34,13 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}) })
g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error { g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
} }
shortcutPatch := &api.ShortcutPatch{ shortcutPatch := &api.ShortcutPatch{
Id: shortcutId, ID: shortcutID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
@ -60,9 +60,9 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}) })
g.GET("/shortcut", func(c echo.Context) error { g.GET("/shortcut", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
shortcutFind := &api.ShortcutFind{ shortcutFind := &api.ShortcutFind{
CreatorId: &userId, CreatorID: &userID,
} }
list, err := s.ShortcutService.FindShortcutList(shortcutFind) list, err := s.ShortcutService.FindShortcutList(shortcutFind)
if err != nil { if err != nil {
@ -78,17 +78,17 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}) })
g.GET("/shortcut/:shortcutId", func(c echo.Context) error { g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
} }
shortcutFind := &api.ShortcutFind{ shortcutFind := &api.ShortcutFind{
Id: &shortcutId, ID: &shortcutID,
} }
shortcut, err := s.ShortcutService.FindShortcut(shortcutFind) shortcut, err := s.ShortcutService.FindShortcut(shortcutFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err)
} }
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@ -100,13 +100,13 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}) })
g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error { g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutID, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err)
} }
shortcutDelete := &api.ShortcutDelete{ shortcutDelete := &api.ShortcutDelete{
Id: shortcutId, ID: shortcutID,
} }
if err := s.ShortcutService.DeleteShortcut(shortcutDelete); err != nil { if err := s.ShortcutService.DeleteShortcut(shortcutDelete); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete shortcut").SetInternal(err)

@ -2,6 +2,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt"
"memos/api" "memos/api"
"memos/common" "memos/common"
"net/http" "net/http"
@ -13,14 +14,14 @@ import (
func (s *Server) registerUserRoutes(g *echo.Group) { func (s *Server) registerUserRoutes(g *echo.Group) {
// GET /api/user/me is used to check if the user is logged in. // GET /api/user/me is used to check if the user is logged in.
g.GET("/user/me", func(c echo.Context) error { g.GET("/user/me", func(c echo.Context) error {
userSessionId := c.Get(getUserIdContextKey()) userSessionID := c.Get(getUserIDContextKey())
if userSessionId == nil { if userSessionID == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing session") return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
} }
userId := userSessionId.(int) userID := userSessionID.(int)
userFind := &api.UserFind{ userFind := &api.UserFind{
Id: &userId, ID: &userID,
} }
user, err := s.UserService.FindUser(userFind) user, err := s.UserService.FindUser(userFind)
if err != nil { if err != nil {
@ -42,7 +43,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
} }
if userRenameCheck.Name == "" { if userRenameCheck.Name == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user rename check request") return echo.NewHTTPError(http.StatusBadRequest, "New name needed")
} }
userFind := &api.UserFind{ userFind := &api.UserFind{
@ -50,7 +51,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
} }
user, err := s.UserService.FindUser(userFind) user, err := s.UserService.FindUser(userFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by name %s", *userFind.Name)).SetInternal(err)
} }
isUsable := true isUsable := true
@ -67,22 +68,22 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}) })
g.POST("/user/password_check", func(c echo.Context) error { g.POST("/user/password_check", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
userPasswordCheck := &api.UserPasswordCheck{} userPasswordCheck := &api.UserPasswordCheck{}
if err := json.NewDecoder(c.Request().Body).Decode(userPasswordCheck); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(userPasswordCheck); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request").SetInternal(err)
} }
if userPasswordCheck.Password == "" { if userPasswordCheck.Password == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request") return echo.NewHTTPError(http.StatusBadRequest, "Password needed")
} }
userFind := &api.UserFind{ userFind := &api.UserFind{
Id: &userId, ID: &userID,
} }
user, err := s.UserService.FindUser(userFind) user, err := s.UserService.FindUser(userFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by password").SetInternal(err)
} }
isValid := false isValid := false
@ -100,17 +101,17 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}) })
g.PATCH("/user/me", func(c echo.Context) error { g.PATCH("/user/me", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userID := c.Get(getUserIDContextKey()).(int)
userPatch := &api.UserPatch{ userPatch := &api.UserPatch{
Id: userId, ID: userID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
} }
if userPatch.ResetOpenId != nil && *userPatch.ResetOpenId { if userPatch.ResetOpenID != nil && *userPatch.ResetOpenID {
openId := common.GenUUID() openID := common.GenUUID()
userPatch.OpenId = &openId userPatch.OpenID = &openID
} }
if userPatch.Password != nil && *userPatch.Password != "" { if userPatch.Password != nil && *userPatch.Password != "" {

@ -16,21 +16,21 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}) })
g.POST("/:openId/memo", func(c echo.Context) error { g.POST("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId") openID := c.Param("openId")
userFind := &api.UserFind{ userFind := &api.UserFind{
OpenId: &openId, OpenID: &openID,
} }
user, err := s.UserService.FindUser(userFind) user, err := s.UserService.FindUser(userFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
} }
if user == nil { if user == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User openId not found: %s", openId)) return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("User openId not found: %s", openID))
} }
memoCreate := &api.MemoCreate{ memoCreate := &api.MemoCreate{
CreatorId: user.Id, CreatorID: user.ID,
} }
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err)
@ -50,21 +50,21 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}) })
g.GET("/:openId/memo", func(c echo.Context) error { g.GET("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId") openID := c.Param("openId")
userFind := &api.UserFind{ userFind := &api.UserFind{
OpenId: &openId, OpenID: &openID,
} }
user, err := s.UserService.FindUser(userFind) user, err := s.UserService.FindUser(userFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
} }
if user == nil { if user == nil {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Unauthorized: %s", openId)) return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Unauthorized: %s", openID))
} }
memoFind := &api.MemoFind{ memoFind := &api.MemoFind{
CreatorId: &user.Id, CreatorID: &user.ID,
} }
rowStatus := c.QueryParam("rowStatus") rowStatus := c.QueryParam("rowStatus")
if rowStatus != "" { if rowStatus != "" {
@ -85,7 +85,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}) })
g.GET("/r/:resourceId/:filename", func(c echo.Context) error { g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
resourceId, err := strconv.Atoi(c.Param("resourceId")) resourceID, err := strconv.Atoi(c.Param("resourceId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
} }
@ -93,13 +93,13 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
filename := c.Param("filename") filename := c.Param("filename")
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
Id: &resourceId, ID: &resourceID,
Filename: &filename, Filename: &filename,
} }
resource, err := s.ResourceService.FindResource(resourceFind) resource, err := s.ResourceService.FindResource(resourceFind)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceId)).SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
} }
c.Response().Writer.WriteHeader(http.StatusOK) c.Response().Writer.WriteHeader(http.StatusOK)

@ -73,7 +73,7 @@ func createMemo(db *DB, create *api.MemoCreate) (*api.Memo, error) {
VALUES (?, ?) VALUES (?, ?)
RETURNING id, creator_id, created_ts, updated_ts, content, row_status RETURNING id, creator_id, created_ts, updated_ts, content, row_status
`, `,
create.CreatorId, create.CreatorID,
create.Content, create.Content,
) )
if err != nil { if err != nil {
@ -87,8 +87,8 @@ func createMemo(db *DB, create *api.MemoCreate) (*api.Memo, error) {
var memo api.Memo var memo api.Memo
if err := row.Scan( if err := row.Scan(
&memo.Id, &memo.ID,
&memo.CreatorId, &memo.CreatorID,
&memo.CreatedTs, &memo.CreatedTs,
&memo.UpdatedTs, &memo.UpdatedTs,
&memo.Content, &memo.Content,
@ -113,7 +113,7 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
set, args = append(set, "created_ts = ?"), append(args, *v) set, args = append(set, "created_ts = ?"), append(args, *v)
} }
args = append(args, patch.Id) args = append(args, patch.ID)
row, err := db.Db.Query(` row, err := db.Db.Query(`
UPDATE memo UPDATE memo
@ -132,7 +132,7 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
var memo api.Memo var memo api.Memo
if err := row.Scan( if err := row.Scan(
&memo.Id, &memo.ID,
&memo.CreatedTs, &memo.CreatedTs,
&memo.UpdatedTs, &memo.UpdatedTs,
&memo.Content, &memo.Content,
@ -147,10 +147,10 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) { func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
if v := find.Id; v != nil { if v := find.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v) where, args = append(where, "id = ?"), append(args, *v)
} }
if v := find.CreatorId; v != nil { if v := find.CreatorID; v != nil {
where, args = append(where, "creator_id = ?"), append(args, *v) where, args = append(where, "creator_id = ?"), append(args, *v)
} }
if v := find.RowStatus; v != nil { if v := find.RowStatus; v != nil {
@ -178,8 +178,8 @@ func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
for rows.Next() { for rows.Next() {
var memo api.Memo var memo api.Memo
if err := rows.Scan( if err := rows.Scan(
&memo.Id, &memo.ID,
&memo.CreatorId, &memo.CreatorID,
&memo.CreatedTs, &memo.CreatedTs,
&memo.UpdatedTs, &memo.UpdatedTs,
&memo.Content, &memo.Content,
@ -199,14 +199,14 @@ func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
} }
func deleteMemo(db *DB, delete *api.MemoDelete) error { func deleteMemo(db *DB, delete *api.MemoDelete) error {
result, err := db.Db.Exec(`DELETE FROM memo WHERE id = ?`, delete.Id) result, err := db.Db.Exec(`DELETE FROM memo WHERE id = ?`, delete.ID)
if err != nil { if err != nil {
return FormatError(err) return FormatError(err)
} }
rows, _ := result.RowsAffected() rows, _ := result.RowsAffected()
if rows == 0 { if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo ID not found: %d", delete.Id)} return &common.Error{Code: common.NotFound, Err: fmt.Errorf("memo ID not found: %d", delete.ID)}
} }
return nil return nil

@ -71,7 +71,7 @@ func createResource(db *DB, create *api.ResourceCreate) (*api.Resource, error) {
create.Blob, create.Blob,
create.Type, create.Type,
create.Size, create.Size,
create.CreatorId, create.CreatorID,
) )
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
@ -84,7 +84,7 @@ func createResource(db *DB, create *api.ResourceCreate) (*api.Resource, error) {
var resource api.Resource var resource api.Resource
if err := row.Scan( if err := row.Scan(
&resource.Id, &resource.ID,
&resource.Filename, &resource.Filename,
&resource.Blob, &resource.Blob,
&resource.Type, &resource.Type,
@ -101,10 +101,10 @@ func createResource(db *DB, create *api.ResourceCreate) (*api.Resource, error) {
func findResourceList(db *DB, find *api.ResourceFind) ([]*api.Resource, error) { func findResourceList(db *DB, find *api.ResourceFind) ([]*api.Resource, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
if v := find.Id; v != nil { if v := find.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v) where, args = append(where, "id = ?"), append(args, *v)
} }
if v := find.CreatorId; v != nil { if v := find.CreatorID; v != nil {
where, args = append(where, "creator_id = ?"), append(args, *v) where, args = append(where, "creator_id = ?"), append(args, *v)
} }
if v := find.Filename; v != nil { if v := find.Filename; v != nil {
@ -133,7 +133,7 @@ func findResourceList(db *DB, find *api.ResourceFind) ([]*api.Resource, error) {
for rows.Next() { for rows.Next() {
var resource api.Resource var resource api.Resource
if err := rows.Scan( if err := rows.Scan(
&resource.Id, &resource.ID,
&resource.Filename, &resource.Filename,
&resource.Blob, &resource.Blob,
&resource.Type, &resource.Type,
@ -155,14 +155,14 @@ func findResourceList(db *DB, find *api.ResourceFind) ([]*api.Resource, error) {
} }
func deleteResource(db *DB, delete *api.ResourceDelete) error { func deleteResource(db *DB, delete *api.ResourceDelete) error {
result, err := db.Db.Exec(`DELETE FROM resource WHERE id = ?`, delete.Id) result, err := db.Db.Exec(`DELETE FROM resource WHERE id = ?`, delete.ID)
if err != nil { if err != nil {
return FormatError(err) return FormatError(err)
} }
rows, _ := result.RowsAffected() rows, _ := result.RowsAffected()
if rows == 0 { if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("resource ID not found: %d", delete.Id)} return &common.Error{Code: common.NotFound, Err: fmt.Errorf("resource ID not found: %d", delete.ID)}
} }
return nil return nil

@ -76,7 +76,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
`, `,
create.Title, create.Title,
create.Payload, create.Payload,
create.CreatorId, create.CreatorID,
) )
if err != nil { if err != nil {
@ -87,10 +87,10 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
row.Next() row.Next()
var shortcut api.Shortcut var shortcut api.Shortcut
if err := row.Scan( if err := row.Scan(
&shortcut.Id, &shortcut.ID,
&shortcut.Title, &shortcut.Title,
&shortcut.Payload, &shortcut.Payload,
&shortcut.CreatorId, &shortcut.CreatorID,
&shortcut.CreatedTs, &shortcut.CreatedTs,
&shortcut.UpdatedTs, &shortcut.UpdatedTs,
&shortcut.RowStatus, &shortcut.RowStatus,
@ -114,7 +114,7 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
set, args = append(set, "row_status = ?"), append(args, *v) set, args = append(set, "row_status = ?"), append(args, *v)
} }
args = append(args, patch.Id) args = append(args, patch.ID)
row, err := db.Db.Query(` row, err := db.Db.Query(`
UPDATE shortcut UPDATE shortcut
@ -133,7 +133,7 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
var shortcut api.Shortcut var shortcut api.Shortcut
if err := row.Scan( if err := row.Scan(
&shortcut.Id, &shortcut.ID,
&shortcut.Title, &shortcut.Title,
&shortcut.Payload, &shortcut.Payload,
&shortcut.CreatedTs, &shortcut.CreatedTs,
@ -149,10 +149,10 @@ func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) { func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
if v := find.Id; v != nil { if v := find.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v) where, args = append(where, "id = ?"), append(args, *v)
} }
if v := find.CreatorId; v != nil { if v := find.CreatorID; v != nil {
where, args = append(where, "creator_id = ?"), append(args, *v) where, args = append(where, "creator_id = ?"), append(args, *v)
} }
if v := find.Title; v != nil { if v := find.Title; v != nil {
@ -181,10 +181,10 @@ func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) {
for rows.Next() { for rows.Next() {
var shortcut api.Shortcut var shortcut api.Shortcut
if err := rows.Scan( if err := rows.Scan(
&shortcut.Id, &shortcut.ID,
&shortcut.Title, &shortcut.Title,
&shortcut.Payload, &shortcut.Payload,
&shortcut.CreatorId, &shortcut.CreatorID,
&shortcut.CreatedTs, &shortcut.CreatedTs,
&shortcut.UpdatedTs, &shortcut.UpdatedTs,
&shortcut.RowStatus, &shortcut.RowStatus,
@ -203,14 +203,14 @@ func findShortcutList(db *DB, find *api.ShortcutFind) ([]*api.Shortcut, error) {
} }
func deleteShortcut(db *DB, delete *api.ShortcutDelete) error { func deleteShortcut(db *DB, delete *api.ShortcutDelete) error {
result, err := db.Db.Exec(`DELETE FROM shortcut WHERE id = ?`, delete.Id) result, err := db.Db.Exec(`DELETE FROM shortcut WHERE id = ?`, delete.ID)
if err != nil { if err != nil {
return FormatError(err) return FormatError(err)
} }
rows, _ := result.RowsAffected() rows, _ := result.RowsAffected()
if rows == 0 { if rows == 0 {
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("shortcut ID not found: %d", delete.Id)} return &common.Error{Code: common.NotFound, Err: fmt.Errorf("shortcut ID not found: %d", delete.ID)}
} }
return nil return nil

@ -60,7 +60,7 @@ func createUser(db *DB, create *api.UserCreate) (*api.User, error) {
`, `,
create.Name, create.Name,
create.PasswordHash, create.PasswordHash,
create.OpenId, create.OpenID,
) )
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
@ -70,10 +70,10 @@ func createUser(db *DB, create *api.UserCreate) (*api.User, error) {
row.Next() row.Next()
var user api.User var user api.User
if err := row.Scan( if err := row.Scan(
&user.Id, &user.ID,
&user.Name, &user.Name,
&user.PasswordHash, &user.PasswordHash,
&user.OpenId, &user.OpenID,
&user.CreatedTs, &user.CreatedTs,
&user.UpdatedTs, &user.UpdatedTs,
); err != nil { ); err != nil {
@ -92,11 +92,11 @@ func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) {
if v := patch.PasswordHash; v != nil { if v := patch.PasswordHash; v != nil {
set, args = append(set, "password_hash = ?"), append(args, v) set, args = append(set, "password_hash = ?"), append(args, v)
} }
if v := patch.OpenId; v != nil { if v := patch.OpenID; v != nil {
set, args = append(set, "open_id = ?"), append(args, v) set, args = append(set, "open_id = ?"), append(args, v)
} }
args = append(args, patch.Id) args = append(args, patch.ID)
row, err := db.Db.Query(` row, err := db.Db.Query(`
UPDATE user UPDATE user
@ -112,10 +112,10 @@ func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) {
if row.Next() { if row.Next() {
var user api.User var user api.User
if err := row.Scan( if err := row.Scan(
&user.Id, &user.ID,
&user.Name, &user.Name,
&user.PasswordHash, &user.PasswordHash,
&user.OpenId, &user.OpenID,
&user.CreatedTs, &user.CreatedTs,
&user.UpdatedTs, &user.UpdatedTs,
); err != nil { ); err != nil {
@ -125,19 +125,19 @@ func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) {
return &user, nil return &user, nil
} }
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("user ID not found: %d", patch.Id)} return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("user ID not found: %d", patch.ID)}
} }
func findUserList(db *DB, find *api.UserFind) ([]*api.User, error) { func findUserList(db *DB, find *api.UserFind) ([]*api.User, error) {
where, args := []string{"1 = 1"}, []interface{}{} where, args := []string{"1 = 1"}, []interface{}{}
if v := find.Id; v != nil { if v := find.ID; v != nil {
where, args = append(where, "id = ?"), append(args, *v) where, args = append(where, "id = ?"), append(args, *v)
} }
if v := find.Name; v != nil { if v := find.Name; v != nil {
where, args = append(where, "name = ?"), append(args, *v) where, args = append(where, "name = ?"), append(args, *v)
} }
if v := find.OpenId; v != nil { if v := find.OpenID; v != nil {
where, args = append(where, "open_id = ?"), append(args, *v) where, args = append(where, "open_id = ?"), append(args, *v)
} }
@ -162,10 +162,10 @@ func findUserList(db *DB, find *api.UserFind) ([]*api.User, error) {
for rows.Next() { for rows.Next() {
var user api.User var user api.User
if err := rows.Scan( if err := rows.Scan(
&user.Id, &user.ID,
&user.Name, &user.Name,
&user.PasswordHash, &user.PasswordHash,
&user.OpenId, &user.OpenID,
&user.CreatedTs, &user.CreatedTs,
&user.UpdatedTs, &user.UpdatedTs,
); err != nil { ); err != nil {

Loading…
Cancel
Save