From c77996a8914d29bb1b21d537da2845cde8f09e2a Mon Sep 17 00:00:00 2001
From: email <imrealleonardo@gmail.com>
Date: Sun, 6 Feb 2022 00:25:41 +0800
Subject: [PATCH] feat: open api for get memos

---
 server/auth.go       |  3 +--
 server/basic_auth.go |  7 ++++---
 server/memo.go       |  3 ++-
 server/webhook.go    | 41 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/server/auth.go b/server/auth.go
index 085346ad..bd42ce3b 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -30,8 +30,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
 
 		// Compare the stored password
 		if login.Password != user.Password {
-			// If the two passwords don't match, return a 401 status.
-			return echo.NewHTTPError(http.StatusUnauthorized, "Incorrect password").SetInternal(err)
+			return echo.NewHTTPError(http.StatusBadRequest, "Incorrect password").SetInternal(err)
 		}
 
 		err = setUserSession(c, user)
diff --git a/server/basic_auth.go b/server/basic_auth.go
index 1e84addc..9bae4da8 100644
--- a/server/basic_auth.go
+++ b/server/basic_auth.go
@@ -72,14 +72,14 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
 
 		userId, err := strconv.Atoi(fmt.Sprintf("%v", userIdValue))
 		if err != nil {
-			return echo.NewHTTPError(http.StatusUnauthorized, "Failed to malformatted user id in the session.")
+			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.
-		principalFind := &api.UserFind{
+		userFind := &api.UserFind{
 			Id: &userId,
 		}
-		user, err := us.FindUser(principalFind)
+		user, err := us.FindUser(userFind)
 		if err != nil {
 			return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find user by ID: %d", userId)).SetInternal(err)
 		}
@@ -89,6 +89,7 @@ func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.Handler
 
 		// Stores userId into context.
 		c.Set(getUserIdContextKey(), userId)
+
 		return next(c)
 	}
 }
diff --git a/server/memo.go b/server/memo.go
index b54aa7f1..0f18e384 100644
--- a/server/memo.go
+++ b/server/memo.go
@@ -98,8 +98,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
 		memo, err := s.MemoService.FindMemo(memoFind)
 		if err != nil {
 			if common.ErrorCode(err) == common.NotFound {
-				return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoId))
+				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)
 		}
 
diff --git a/server/webhook.go b/server/webhook.go
index 017e7efb..5318668f 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -47,6 +47,47 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
 
 		return nil
 	})
+	g.GET("/:openId/memo", func(c echo.Context) error {
+		openId := c.Param("openId")
+
+		userFind := &api.UserFind{
+			OpenId: &openId,
+		}
+		user, err := s.UserService.FindUser(userFind)
+		if err != nil {
+			return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user by open_id").SetInternal(err)
+		}
+		if user == nil {
+			return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Unauthorized: %s", openId))
+		}
+
+		memoFind := &api.MemoFind{
+			CreatorId: &user.Id,
+		}
+		showHiddenMemo, err := strconv.ParseBool(c.QueryParam("hidden"))
+		if err != nil {
+			showHiddenMemo = false
+		}
+
+		rowStatus := "NORMAL"
+		if showHiddenMemo {
+			rowStatus = "HIDDEN"
+		}
+		memoFind.RowStatus = &rowStatus
+
+		list, err := s.MemoService.FindMemoList(memoFind)
+		if err != nil {
+			return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
+		}
+
+		c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+		if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
+			return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
+		}
+
+		return nil
+	})
+
 	g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
 		resourceId, err := strconv.Atoi(c.Param("resourceId"))
 		if err != nil {