fix: favorites issues

This commit is contained in:
Rogee
2024-09-21 14:09:23 +08:00
parent f8a7e87965
commit 63b0eb8b44
4 changed files with 27 additions and 16 deletions

View File

@@ -21,5 +21,5 @@ type ChannelMessages struct {
CreatedAt time.Time
GroupID int64
Published bool
Like bool
Favorite bool
}

View File

@@ -26,7 +26,7 @@ type channelMessagesTable struct {
CreatedAt postgres.ColumnTimestampz
GroupID postgres.ColumnInteger
Published postgres.ColumnBool
Like postgres.ColumnBool
Favorite postgres.ColumnBool
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
@@ -76,9 +76,9 @@ func newChannelMessagesTableImpl(schemaName, tableName, alias string) channelMes
CreatedAtColumn = postgres.TimestampzColumn("created_at")
GroupIDColumn = postgres.IntegerColumn("group_id")
PublishedColumn = postgres.BoolColumn("published")
LikeColumn = postgres.BoolColumn("like")
allColumns = postgres.ColumnList{IDColumn, ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, LikeColumn}
mutableColumns = postgres.ColumnList{ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, LikeColumn}
FavoriteColumn = postgres.BoolColumn("favorite")
allColumns = postgres.ColumnList{IDColumn, ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, FavoriteColumn}
mutableColumns = postgres.ColumnList{ChannelIDColumn, UUIDColumn, ContentColumn, MediaColumn, PublishedAtColumn, CreatedAtColumn, GroupIDColumn, PublishedColumn, FavoriteColumn}
)
return channelMessagesTable{
@@ -94,7 +94,7 @@ func newChannelMessagesTableImpl(schemaName, tableName, alias string) channelMes
CreatedAt: CreatedAtColumn,
GroupID: GroupIDColumn,
Published: PublishedColumn,
Like: LikeColumn,
Favorite: FavoriteColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,

View File

@@ -12,14 +12,16 @@
</div>
</div>
<div class="grid grid-cols-2">
<button class="py-2 bg-slate-100 hover:bg-slate-50 text-center">Like</button>
<button class="py-2 bg-slate-100 hover:bg-slate-50 text-center" @click="toggleLike()">
{{ item.Favorite ? 'Unlike' : 'Like' }}
</button>
<button class="py-2 bg-slate-100 hover:bg-slate-50 text-center text-red-600" @click="delMessage()">Delete</button>
</div>
</div>
</template>
<script>
import { deleteMessage } from "@/services/messages";
import { deleteMessage, toggleFavorite } from "@/services/messages";
import { computed, defineComponent } from "vue";
import MediaItem from "./MediaItem.vue";
@@ -43,6 +45,13 @@ export default defineComponent({
return nl2br(content, false);
});
const toggleLike = async () => {
const itemId = props.item.ID
await toggleFavorite(itemId);
props.item.Favorite = !props.item.Favorite;
}
const delMessage = async () => {
console.log("delMessage")
if (!confirm("Are you sure?")) {
@@ -73,6 +82,7 @@ export default defineComponent({
item: props.item,
processedContent,
delMessage,
toggleLike,
};
},
});

View File

@@ -13,6 +13,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/filesystem"
log "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/spf13/cobra"
)
@@ -41,6 +42,7 @@ func serveCmd(cmd *cobra.Command, args []string) error {
app.Static("/medias", "/share/telegram/outputs")
app.Use(log.New())
app.Use(favicon.New(favicon.Config{
Data: frontend.Favicon,
}))
@@ -128,10 +130,8 @@ func serveCmd(cmd *cobra.Command, args []string) error {
offsetPk := c.QueryInt("offset", 0)
tbl := table.ChannelMessages
cond := tbl.ChannelID.EQ(Int64(int64(channelID))).AND(
tbl.Like.EQ(Bool(true)),
)
cond := tbl.Favorite.EQ(Bool(true))
if offsetPk > 0 {
cond = cond.AND(tbl.ID.LT(Int64(int64(offsetPk))))
}
@@ -152,7 +152,7 @@ func serveCmd(cmd *cobra.Command, args []string) error {
})
// toggle favorite message
group.Patch("message/:message/favorite", func(c *fiber.Ctx) error {
group.Patch("messages/:message/favorite", func(c *fiber.Ctx) error {
messageID, err := c.ParamsInt("message")
if err != nil {
return err
@@ -172,11 +172,12 @@ func serveCmd(cmd *cobra.Command, args []string) error {
return err
}
_, err = tbl.
stmt := tbl.
UPDATE().
SET(tbl.Like.SET(Bool(!msg.Like))).
WHERE(tbl.ID.EQ(Int64(int64(messageID)))).
ExecContext(c.Context(), db)
SET(tbl.Favorite.SET(Bool(!msg.Favorite))).
WHERE(tbl.ID.EQ(Int64(int64(messageID))))
_, err = stmt.ExecContext(c.Context(), db)
if err != nil {
return err
}