投票系统存在逻辑问题
我正在寻求有关上下投票系统的帮助。
目前我有一个投票表,引用了投票的用户、被投票的用户和投票类型的信息(停车位),
CREATE TABLE parking_spots_votes(
vote_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
parking_spot_id INTEGER DEFAULT NULL,
key parking_spot_id_fk (parking_spot_id),
FOREIGN KEY (parking_spot_id) REFERENCES parking_spots(id),
uploaded_by_user_id INTEGER DEFAULT NULL,
key user_id_fk (uploaded_by_user_id),
FOREIGN KEY (uploaded_by_user_id) REFERENCES parking_angel_users(id),
vote_casted_user_id INTEGER DEFAULT NULL,
key vote_cast_user_id_fk (vote_casted_user_id),
FOREIGN KEY (vote_casted_user_id) REFERENCES parking_angel_users(id),
vote_type INTEGER NOT NULL
)
可以是 0 表示不投票,1 表示投赞成票,2对于投反对票,
我现在遇到了一些逻辑问题。
例如
- ,如果用户已经对 parking_spot 投票了怎么办
我如何检查用户是否已经投票,如果他还没有投票,则插入,但如果他已经投票,则不返回已投票。
如何更新用户(uploaded_by_user_id)分数。加 1 表示赞成票,减 1 表示反对票。
所以一般流程是,
用户按下投票,服务器检查是否已经投票,如果是,则不能再次投票。 如果不是,则 vote_casted_user_id = 当前用户, parking_spot_id = 当前信息, uploaded_by_user_id = 上传信息的人, 那么 uploaded_by_user 分数将根据投票类型进行更新。
我正在使用 java servlet,通过 JDBC 连接到 MYSQL 数据库。
有什么想法给我吗?
i am looking for help on an up down voting system.
at the moment i have a voting table that references the user that voted , the user who was voted for and the piece of information(a parking spot) that was voted for
CREATE TABLE parking_spots_votes(
vote_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
parking_spot_id INTEGER DEFAULT NULL,
key parking_spot_id_fk (parking_spot_id),
FOREIGN KEY (parking_spot_id) REFERENCES parking_spots(id),
uploaded_by_user_id INTEGER DEFAULT NULL,
key user_id_fk (uploaded_by_user_id),
FOREIGN KEY (uploaded_by_user_id) REFERENCES parking_angel_users(id),
vote_casted_user_id INTEGER DEFAULT NULL,
key vote_cast_user_id_fk (vote_casted_user_id),
FOREIGN KEY (vote_casted_user_id) REFERENCES parking_angel_users(id),
vote_type INTEGER NOT NULL
)
vote type can be 0 for no vote, 1 for up vote, 2 for downvote
now i am having a little logical trouble.
for example
- what if the user has already voted on a parking_spot
How do i check if a user has already voted and if he has not then insert but if he has then do not a return voted already.
How do i update a user(uploaded_by_user_id) score. plus one for an up vote and minus one for a down vote.
so the general flow would be ,
A user presses up vote, the server checks if has been voted for already, if so then you cant vote again.
if not then the vote_casted_user_id = current user , parking_spot_id = current info , uploaded_by_user_id = the person who uploaded the info ,
then the uploaded_by_user score would update depending on the vote type.
I am using java servlet with a JDBC connection to a MYSQL database.
Any ideas for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些基本上是同一个问题:如何测试用户是否已经对停车位进行了投票?好吧,只需运行一个查询来获取 parking_spot_id 和 vote_casted_user_id 匹配的记录。
如果不是太麻烦的话,我会将 vote_type 的值更改为 +1(表示赞成)和 -1(表示反对)。那么这只是
SELECT SUM(vote_type) FROM table WHERE uploaded_by_user_id=...
我希望这会有所帮助
These are basically the same question: how do I test whether a user has already voted on a parking_spot? Well, just run a query that fetches the records where the parking_spot_id and the vote_casted_user_id match.
If it's not too much trouble, I'd change the values for vote_type to +1 for upvote and -1 for downvote. Then it's just a matter of
SELECT SUM(vote_type) FROM table WHERE uploaded_by_user_id=...
I hope this helps
您可以考虑使用 parking_spot_id、uploaded_user_id、casted_user_id 设置主键,
这样当同一用户对同一上传用户的同一停车位进行投票时,您可以捕获异常并处理“您已经投票了!”
如果你不喜欢这个
您可以通过在保存投票之前编写自己的控制机制来处理流程,
编写一个选择来获取带有 parking_spot_id、uploaded_user_id、casted_user_id 的记录计数,如果计数大于 0,这意味着已经有投票,则不要保存并给出您的消息。否则保存您的投票并选择用户的分数通过投票增加它并更新记录。
希望这有帮助。
You can consider setting your primary key with parking_spot_id,uploaded_user_id,casted_user_id
so when a vote comes from same user for same parking spot of the same uploaded user you can catch the exception and handle it that "you have already voted!"
If you don't like this
You can handle process by writing your own control mechanism
before saving vote write a select that get count of the records with parking_spot_id,uploaded_user_id,casted_user_id if the count greater than 0 this means already have a vote then don't save and give your message.Otherwise save your vote and select user's score increase it by vote and update the record.
Hope this helps.