% -*- mode: latex; -*- mustache tags:  
\ifx\wholebook\relax\else

	\documentclass[10pt,twoside,english]{root/support/latex/sbabook/sbabook}
  \usepackage{import}
  \subimport{root/support/latex/}{common.tex}

  \hypersetup{pdfinfo = {
    Title = {TinyBlog: Presentation and Model},
    Author = {},
    Keywords = {pharo, smalltalk}}}
  \begin{document}
\fi

\chapter{TinyBlog: presentation and model}
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 \ref{TinyBlogOnPharoCloud}).
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.


\begin{figure}

\begin{center}
\includegraphics[width=1.0\textwidth]{figures/TinyBlogOnPharoCloud.png}\caption{Final state of the TinyBlog application\label{TinyBlogOnPharoCloud}}\end{center}
\end{figure}

\section{Installing Pharo}
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: \url{http://mooc.pharo.org/}. For this project, we suggest to always use this image because it contains all the packages you need.
\section{The model of posts}
The model part of TinyBlog is really simple.
We start here with the \textcode{TBPost} class.
\subsection{TBPost Class}
We adopt the following naming convention: all class names will be prefixed by \textcode{TB} (for TinyBlog). You must choose another prefix (e.g. \textcode{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 \textcode{TBPost} class using:

\begin{displaycode}{smalltalk}
Object subclass: #TBPost
   instanceVariableNames: 'title text date category visible'
   classVariableNames: ''
   package: 'TinyBlog'
\end{displaycode}
\subsection{Description of a post}
A blog post is described by 5 instance variables.

\begin{tabular}{lll}
\toprule
\textbf{Variable} & \textbf{Signification} &  \\
\midrule
\textcode{title} & post title &  \\
\textcode{text} & post text &  \\
\textcode{date} & date of writing &  \\
\textcode{category} & name of the category of the post &  \\
\textcode{visible} & is the post publicly visible or not? &  \\
\bottomrule
\end{tabular}

All of these variables have corresponding accessor methods in the 'accessing' protocol.

\begin{displaycode}{smalltalk}
TBPost >> title
   ^ title
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> title: anObject
   title := anObject
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> text
   ^ text
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> text: anObject
   text := anObject
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> date
   ^ date
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> date: anObject
   date := anObject
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> visible
   ^ visible
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> visible: anObject
   visible := anObject
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> category
   ^ category
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> category: anObject
   category := anObject
\end{displaycode}
\section{Post Visibility}
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.

\begin{displaycode}{smalltalk}
TBPost >> beVisible
   self visible: true
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> notVisible
   self visible: false
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost >> isVisible
   ^ self visible
\end{displaycode}
\section{Initialization}
The \textcode{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 \textcode{unclassifiedTag}  method.

\begin{displaycode}{smalltalk}
TBPost class >> unclassifiedTag
   ^ 'Unclassified'
\end{displaycode}

Pay attention the method \textcode{unclassifiedTag} should be defined on the class-side of the class \textcode{TBPost} (click on the \textcode{class} button to define it). The other methods are defined on the instance-side: it means that they will be applied to \textcode{TBBlog} instances. 

\begin{displaycode}{smalltalk}
TBPost >> initialize
   self category: TBPost unclassifiedTag.
   self date: Date today.
   self notVisible
\end{displaycode}

In the above solution, it would be better that the \textcode{initialize} method does not hard code the reference to the \textcode{TBPost} class. We will present and discuss this point later in the MOOC.
\section{Posts creation methods}
On class-side, we add methods to ease posts creation for blogs that belong to a category or not.

\begin{displaycode}{smalltalk}
TBPost class >> title: aTitle text: aText
   ^ self new
        title: aTitle;
        text: aText;
        yourself
\end{displaycode}

\begin{displaycode}{smalltalk}
TBPost class >> title: aTitle text: aText category: aCategory
   ^ (self title: aTitle text: aText)
            category: aCategory;
            yourself
\end{displaycode}
\section{Creating posts}
You can now create some posts.
Open the Playground tool and execute the following snippet:

\begin{displaycode}{smalltalk}
TBPost 
	title: 'Welcome in TinyBlog' 
	text: 'TinyBlog is a small blog engine made with Pharo.' 
	category: 'TinyBlog'
\end{displaycode}

If you inspect the resulting object of the above expression (right click on the expression and select the \symbol{34}inspect it\symbol{34} menu entry), you will get an inspector on the newly created \textcode{TBPost} object.
\section{Testing post classification}
We define a method for testing if a post has a category or not.

\begin{displaycode}{smalltalk}
TBPost >> isUnclassified
   ^ self category = TBPost unclassifiedTag
\end{displaycode}

Similarly to \textcode{initialize}, it would be better to not hard code the reference to the \textcode{TBPost} class.
\section{Conclusion}
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.


\ifx\wholebook\relax\else
\end{document}
\fi