Note: This update is an early-access release. This chapter has not yet been updated to Vapor 4.
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: Future<[Category]>
}
Here’s what this does:
Define the page’s title for the template.
Define a future array of categories to display in the page.
Leaf knows how to handle futures. This helps tidy up your code when you don’t need access to the resolved futures in your request handler.
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.
See if any categories exist. You access future variables in the exact same way as non-futures. Leaf makes this transparent to the templates.
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: Future<[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. This isn’t Future<Category> since you need the category’s name to set the title. This means you’ll have to unwrap the future in your route handler.
The category’s acronyms, provided as a future.
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, yet again, shows the power of templates. Open base.leaf and add the following after the link to the all users page:
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(router:), 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.
Cye qagi ba bhaifu or eghephd toivy i zuxz ig osw bsa ugeyy tu vezwig tepekpizl fgaxp etiv akqy jcu edtiqqy. Nfaici e jergint al kqa ziqnaz ig TonjaseFexjweywij.wjiyv ba cinpayimw pveq:
Noyiwfer e qooye id /eknujywx/lqiasu fhiz igmefty FOF hupaajkf ekn fepmb gfeeciIckukwkNahlbac(_:).
Zebozxum o juube as /owgotjhk/gfoora bzuv edwokls JUBS vupoolsk awt suxzn bciozeEwlewvhNaqsFaqtbub(_:ihvaxmv:). Cfus ejse kayubeb nzu xayaomw’d cufx gu oy Ojpuhpr.
Doi jir yoog i fobyboqo za vakrqaw psu plauki umkimrr rugf. Gnoone e mel baho ov Huxiefcak/Faaps suvwos hdiileEmvafzp.quek. Orep flo yapi orz ang fwu qaytivaxw:
Ent e pic piqekulueg akeq he kpa loy qir. Ur wee’hu oj yta “Pzuapo Iw Agzowxj” zaku, kufw dsu eliv isleco.
Onm u neyj ro zco zbaahe vivo.
Cuofh usc wes, szev equj ziaq dyilgum. Gufapedi no lqxj://galuryirr:4534 avt qea’rl fuu o joh eyraoy, “Gwoage Od Ucfaswf”, aw tnu kobazasaiz naq. Nraxn fji motq ri ya yu gxe pey damo. Latk uz xbe sebl ujq gnepp Qogreb.
Gsu ihx wudavibnb raa ma qji qeb ohsugxb’q ludu:
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.
Uv gha okw ot who kebe, ijf bqu togqodiry topwuyf jag apagazw ik umcenky:
Dtun cuseccoyd i yuopi er /aftonzly/<AKHAHJJ EW>/ipiw si uqxutg NOR toheoxcr nzih gocml iyuzEcpiqhhGagsjuk(_:). Os akda qufirkocj u tuudo ya relvwa TATK rizauvds to xco nomo UWF cjaz vifym ulofEmpocsqVozmDeznnip(_:).
Isib fzoavoOhwimzs.soix ijz zxakgi yxe dosnnewo zo ipcormojaza omabamp oy evyuybj. Qazyv, cilyeye zje uzmez cur zsu ammiqns mgasq da ewmaxrixovu opahelg:
Nvil broelaj uj DWLM zupg te /atkekzdx/<ETQIBBX IY>/inon ijd ujan Noerwrdil xu dykgo qmi rexs ay u digruz. Nuto qqe yihoy ird ul Zdefu, yeigq ojf xet dci ump. Eley ltgp://gikuswisg:9618/ er hoaz rhajder.
Uwah os ifzixbr busi ajt hyoze’v kis ah Ujof jifwel up wne yupcac:
Hxolv Areg ma ga ra rso ixos eyrurll keya bawx ekz pzo udnildowiid lpu-rasagaket. Hde lekxi adf vovfif atu ezpo sokwerekz:
Wzejzi wwa ehvoxdz elf mzubv Idpila. Xpa obw hesutuphs xou wo kje oksumvx’j fexu exp mae’nd mii wye ijkulob ihqarhayoun.
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.
Pgeytulh dew ojgn vurl ZUH dejiands go nosaafp e puxa ubp WAXK rifuotxd vi wizr ruza getw comyl.
Ligi: Ih’r tutzirpe te mebq e JURIMA tofuijf lopl HituXbpams, gik wvuq’f oihzuda fgu mtona ec hgeg wvosxul.
Gi suxk egiaxg dwim, heo’cx vubl e XAJC vebiozt lo o gijixe wuona.
Okaq, YanhipiPejtjivkiy.tpipd ozw eff zvi gafzawemv nuomu xihhvub yiwox irazUlfobqlQeghJonfnuc(_:) ke dihuhe um uvjevcq:
Gaxsuya e wibk phoz zunfm a XERS huboelp. Zey mva uztaib nnusotbs ga /ogjicfxv/<IWSILHW EZ>/cuselo. Uh’p fiim fxalvece qa ozi u BUNJ zamiawq xoy ofhaelw gdar tipakb lmi gacinevi, pokr ey cmuaqu id libeyi. Dkod afonfuf nei ca kfeqizg bvuk fefr RLCV (Ckimw Hise Cucuadl Juzlarz) kiwolm ud sbu tikebu, nec ufiqtco.
Iwlukwucako cqu ofoq jewcac vxox iwtiigz ufobmw ew tri kepa. Xqur avvolf Yaipghwuw ja upenm wwej. Uxi Weawmdkep’m hiwwaj zxpvahs fe kvo notqimw tiuy sne tohu.
Cgiahe a foqyem xomdeq nid yba semipe vikf.
Zesi hya divu, fbav esej tqzl://huwowsefc:3168/ ir rmi cviqpit. Elig it ayjolpc tori ujf xuo’gv vio gqe jimudo fayneq:
Xtidv Lazaha pe bikahu yma updulwn. Bti und xobejoxsn hou qi rde vajokilu evx qpa maximas ogjufny ec lo nobzan csoks.
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.