Random-Saying-Generator

Random Saying Generator

Web-service with RESTful API that allows to generate a random saying, like or dislike it and add own saying (adding a saying is currently unavailable).
Application uses Play framework and Cassandra as a database.

Running

# developing mode
./gradlew run

or

# release mode
./gradlew dist
./build/stage/playBinary/bin/playBinary

And then go to http://localhost:9000/sayings to see the running web application.

Database configuration commands:

# drops existing keyspace with all data
./gradlew dropSchema

# creates keyspace and necessary tables (just executes commands from "conf/create.cql" file)
./gradlew createSchema

# fills existing tablies with data from "conf/init_data.txt" file
./gradlew fillTables

API

GET /sayings HTTP/1.1

controllers.Main endpoint, from it you can generate random saying or add own.

RESPONSE

HTTP/1.1 200 OK

Content-Type: application/hal+json

RESPONSE BODY

{
  "_links": {
    "self": { "href": "/sayings" },
    "random": { "href": "/sayings/random" },
    "add": { "href": "/sayings/new" }
  }
}

GET /sayings/{id} HTTP/1.1

Gets saying with a given id.

RESPONSE

HTTP/1.1 200 OK

Content-Type: application/hal+json

RESPONSE BODY

{
  "saying": {
    "text": "Text of saying",
    "author": "Author",
    "likes": 120,
    "dislikes": 15
  },
  "_links": {
    "self": { "href": "/sayings/{id}" } },
    "rate": { "href": "/sayings/{id}/rate" },
    "random": { "href": "/sayings/random" },
    "add": { "href": "/sayings/new" }
}

GET /sayings/random HTTP/1.1

Gets random saying.

RESPONSE

Exactly the same as for request GET /sayings/{id} HTTP/1.1

POST /sayings/new HTTP/1.1

Adds new saying to the system.

Accept: application/json

REQUEST BODY

{
  "saying": {
    "text": "Text of saying",
    "author": "Author"
  }
}

RESPONSE

HTTP/1.1 201 Created

Location: /sayings/{id}

If such saying already exists:

RESPONSE

HTTP/1.1 409 Conflict

Location: /sayings/{id}

Content-Type: application/json

RESPONSE BODY

{
  "message": "Same or very similar saying already exists."
}

POST /sayings/{id}/rate HTTP/1.1

Rate the saying as liked or disliked.
Value of field “rate” shoud be 1 or -1, otherwise “400 Bad request” will be returned.


Accept: application/json

REQUEST BODY

{
  "rate": 1
}

RESPONSE

HTTP/1.1 204 No Content

Supported HTTP Status Codes:

200 OK Successful request.

201 Created Resource posted in request was successfully created.

204 No Content The server has fulfilled the request but does not need to return an entity-body.

400 Bad Request Wrong URI or JSON representation of data.

404 Not found The requested resource could not be found.

409 Conflict Same or very similar resource already exists.

500 Internal Server Error Unexpected condition was encountered on server and request can’t be handled.

Visit original content creator repository
https://github.com/mikhail-kukuyev/Random-Saying-Generator

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *