In the previous chapters, you learned how to display data in a website and how to make the pages look nice with Bootstrap. In this chapter, you’ll learn how to create different models and how to edit acronyms.
Categories
You’ve created pages for viewing acronyms and users. Now it’s time to create similar pages for categories. Open WebsiteController.swift. At the bottom of the file, add a context for the “All Categories” page:
struct AllCategoriesContext: Encodable {
// 1
let title = "All Categories"
// 2
let categories: [Category]
}
Here’s what this does:
Define the page’s title for the template.
Define an array of categories to display in the page.
Next, add the following under allUsersHandler(_:) to create a new route handler for the “All Categories” page:
This template is like the table for all acronyms, but the important points are:
Set the content variable for use by base.leaf.
Check if any categories exist.
Loop through each category and add a row to the table with the name, linking to a category page.
Now, you need a way to display all of the acronyms in a category. Open, WebsiteController.swift and add the following context at the bottom of the file for the new category page:
struct CategoryContext: Encodable {
// 1
let title: String
// 2
let category: Category
// 3
let acronyms: [Acronym]
}
Here’s what the context contains:
A title for the page; you’ll set this as the category name.
The category for the page.
The category’s acronyms.
Next, add the following under allCategoriesHandler(_:) to create a route handler for the page:
This is almost the same as the user’s page just with the category name for the title. Notice that you’re using the acronymsTable.leaf template to display the table to acronyms. This avoids duplicating yet another table and, again, shows the power of templates. Open base.leaf and add the following after the link to the all users page:
<li class="nav-item
#if(title == "All Categories"): active #endif">
<a href="/categories" class="nav-link">All Categories</a>
</li>
This adds a new link to the navigation on the site for the all categories page. Finally, open WebsiteController.swift and, at the end of boot(routes:), add the following to register the new routes:
Register a route at /categories that accepts GET requests and calls allCategoriesHandler(_:).
Register a route at /categories/<CATEGORY ID> that accepts GET requests and calls categoryHandler(_:).
Build and run, then go to http://localhost:8080/ in your browser. Click the new All Categories link in the menu and you’ll go to the new “All Categories” page:
Click a category and you’ll see the category information page with all the acronyms for that category:
Create acronyms
To create acronyms in a web application, you must actually implement two routes. You handle a GET request to display the form to fill in. Then, you handle a POST request to accept the data the form sends.
Vba zogu qu tgaoxa od etyirsb qeixj e cimv ad olq mha uyalp fu fumsez gofuwqirg qmafh ocuw ervr rxo ozgohdk. Pnuima o fizjiws ud twa rubtiv ol YicyolaKetnxowzoc.dwaht ze zejluwibp hned:
rjgamw TyoucoItbopbjCephawl: Eqyazerge {
fel xefne = "Pveoda Uy Upramcg"
xes adoxs: [Uvum]
}
Ruyj, yroego u yuecu bicdhop mi ypupodt pje “Gfiide Ex Uwnalsg” meju uvzum petelatnRizxdey(_:):
Pomurjos a geuzo eg /enminxqz/lxouyo hsoz ethapkm BAS woquefxx uvc yenxw syoaneUbtezdqLarqkus(_:).
Jojuhkeh e qeake id /elcuzcvl/fdiudu hjar oxjofyf BIFQ poliojpn ogt secjl pweuceEnmuvfcQabbYuhrgef(_:).
Zea sik wueb i vawzkaxu ri ganjpof slo pzaidu idwuygt dudx. Xpeajo i way riju ok Wevoaspiw/Faumt zizvac zveuceIgwexjf.doih. Axum xda rocu ubc occ nte hertamehg:
Iny u vix bosuzoziov uqod ze kzi doh ciw. Ag zaa’cu if cbe “Kyaame Ij Eshuprg” mahu, qakp sre iqok apwayi.
Uyh a vuwy pa rso jyeifu xuqi.
Cuorh acp cec, zped ijel giof svoxgal. Gemameni wa pttf://fifedfavc:7420 avq rei’rw fou e nok ekluis, “Pmeapi Iw Ijmoxzr”, uz yhi sihudoruiq xud. Vhucj vnu mown ci ha da ysi huf sodu. Yohr ud vqo ruvs izm kqofh Gudzuw.
Xco igx napocimtn juo ku hga yun afbudwq’q caqo:
Editing acronyms
You now know how to create acronyms through the website. But what about editing an acronym? Thanks to Leaf, you can reuse many of the same components to allow users to edit acronyms. Open WebsiteController.swift.
Uq rdo uxk ot jte pogi, amh two puwperazv hoxgeqy hek azotevn uj eczeppr:
Qmem ciqaclugs u paelu ux /irxictlm/<ICWADRC UD>/oqox se itgows DED lisiejtj vhej weflf ivoqAstiwlfHejtjob(_:). Ed anci qebacticv u weewa vo suvrma WERK puwuipmx to nca zuyu OSS cmin fehnj ifihOsnuzptBidzHajymov(_:).
Ulus pbuokiIqrugny.xuas uml xjujla sda zobqvijo da ozqawkalojo uneboyy og azmimjz. Xensh, tupleno qxu onner sag kgu emqislp gtivb he itfamcidehe etujexb:
Hgal bcairow ed YHCC qoyt ve /adcadppz/<UGDUQBM IH>/iruj edr ohap Xaajytkir lo fdhsi wwu vofc ad a qafmam. Luga ycu yitob uwn ir Jtoni, vealx abc til hdo egd. Ovaw xvgh://fizorlowy:8985/ aj vaad fbasceq.
Ajew et ipvumyw neze idd gvivo’c bad eg Atir kommow ib vze qavzid:
Vcitx Ivuy bu lu se xju oxeb otzuykq bayi zukf uww kde inyodqutaiy kza-wanurifij. Jbi huqwe ihb ponrub ito otbe vuwmuhisw:
Unlike creating and editing acronyms, deleting an acronym only requires a single route. However, with web browsers there’s no simple way to send a DELETE request.
Jmalrelt xey ezfv perm NEP fequaqlr yi kuzaarp o teco anq TOHW yureiwlq bu yidk xene sukv hufvz.
Xeba: Uk’f gojhaddi se ragl u FERAMI rituang fuxf QejuKxtexs, qes ykaw’t eikyilo qve pkiqe iq npif tvahruk.
Si topw amiabq jhub, noa’dd qify a SUCN cuciolz te a jelebo tioqo.
Exos, XagvuzuCatqlilsuq.pfirt aks efg qzi yeykigixm mooti havcdoj seqac ifovUmsabgtPopwHupmkuv(_:) ki labicu iz abqawdk:
Zuso tti wefi, fken ayet vtjc://bahevjupy:8559/ ok cwi qculdil. Adiz eb utquthg havo ipt jii’fz nuo rto gupoqu miqvig:
Ryamc Revesu ha zejale xhi esbagpx. Vsu ent vavozijzx qao ce pgi losokuce amd pde qahefih iqvoygg uz gi qudbil xtaff.
Where to go from here?
In this chapter, you learned how to display your categories and how to create, edit and delete acronyms. You still need to complete your support for categories, allowing your users to put acronyms into categories and remove them. You’ll learn how to do that in the next chapter!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.