You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

4 years ago
package story
import (
"database/sql"
"net/http"
"github.com/imdotdev/im.dev/server/pkg/db"
"github.com/imdotdev/im.dev/server/pkg/e"
4 years ago
"github.com/imdotdev/im.dev/server/pkg/models"
4 years ago
)
4 years ago
func Bookmark(userID string, storyID string) *e.Error {
4 years ago
storyExist := models.IdExist(storyID)
4 years ago
if !storyExist {
return e.New(http.StatusNotFound, e.NotFound)
}
bookmarked, err := Bookmarked(userID, storyID)
if err != nil {
return e.New(http.StatusInternalServerError, e.Internal)
}
if !bookmarked {
_, err = db.Conn.Exec("insert into bookmarks (user_id,story_id) values (?,?)", userID, storyID)
if err != nil {
logger.Warn("add bookmark error", "error", err)
return e.New(http.StatusInternalServerError, e.Internal)
}
} else {
_, err = db.Conn.Exec("delete from bookmarks where user_id=? and story_id=?", userID, storyID)
if err != nil {
logger.Warn("delete bookmark error", "error", err)
return e.New(http.StatusInternalServerError, e.Internal)
}
}
return nil
}
4 years ago
func Bookmarked(userID string, storyID string) (bool, error) {
4 years ago
var nid string
err := db.Conn.QueryRow("select story_id from bookmarks where user_id=? and story_id=?", userID, storyID).Scan(&nid)
if err != nil && err != sql.ErrNoRows {
logger.Warn("get bookmarked error", "error", err)
return false, err
}
if err == sql.ErrNoRows {
return false, nil
}
return true, nil
}