Like/Dislike button logic on client or server? - frontend

i am working on my full stack blog site and currently making like and dislike component for article (id, name, likes, dislikes) like this:
[like 34 ] - [dislike 12]
where like/dislike are buttons with their corresponding counters. When i press like 2 times it should unlike and should do the same with dislike. When i press dislike after like it should unlike and dislike and opposite.
My question is, as there are a lot of API calls here, should i make all logic on front and just send basic PUT to server with changed article, or should i make /like, /unlike, /dislike, /undislike... on back and do the logic there.

Related

How to send multiple statements in google assistant app?

I am creating a Google Assistant app for telling quotes, I am currently using Api.ai with ApiAi NodeJs webhook. I wanted that my response should be in this way:
Innovation is the only way to win.
By Steve Jobs
Want one more?
Note that all the three lines are different lines. I know it is possible if I just use api.ai's ux without webhook (using multiple Simple Response) but I cannot figure out how to do it when combined with webhook.
I tried:
assistant.ask("Innovation is the only way to win.");
assistant.ask("By Steve Jobs");
assistant.ask("Want one more?");
But it seems to speak only the first sentence. I also tried by replace it with:
assistant.tell("Innovation is the only way to win.");
assistant.tell("By Steve Jobs");
assistant.ask("Want one more?");
But it exits just after the first statement. How to do it?
Both ask() and tell() take their parameters and send back a response. The only difference is that ask() keeps the conversation going, expecting the user to say something back, while tell() indicates the conversation is over. If you think of this in terms of a web server, both ask() and tell() send back the equivalent of a page and then close the connection, but ask() has included a form on the page, while tell() has not.
Both of them can take a RichResponse object, which may include one or two strings or SimpleResponse objects which will be rendered as chat bubbles. You can't do three, however, at least not according to the documentation. So it sounds like your best bet will be to include one SimpleResponse with the quote and attribution, and the second with the prompt for another.
This also sounds like a case where you want the audio to be different than the displayed text. In this case, you'd want to build the SimpleResponse so it has both speech fields and displayText fields.
That might look something like this (tho I haven't tested the code):
var simpleResponse = {
speech: 'Steve Jobs said "Innovation is the only way to win."',
displayText: '"Innovation is the only way to win." -- Steve Jobs'
};
var richResponse = assistant.buildRichResponse();
richResponse.addSimpleResponse(simpleResponse);
richResponse.addSimpleResponse('Do you want another?');
assistant.ask( richResponse );
This will also let you do things like add cards in the middle of these two blurbs that could, for example, contain a picture of the person in question. To do this, you'd call the richResponse.addBasicCard() method with a BasicCard object. This might even be better visually than including the quote attribution on a second line.
As for design - keep in mind that you're designing for a wide range of devices. Trying to focus on the line formatting when you have display modes that are different (and sometimes non-existent) is of questionable design. Don't try to focus on what the conversation will look like, instead you should focus on how much the conversation feels like a conversation your user will have with another person. Remember that voice is the primary means of this conversation with visual intended to supplement that conversation, not rule it.
From what I can gather from the documentation, .tell and .ask both close the mic. Try putting all of your statements into one string. As far as I can tell, .ask doesn't actually affect the tone of the speech; it just tells Assistant to wait for input.
assistant.ask("Innovation is the only way to win. By Steve Jobs. Want one more?");

How to build a facebook-like fast search by starting of words?

I have to build a search textbox in a web page similar to facebook search box. Client side there will be ajax calls. The user need to search into around 300.000 elements that have a description of a few words or an alphanumeric code. When user enters the beginning of a word, a call is made to the server which return best match based on the starting of any word or code but also suggest first the elements most recently by the user, then by the group the user belongs to and finally from the entire set. Result can be limited to 10-20 items.
How can I build a fast search by key with the value just the description of the element? We use SQL server but any other DB could be OK.
The implementation at the time was very complex to summarise here but I came across recently to UI-select that solve the front end problem nicely and it's very good component if you are using Angular
https://github.com/angular-ui/ui-select
then backend you can put whatever you have (I did with Redis)

How to put a limit of the clicks on the button in real time?

I have a question recently: How can we realize the restriction on the button clicks in real time?
For example, there is a restriction in 10 clicks on a button. For example Bob clicked on the button and then Bob and Rob saw that there stayed 9 clicks. After the 10th pressing the button is disabled.
What will you advise?
Use a database to track the number of clicks
Depending on your functional needs, you probably want to be on the "immediate consistency" end of the spectrum vs "eventual consistency" so either use a traditional relational database with constraints and transactions or store the clicks in a single mongodb document you can update atomically.
MongoDB You might set up a ClickTrackers collection and a document like:
.
{
button: "reserveSeat":
clickedBy: [userId1, userId2]
}
Then you could use $addToSet
a redis set might work well (assuming you are allowing 1 click per user)
You will want some real-time libraries to help you push updates out to connected browsers. Perhaps meteor.js or socket.io.js
There's no way to avoid the edge case of lag between showing a user there's 1 click left, someone else clicking it, and then the first user clicking it before the click count decreases to 0 (like buying tickets to a popular concert, for example), so you must handle this and write tests for it and have a UI to say to the user "oops no clicks left, actually, sorry"
Hopefully that gets you started, but as it is your question isn't following the stackoverflow guidelines as it is "too broad" and you haven't posted any code you've tried, so go try something and come back with some code snippets.

Take data from query string and put into another form value (for both display and posting)

With that said, there is a question that seems to be asked a lot and has answers - but I'm not a programmer 'enough' to understand all the different explanations. I was hoping for something more clear and concise to my very specific example and need.
I have two forms. (I might also add this is being used on Wordpress)
One form is on the first page and will collect age, home value, and debt owed - all in drop down select fields.
The second form is on the next page and will collect other contact information and upon submit - will post all the fields to my CRM.
I have been able to successfully get the data from the first form into a query string on the page of the second form. But try as I might - I just can't figure out how to get the field data from the query string into my field forms. (Both display or pre-populated and as a value ready to be submitted once the rest of the fields are completed.)
Here is an actual query string:
http://example.com/2-form-test-2-of-2/?age=75&value=572%2C500&lien=107%2C500&sendbutton=#.UYSBEKKG2So
I simply need to get "age" - "value" and "lien" from that query string showing in the fields on the 2nd page. Simple is best - even if it's 'hack-ish' as I will most likely just include the code directly on the Wordpress page that includes this form.
One possible solution is to add a piece of javascript that gets fired on page load and:
1) disables form post button
2) populates form fields with values from URL
3) enables form post button
If these values are just suggestions for user, which he could easily change before posting — this should work.
If, on the other hand, these values are internal and unchangeable by user, you should not expose these in editable form and allow user to change those by editing URL or form posting (but including these as read-only page fragments is apparently ok).
This is place for only programmers. To become a programmer, please refer some online resources and ebooks. without knowing basic concepts, you can't understand anything.
even after understanding basics, if you have doubts, you can ask here, providing the code samples you have developed.

