React Native & Golang tutorial | #4 Ticket Endpoints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so on the previous video we were working on the events end points and on this one we are going to work on the tickets end points if you are new to this channel welcome and this actually is part of a full course we have on react native and go length so if you are interested on building this app make sure you click on the link on the description to watch the full course all right so let's create our model for the Chet modeles chet. goo let's open this file let's say here package models let's say type ticket struct and we're going to have the ID in here just like we have on the events so let's just grab that from here cuz it's it's the same actually all right so we also needing here the event ID in J it's going to be just event ID the event is going to be just the event and in Json it's going to be event and in G foring key and this is going to be event ID semicolon constraint on update Cascade comma on on delete Cascade entered it's going to be a boan so in Jone it's going to be enter it and the default value here is going to be false all right so let's also have the they created that and updated that as they are very similar to these ones let's just grab from the event and paste that in here so let's also create our chage repo repository which is an interface oops which is an interface let's have the Gap manyu let's have the context which is a context and this is going to return a slice of pointers to tickets and an error all right so let's have a g one and let's have the same in here and this one we need a ticket ID which is on sign integer let's copy this let's paste that in here and this is going to be just a poed to a ticket let's have the create one and the create one we need to pass a Chet let's pass pointer to a ticket and it's going to return the same as the one from above let's also have the update one this uh this is is going to have the similar prams for the G one and this will also use the update data which is a map uh where the strings where the keys will be strings and the values will be interfaces this one will return the same right so let's have one more thing here which is a type val day ticket which is a stct and this is going to have a ticket ID ticket ID which is an unsigned integer and when it's Jon it's going to be a CH IG all right so that's it for the model let's Now work on creating the repository so let's do the following do uh repositories ti. go let's open the file let's say in here repositories let's create the function fun new ticket repository which going to have a a pointer to g. GB and this is going to return ticket repository all right so let's return in here a pointer to ticket repository and this we receive a database as we don't have the ticket repository on this file let's create a type ticket repository which is a struct that receives the DB which is a pointer to g. DB and this is complaining because we don't have the methods defined in here so let's start with the G manyu pointer to ticket repository get many this is going to have a context which is a context. context oops and that's all and this will return a slice of pointers model oops not even ticket and an error so let's create here a slice of pointers model check get our response from r. DB the module it's going to be modules ticket we want to preload the tickets so we have the actual data from the event we want to order each of the tickets by the updated at descending order and when we find those the value destination is going to be the tickets if there is an error and error is not new let's see let's return new so we don't have tickets and the error it's going to be just response the error in here if everything goes fine let's just return the tickets and error is going to be new all right so let's copy this one and let's now do the get one and here we need to add the ticket ID and sign integer and this is going to return a pointer to a single ticket and here this will also be pointed to a single ticket so we need to change this here we don't need this we can just say chage because this will be a ticket and we want to preload as well and we want the first one and our destination is going to be the ticket and in here we just return the ticket let's again just copy this and the next method it's going to be the create one and here instead of passing the ticket ID let's pass a pointer to a ticket and here we don't need this the model is going to be the ticket and in here we just need to create so let's just call create best ticket in here we actually want to return something different let's use our get one let's pass the context and let's pass the ticket IG let's duplicate this function and let's now create our update one so in here we want to pass a ticket ID which is an un sign integer and then we want to initialize so let's grab that from maybe from here let's paste that in here our model is going to be the ticket and we want to update where the IG is the ticket ID and this updates using the update data that we forgot to pass in here so let's add that dat data which is a map string interface oops interface so let's pass the update data if we have an error let's actually call this update response it is more concise and after we update that let's just return the ticket using our G one all right so that looks good for the repository let's now create our Handler ticket Lo go let's open that let's say package handlers let's say in here new ticket Handler which has a router which is a fiber router and a repository which is going to be a modeles ticket repository all right so let's have our Handler in here which is going to be pointer to ticket Handler which we don't have yet so let's create this so this is a type tick Handler which is a stct and this we have a report z t which is just uh models ticket repository and in here let's pass the repository which is just this repository let's now add our end point in here let's do oops get let's use the Handler get many when it's a post let's do create one when it's a get but with have a ticket ID on the brms let's do a get one oops when it's a post to this end point let's do a validate one all right so we don't have this handlers yet so let's create the method receivers all right so function Handler which is a pointed to ticket Handler get manyu and as always context is going to be a pointer to fiber context which returns an error and for our context it's going to be very similar to what we have let's grab that from the event let's copy this let's paste oh we didn't copy that let's copy let's paste that in here so let's now use the repository method so let's see tickets error from Handler repository get many let's pass the context and that's all we need to do in here and for the rest of the code it's going to be very similar to what we have on the events so oops no the whole code only this part let's grab this part of the code and let's paste that in here and the data will be our tickets so let's now create the get one pointer ticket Handler get one the same in here context fiber pointer context error let's use our context from here to get ID that's using here string com aoy and context fors ticket ID all right so let's so let's now use our repository method from Handler repository which has the meth get one so let's pass the context and the tiet ID and for the error handling it's going to be very similar to this so let's just copy and paste that in here and then change this yeah that's all we need to do for this one all right so let's now do the create one let's actually copy the whole G one Handler let's change this to create one we have our context we do need to initialize in here a ticket which is model ticket instead of get one let's say create one and instead of the ticket ID it's going to be the ticket but this is going to be empty cuz we are not getting any info from the request body so let's use the body parser if error if error which is context to body parser and the destination is going to be the ticket if error is not new then we're going to return something similar to this to this error but the status is going to be um status unprocessable entity and the data it's going to be new let's say status created and return the ticket all right so for the validate one it's going to be very similar to the create one so let's just copy this and let's say in here validate one and here instead of just ticket let's say validate validate body which which is going to be validate ticket and let's use that in the body part here validate body and in here we want to call the function oops update one and we want to pass in here the following so let's create a new validate data which is basically uh let's make a map string interface and let's add to the validate data oops validate data let's update the entered field with the value true and we need to pass the ticket ID in here so let's do validate body ticket ID for the update data let's just pass validate data so in here let's update this message to ticket valid actually let's just say welcome to the show and this one let's see that is okay yeah that's fine all right so that's it for our handlers we are actually going to do a couple more updates on the next lesson on the tickets also on the events but let's keep it simple for now cuz we don't have uh user tape table so we don't have to do any more changes on both of these models let's do a couple more things which are basically updating our Auto migrate to contain our new table which is modu ticket let's update main cuz we don't have anything here initializing our stuff for the tickets so let's say ticket Ripple Ripple repositories new ticket repository let's pass the DB let's just duplicate this let's create a new new ticket Handler let's passing here the router for the ticket end points and let's pass the ticket repository all right so let's start our server all right so we have an error in here if you get this error you just have to open Docker cuz Docker is not open all right so I have now Docker open so we can try again to start so let's do that so it's building all right so our web server is running let's open Postman we forgot to call this one one all right so on the ticket booking let's create a new folder let's call this ticket add a request it's going to be URL ticket and we don't have any data cuz we don't have any tickets so let's call this get mini let's create another request let's say one and this is going to be a post to the ticket if we send this this is going to be un processible entity CU we didn't send any data event IG and we do need an event that actually is in the database so let's get many here from the events let's use the ig4 and let's say n ID is a four all right so here's our ticket it has the ID the event ID it also has the actual event with its data and entered is obviously false so let's not to fet this ticket using the Gap one end point and in here let's just say one and we get our ticket the ticket we just created let's call this get one let's close this let's close this one all right so one more missing which is post ticket validate all right so for this one it's going to be very simple let's to send a body where the entered field is going to be set to true we also need to send the ticket ID so let's send the ticket ID let's say one all right so that updated as you can see the entered is now true basically this would mean that our ticket will be disabled when we see on the ticket list when start building the react native app let's also update the name in here this is post one actually let's not call this post one cuz we already have one in here with the same name so let's do validate one all right guys so that's it for this lesson on the next lesson we are going to start working on the authentication creating an account logging in start building the QR code functionality so I hope you enjoyed this lesson and I see you on the next one bye-bye
Info
Channel: Vaillant
Views: 178
Rating: undefined out of 5
Keywords: reactnative, android, ios, programming, go, golang, mobile development, software engineering, app development, programming tutorials
Id: 4D9Sn-hcY8U
Channel Id: undefined
Length: 21min 1sec (1261 seconds)
Published: Mon Jun 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.