ASP.NET Core Web API .NET 8 2024 - 2. Models

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so in this video we're going to be talking about models and one to many relationships but before we even do that people always say we need to go deep Teddy deeper and we're going to go deep in this video and we're going to get very philosophical and I'm not even joking so why do we even need apis like what's the big deal about apis and why is it that every corporation in America needs them and will pay hundreds of thousands of dollars for you to maintain and build apis well really at the end of the day an API in this context there's all different types of apis out there but in this context an API is basically code that sits on top of a database and allows us to interact with databases in a very safe and self-contained way we don't just want anybody to be able to touch our data in all different types of ways we are very chased we are a very chased database we don't want people just grabbing all our data in different types of ways so we have an API but let's even go a little bit lower than that why is it that corporations want databases so bad corporations need databases because databases allow us to store data almost like in a filing cabinet but linked together and at lightning speed and if that confuses you a little bit just let me keep going first things first databases are like a filing cabinet because they allow us to methodologically store data in different categories and different ways very similar to a filing cabinet we will have these things called models that are pretty much each part of this filing cabinet and within the filing cabinet we have little pieces of paper we have little pieces of data that are stored in individual files within our filing cabinet so think of the filing cab it as the database and the models as little forms or little blueprint for forms the models allow us to create these little pieces of paper of which we can then store in our database another important thing is that these models these databases they're no different from Excel spreadsheets all a database is is a fancy Excel spreadsheet that we can link together with foreign keys and primary Keys which I'm going to talk about out here in a second but let's just go ahead and let's build out our models then we can start in on the primary keys and foreign Keys okay so the first thing that we're going to do is we're going to right click on our API folder and we are going to create a new folder where we are going to store our models now remember models are just one drawer in the filing cabinet we are going to have a stock model because obviously if we're going to design a stock app we need a place to store our stocks and then also we are going to create a comment model so the first thing that we want to do is we want to go in our stock and we want to type in p o and then we want to tab if you PR o you prop and then you tab what you're going to get is you're going to get this int and then you're going to get a property int what is actually an INT well we can't just store whatever we want to in our property if we want to put an ID we have to put an integer as an ID int is short for integer what is an ID well when we actually create these forms whenever we create these pieces of data we need to be able to uniquely identify them think if you want to this metaphorical dentist office and there were three teddies in there and three my last name is very common too Teddy Smith there could be could actually be other Teddy Smith in this metaphorical dentist office office that we are about to create so we need IDs to be able to make sure that we are individually identifying things the next thing that we are going to do we're going to do another prop we're going to have a string we are going to have a symbol why do we need the string well think about if you try to since we are using an ID we are using a number to identify an ID if we change this to a string that would not be what we want we want a number and the same thing we want to be able to identify our stock with a string AKA a word that is a symbol and if we put an integer there that really would not make sense same for the next one we're going to have a string and then we are going to have the company name also here you want to go ahead and put an empty string here just like this put string. empty or you're going to get you're going to get what are called null reference errors that are going to tell you that this is empty when it should not be so a string empty is basically putting this in there and it kind of makes sense if you store something into the database you probably want an empty string in there and you don't just want to put a null because a null is going to be bad next thing is we need to add a data a we're going to have to put money we're going to be dealing a lot with money when we're software developers so we need to be able to make sure that when we actually input a decimal when we actually input a number we are going to make sure that it is only a monetary amount and the way that we do that is we do this we're going to say type and this is going to force the SQL database to limit it to 18 it can only be 18 digits and it can only have two decimal places and that is going to be enough for being able to ensure that it is a monetary amount next thing that we're going to do is we are going to input we're going to input a decimal type if you just put an ID an integer is a whole number if you don't know the exact amount or you don't know the exact definition for what an integer is an integer is a whole number and in the case of money money can be not whole numbers if you're storing money you're going to have to deal with decimal places so that's why we we changed it to a decimal and we need this column actually spelled diale this supposed to be decimal sorry about that decimal just like this not dice mail it's funny I said that but it's supposed to be decimal and the decimal is going to prevent it from being over two uh decimal points when we store money it's pretty simple next thing is we can just go ahead and copy this since we are going to have a dividend this so this is going to be uh the dividend if you don't know much about stocks stock the stock if you invest in stocks it pays a dividend and let's see actually let's call it last div so that it correctly matches the uh F Financial modeling prep API if you don't know what that is go ahead and check out the react course next thing that we're going to do is we're going to have a prop here going to have a string going to have industry so I'm going to say industry and we're going to make sure once again that is string.empty otherwise we're going to get an error looking great lastly we're going to have the market cap the market cap is the whole entire value of the company so in theory if you were to buy Apple I think Apple you could if you had $1.2 trillion which I don't think anybody in the world has any that much money right now you could theoretically Buy Apple but we need to set this to a long because market caps can be in the trillion so and regular integer will not do and we need to prepare that for some reason somebody enters in a trillion dollar market cap and we need to have a long there okay so the next thing that we're going to do is we are going to go ahead and work on our one to many relationship now a comment is the perfect relationship for a one to many because it's very easy to understand if you are going to leave a comment on a profile if you are going to leave a comment on a blog post you are going to have to have a one to many relationship and you're going to have to tie them together so this stock is going to have the ability to leave bad comments and when we are actually talking about one to many relationships we have to have this thing called a primary key and a foreign key relationship and this is actually why one to many relationships and primary keys and databases in general are so powerful is because you can link stuff together that you can't do in just a regular old filing cabinet that I talk about and the way that we're going to link them together is a primary key and a foreign key and a primary key is going to be the p parent whenever you think of primary key think of the parent whenever you think of a on to many relationship whenever you see a foreign key whenever you see FK you think of the children a parent can have many children a blog post can have many comments but it can't be the other way around and that's how you identify a on to many relationship and that's how they are linked together and there's different ways of linking things and there's different ways of actually forming these patterns that we will talk about later but it's just good to understand that whenever we are linking database tables whenever we are linking our actual cabinets in our database within our filing cabinet whenever we are linking our each individual drawer right here or whenever we are linking each individual Excel spreadsheet it's done so through a foreign key and a primary key and just remember the primary key is almost like the parent it's the primary one and the foreign key is the children that it is the children it allows us to have multiple children that are tied to the parent but how do we actually model this well we already kind of halfway modeled it within our stock if we look at the list a list is a data structure that allows us to have many of something it's almost like an array in JavaScript but we also need to form the relationship within our Commons and it seems kind of unintuitive but if you think about it whenever we actually have the stop we are going to have many comments so if we're going to have many comments we need to have it listed but how are we going to link them within the comment the way that we link them is just like this and this is what is called convention there are many ways to form a oneto many relationship but in our case we're going to do so by convention and Convention basically means that enity framework net core if you don't know a lot about Entity Framework net core is going to go ahead search through our code and form this relationship for us before we actually had to do this all manually through something called fluent API which we will be using later but right now we can just get away with net core searching our code and forming this relationship for us and the way that we actually form the relationship is this we have stock and it's going to connect our stock through our comment now this is the actual key this is what's going to actually form the relationship within the database and Entity framework is going to set this up for us but you may be asking yourself well if we already have this key right here what do we need this for this is what's called a navigation property a navigation property is very important because this is what's going to allow us to be able to access this part this is what's going to allow us to be able to dot into this later and if that doesn't make sense just think about it like this the navigation property is going to allow us to navigate within our models it'll allow us to actually navigate within this relationship so we could go like this or we could go company name and we could and we could essentially dot into this relationship and be able to access the other side of the model once again kind of confusing but that's what it is in a nutshell and as the course progresses on you'll get what I mean and I'll be sure to call out where it actually exists but for right now I need to stop talking and I need to go ahead and finish out the rest of this so we're going to make this ID once again already explained to you what an ID is then we're going to go down here we're going to do the same thing p o going to have string then we're going to have a title for our comments and we will also have let me see here prop we'll have string and we'll have content just like this looking good then next thing is we also need to have our um string. empties right here so we don't get errors I'm going to go string.empty then we're going to go down here and we'll say prop go date time go created on and then we're going to set this to a date time and whenever this is actually nuded up whenever this actual piece of paper this piece of data is actually created what's going to happen is that whenever it's created and whenever it's actually put into the database this will actually run and the date time is going to be set at the exact point it is it was created which makes a lot of sense but anyway that is enough for today the video is getting kind of long so I'm going to go ahead and stop everything here if you guys enjoyed this make sure to smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 2,447
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: jMFaAc3sa04
Channel Id: undefined
Length: 13min 57sec (837 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.