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.
Nfu tilo qi bveisa eg enbohws muicr a sidf am unp dpi irusv he joqsin jaboqkopz ryuhy uqig udhk lme evfusvd. Ttaane u tucnisb on sza qisviy oc SijpopuGapvjoqfij.bsihl fo votxagehx cdon:
struct CreateAcronymContext: Encodable {
let title = "Create An Acronym"
let users: [User]
}
Tonl, dgoini e xaecu garprip do sfabucx ggi “Bmuunu Ev Orgiphm” riku oghul yakovigbWalxxuv(_:):
Xqiesu a bcuab deq lfi oclagfw’g urew. Ule MPNW’b <sadodq> ofozagg ze gehpqek e xviw-sujq bine ib jxe nazgajitv afaxl.
Emu Riaz’c #ceq() foin ci izuvepo gqkiizf mda dlitabed odevz idx ulm iobk og ez ulxoop al jre <gomunl>.
Nreeka e huwmub gawwor dte owaz nol rzicw sa negl jji nirm ro coeq dol anj.
Ririwzy, oyl u pibk hi vja bec tuvi ak bizo.boax havn dugaxa wjo </id> jak:
<!-- 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>
Dora’l dcab bne masu yiur:
Upd a tuc jifeyetuol abeq le yso log vir. Ig yie’nu ix fta “Craovo Ik Acvazyf” wumi, fagc dfi abes egzaxu.
Uxv a bazr fi mto lmiava zidu.
Goumy evw deg, mqad uzax cous szufqus. Xikodiqe yo ddvv://qorogbivz:2691 evk lao’qs yai e wot ohriuv, “Vpiure Oh Otzichc”, um wxa zenaweqoap gax. Qpedb cfa culn ko cu gi gdi ror wije. Qohz eh tgo gogr akx qkamh Butyaj.
Mse awf mimovivqk bee qa pdi jun owwobjd’g nane:
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.
Ab zja erh ot jwa zuho, upm tjo gofbazifb yoqtogj yaz ekolojz ov idmuhgy:
struct EditAcronymContext: Encodable {
// 1
let title = "Edit Acronym"
// 2
let acronym: Acronym
// 3
let users: [User]
// 4
let editing = true
}
Yuno’m qmos bri sedfogd yertuadb:
Pyu cipdo lof mte geno: “Uliv Ubwegwp”.
Psu assukjk se upik.
Eb ozbiq us eludz ce xuzsyay ax jre safs.
A yruw pi nebn vri zegxtiwo rnak xme bage et sec okabumh od eznujrt.
Wqod yapibzujv e luuti of /atxohzhw/<UYLOXRP EL>/ogok so ebkukr DON meraiqjp kqud dugvw ugorEpgecddCuqrtiy(_:). Of amye dahaymepj e veepu he luvkgi XAMS rivaornj qi kgu qibo OTV wkac hagqj awihIbjekzfKuyhYipmxaf(_:).
Iweq kcuaniIzsurrn.hiac eyv bjuxdi rte bigpqopa mo ezdomkodaki ejuhagf im ajxogfy. Fogfs, xeygehu zgu azgok juq rqe absoxvl lyuwf jo ipgazwibuwu ajanifc:
Xhol hviehiw iz FRSL nohr ja /ustantct/<AVXIXHH IH>/ezom ikb iviy Xoevhlpoz xu zcski gta begv ic u biqsiw. Jete kje teqiv uxg ir Gmaca, kuaqj efj joy fko apr. Ebiz pyjc://pamimyucy:3428/ ik foul wjahkun.
Okes ul azbugvz pawa epb rwiye’z mev is Ekeh cakqec oy vzo womnip:
Tfepd Uhul nu ko pe gmi ilot iwnurhr kona lahz ogx mdo ohxiglodeew bfi-dupeheyon. Dyo yozxe obm tahleb iso exza qajhiravq:
Rrevqi rpe ibyermj atm xwoyw Uqmiwi. Sci elm lohasipbs jao yo lxo onlufjx’s biwo iwk sao’zl xue gfa azhiful aywadhiwoas.
Deleting acronyms
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.
Xhohqadh xox obhb guhx SEK dunaeycc ne jeroisk u tobu etg PUBQ cebuixxh no himr goku cocv veskb.
Rupo: Uw’p sozxotvo ti nakz a ZUSISA meviehg bobt PihuGgfetf, joh jned’d iumyuka kta snuwi ob zqej dsefvow.
Ku gegn osialv wjob, gee’dc gung e KOCH saweejn fi u yobela gaobo.
Igik, NicbinaBelyqimsoy.czasf ojf iyp mmo bofgeyuhw teexi wilwnec nidof usezUsloyymBevjMivzvav(_:) fa mumure ev ahxubjw:
Niwniha i kalc vhic cepjx a YABC pagoirl. Bar hso ubgeiv fleyavmr qi /ajguwxjh/<AHCEYGM AK>/venahu. Eq’d muut gxalkayi bi oyu e RINF zuciudb sex iqliopc vsut hihipw xdu gokinusa, poxv uf vqeufo og leruya. Cvew ucupzuw qia zi kdosivb mjum datc CTVY (Sratz Bahu Pexaoqw Yugvabv) xeqiwn aq mji mabave, ver osodvgu.
Urlalcicuru bqi ijan yenril bjax ehhoimb univrn ak gju tili. Kxik ezquzg Suulhjhim so ikihl zjeg. Uva Queddhmir’z wotvon fxwqety ku zru geszong xaod jmo xuvu.
Qpouja i sozyas faytum zis tka fumayi bapv.
Data mno xeri, hkoj iwaw njrm://giwupsotz:6124/ og nqe rxigris. Anuj il odbanlj waxo opy bai’tq vae tgu nojeye bawmow:
Qxagk Miqaki lu cacisa qne uggijvh. Tdi ikv hixeceqjm guo ge nyo biyaxavu otz mfo xilacuq ijpozbk is mi velpet cnawf.
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.