How to use postgres with golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys jack is back today we're going to be building an app to store users and their books in a database as well as make this information available in an api so that you can take what you learned from here and create some cool apps using react view angular spell or whatever you want to use to consume the api let's get into it all right so before we get into the code let's quickly go over which technologies we're going to be using for this app so we're going to be building an app that stores users and their books in a postgresql database we're going to be using a package called go rm because it simplifies things and it makes it so that we don't have to mess around writing any raw sql we'll be storing all of the sensitive information in environment variables and loading them through our terminal we will make all of the data available in an api and implement the ability to create read and delete data through the api i will be using postman to test out the api but you can use whatever you want all right so i'm assuming that you've already got go installed if you're actually watching this video but i will not assume that you've already got postgres installed pause at this point of the video i'm assuming you're using a unix-based terminal to execute all the commands that i'm about to show you if you're still using windows powershell or the command prompt what are you doing do yourself a favor and click the link in the description to a video to get yourself up and running with the windows subsystem for linux and the brand spanking new windows terminal all right trust me you won't regret let's get back to the video anyways on the screen right now all the commands you need in order to download postgres i'll have them in the description below as well so you don't have to type them all out but without further ado let's get into the video let's actually get started setting things up here so first things first what we actually need to do is go ahead and make some changes and some little tweaks to postgres because it's not set up right for development straight away out of the box we need to make a few little changes all right so first things first by default when you download postgres it creates a new user on your system and we need to we need to set a password for that user so we can do this by saying sudo pass wd wd and then the name of the user postgres and if we hit enter we can type in a password that user re-type it in oh for goodness gracious sake and password updated successfully all right cool so the next thing we need to do is actually start the postgres server we can do this by typing service postgresql l star and we need to add the pseudo keyword at the start of that sorry so that should go ahead and start the database server now the next thing we need to do is actually go into the postgres shell and just we just need to we just make a few a few little threes just a few last tweaks all right now the way we actually go into the postgres shell is by typing in sudo u postgres and then psql okay and as you see we're in the shell so now what we need to do is we need to change the password for the postgres user again this password is going to be the one that we actually use to connect to the database from the program and then we also need to create the database that we're going to use for today's video so the way we change the password is we can type alter alter user name of the user postgres we need what we're going to change the password and then we need the new password and some quotes for this tutorial it's going to be abc123 obviously guys if you're actually gonna you know make an actual project you probably want a password a little bit more secure than just abc123i and uh guys i've just made a classic mistake at the end of this line you need to make sure that you have a semicolon if you don't you'll run over to the next line no big e just type in a semicolon hit enter you should see alter role be printed out through content everything's good all right so the next thing is we need to create the database it's really simple just type in create database name of the database i'm going to call my database book keeper semicolon at the end of the line hit enter that should go and create the database and we're all set now what we need to do is type in backslash q press enter and we're out of the shell all right so now we're actually ready to start doing a little bit of coding so then what you need to do firstly is go to your uh go path or your go workspace and i'm basically going to make a new directory i'm going to call it uh book keeper and then i'm going to cd into that directory so now what we can do is open this up in your favorite code editor visual video code is what i'll be using today because it's just it's just it's close to my heart visual studio code it's very close to my heart alright so first thing is first we need a package we need to declare the package package main so with this app we're going to have some users and each user is going to be able to have like a bunch of books that are assigned to them if you can think about a library people can take out books and you know that book will be associated with that person so first things first we need to talk about models in databases so we can think about models as a way of describing an entity what is an entity well it can be pretty much anything let me give you an example if we're making an app that needed user accounts we would probably build a model to describe all of the different attributes of user for example we probably want a user to have a username an email a password and an h now for each of these attributes we would need to assign a type so the username would be a string for example an email could be a regular string but you'd probably build your own email struct to represent exactly how that string should be structured a password now a password is always a special type of string that's handled differently across different sort of technologies which is beyond the scope of this tutorial and then we have an age which could just be an integer now finally models can have relations with other models so in this app that we're building today we will have a user that has a one-to-many relationship with some books now there is another type of relationship called one-to-many which is sort of like for one phone there can only be one phone number all right so now that's cleared up let's get back into the code all right so now that we've talked about that we can go ahead and say type person struct so in a database model or in a struct that we're going to use as a database model we need a we need if we would need a few things to be declared first so we would need stuff like a unique id we would need a date deleted date created and date updated now if we were to put that in the struct it would make everything really cleared and we would have to like put out a bunch of sort of like almost redundant information in every single struct and it would get really bloated and there's no real point to doing that so thank goodness um jinzu the guy who made gouram made something called gorm.model and go ahead and import uh the garm here and basically what this will do is we'll insert stuff like the unique id the date creates the date update and the data leader automatically and it's just one line of code instead of what three four lines of code that's taking up all the space in our struct so that's pretty cool next thing we need to do for the person is every single person should have a name which is going to be a string the rest of this is pretty much just like a normal struct email string obviously if you're actually building out this app you probably you probably wouldn't just have it as a regular string but i'm going to have it as that for now and we're going to have books now books is going to be an array of type book we're going to create that in just a minute now email i want this to be unique i want every single person to only be able to have one email like only one email for every person can exist in the database so if two person try to use the same email there'll be an error now the way that we can do that is we need to use backticks and we can say gorham and we want to say uh quote type now the type is going to be a varchar type varchar 100. basically this is saying that we don't want the email to be over 100 characters and we need a semicolon in there with no spaces you want unique unique unique unique unique underscore index unique index is what will actually make sure that uh you know there's only one of these that exists in the database okay so now we need to make this book struct so we can come down here and say type book struct and for a book obvious this is also going to be a girl ramp.model model okay we're going to have a title which is a string we're going to have a author which is a string we're going to have a call number call number is basically like the number that we would use to identify the book if it was a real library so this is going to be an integer be careful that if you're using integers make sure that if the numbers are too big to be computed by goal line so for example if i like if i came down here and said var num equals you see it it's too big of a number to be using the program so if your call numbers are this long make sure that you store them as strings and not numbers but anyway that's sort of redundant information then we have a person id now because we're going to have a relationship between a book and a person we want to make sure that we know which person each book is assigned to and that's what person id is going to be now notice that i have you have to put the name of this struct here and then put id afterwards um that's sort of just how gorham works so if you want to have a relationship you sort of have to use the name of the struct and then something after it so this is going to be an integer uh and yeah i also want the call number to be unique for each um for each book so i'm just gonna say unique index for each call number okay cool so now we can go ahead and start writing up our main function so func main so uh the first thing we need to do when we talk about databases is we need a database connection string now this is going to have all of the information that our app needs to gain access to the database right now it's unsafe to store this stuff in in your code especially if you're pushing it up to github pushing stuff up to github you know obviously people can abuse that sort of data and they can access your database from you know whichever server they want so make sure that you're you're not storing this type of sensitive information in the mate like they're just the regular code we need to store it using environment variables which if we were pushing up to github we were putting a dot get ignore file now the way we can do this is by coming in and type in dot env all right and i'll show you all the information that we need to put in this file real quick let me just pull it up here all right so what do we need in this file so we want a dialect so dialect the dialect is going to be postgres it's basically like the type of database we're using then we're going to have a host which for now it's just going to be local host because we're in development then we want the database port the database port this is going to be by default it should be five four three two uh you might have to double check but if you've installed it the way i showed you in the video at the start it should work just fine so then we have we have user the user is going to be the default postgres then we need a name so the database name this is going to be book keeper the one that we created earlier in the video then we need a password password is abc123 and that should be everything all right cool so i'm not going to be using go.nd because i've encountered some problems with go.n for some reason there seems to be some authentication issues with it in visual studio code you can just press ctrl alt and down arrow to select multiple lines we're going to type export here this is basically going to let us sort of uh from the terminal we can set these environment variables instead of using go.nth okay so in the main function we need to go ahead and get all of those uh environment variables so let's go and do that so i'll just say uh getting or loading is probably that word getting loading environment variables i've probably spoiled that wrong overnight yeah invite run meant variables jesus christ okay so the first thing is a dialect uh dialect is gonna be what is it gonna be it's gonna be os.get and the uh dialect okay basically just paste this a bunch of times so then we need the host which is going to be the host then we need the db port which is going to be the database port that what it was yeah db port so then after that we need the user which is user then we will need the db name db name which is going to be the db name oh sorry just the name uh all caps what am i doing name then finally we will need the password which we stored as password okay cool so now what we need to do is go ahead and actually create the database connection string which we can do under here so uh database sorry database connection string string there we go so typically database connection string is called the db uri that's typically like what it's called usually so that is going to be fmt dot sprint f like so okay so on youtube me just have a look at my laptop so i can see what i'm doing here so you have host hostos host we have a user we have a uh db name of course then we have ssl mode which i'm just gonna i'm gonna disable it because it's a bit useless for development then we have password then we have port [Applause] okay and then after this you can just go ahead and put everything in there so we want host user then we want db name db name [Music] then what do we want we want password password then we want db port is that everything dialect oh shoot we're going to use that in a minute not just now also up here go ahead and install fmt or import fmt and the os package from the top so we're going to be using a minute so what we need to do now is it's going to make it a lot easier if we declare a few variables globally so var db is going to be pointed to gram dot db okay and we need to also create a variable for errors error there we go make sure you declare both of these global because i've encountered some issues if you don't do that and it's just incredibly frustrating all right so down here we need to go ahead and open the connections a database so opening is the eyespot opening two p's no it's not tps opening connection to database okay cool so down here what we need to do is basically just say db error equals gorham dot open uh we're going to have the dialects die dialect and the db uri okay so that will basically open up the connection database so we need to do some error handling quickly so we want to say if error is not equal to nil log fatal uh error of course make sure you import the logs package for that to work smoothly and if that is successful fmt.print ln uh success fully connected to database hey lovely job blue okay so next thing we need to do is if we if the database is not in use and the application is not running we want to make sure we close the connection to the database because there are a few select situations where stuff can go wrong if the connection's still open so we want to do is basically say we want to close connection to database uh when the main function finishes finishes there we go the way we can do that is using the defer keyword so the defer keyword of course it will basically say do this after the function that is currently running has finished so we want to say db.close basically what it's saying is go ahead and close our connections to the database after the main function has stopped running now you don't have to worry about it closing abruptly because we're going to have a web server running and a web server will continuously um will continuously run until you know until you close the app so after the web server stops running you'll just make sure to close the connection which is good okay so the next thing is next for now now what we need to do is do some database migrations if you don't know what database migrations are it's basically stamping these structs into the database so we can't create a person or a book if the database doesn't know how to create them a migration is basically going to say all right look if you ever get a request to create or update or delete a person all you need to do is um if you just make sure you get the name a person should have a name an email some books you know this will make sure that they already have an id a day created blah blah blah it's basically telling the database okay listen um a book has these attributes please make sure that that's how they look in the database basically so we're going to make migrations to the database now migrations only need to be made once and then like you don't need to do them again so this so rm will only do this if they have not already been created the way we can do this is super easy it's just db dot auto migrate and then we can put in here uh struct so person and it can just be an empty person's truck so we can go ahead and create a second one for a book as well okay cool so now um that's actually pretty much it at this point i'm going to go ahead and save it so what this should do is make some migrations to the database and if the connection's successful it should print successfully connected to database so i'm going to go ahead and really quickly test this out because i might be you know you never know about these things uh also before we run this because we're not using go.m we have to do source dot env in the terminal and that will set all the environment variables and then we have to do go run main.go and that should print out hopefully connection successful unknown driver postgres okay uh that's correct uh sorry guys uh may may not forgot about this we need to do an underscore here all right and then we need to do github.com so basically what this means is we're not going to be using this directly in the code but we will be we will use some of the middleware if i if i type this in correctly shoot hold on give me one sec here what did i do for go get uh what is it dialects slash postgres let me just grab this and i'll paste that in there okay there we go so github.com around slash dialogue slash postgres make sure we put an underscore at the start because we're not going to be using it in the code but we will need some of the middleware associated with this package and that's basically what this underscore means so now we need to go down here save it and we can go ahead and try run it let me just can i just exit that there we go cool so we need to go main.go hey successfully connect to database there we go i don't know all right whatever but it's working for me so if it's not working for you just have a quick look at the code again make sure you've not got any spelling errors make sure these are global variables make sure that you type source.env in your terminal session before you run the the program and other than that i don't really know if you've got any problems at this point make sure you put them down in the comments below i'll probably reply to you within the hour i'll try to so without further ado let's get into the next part so now we've pretty much got everything working so if we want to create something i think what we should do first is try and create some stuff in the database uh just normally using regular code before we transcribe it to an api because the api part is really repetitive to be honest it's just a lot of the same code um it's really boring so i just wanna i just wanna make sure this is working the way it should be first so at the top here let's create some actually above these below these above these uh it doesn't really matter to be honest i'll do this above so we are going to have a variable called p or pull which is gonna be an array of people we're gonna create one person so a person all right here we go so what we want is we want a name it's a name name jack why not it's just this mean it's just my name in it it's my name email jack email.com and we want uh we don't actually need to declare books here at all we don't need to have it here in any way shape or form okay so then let's create uh books so we want this to be an array a type book yeah i think so why not so in here uh so i don't know random stuff right title uh i don't know i don't read books uh let me look up and find the book uh why not the the the rules of thinking the author of god's sake this is so frustrating i can't type i need to learn to touch type i don't know how to do that yet the book was written by richard templar all right then the call number uh call number some random one two three four why not and then the person id now because this is the first because you know we're going to make one person a database this person i can tell is gonna have the id of one because it always starts at one and it's the first person in the database so we'll set that as one okay i spelt title wrong of course and i'll basically just create a two and uh the rules of treasure you know what i'm just gonna call it book two author authortube you know call number one two three four five personality one okay so we've got some people doing some books we wanna basically uh we wanna put that in the database when storing the database so the way that we can do that my friends let me just take another look at my laptop all right so what we need to do to create um a person we can just do db.create and then we can go ahead and say we want gaucher and i got sick and person all right so db.create and person what's going on oh people why did i call it people what am i doing all right anyway so there we go now because this is an array um we basically want to do four index to equal range books we basically want to do um db dot create books books books books books index there we go there we go there we go there we go there we go all right fantastic work lads let's go pretty sure we actually need that there for now we don't need that okay so okay i don't want to do it anyway so we will need it here though all right we will need it here i'll touch on this later but uh you know pointers and go are still they still blow my mind i'm just trying to think of how to do it okay so let's try and run this and see if it works i actually don't even know if it will work to be honest half the time that's just it could work it could also not work you know here's what it is let's see if it works oh it looks like it works i don't i feel like it worked let's open up a new tab and let's see what's going on so let's go ahead and say sudo dash u let me make this bigger for you lot basically going to go back into the postgres postgres p sql okay type in your password okay so we're back here now what we need to do is we want to see if we create anything in the database right so you can list out all of the different databases you created with dash l or backslash l and you can see you've got bookkeeper i've got some other databases here um you can press q to get out of it now we need to select bookkeeper as the database that we're currently focusing on so we can do that by typing in backslash c and then book underscore keeper all right hit enter and we're now connected to the bookkeeper as user postgres okay so what we need to do to see um basically all the information is we can quickly just type backslash dt that will show us all of like the migrations or the relationships in the table now we want to see if we've created a user way we can do that is all caps select uh select select select is it i think it's you can just type it in like people select people from sorry not select people from i'm having a little bit of a brain fog we want to select all or asterisk or star whatever from people you want to select all of the all of the people basically make sure you have a semicolon at the end of the line and you can see it worked we have a jack and we have the email here that's pretty cool all right so we want to see if the books were created as well we can do the same thing from we get all the books semicolon boom here we go so you see the date created i'm assuming that's the date created or whatever you've got the rules of thinking by richard templar and then we've got book two author two yeah so everything's working good all right so everything is actually being stored in the database which is pretty cool again backslash q to exit and i'm gonna go ahead and close this tab down all right so let's get back to work so now that we know this is working we can basically work on getting the api up and running now the api is probably the most repetitive part of this tutorial so i i beg and i urge you please try and stay focused for this part because it's very repetitive there's a lot of different controllers and handlers and this and that and it just gets really really frustrating let's have a look at the list of imports we've got now nothing's really changed all right i'm just trying to make sure because i've got the go extension installed which automatically does that for you sometimes i forget okay so let's go ahead and start off by making all the different routes for our api so api route all right so router is equal to marks dot new router now uh make sure you've got gorilla marks uh imported and we can go back down here and uh let's just say router dot handle funk we want to handle slash let's start off by getting all of the people all right and then we want to say get people get people all right and uh we want to make sure that you can only send a a get request so if you go to this you can only get people you can't send post requests and this and that um just to make sure that no one tries any funky business trying to delete people from your get people route um okay so i'm going to quickly code this one up first i'm just going to code one up first then we can basically copy and paste all the code for everything else so get people make sure you have w and r so w is http.responsewriter and r's are pointer to http.request and then in here um get people we basically want to get a variable so people p p or p or pull p or pull is gonna be um what's it gonna be my friend it's gonna be an array of persons an array of persons well um basically we're gonna do uh db.find and we're going to say we want to find some people all right so basically what we're doing here is we're saying okay we haven't we have a variable this a variable is of type array of people so go into the database and find an array of people all of the people right so that's what fine does it basically finds all of them all of the uh the models that fit this struct and it returns them and because we have it in an array it will basically return all of them okay so then we want to send this data back in json so we say json.new encoder w in that bracket and then dot encode and we want to encode the uh the people and send that back this needs to be a pointer to people okay so we're good so now what we need to do is basically uh go ahead and test shoujo's about so now what we need to do is basically import some stuff so you will need to get uh encoding slash json installed and you will also need um http now we need to start up our server so next we need to do is http listen listen and serve 8080 and we're gonna pass in the router like so save that and let's go and run it main don't go all right so we're looking good um let's go ahead and test this out so i'm going to be using postman all right so now the postman is loaded let's go ahead and create a new workspace so we want to test out low local host port 8080 slash people okay and then we want to go get request yeah we're good so we can just send it and boom we get back the json for this person now i notice how books is null uh we would have to do a little bit more work to get that work in i'm not going to do it in like this i'm going to i'm going to say so that whenever you want to get a specific person that is when you'll get the array of books back but you'll be able to change that if you want to it's very easy to change okay so let's go back now let me know this is working and we can go ahead and basically implement everything else we need so uh here i'm just gonna say api controllers okay all right so uh here we want to be able to get a single handler funk we want to be able to get a single person now if we want to get a single person we're going to need to have the id of that person not necessarily just the id but it's the easiest thing to get to to use right so we can go ahead and say um you know basically what will happen is someone will type in person and then slash and then a number and that number will be stored as the id in memory all right that's kind of how that works if you've not seen it before don't worry it's it's not that difficult it's not that difficult to get behind so say get person and again this is going to be a get request route then we want funk get person scroll down a bit this is also going to take in w and r and uh we basically first thing we need to do is say params so parents mox dot vars and we want to grab so just pass in r there so that's going to read the request head so that's basically how we're going to get our id it's going to be stored in this params array this is actually an array um well so it's going to be stored in this params variable anyway so uh after that what we need to do is we want to be able to find one person right so we need to declare a variable for a person to a person there's going to be a type person all right and then here what i want to do is say db dot first okay db.first is basically it will only ever find the first person with the query you give it so in this case i want to find the first person with whichever id was passed into the url so i'll say we want a pointer to person person and we want the the id so we can see we can get the id from the url by saying params and then id like so okay and that should work like a charm so after that we can go ahead and send that back through json so json.new encoder w dot encode person okay and uh yeah so that should work so we shouldn't be able to get one person now when i get one person i want to get and all of their books as well so not just the person i want to be able to get the person and all of the books associated with them so the way we're going to do that is we need to clear another variable here so books there's an array of type book then down here what i want to do is say database dot model okay and then we want to get person dot related and i want to get all of the related books okay so this is basically going to take a look at the specific person and it's going to get all of the books that are associated with that person okay so then what we need to do lastly is say person dot books and we're going to set it equal to books okay and then we can go ahead and send that back to json okay so now what we can do is test this out quickly so we can go ahead and start server again first voice so we can go ahead and start the server again and we can go ahead and get person and we'll just get the first person so now you can see we've got the first person just like before but now we've also got the books associated with that person and as you can see there's two books associated with that person okay so now uh what do we need to do so we can get to people we need to be able to create a person and we need to be able to delete a person as well right so in order to do that we can say funk um create person create person it's going to take in w and r just like before and uh for this one we're going to do these are the same things that we've done before pretty much so first it's going to be a person we're going to basically the way this is going to work is someone or an outside source is going to have to send some json we're going to turn that json into a person struct and then we'll create that struct in the database okay now the way we can decode some json is json.new decoder taken not w r dot body okay r dot body is basically all of the json data that's being sent in okay so then we need to go ahead and decode it and then we need to go ahead and decode it into to this person variable here okay cool so now what we need to do is we need to say created person is gonna equal db dot create person okay so now what we can do is we can say uh error is equal to created created the person dot error like so and we just need to do some basic error handling quickly so if error is not equal to nil um json dot new encoder w dot encode uh there's gonna be error so we'll basically send back an error um instead of the creator person okay otherwise if everything's successful we grab this and instead of sending back the error we'll just go ahead and send back uh the person okay so that's that's good that's cool that is fantastic work so what we need to do at this point is go ahead and should we test it out i think we should test it out right so i'm pretty sure this should work i don't see why this wouldn't work there's obviously there's gonna you can see there's some errors here i'll handle that in a bit all right um but i'm not really concerned right now so in the router you can say router dot handle funk and i want to have this at slash create slash person person okay and then we'll just have the create person this one since we're creating something is not match what's going on what's going on what's going on what's going on because we're creating something this is going to be a post request we're going to post something to the database okay save that about boy com1 let's go back to the postman and let's go ahead and just first of all need to start this up i'm getting ahead of myself guys i'm really excited i'm really excited i'm really excited all right so and then we can go to create slash person okay now if we click on body we need to go ahead and create um [Music] we need to go ahead and create a request so a user is gonna have a name that name is gonna be um i don't know we're gonna create why not you know why not just why not you know what i mean name you know musk uh we're gonna have a uh yeah yeah yeah yeah yeah what was it it's just name and email right so email is elon email.com okay so we go ahead and send that i think that should work method not allowed that is because we have to make it a post request send that boom here we go so we've got an idf2 created at blah blah so now if i if i just show you if i actually send this again we should get there we go severity error code yeah so basically duplicate key that's because we have the same email so that's working good as well okay so that's pretty good that's working exactly how i was hoping for it to work which is shocking to be quite frank if you you won't believe how many takes these videos take it like stuff always comes up i'm telling you so now the last we need to do is be able to delete a user so we want to delete person person wr and just like that but we want to say uh if we want to delete someone we're going to have an id in the url again just like for getting one person we need to know who we're going to delete right so we want to have do the same thing as before set params is marks dot vars okay uh and just put an r there all right and then we need to come down here and we need to need two two two two two we need to go ahead and create another person so a person this type of person okay and then what i need to do my friend is give me a second to think right my brains are melting right now jesus louises so we first of all we need to get all of the information from this person in the database it's the best way to do it right it's the safest way to do it so we can do db.first again we need to get one specific user right and if we can't there's nothing to delete right so that's basically how this works we look for the user if you can't find him send back um you know an error or this will print an error to the console actually i don't think it would do anything to be fair i don't think it actually does anything i think it just you know the leak request just won't happen we basically need to make sure that we there is someone in the database that has these credentials or this id so then once we find that person sadly they will have to be deleted from the database all right and then we can go ahead and send back a json json.new encoder w dot n code and that will be person fantastic absolutely fantastic work all right so yeah that's actually pretty much it the only thing left to do would be to update a user i'm gonna leave that up to you as a um as the viewer as a task to the viewer so i'm going to go and test this and if it works we'll just quickly go through everything we need to do we need to create a route for this first so router dot handle func slash delete slash person and then we need to have an id in there and we'll just say delete person person and then we can only send delete methods to this so after this we'll just go ahead and work on being able to create uh delete and get books and then uh i think that's pretty much it dude oh yeah so this is because we sent that elon musk thing twice so it prints out to the console as well all right so let's restart the server right cool let's go back to postman and i'll go ahead and say i want to delete delete delete delete delete delete a person uh slash two elon musk is number two if i send the delete request to that send that so it's there we get back this um the json of the user then we can go back to people p or pull p or pull and you'll be able to see yeah so elon musk is not here he's not here anymore he's he's vanished he's he's passed away sadly he doesn't exist in my database anymore okay so now we've done everything we need for the people we just need to work on the books which is basically i mean it's pretty much the same stuff right this is what i was talking about this stuff does get really really repetitive but it has to be done you know so slash books get books dot methods get um i can basically just copy and paste this to be honest with you uh book change that to capital get rid of that esther okay and then we need to go ahead and we also need an id for this one sorry all right so we need an id there and then uh creates we need to be able to create books so so there we go create book and then you want to be able to delete a book as well so book and book there we go so that's all the routes we need um let's go ahead and create a lot of stuff so this will be p pull controllers and down here we're going to need to create the uh book books book book book controllers okay all right so let's say we want to be able to get a single we want to be able to get the books all the books all the books every single book that's in the database so to do that we need uh books books an array of book then what do we need to do what do we need to do what do we need to do let me have a look up here we need to find i'll just copy and paste this to be honest so we need to find them and and send them via json so books there we go then we need a function called get book get book get book singular all right and then in here we need to basically grab the id from the url okay then i'll just create a variable book book book book book is a book then we need to do db.the first book params id then we can go ahead and send that not a decoder encoder there we go in code and we want to encode the book okay so let's get a singular book then we need to be able to create a book and i'm going to copy and paste because i can't be bothered to keep sizing so create create book so ah book we want to change all of these to caps caps caps caps caps there we go so then we need to be able to delete a book as well last one guys come on you almost made it book grab all of those and change that to book capitalize that and that should be it okay yeah so like as you guys tell like this last bit controllers are not fun to write okay they're not fun at all they're probably the most annoying part actually of uh the whole goal server development stuff right but just really frustrating okay oh shoot we should probably check logger.fatal the server guys always do this i don't know why i didn't do this at the start so yeah let's go ahead and test it and make sure it's working because it could possibly not be working you know you never know successfully connected to database okay let's go ahead and test this out so i'm going to go ahead and i want to create some people so i'm going to create another person so create a person i'm going to send a post request it's going to be called you know what person person free right person at email.com send that request and there we go so we've got person number three then i want to basically create instead of a person i want to create a book so a book is going to have um of course it's going to have a title title whatever title you want to call this book i'm going to call it book 3 then we need an author which is going to be author 3 then we need a call number call number number which is gonna be uh a number so one two three four five six um then we need i think this is finally finally a person id which is four right yeah because the one we just created is four so there we go so that's pretty much here so let's send this and i hope it works i hope this works i really hope this works hey okay so now what we can do is we can we can we can go ahead and get all the books get all the books you can see you've got a bunch of books here and then if i want to get all the people send that request you see we've got all the people that currently exist which is only two people then i can get a singular person like so uh so i want to get the person with id number four you see we've got this gazer so we've got um we've got person three we got their books so they only have one book and that book is called book three and it has an author or three call number person id okay we can we can go to delete this book so we can we can uh we can go ahead and slash delete this book so delete book what id was it it was an id of three so i can go ahead and delete this book send that request we can see it came back with this json object so that means deleted i can send a request for person four again and you can see now they don't have a book it's just an empty array okay guys that was it for this tutorial thank you guys for watching really appreciate it god bless and i'll see you in the next one peace
Info
Channel: JackIsBack
Views: 11,579
Rating: undefined out of 5
Keywords: Go, golang, postgre, postgres, postgresql, gorm, go-rm
Id: -yofrWjrjt4
Channel Id: undefined
Length: 57min 25sec (3445 seconds)
Published: Mon Feb 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.