CouchDB a real world example

Tonight in my daily tech Googling I came across couchDB, after seeing tons of presentations about how it perform ten to hundred times better then any RDBM, how it would save us from SQL languages, tables, primary keys and so much more. I decided myself to try it myself. Only problem it seems I am unable to figure out how it works.
Like for a start I would like to code a web contact manager using couchDB. The project would enable user to do basic stuff like
Create/ Edit / Delete contacts
see a list of their contact ordered
search them on various criteria
So how do I start ?
Here some of my thoughts
create a database per user like July, Ann
in those DB, add some document with type contact, the document would look like this at first place see code 1
create / edit / delete is straight forward just need to do the PUT, POST, DELETE in the good database
searching would be handled by couchdb-lucene like dnolen suggested
now here come the difficult part, I don't really understand the whole map/reduce concept and how I can use that to do the jobs I used to do with SQL. Also with views how do you handle paging, also grouping.
I would like to build a screen with a paging set of links something like this
John, Doe
Johny, Hallyday
Jon, Skeet
A B C D E F **J** etc .... <-- those are link to see persons with that first name
what view should I create to achieve that, if you can provide samples it would wonderful.
Contact document.
{
type: 'contact',
firstname: 'firstname',
lastname: 'lastname',
email: ['home': 'foobar#foobar.net', 'work': 'foobar#foobar-working.net'],
phone: ['home': '+81 00 0000 0000'],
address: []
... some other fields maybe ...
}
The upcoming book by O'Reilly is free to read online:
http://books.couchdb.org/relax/
Just install and play around - you can do straight http requests using curl on the command line, or use the built-in web interface called futon.
Storing and retrieving data is really easy, the hardest part is thinking in terms of map/reduce-views instead of sql queries.
IBM has a great tutorial, making use of curl to read/write via the REST interface.
Your application is quite easy to do with CouchDB. You would have a database per user. Contacts are simply documents in a particular user's database. CRUD is just talking to the database using HTTP. You could create views that emit keys (last name, first name) to allow for sorting.
For powerful search I would recommend couchdb-lucene.

Resources