Hello, and welcome back to the Pharo course.
This segment covers two more aspects.
I'll cover them in depth.
You'll use them yourselves in the TinyBlog exercises.
First, what web development can we do once we have described data?
Second, how does Seaside use REST?
Seaside is one of the frameworks for REST in Pharo.
Let's start with the first subject.
Magritte is a framework based on the hypothesis
that if you describe your data once,
it can generate several objects based on this data.
Webforms and graphic interfaces, for example.
In this course, I'll be focusing on
showing you how to generate Seaside components
so that you can code web applications faster.
We'll take the class "Address" as an example.
These are Swiss addresses.
An address is defined as a street, a place,
a postal code, and a canton.
I have an instance which describes a particular street.
So far, this is just standard programming.
Now, what Magritte does
is describe the fields in the class.
It will describe a string in such a way as to define it as a street name.
The postal code description is more interesting.
First of all, we are going to say
an address is not valid without a postal code.
Addresses lacking postal codes are invalid.
And all Swiss postal codes are figures between 1000 and 9999.
This is specified in the NumberDescription.
This information can be taken into account
for automatic form validation.
For Place, we also describe it as required, and as place.
Moreover, in Switzerland, there are 26 cantons.
The canton is part of a list, so we have SingleOptionDescription.
It is required. We want it to be sorted, and the options listed.
So the list of options is limited to these 26.
Once that's done,
what can we do with Magritte?
Here is how the address is described in Pharo.
We'll define a descriptionStreet as a StringDescription,
with such and such a label and priority.
For street numbers, I define a NumberDescription:
I assign a different priority and label.
I specify "Required" and give my minimum and maximum values.
The very first thing I can do with this description
is write a little tiny program to interpret the address
and generate reports.
I take an address,
and run it through this program,
which generates a little report:
The name of the street,
The place - Bern, and the canton, Bern.
Postal code 3012.
I can use the fact that my data has been described
to generate reports.
That was the simplest kind.
But now we'll do more complex things with Seaside,
like creating a visual component.
Here, I am saying,
"Address object, I want to see you as a component."
That generates this part.
I'll add a validation form, which gives me "Save" and "Cancel."
And I'll write a "call" command so it displays.
As you can see, I managed all that without even using the Seaside DSL.
Described, my web components are generated automatically.
That's powerful!
Here is what the company Quuve does
Quuve is an American portfolio management platform.
The whole display was done with Seaside and Magritte.
All the reports are generated automatically.
That's a serious gain in productivity!
The idea of this course is to give you some intuition
of what can be done when you describe your data,
especially with web component generation.
You'll do that in TinyBlog.
Now, I'll show you the potential in Seaside's REST layer.
It enables client-server communication in the absence of visual components.
REST is integrated into Seaside smoothly.
Domain objects will be annotated.
You'll have a natural conversion between URL parameters
and SmallTalk or Pharo methods.
To give you an idea of the complexity,
we'll define what we call a "filter":
TinyBlog Restful Filter.
is a subclass of WARestful - it doesn't matter.
The important part is the filter I add down here when I create my application.
You'll see it in the exercise.
Now, let's imagine that I want to get all the blogs
that I have on my TinyBlog server.
Here's how:
First, I define a method
in the RestfulFilter class, a method called listAll.
It corresponds to the anchor I'm going to use:
I tell it to use its "get" from the REST protocol
to generate text in json form.
I create a Pharo string stream
I want the stream to contain all available blogs.
I'll take each blog - and you see another iterator -
Take each blog and convert it to Javascript,
separated by commas.
That gives me what I want.
Now we want to do something a little more advanced.
Say I want to find a blog by searching for a title match.
Here, I can use "post," for example.
I'll say I want this blog post, by giving the title next to it.
I do that by defining a "post" method that takes a title as a parameter
along with the "path" concept.
I tell it, when the URL contains http..., post/
and something here, those characters are the title.
That's what we see here, inside the wavy brackets.
Likewise, I tell it, that will produce text in json form.
Then what I do is call upon TinyBlog functionalities.
I ask, do you have a post with a title specified as a parameter?
Yes? If it's empty, I return an error.
Otherwise, I put it in my stream and render my stream.
There are lots of things we can do.
If I want to do a search, it's the same thing.
In my URL line, I type
"search: title" with a value, it will be matched automatically.
My title will appear here.
That's how you work with REST in Seaside.
You can have your graphic components in Seaside
and a way to expose your domain functionalities
with a REST API.
I've only shown you "gets."
But all the other REST functionalities are available:
Delete, Post, Get, etc.
I also showed you how Seaside, paired with Magritte,
generates powerful forms, automatically.
And REST is well integrated.
Often, people use Teapot as a REST solution, as well.
It's a Zinc overlayer for doing REST prototypes.
It's a quick way to find out if their REST API is working.
Have fun with it.
There's documentation in the Web Enterprise Book.