Fixes: #15, #13, #14, #11, #8 Add the IDEAS to a 'Suggestions' section in the FUTURE_PLANS file. * Updated the cabal instructions for level 03 * Removed the duplicated config loading in level 05 * Add the implementations for File.hs for levels 06 & 07 * Renamed the slightly misleading 'readObject' function * Fixed the capitalisation of init and close DB functions between levels 04 & 05.
1.7 KiB
Level 02
This level focuses on using our application's requirements to design some data structures, and how we can then use those data structures to guide construction.
By using these data structures we provide a mechanism for utilising the compiler as a pair programmer. It will inform us when we've forgotten to handle a given path, tried to use information we don't have access to, or haven't validated our inputs sufficiently.
To build this REST application we're going to need some requirements:
Requirements
We have a WebThing(TM) somewhere and we would like, for some unknown reason, to be able to add comments to various topics on this WebThing(TM).
"Spec"
This will be a REST only application, there won't be any HTML involved.
Let's pretend we've completed a dozen specification meetings with our Project Manager, resulting in the specification below:
We have to be able to:
- Comment on a given topic
- View a topic and its comments
- List the current topics
So we will work towards building the following routes:
# To comment on a given <topic>
POST /<topic>/add
# To view all the comments on a given <topic>
GET /<topic>/view
# To list all the current topics
GET /list
The starting point for this exercise is the src/FirstApp/Types.hs
.
Running the program:
# Using cabal
$ cabal run level02-exe
# Using stack
$ stack exec level02-exe
Accessing the program:
Using curl
:
# Running a POST
# Valid request
$ curl -XPOST -v localhost:<port>/puppies/add -d "Puppies are awesome."
# Invalid request (should trigger an error in the program)
$ curl -XPOST -v localhost:<port>/puppies/add
# Running a GET
$ curl -XGET -v localhost:<port>/puppies/view
$ curl -XGET -v localhost:<port>/list