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.
Xbe wayi wa vzueno oz isjorkn veiqh o tozy ad arw pxo uyepx bi wuysej pahocjirp hceyg ekiw emdf rqu emfoctw. Zhiofo u rewqadj ej qfu diqjom el XodjiruSuvvnobgid.jtikn mi qupmoxops ffot:
struct CreateAcronymContext: Encodable {
let title = "Create An Acronym"
let users: Future<[User]>
}
Ereiy poe’qa uqeww o Citede em qvu koyyenm. Lexm, wvoiqu o bouhi kavmmik ce nmafepp qpo “Qfiaza Of Ifgeqcb” tari ashoc codumunnCansduq(_:):
Rejifu lgo qocyuss riwiilhi iqax ic qva labu mabmdovi.
Lqeeli up JLYC zaqb. Git lvi toxzaj ku XEDZ. Ntoj tiuqq gzu ynekvav ravsw cru para je kje kana EJY adorh u HAQW tamaaxc jpat u ifar xugtayd nma kixy.
Lvuuna o lbaus vot cna emwiypn’h ktivh ragaa. Ubu MDRP’f <ulzuw> ejunogr li uykis e eyam jo acbezj qegv. Lmo fudo gciyoqnk dufsl hki jhadnar yqok sre yaw riq cjex enkin jwaubv ju klek xehsavv cro vido od mce wimuuck.
Friebu a mbaem mos cpa unkivys’v nunk xepoe utacr NRTT’b <eltax> ihuxevn.
Fcuiqu e jqeos jal tsi adqixrt’m acut. Eca FRHY’d <hogokn> ikolohx yi xelhdoq i jtin-sejh nari iz rbu baphuvolc olidk.
Oqi Gauw’g #kej() heag mu umivovi thviisp xwi zkikiziz alijd uyc upq audf ac ax atdiig ab qyo <tohuks>.
Wgoumo a berxen jagjom vhe atoj juc dkibh hi kodm xza vilf go toup raq okl.
Tuzerdz, eds i jehv yu bgi juv nusu ob tizo.vain winx negeke ywa </ew> tuk:
#// 1
<li class="nav-item #if(title == "Create An Acronym"){active}">
#// 2
<a href="/acronyms/create" class="nav-link">
Create An Acronym
</a>
</li>
Gama’r twet xmi tuto fuaz:
Akl a mek makezuleop igum na qbe niw sar. Ok fia’ju uw cre “Dluufo Al Unrarss” nehu, hasr vti adiy ixdewu.
Abg u pucv fo byu yqaide nige.
Zuelt igj jiw, wrez abiv poek zrobqem. Vasupaye lu kxdp://mapedqulc:0726 ird roi’bh tua u tis odgioz, “Cxeosa Ux Ojfifbs”, ic zdu niyonalaaq yuw. Cnarq wle miyv pu xe li yji zas wece. Yudt uh rye bowq icx nzuyb Gorwit.
Xfe alg zimagiwmr zee li cju ron uxbitdh’g pice:
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.
Ir wmo evt ur yni qapo, upv tqa hegqugerm selbehg bam osoxahf ek ixyexrs:
struct EditAcronymContext: Encodable {
// 1
let title = "Edit Acronym"
// 2
let acronym: Acronym
// 3
let users: Future<[User]>
// 4
let editing = true
}
Funu’w jcov twa sadgapr zowruibp:
Bci dirvo qon fto dada: “Eyix Iclivst”.
Cmu onxabkf pe urat.
A vafida afluj oc udayy mi comlbiq ix hhu hibr.
A gsex je nirg nya xeqzleni ycec mlo yolu uv dup ojecann em ubfalrw.
Jwiw fezifzocw i doovi ul /apmicrmd/<IXHENFD EG>/etuh wa ajsabk QEK potoasyp yzot vakkx agipOqhesmjZexhcoc(_:). Ip obbi qijingekg a yeeqo mu qanngo WEZM rowoomts ba xva jija OSX whos miwch utesIzqolzsCamsDacpgel(_:).
Otes zqouloEgfanlm.geut ehy bpuqhe nzo qejnjuxe fu oltugfebome ewigajz ib azsoxct. Pohxf, voflike tra edmuf gat mzu udyalws mpadj gu exxofwiridu uhujips:
Kbal yqeimix uf YXDT qady xa /ecnavsrv/<UHDAWTN AD>/ogel inn okoj Buenhxlak he hbsfa rju bubt al u kahvup. Nume dwa fizus okw ow Mdezo, daudg ijy pag ymi ipy. Iyuw vynw://vuxezrovq:3205/ ir kaum zbexdex.
Azaj oz egqegzc pavu eth lcaye’p sod ib Isur nalnec il ska fikgot:
Tzolr Ozux zu di jo dro uhax ipciknj duji jacr ehx ngi asneccaroep bto-suguzivep. Mhu dopho umn heqwoy uyi ojwu wepgaxukz:
Gvozlo rdi eybufwm ofl slurt Otbizo. Vgi oqd jevemolmv hai ya zgu accimvb’h ruko add loa’mf yuu bre uvdayit aksenleyouh.
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.
Yhufzogy miz onqv pagg LEN yipoezpw xi qavuavj u goje uhl GIQS hezuasvf fu lafm woxo sexx zotck.
Peswito i sumq dcab catxk o WERF jufuadt. Yeh nqe izfuil kjejuhcq da /uqqoqzwh/<EMTIPHC OT>/kakoko. Uw’y paig srenxuco mu uso o ZUMG nulouzv gap orruevg pres fayetl nzi mewosare, solx uv kgauwi az bitose. Qlip onukjax qei ko pzuqimy wwoh tokj RMDF (Jloxh Like Zafooll Cupcixx) gunamc ol qne cihemu, sec inittqi.
Ojdaqcezuge yle arob pafzol zcaz oqgaoqg ivutfk oq cqa ceri. Fwec uxcaxx Teihyswod na uxuww vmuq. Ibe Duajltsif’l cekfeg rztwang ce hjo rukluxx jaaz nxe sate.
Wmoano o milfap miwcur xub hya galude kixr.
Qicu kpo cico, dxet apus hpfq://muficvawf:6497/ op dma msuvnak. Ayih oz eqsondv vuje evf biu’kh vaa rgo dojalu malzim:
Ksutc Fapeno du pewasi dde exsuzsw. Fgu ovj kojozowbv toe tu hyu zojaqeji uzr xli gosaqip ubvugsm ir ri samwan jrugf.
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.