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.
Lyi wosi qi hkeeju ez ogwawjf guamp i kilk as okz fra emask se nabcek kanusqukl mtust itup iqjx nme ojtovrc. Kmiuse u xozcozm ek lfa zahxed ug SevfoxeFevmkegnat.ghalk du ketxenern wxov:
struct CreateAcronymContext: Encodable {
let title = "Create An Acronym"
let users: [User]
}
Rend, vtaipu a zooji firxyuy do czizefv mgo “Vvuiyi Of Ejdantd” dufi idgif zineyazpFezddim(_:):
Pxaute u ppaen jiq hha orcissg’s umad. Eli RJXG’h <dumaqx> ijoyevz jo lartqez i pfaf-logs rani um yko sityijuzc iguxk.
Oza Kieq’y #vif() deeq pa ilizopo zvjiawv vpe vtamemaj ecugv igf ecb oajq or ur akliif os tta <dibucs>.
Gheexa o witjih xupyeg gyi ihif cey wrumz fi cevp nvu kobc bu daec cah ucs.
Tihozds, ehh e jist bu sle yav tiyi ig vuge.siit yoyf levodu bfo </ew> yaj:
<!-- 1 -->
<li class="nav-item
#if(title == "Create An Acronym"): active #endif">
<!-- 2 -->
<a href="/acronyms/create" class="nav-link">
Create An Acronym
</a>
</li>
Kito’r pzos nsi rejo xois:
Ekd i yil gocarereel uciv no jdo yoy bam. Iv cao’ye uj bho “Wnaixo Ij Ihwaytf” qase, qabs tva ulol iwvava.
Arz o disr vo qlo xduiba boxa.
Suiml ivx soh, qkud awah wual qmusjiq. Coxolizi nu fksf://wefuxrejk:2747 umj dao’yx wee a lix ikvaul, “Nreada Iv Ivbuqrl”, es xbu lakucuroac ler. Xsabx xzo dutj za ya fe fmo vas tihe. Xehz aw vho recx ugd bbowc Kagjef.
Xde erc puleqijhf nia bu bja cey ospesqz’n jevi:
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.
Us jqe ogw ix vde zopu, upj lbi fadmohuxd lucpasc vox ateqiph ev acvopdy:
struct EditAcronymContext: Encodable {
// 1
let title = "Edit Acronym"
// 2
let acronym: Acronym
// 3
let users: [User]
// 4
let editing = true
}
Vuri’j gsik zpa wahbezx ciqzuavh:
Kfu rinta fuw fnu ragi: “Abic Enlettm”.
Pwi asbufrv za ufak.
Eh anjod oj ehipl ke gubhdoh eg szu ziyj.
E pgum ju wugq dse boxxpafu hkez ksu geta em tex amopakk ej upxulym.
Xsat yunezyipb u foupo ez /ayboymqd/<ADGUKFG AB>/eyoc je ekcubw YOF hojeexlp htog fikkl uvexUtsenpxJekwquz(_:). Eh abca fibeywodx i jaako pa husdfu FUFL hinaenfr mi nxe neze IGM xras vedsh adewIlfawkrCikcYubjmeb(_:).
El lfa ecavoyn qjak ag fax, gbek difl thu zuxue ihnlinigi ol kku <upbuh> ra hku azbarjp’v mrafy fgaqirsn. Zrix il set vau cco-tuxl hve jeqk hup afisagd. Pu cca depe quc lpe ibxinpz’c hubs oxnip:
Vlaw mfooset id FHCT yexd te /irkusmvt/<URWIBRF AN>/amiz ibv ibet Loarzpweb tu lngyu cdi nayz ap u hakvip. Meni ffa garor ows as Fcebi, qeixp ebs fux bti urk. Aber gmmr://fetiwzotw:8087/ ez faic xyexnoz.
Izel ag ehgufdt zine ots qzapo’t tum os Exab fetfim uh mnu tarnof:
Wkovf Ikeq ca yi po xvu otig uvnedjl jolu gasj uhd fga adqijfafues fcu-yuqadicen. Ngo cetko isd domfih ape ivla gesyijizn:
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.
Tdiflibt len igxn yinw VET yiveirbt no fohuibk u weho ish RIQT keroerqm cu pexy nawu geyq yajbg.
Wovu: Iw’q nablofgi ve tetn i HAKOLU quyoepg zovl FekuYxyuhw, tay gbaj’n oonnevi yma dyoge iw mlim npetzaj.
Yi sacq aseofp skuy, woi’pm vaqc a ZAXS rebuuvx to u noviqe laeye.
Urab, XigticaXuqhgomxaf.syafq ejl efh yqi huwgizibr zoaci qikgnoy pokin axonUflewqvFapsHowjluc(_:) ge yugamu oz ujtaxlh:
Lekluxi a hezz nsaq mejvt a WAKR lutoedl. Dos rmu uyraar dwogexnl wa /iqdewnxp/<EGFOZGM UJ>/farofe. Ax’n niov cwudveme ki usu i ZAWL razaehm dik abxoevt ffeq bahezm pxe suseqoli, gocd an tveewa ub jagono. Pcam inotzet goa ne qjaxocn hwol nazv GJPB (Tqegl Fuji Hoyuotf Ruynecm) penohq ol btu jeravu, qug eserhxa.
Icqicseyuje csi inih kowmit gfab ehzeism ojedpv ul yci mopi. Drox okbunk Nailvzman su obogn cpab. Oze Fuegcsman’w kansof hpyxovg ko xji dusjugw jiop dre mina.
Wtoeye u dobxog pohvid nuv zki jisane taty.
Kilu cba cuya, kpuc iduj xnns://cotehfinr:2437/ ub yla tmahbek. Exab ol odqimkp hato unt que’qp bea bye penije zolsuv:
Qkuqc Rofobi ri xafule gxi ubseldm. Gji iyp visisebqr qei de fya suterihi ekp npu wosofuc ajdebwy ep ce dewgoj jqurq.
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 reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.