Unverified Commit 697d01e3 authored by boojack's avatar boojack Committed by GitHub

feat: add `visibility` field to memo (#109)

* feat: add `visibility` field to memo

* chore: fix typo
parent aed13747
package api package api
// Visibility is the type of a visibility.
type Visibility string
const (
// Public is the PUBLIC visibility.
Public Visibility = "PUBLIC"
// Privite is the PRIVATE visibility.
Privite Visibility = "PRIVATE"
)
func (e Visibility) String() string {
switch e {
case Public:
return "PUBLIC"
case Privite:
return "PRIVATE"
}
return "PRIVATE"
}
type Memo struct { type Memo struct {
ID int `json:"id"` ID int `json:"id"`
...@@ -11,6 +31,7 @@ type Memo struct { ...@@ -11,6 +31,7 @@ type Memo struct {
// Domain specific fields // Domain specific fields
Content string `json:"content"` Content string `json:"content"`
Visibility Visibility `json:"visibility"`
Pinned bool `json:"pinned"` Pinned bool `json:"pinned"`
} }
...@@ -22,6 +43,7 @@ type MemoCreate struct { ...@@ -22,6 +43,7 @@ type MemoCreate struct {
// Domain specific fields // Domain specific fields
Content string `json:"content"` Content string `json:"content"`
Visibility Visibility `json:"visibility"`
} }
type MemoPatch struct { type MemoPatch struct {
...@@ -32,6 +54,7 @@ type MemoPatch struct { ...@@ -32,6 +54,7 @@ type MemoPatch struct {
// Domain specific fields // Domain specific fields
Content *string `json:"content"` Content *string `json:"content"`
Visibility *Visibility `json:"visibility"`
} }
type MemoFind struct { type MemoFind struct {
...@@ -44,6 +67,7 @@ type MemoFind struct { ...@@ -44,6 +67,7 @@ type MemoFind struct {
// Domain specific fields // Domain specific fields
Pinned *bool Pinned *bool
ContentSearch *string ContentSearch *string
Visibility *Visibility
// Pagination // Pagination
Limit int Limit int
......
...@@ -22,6 +22,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { ...@@ -22,6 +22,9 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
} }
// TODO(steven): remove this line after frontend is ready
memoCreate.Visibility = api.Privite
memo, err := s.Store.CreateMemo(memoCreate) memo, err := s.Store.CreateMemo(memoCreate)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
......
ALTER TABLE memo ADD visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PRIVATE')) DEFAULT 'PRIVATE';
...@@ -44,6 +44,7 @@ CREATE TABLE memo ( ...@@ -44,6 +44,7 @@ CREATE TABLE memo (
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')), updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL', row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
content TEXT NOT NULL DEFAULT '', content TEXT NOT NULL DEFAULT '',
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PRIVATE')) DEFAULT 'PRIVATE',
FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE FOREIGN KEY(creator_id) REFERENCES user(id) ON DELETE CASCADE
); );
......
...@@ -34,13 +34,15 @@ INSERT INTO ...@@ -34,13 +34,15 @@ INSERT INTO
memo ( memo (
`id`, `id`,
`content`, `content`,
`creator_id` `creator_id`,
`visibility`
) )
VALUES VALUES
( (
103, 103,
'好好学习,天天向上。🤜🤛', '好好学习,天天向上。🤜🤛',
101 101,
'PUBLIC'
); );
INSERT INTO INSERT INTO
......
...@@ -22,6 +22,7 @@ type memoRaw struct { ...@@ -22,6 +22,7 @@ type memoRaw struct {
// Domain specific fields // Domain specific fields
Content string Content string
Visibility api.Visibility
} }
// toMemo creates an instance of Memo based on the memoRaw. // toMemo creates an instance of Memo based on the memoRaw.
...@@ -38,6 +39,7 @@ func (raw *memoRaw) toMemo() *api.Memo { ...@@ -38,6 +39,7 @@ func (raw *memoRaw) toMemo() *api.Memo {
// Domain specific fields // Domain specific fields
Content: raw.Content, Content: raw.Content,
Visibility: raw.Visibility,
} }
} }
...@@ -116,21 +118,21 @@ func (s *Store) DeleteMemo(delete *api.MemoDelete) error { ...@@ -116,21 +118,21 @@ func (s *Store) DeleteMemo(delete *api.MemoDelete) error {
} }
func createMemoRaw(db *sql.DB, create *api.MemoCreate) (*memoRaw, error) { func createMemoRaw(db *sql.DB, create *api.MemoCreate) (*memoRaw, error) {
set := []string{"creator_id", "content"} set := []string{"creator_id", "content", "visibility"}
placeholder := []string{"?", "?"} placeholder := []string{"?", "?", "?"}
args := []interface{}{create.CreatorID, create.Content} args := []interface{}{create.CreatorID, create.Content, create.Visibility}
if v := create.CreatedTs; v != nil { if v := create.CreatedTs; v != nil {
set, placeholder, args = append(set, "created_ts"), append(placeholder, "?"), append(args, *v) set, placeholder, args = append(set, "created_ts"), append(placeholder, "?"), append(args, *v)
} }
row, err := db.Query(` query := `
INSERT INTO memo ( INSERT INTO memo (
`+strings.Join(set, ", ")+` ` + strings.Join(set, ", ") + `
) )
VALUES (`+strings.Join(placeholder, ",")+`) VALUES (` + strings.Join(placeholder, ",") + `)
RETURNING id, creator_id, created_ts, updated_ts, content, row_status RETURNING id, creator_id, created_ts, updated_ts, row_status, content, visibility`
`, row, err := db.Query(query,
args..., args...,
) )
if err != nil { if err != nil {
...@@ -145,8 +147,9 @@ func createMemoRaw(db *sql.DB, create *api.MemoCreate) (*memoRaw, error) { ...@@ -145,8 +147,9 @@ func createMemoRaw(db *sql.DB, create *api.MemoCreate) (*memoRaw, error) {
&memoRaw.CreatorID, &memoRaw.CreatorID,
&memoRaw.CreatedTs, &memoRaw.CreatedTs,
&memoRaw.UpdatedTs, &memoRaw.UpdatedTs,
&memoRaw.Content,
&memoRaw.RowStatus, &memoRaw.RowStatus,
&memoRaw.Content,
&memoRaw.Visibility,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
...@@ -163,6 +166,9 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) { ...@@ -163,6 +166,9 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) {
if v := patch.RowStatus; v != nil { if v := patch.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v) set, args = append(set, "row_status = ?"), append(args, *v)
} }
if v := patch.Visibility; v != nil {
set, args = append(set, "visibility = ?"), append(args, *v)
}
args = append(args, patch.ID) args = append(args, patch.ID)
...@@ -170,7 +176,7 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) { ...@@ -170,7 +176,7 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) {
UPDATE memo UPDATE memo
SET `+strings.Join(set, ", ")+` SET `+strings.Join(set, ", ")+`
WHERE id = ? WHERE id = ?
RETURNING id, creator_id, created_ts, updated_ts, content, row_status RETURNING id, creator_id, created_ts, updated_ts, row_status, content, visibility
`, args...) `, args...)
if err != nil { if err != nil {
return nil, FormatError(err) return nil, FormatError(err)
...@@ -187,8 +193,9 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) { ...@@ -187,8 +193,9 @@ func patchMemoRaw(db *sql.DB, patch *api.MemoPatch) (*memoRaw, error) {
&memoRaw.CreatorID, &memoRaw.CreatorID,
&memoRaw.CreatedTs, &memoRaw.CreatedTs,
&memoRaw.UpdatedTs, &memoRaw.UpdatedTs,
&memoRaw.Content,
&memoRaw.RowStatus, &memoRaw.RowStatus,
&memoRaw.Content,
&memoRaw.Visibility,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
...@@ -214,6 +221,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) { ...@@ -214,6 +221,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) {
if v := find.ContentSearch; v != nil { if v := find.ContentSearch; v != nil {
where, args = append(where, "content LIKE ?"), append(args, "%"+*v+"%") where, args = append(where, "content LIKE ?"), append(args, "%"+*v+"%")
} }
if v := find.Visibility; v != nil {
where, args = append(where, "visibility = ?"), append(args, *v)
}
pagination := "" pagination := ""
if find.Limit > 0 { if find.Limit > 0 {
...@@ -229,8 +239,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) { ...@@ -229,8 +239,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) {
creator_id, creator_id,
created_ts, created_ts,
updated_ts, updated_ts,
row_status,
content, content,
row_status visibility
FROM memo FROM memo
WHERE `+strings.Join(where, " AND ")+` WHERE `+strings.Join(where, " AND ")+`
ORDER BY created_ts DESC`+pagination, ORDER BY created_ts DESC`+pagination,
...@@ -249,8 +260,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) { ...@@ -249,8 +260,9 @@ func findMemoRawList(db *sql.DB, find *api.MemoFind) ([]*memoRaw, error) {
&memoRaw.CreatorID, &memoRaw.CreatorID,
&memoRaw.CreatedTs, &memoRaw.CreatedTs,
&memoRaw.UpdatedTs, &memoRaw.UpdatedTs,
&memoRaw.Content,
&memoRaw.RowStatus, &memoRaw.RowStatus,
&memoRaw.Content,
&memoRaw.Visibility,
); err != nil { ); err != nil {
return nil, FormatError(err) return nil, FormatError(err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment