# teaching machines

## CS 347: Lab 14 – The People’s Dictionary

October 6, 2021 by . Filed under fall-2021, labs, webdev.

Dear students:

Welcome to lab. Now’s your chance to apply the ideas you read about. Find a partner and complete as much of the task below as you can. At the end of our time together, paste your HTML files into Crowdsource in order to receive credit.

### The People’s Dictionary

Your task in this lab is to create a web service for managing a crowd-sourced dictionary similar to Urban Dictionary. Support the following endpoints and test that each works with curl:

• POST /:word, which adds a word (if necessary) and a new definition for the word. The definition is assigned a new integer ID that is not used by any other definitions for the word. It yields a structure with the fields of the new definition. The JSON payload of the request looks something like this:
{
"definition": "A mix between a phoenix and a felis domesticus."
}

The JSON payload of the response looks something like this:
{
"ok": true,
"result": {
"word": "ashcat",
"id": 16,
"likes": 0,
"definition": "A mix between a phoenix and a felis domesticus."
}
}

• GET /:word, which yields a word and its definitions. The definitions are returned sorted from most-liked to least-liked. The JSON payload of the response looks something like this:
{
"ok": true,
"result": {
"word": "promise",
"definitions": [
{
"id": 1,
"likes": 57,
},
{
"id": 0,
"likes": 4,
"definition": "A structure used to represent a pending computation."
}
]
}

If the word doesn’t exist, then a 404 response is given with ok set to false and an error message given for result.
• GET /words, which yields a list of the words in the dictionary in alphabetic order. The JSON payload of the response looks something like this:
{
"ok": true,
"result": [
"ashcat",
"computron",
"dexahecimal",
"heaps",
"pdf",
"unary",
"..."
]
}

• PATCH /:word/:definitionId/like, which increments the number of likes of the specified definition and yields the incremented number. The JSON payload of the response looks something like this:
{
"ok": true,
"result": {
"word": "ashcat",
"id": 32,
"likes": 1,
}
}

If the word or definition doesn’t exist, then a 404 response is given with ok set to false an error message given for result.
• PATCH /:word/:definitionId, which updates the text of the specified definition, resets the number of likes to 0, and yields a structure with the updated fields of the definition. The JSON payload of the request looks something like this:
{
"definition": "Someone who shovels coal into the furnace of a steam engine."
}

The JSON payload of the response looks something like this:
{
"ok": true,
"result": {
"word": "ashcat",
"id": 32,
"likes": 0,
"definition": "Someone who shovels coal into the furnace of a steam engine."
}
}

If the word or definition doesn’t exist, then a 404 response is given with ok set to false an error message given for result.
• DELETE /:word/:definitionId, which removes the specified definition from the dictionary. The JSON payload of the response looks something like this:
{
"ok": true,
"result": {
"word": "ashcat",
"id": 32
}
}

If the word or definition doesn’t exist, then a 404 response is given with ok set to false an error message given for result.
• DELETE /:word, which removes the word and all of its definitions from the dictionary. The JSON payload of the response of the response looks something like this:
{
"ok": true,
"result": {
"word": "ashcat"
}
}

If the word or definition doesn’t exist, then a 404 response is given with ok set to false an error message given for result.
• GET /search/:substring, which yields entries that contain the specified substring either in the word itself or in one of its definitions. If the word itself contains the substring, all of its definitions are returned. Otherwise just the definitions that contain the substring are returned. The JSON payload of the response looks something like this when the requester queries cat:
{
"ok": true,
"result": {
[
{
"word": "ashcat",
"definitions": [
{
"id": 0,
"likes": 4,
"definition": "Someone who shovels coal into the furnace of a steam engine."
}
]
},
{
"word": "segfault",
"definitions": [
{
"id": 6,
"likes": 3,
"definition": "A catastrophic failure."
}
]
},
]
}
}


### TODO

The next step of your learning is to complete the following tasks:

• Read the chapter Consuming a Web Service in All Over the Web. Be sure to follow along to the code samples. Recreate each exercise yourself.
• Research some aspect of web development on your own. Find articles and videos beyond what’s assigned. Summarize what you learn in a couple paragraphs of your own words and a snippet of source code in your next blog entry before Friday morning. Clearly put the date of the blog entry on your index page. If any of the requirements is not met, you will not receive full credit.
• Start thinking about your API for project 2, which will be due around the end of October.

See you next time.

Sincerely,