0

I made a Web Application in asp.net MVC5. In my application I have some products. On every product a logged user can press a like button. How can I disable the like button after the logged user already pressed it. I want other users to be able to press that button, but not the users which already have pressed the button.

I don't want to save in my database all the users which have liked the product because I will have to many values in my database.

Can anyone give me an advice how I should do this? At least maybe I could save something in session.

Thanks!

Jason Portnoy
  • 757
  • 8
  • 23
Alin93
  • 3
  • 2
  • 5
    If you're not saving the likes in a DB then why even have a like button? – Rick S May 29 '15 at 19:40
  • 3
    "I dont't want to save in my database all the users which have liked the product because I will have to many values in my database." Databases are made to hold data. Unless you have very specific requirements that prohibit it for some reason, I would re-think this assertion. Persisting a user's likes seems to be the obvious solution here. – Dan J May 29 '15 at 19:40
  • add an OnClick event to the button, and then in that disable the button. – Dreamweaver May 29 '15 at 19:40
  • @RickS I don't save the person which have liked that product, but I save how many likes a product have. – Alin93 May 29 '15 at 19:42
  • So you don't want to use persistent storage for such information? You can use, for example, [session data](http://stackoverflow.com/questions/560084/session-variables-in-asp-net-mvc), but after he logs out, then logs in, he will be able to like it again. Is it acceptable? And session variables have their own [peculiarities](http://stackoverflow.com/questions/560084/session-variables-in-asp-net-mvc). – Eugene Podskal May 29 '15 at 19:43
  • 2
    Unless you persist this information somewhere, a user will be able to like a product multiple times. – Rick S May 29 '15 at 19:45
  • 1
    Store it in a cookie? Sorry, I know that's not an answer, but as others have said, if it's not stored somewhere, you're not going to be able to accomplish this. – James R. May 29 '15 at 19:47
  • 1
    Cookie would be fine but if the user is logged in on another machine (or they clear the browser cache), they could like the product again. – Rick S May 29 '15 at 19:51

2 Answers2

5

Just create a simple DB table called UserLikes

It can have two columns: UserId and ProductId

Whenever a user Likes a product, make an entry in the table. Then when the user is viewing the product page, if you see that entry in the table for that user and that product, disable the Like button.

Rick S
  • 6,476
  • 5
  • 29
  • 43
  • 1
    +1 Unless your website is very, very, very, very, very, *very* popular, this is not going to bring your infrastructure to its knees. – Dan J May 29 '15 at 19:59
1

If you're using HTML5 store it in the localstorage . It works just like session data but it will not expire when the session is over. Only when the user clears their cache from the browser it will be removed.

If you're not using HTML5 then you have to store it in a cookie via the server and set an expiration date to whenever you feel is necessary.

Nonetheless, your data will eventually become inaccurate when people clear their cache and go back to the site click like again since you're not making people log in. You cannot track who has already tracked who liked what and how many times they liked it. When that situation occurs you need to make people log in and track what they liked already via the DB but only if you really need accurate like count I would do that.

imGreg
  • 900
  • 9
  • 24