In this project, we will guide you to develop a mini project: a small web application, named TinyBlog, that manages a blog system (see its final state in Figure 49.1). The idea is that a visitor of the web site can read the posts and that the post author could connect to the web site to admin the application and manage its posts (add, remove and modify existing ones).
TinyBlog is a small pedagogical application that will show you how to define and deploy a web application using Pharo / Seaside / Mongo and frameworks available in Pharo such as NeoJSON. We will show you how to expose your application via a REST server. Presented solutions are sometimes not the best to offer you a room for improvement. Our goal is not to be exhaustive. We present one way to develop TinyBlog nevertheless we invite the reader to read further references such as books or tutorials on Pharo to deepen his expertise and enhance his application.
In this project, we assume that you use Pharo 5.0 and since we will also use some extra libraries and frameworks dedicated to web development such as: Seaside, Magritte, Bootstrap, Voyage, VoyageMongo, ... we provide a specific Pharo web image that already includes all of these libraries. You can download it at the following address: http://mooc.pharo.org/. For this project, we suggest to always use this image because it contains all the packages you need.
The model part of TinyBlog is really simple.
We start here with the TBPost
class.
We adopt the following naming convention: all class names will be prefixed by TB
(for TinyBlog). You must choose another prefix (e.g. TBM
) if you want to be able to load the correction code in the same Pharo image as your code to compare both implementations.
Define the TBPost
class using:
A blog post is described by 5 instance variables.
Variable | Signification | |
---|---|---|
title |
post title | |
text |
post text | |
date |
date of writing | |
category |
name of the category of the post | |
visible |
is the post publicly visible or not? |
All of these variables have corresponding accessor methods in the 'accessing' protocol.
We should add methods to make a post visible or not and also test if it is visible. Those methods are defined in the 'action' protocol.
The initialize
method ('initialization' protocol) sets the date to the current day and the visibility to false: the user must explicitly make a post visible. This allows him to write drafts and only publish when the post is finished. By default, a post belongs to the 'Unclassified' category. This category name is defined on class-side by the unclassifiedTag
method.
Pay attention the method unclassifiedTag
should be defined on the class-side of the class TBPost
(click on the class
button to define it). The other methods are defined on the instance-side: it means that they will be applied to TBBlog
instances.
In the above solution, it would be better that the initialize
method does not hard code the reference to the TBPost
class. We will present and discuss this point later in the MOOC.
On class-side, we add methods to ease posts creation for blogs that belong to a category or not.
You can now create some posts. Open the Playground tool and execute the following snippet:
If you inspect the resulting object of the above expression (right click on the expression and select the "inspect it" menu entry), you will get an inspector on the newly created TBPost
object.
We define a method for testing if a post has a category or not.
Similarly to initialize
, it would be better to not hard code the reference to the TBPost
class.
We have now a first part of the model and it is a good time to save your Pharo image (menu item 'save'). In the next session we will show you how to save your code (package) with the Pharo versioning system.