Master spring Boot and Spring Security by building a shopping cart backend project starting with project generation you'll learn how to create and map entity classes Implement crud operations and develop services and controllers for products categories and carts as you progress you'll integrate Spring Security and JWT for secure user authentication ensuring your application is both functional and secure by the end You'll have a fully tested and secure backend application ready to support a dynamic shopping cart system Simpson Alfred created this course all right hello guys this Samson here from daily Cod work so today we're going
to be having a good time together right here on this channel so in this video we're going to build a complete shopping cart backend application together step by step line by line from the start to the finish and this application we're going To build the back end it's a back end application actually so we're going to build with spring Boot and completely secured with Spring Security and JWT so in case you're a fullstack developer or you're learning to become a fullstack developer um in my channel daily Cod work there are a lot of full stack
applications right there where we actually build complete Java fullstack applications that we build with spring P back end and rejs front end all right so Having said that let's Jump Right In All right so as usual we're going to go to start spring.io to generate a brand new spring project so here I'm just going to go to the browser I want to say start. spring.io right here and click on enter then we're going to be taken down to the spring initializer website we going to generate a brand new spring boot project so right here we
can see that the Java has been Java has been selected by default and by Default m is selected and here we have the default selection right here three spring build version 3 3.2 uh 3.3.2 which is the latest table version as at this recording time so we're just going to leave all this ones right here in the defa selection and let's go down right here so the group here I'm going to change to daily code work so we're going to say this one is daily code work um daily code work so the name of the
project we're just going To say dream we're going to say dream shops dream shops and that is the name as well and um so right here we're just going to say um a simple a simple e-commerce demo app right here and the package name right here I'm going to I'm going to just remove the high right here so we're just going to have the dream shops right here and then we're going to leave this selection on J and we're going to leave it on Java 17 Right here so we're going to come up here to
add some dependencies so the first we going to add right here is the web so we're going to have the web and then we're going to have data jpa I'm just going to say jpa spring data jpa then here we're going to have the lumb this is very helpful and so we have the jpa then we going to have the um for now we just stick we still going to stick to my SQL so we're going to take my SQL here so have the spring web We have the jpa we have the lbook and we
have the mysq and uh so we're going to have B validator so we have validation right here to validate our to do some validation later so now um these are just the ones we need for now so as time goes on if we need any dependencies we're going to come back right here to add it for sure so now I'm going to hit on generate right here so then I'm going to go right here Okay so this is um just choose a directory to actually um to store your project so for me I'm going to
choose a directory I've already prepared right here so I'm just going to save right here so so this project has been saved I'm just going to open the directory right here so then I'm going to right click and I'm going to extract all right here so just extract all right here and then I'm going to right click on this extracted package extracted folder and Going to come here to have more options then I'm going to click on open folder as it your project right here so I'm going to click on this and here we see
our project loading right here you can see see right here is still loading and now it seems our project has finished loading so let's open up our project right here so here is the main here is the main package right here and here is our Dream shops application Right here the study point so now we have we have successfully generated our brand new project all right so now that we have generated our our project so before we come back here I'm going to show you something on the browser so let's go to our browser so
right here I'm going to slideshow our our project presentation so right here um the first thing we're going to do is this we're going to TECO this product right here so Here we have the product entity class and here we have we see that we have category right here and we have another entity here called image so now um one a product belongs to a category right here so here we can have many product can belong to one category right here so here we have many to one which is many product to one category right
here and here we have one product can have many images right here so we have one to many so one product to a number of images Right here so here we have one to many one product to many images and many product to one category right here so that's just what we're going to model right here so let's go to to actually model this out so I'm right here back to intellig so the first we to do right here is to create a package so this package I'm going to call it model and then we're
going to create three classes right here Product image and we have category category category right here okay okay um so here the category has a name has an ID and the category also has a name and um what else the category we have is the product so we're going to have list of product right here uh sorry so we have list of products so let's take this list from java. right here so these are the things we just Need right here so let's go to the um let's go right here to the product I'm going
to close this so I'm going to zoom in a little bit so the product also has an ID and the product also has a name the product has a description and the product has a price but meanwhile I'm going to change this price to Big decimal and also the product has a quantity right Here and meanwhile I'm going to change this to event three it doesn't really matter you can leave give it any name you want to give it but I'm just trying to get on the event so that we can um it the same
to quantity actually because later um some of our some of our entities are going to have entity later that some of our entity are going to have quantity later so I just want to differentiate this one here that's coming from the product from the ones For example when the user is going to book is going to order later it's going to select product later is going to have quantity of product in the cat right so I just want to use this one as eventory so that we can actually use this one to track the number
of inventory we having that's the number of product remaining in the stock after the user has ordered right um so right here we're going to have this we will have name description price and Quantity and then we're going to have the brand so the product is going to have a brand actually so let's say private string then we going to have brand the brand of the product and uh meanwhile I just want I just want this one right here to be I just want to cut it to be at the bottom right here yeah so
here we have the name we have the brand we have the price we have the number of product we have in the in Our warehouse and we have the product description so now we're going to bring in the relationship so we're going to say um category right here okay um meanwhile the product also has an image right so let's bring in also the list of image so here we're going to have the list of images so we're going to have image and we going to have images so these are the two entities that these are
the two Entities that this product is going to have relationship with um so let's go here so the image also is going to have something the image going to have an ID as well so have long ID and also the image is going to have the name so we're going to say file name and the image going to have the type so this I'm going to say file type right here and then the image we're Going to have the image as well so let's say private um so going to be blob and just call this
on image right here and also we're going to pass in a URL whereby we can get the image downloaded right so we're going to say private string so we going to say um download we going to call it for download url url okay so this just all we need to do Right here then we're going to bring in the product relationship say say private product we going to say product so now we have finished all um all our entity creation so we're going to set up the relationship between this relationship with each other right so
for example the the image right here is going to have um uh one many to one with product right so many images belong to One product so we're going to say at many many to one and here we just going to map we're going to um set the name of the column with which it's going to be joined together so we're say adjoint adjoint column so I'm going to set the name to product ID product um sorry this ID product ID right here so this just all we need to do right here in the product
right here So we have many to one many images belong to one product and the name at which this column is going to be joined in the product CL is a product table is product ID right here okay so before we leave here we're just going to bring in the notations right here so we're going to have the geta seta so have geta and we going to have [Music] seta set and then we're going to bring in some of this um we're going to bring In Constructors so let's have all our Constructor and we're going
to also have at known Constructor then we're going to say this class is an entity in the database it's a table in the database so we're going to bring in the entity annotation right here and now we're going to set up the I the primary key key and then we're going to set up the strategy at which the primary key is going to be Generated so we're going to say strategy do identity so we're going to get the the identity strategy right here and this one here we're going to annotate the L OB just going
to annotate this by L so this is just all we need here for now so let's move down to this product let's go and finish up work with this product right here set up the relationship inside this product entity so right here we have the images right here so now we going to have at One at one one product to many to to list of images right so in here we're just going to have we're just going to map by m by product and then we're going to Cascade all going to say all right here
and then we're going to set the off and remover to true so what we just did here is that we casc all and set the off remover to True which means when a product is being deleted all the images that are Associated with that product are going to be deleted along so if there's any images in the database that is not being referenced by any product is going to be removed right here that we say this offer REM to True right here and this just all we need to do right here so here we can
see uh we are still having a kind of okay should not be a product um okay right here so um the next we're going to do here is going to we're going To bring in these annotations right here so we going to have the GAA seta this one is seta actually so we're going to have the seta and then we're going to have as usual we're going to have the ATN Constructor and also going to have at all our Constructor for now and then we're going to bring in the add entity and to tell jpa
that this class is going to represent a table in a database so now Let's break in the primary key so this is the primary key right here and let's set up the generation of the primary key how going to be generated so going to set the strategy to Identity right here so let's go back here we see that the error is gone so now we are done with this um image class image entity and also we are still working on this so the next we're going to do here is to work on the relationship between
the product and the Category all right so now that you have done with the with the setup between image and and the product so let's go to the category class and set up the relationship between these two entities so right here we have the list of product right here so what we're just going to do is to bring in The annotation of one to many so have one category belongs to many product right and then we're going to just come here we're going to Map so we're going to map by category category category right here
and this just all we need to do right here um so let's go back to the product so right here in the product what we're just going to do here is to bring in the many to one annotation right here because we have many products that belong to one category right so here we going to have at many to one this one right here and after that we're just going to set up the C the um we're going to set up the The the sorry the Cascade strategy we're going to say cascade. all um so
that when a product is deleted um be careful why use this all right here because there there could be some negative implication of us using this cascade. all because this cascade. all actually means that whatever happens to to product is going to be cascaded down to this relationship right here so you might end up deleting category from the database remember that category is a Stand alone entity right so category does not depend on product and product does not depend on category which means when a product is deleted there the category the table the category table
Still Remains right it's unlike the image when a product this image image right here depends on a product right so when the product is deleted the image is gone but category does not depend on a product so the category is a standard loan table and product is a standard Loan table so when that product is for example we are selling um Samsung laptops right and we have category for that for that one and if we decide not to be selling Samsung laptops again we delete Samsung product from our our database then the category that is
referring to that Samsung should be deleted if that category is not apply to other other products of different of of different brands right so the reason why we're using this c. right here is that Because now there's a relationship right here so when this product is deleted the relationship between this product and this category is going to beet deleted as well right so they not going to be there's not going to be relationship between so that relationship is going to be broken so that the product can be deleted um so um that's just all we
need to do right here but now we're going to set the ID the column that is going to track them together Right so here we're going to say adjin column then we're going to set the name of this column to category ID category ID so we going to say category ID right here oh sorry uh um this is double quote sorry guys I wasn't looking I was looking somewhere else all right so this is just what we need to do here okay so now that we have done this now the next thing we're going to
do here is to um let's check what is Happening here so we need to go right here that is why we're having this error here that is saying uh this one is not uh something like that right so let's go right here to make this class an entity in the database but meanwhile before then we going to bring the the lb annotations to actually make this one um um do his job completely so let's get um get a seta and let's have um Constructors um we might we might change some of this annotation as time
goes on But for now let's just leave it like this so now we have gotten this setup and let's go back to our product and um now this one here is actually having some issues all right so let's go back to this Cate ree right here and we're going to set up so let's set up the primary key right here and then we're going to set up the generation strategy as usual so here I'm going to say strategy do Identity strategy I'm going to say identity right here so this just all we need right here
let's go back here and um this guy is having a problem I don't know what is problem many to one attributes should not be category many many where is it many to one attribute type should not be category y so right here we have many to one which means many product belongs to many products belong to one category right here and we have the adjoint Column name and we have the ad many to manyu right here and then let's go here so here we have one one category to list of product right here and here
also we have the okay I see because we need to make this one an entity so this one is not long it's not yet an entity so we're going to tell hiate that this one is a table in a database and we see that this error will be gone okay cool that is the that is the ca so now we have here we have this one is an entity and this one Is an entity and this one also is an entity right here um so now I think we have finished setting up the the relationship
between our product and it um and his collaborator is um his Partners right I don't know the English use there but actually we have finished setting up this now we are going to run our project for the first time to actually see the structure of these three tables in our database before we continue to the next task so for that I'm just going to open up our property files right here and here I'm just going to pull in you see that our property file is empty so now we are still actually we are still using
property file but you can use EML if you want but for now we just sticking to property files so now we can see that this file is empty so um let's just pull in some configurations right here um so here actually link to our this is the data source which is our Database default Port of MyQ right here so our username is root admin is password and um so this are just the setup we need to actually get our our table our our database created so here we're going to set the name of our database
P so this one is dream um dream shops right say dream shops dream shops DB this is shops and we going to say DB so we say dream shops DB and uh now before we run this application make sure this schema is Existing in a database right so we're going to copy this one right here and going to move to my SQL to create the schema before we run the project so let's move on to my S workbench and create a new schema from here then I'm going to paste the one of copied and hit
on apply and then we will apply again and going to finish so let's refresh and let's make it the default Right double click to make it default so let's go to Intellig to run our project for the first time so I'm just going to come here to run this project and let's enable The annotation processing and let's wait for a while while our project runs I'm going to EXP and this right here okay so we see that our table has been created now the reason why we are getting all this one the reason we getting
this query right here is as a result of this one right here this as a result of this one Right here this two right here these two lines are the ones actually giving us this one right here so that's why we are getting this query generated right here so now our project has run successfully so let's go back to my SQ workbench and let's refresh one more time and now we see that three tables have been created we have category let's bring this up then we have the image and also have the product right here
okay so let's see the structure of our product of our table so Let's go to database reverse engineer and um let's move next and uh just move next so let's select our database this our database here select your database um remove this select this one right here and then let's go next and let's go next so here we have three tables so let's execute and let's go next and let's on finish so this is the table we've gotten right here in our database um so we see right here that Our table has been created so
here we have products you know we have um one product to many images right here right and we have um many products right here to one you can see you can see here we have many right here right so here we have many you can see this one right here this three this thing right here so we have many products to one category here we have one product to many images right here so now this the first stage of our application so we have gotten our First relationship um created right so let's move back to
intelligent so so let me remove this one and now I'm going to stop our server so the next we're going to do right now before we move to other task I'm just going to close all this let's close all this right here um so the next we're going to do right here we're going to in our next video we're going to start implementing the services and the controller for these three entities we Have created so that we can finish everything about this one and test it before moving on to other entities so see you in
the next video all right so in this video we're going to start creating the services for this model we've created so the first thing we're going to do right here I'm going to I'm going to close this one and we going to right click on the main package and then we're going to create a new package so this package we're going to Call it service and in here inside the service I'm going to right click again inside the service we're going to create another package the this I'm going to say product product yeah so we
going to say product right here and then here again we're going to create we're going to right click again and create another package and this time around we're going to call this one um image so we're going to say image and Then we're going to create another one um inside the service we're going to create another one and this one we're going to create we're going to call it category right so I'm going to say category yeah category okay um so now the first one we're going to implement is the is the service is the
product uh I'm just going to say product so I'm going to say product service and meanwhile I'm just going to Copy this one and then we're going to hit on enter so I'm going to come right here we're going to create an interface for this product service so then I'm just going to call it I product service and this is going to be an interface and going to hit on enter right here so now we're going to all the method we're going to need in the products we're going to create Everything right here so the
first we going to do right here is the product to uh sorry is the method to actually convert sorry is the method to add a product to to the database so we're going to say product add product that's correct and we're going to come back to this later then the other one is the one actually we're going to get all product this is correct and let's just go down for to do the job for us so here have product get Product by ID this one is correct um actually it's actually correct but meanwhile I'm going
to change this to long and I'm going to pull it up so the next one we're going to do right here is the want to actually delete a product by ID this one is also correct and this one is to update the product this one one is also correct thank you so much tell you make the job easy for us um so now what we're just going to do right here this product Right here this update is going to go also with the product ID so we're going to say product ID product ID right here
and um like I said we're going to come back to this one we're going to come back to this ones later but meanwhile the next one we going to create right now we we have got to the one to get all products um get product by ID delete product by ID update product so the other we going to Have right now is the one to meanwhile I'm going to copy this one right here that is of list down here because we're going to have a lot of them like that so now we're going to have
list of product so this one here we're going to get the the product by category actually not category ID we just we just want to get it by category get product by category and the next one we're going to do right here we're going to get Product also um so get product by brand right here this correct and this one here is going to be the brand name so we're going to say string and this one is going to be brand brand and this one also is going to be the category not actually category ID
but it's going to be category because it's going it's going to find by the name of the category not the ID okay and the next one we're going to have right here is the one to actually get product by category and brand I think this one is correct let's take this one right here so we're getting products by category and brand so for example we can get all product for Samsung the category uh maybe Gadget we can get all gadget for Samsung from our database or we can get all Gadget from Apple from our database
so here we're getting get all products by category and Brand right this one is correct so here we have the category here we have the brand so the next one we're going to have right here is the want to get get product by brand and category what is the difference yeah the different is just the reverse case just the change of position right this is not what we want now the next one we're going to do right now is the want to actually get products by name so we're going to say um List we're going
to list of products meanwhile I'm just going to take this one that is suggesting but I'm going to change this one so going to say get product by name by product name right and then we're going to remove this one right here and we going to change this one to name product name right here and the next we're going to have right here is to get product by brand and name so let's say list um so here we say get product by we We've got by category and name so so this we going to say
by brand and name brand yeah so for example we can get um we get all the product for that brand by the name the name here we're getting all the products by name so which means for every brand we're getting by the name right but here we're just going to get the product that name of that brand for example we're just going to say okay we need um we need Apple Apple phones so The name the name of the brand is Apple the name of the product is phone so for if we say Apple phones
then all the phones that Apple has are going to come out right here but if we say by name if just say phones then all the phones both LG Samsung apples everything going to come out but if we specify the brand then we're going to filter the name of that product by the brand right then the next we're going to have right here we're going to Count so we're going to count products we're going to count products by by the category not actually category by the brand and names right here so here we're going to
have brand so have brand then we going to have name name right here so all this we are doing right here is this is just a kind of um jpa this are jpa finder method we're using right here so all this all these combinations we are doing right Here is going to jpa is going to write all this query for us we don't need to write any query just few of them we going to write later um so now let's come here so this one one is just going to be the brand and the name
right so we're going to have the string this one is going to be brand right here and then we're going to have the name here so I think this are just all what we need for now 1 2 3 4 5 6 7 8 9 10 and 11 so if there are any ones we Need to add we surely come back here to add them but for now this is just all I could remember this just all we we're going to we going to need right now to actually get this implemented so um let's go
now to the product service and let's Implement so let's Implement i product service and let's pull in all the method we have declared in there so let's pull everything in right here oh yeah okay all right so in this video we're going to start implementing all These methods one after the other so the first thing we just going to do right here is to bring in the repository so here I'm going to say product repository though we have not created this one right here let's see if it tell can help us do that so let's
okay right here okay let it create it for us so here we're going to just choose the product right here and then we going to choose the package so right here we've Not gotten any package called repository so we're going to click on this one right here and we going to come here to create a new package and this we're going to call it repository and just click on okay and click on okay and then finally click on okay so here we just click on okay and this been created for us so now let's scose
this down so we've gotten right here um so the first one we going to do right here so here we have this we are Going to come back to this one later um so let's do this one so we're just going to say return repository. findind find by ID this find by ID and that we just going to pass in the ID all else we're going to actually throw a message right here so here we're just going to say um pass in a function right here and uh we're going to create a custom message Letter
we going to say product not found U new product not found we going to say new new product not found product not found not found exception and in here we're just going to pass in a message say product not found so say product on found right here and then we're going to create this one is going to be let me break this one down first break it down so this one is going to be a custom class we're going to create later so let's just come right Here to create um let's create this class but
meanwhile we don't have a package right here so let's go here to choose a package so what I'm just going to do right here we're going to create we going to click on this this main package and create a new package so this I'm going to call exception exceptions right here and um I'm going to click on okay and click on okay and then click on Okay so this one we're going to extend your run time exception right here and we're just going to say message this one we're going to say message right here that
we just going to come down here to call the super and pass in the message so just all we need to do right here so let's close this down and um okay this all is through yeah so now we're done with this so the next thing we're going to do Right here is the delete so here we just come come here to say product product repository. find find by ID so first go to find if the product actually exist before we delete so find by ID if exist then we're just going to call the delete
to actually delete it so here the first thing we do right here is to find the product by ID right so if the Product does exist then we're going to we just going to delete it right so but if it doesn't exist here we can just say okay or S we can just come here to say okay if it doesn't exist we can we can just throw a message by using the r s right here then in that case we're going to come down right here to pass in a function to actually um throw a
message for us so here I'm going to break this one right down um break it down right here And then I'm going to have this that I'm going to throw throw new product not F exception so let's break it down from somewhere um okay let let me break it down from here okay so in this case now if the product is not found it's going to throw exception but if if the product is found is going to get it deleted right um so the next we're going to do right here let's come to get we're
going to come back to all This ones later because they are complex the update the ad they are really complex so we're going to make this one we're going to come back to this so now let's find this one is very easy let's find all products by ID so let's just say product repository do find all this is very easy the easiest of all right so now let's get product by category so here we're just going to say uh product repository. Find find by category category category right and then we're going to pass in the
category and then we're going to create this one right here as a custom as a find as a custom finder jpa finder method in our repository right and right here we have this product right here and we have um category five by category uh but meanwhile this category is an object let's see what is happening right here so exp this one right here and so this One is an object and this one is supposed to be the name name of the category so what we just going to do right here we're going to come sorry
what I me object is that this one is a complete um it's not a string right it's not just a name but it's a complete object of which actually requires the object pass in here but we just trying to find this category by name so what we're just going to do right here is to come back right here so we're going to We're going to say find by category name right here and we just going to go back right here to say the name of the category right here and this will solve the problem so
we're going to actually find the CATE this one this qu is going to be generated based on the name of the category we try to find right and that solve the problem right here and the next we're going to do right here is the brand so let's just go here to return so just going to return uh Sorry going to say repository find find by so this one is find by brand by brand right find by brand and then we're just going to pass in the brand name right in there the name so this one
also is a custom jpa finder so this one is a straightforward one and let's go back right here so the next we're going to do here is to find product by category and brand so here we're going to say repository. Find do find by category Yeah by category name actually it's not category name category and and brand so we're going to remove this one right here so we actually find by category and brand so so here we're going to pass in the category and we going to pass in the brand right here so let's create
this one right here as a custom finder method but meanwhile there's another problem Right here so this one also just like we did last time it's going to be category name and brand right here category name by brand so now let's go back to this product right here and see what is happening so right here we see we have category right here we have this category so if you are find it by this category this category is a complete object it has an ID it has a name it has other things so if we go
right here we see that this category has An ID and the name so if we are passing in that object it mean we are finding by category both the ID and the name but what we just finding we just trying to find the category by the name right here right so that is the query we are combining right here to find find by category name and brand so right here the product actually has a brand right here so we are getting the name of this category right here plus the brand to generate the query so
here we just going To okay the category name is um is is electronics so the brand is Samsung so we can just find All Electronics that belong to Samsung right so we just put the brand Samsung and what category we finding um Electronics so we just okay find all Samsung Electronics from the warehouse right so that is just what we're doing right here um so now that is gone so let's go f find find by name find product by name so this we're just going to say Repository right here Find oh sorry this is dot
find by name uh this is name right here and then we just pass in the name so let's create this one also as um okay so it's straightforward so let's go back here again so the next one right here is to find by brand and name have we not done that okay this is category name this is brand this is Cate this is category this is find by category this is find by brand this is find by Category name and brand this is find by name so this one is find by brand and name right
okay um so let's go here to do same thing so let's say repository. find find by category name no this is find by brand find by brand and and name right here then we going to pass in the name uh this one Al this one actually is going to be brand so that we don't get confused so here we're going to pass in The brand and then we're going to pass in the name right here and let's create this one okay so this one also is a straightforward so we have the brand we have the
name right here so we have the name we have the brand so we just combining these two attributes together to generate the jpa finder query finder method to actually to get that query generated for us at wrong time right um so the last one right here is to Actually count the product count the product by brand and name so actually we can just count for example we have Samsung laptop we can how how many Samsung laptops do we have in our warehouse so put the brand name and put the name laptop or we are looking
for Samsung TVs so okay how many Samsung TVs do we have in a warehouse so we count by brand and name that is just what this method is doing for us so it's also a straightforward method So here we're just going to say repository do count do count you see right here so this one this this one right here tells us that we can actually write a a find out method so here we see count if we are just going to count this product we're just going to say repository. count it's going to count the
whole product but now we are writing a condition with which the the product should be counted so we should count by BR and by name so this one right here this dot dot right here means you can use this count right here to form any query you want to form provided it follow the rules of jpa finder method right so here we're just going to say count by um brand count by brand a name a name right here and then we going to pass in the brand and then we're going to pass in the name
right there so then we're going to generate this one in Our repository the repository right cool so you see there's no noise here it doesn't make any noise because all these ones actually follow the rules of creating jpa find out queries right okay so now we are done with all these so now let's go back to the implementation of this update and the add products to database all right so in this video we're going to start implementing the add method and the update method but the First we're going to do right here is the add
one but meanwhile before we start implementing this one right here I'm going to call you attention to this um dependency right here you can see that we are using the product repository as a dependency right here right but you can see that this product is not highlighted it's like we've not it's like we've not used it but despite the fact that we have used it right here is still showing that we have not used it Right so which means it has not been injected and the reason why it has not been injected is because we
are using this annotation right here so this one is a lbook annotation that is used for Constructor injection right and this annotation right here will only pick up variables right here properties that are finer non-static finer that is what it's going to do so now this this this um this dependency right here is not yet injected by this annotation so for us to Get it injected let's make it finer right here you see so now that we make it finer it has changed color which means it has it has seen that we have used it
right all through here right so always remember anytime you using this annotation to do constru Constructor injection always make sure that the dependencies that you that you're using it to inject is a finer you declare it as a finer right okay so having said That now we're going to start implementing this ad method um this ad product method right here so the first we're going to do here I'm going to open up our project directory and then we're going to come here to create a new package so this one I'm going to call request and
in this request I'm going to right click and create a class called add product add so I'm going to say add product Request and I'm going to click on okay um so why are we creating this one right here um so the the thing is that let me go here let's let me go to this model right here so it's not advisable most times it's not really ADV it's not a good practice to actually work with this model direct because this is the model that is this is the direct table in our database you can
see right here it's an entity right and it's holding different Properties different properties right here this one is holding so it's not really advisable to work with this model direct because most times we going to run into problems of relations and so on and so forth like that so the best way to work with this product right here is to create a copy of it and for example right now we are going to add a new product right so now what are the things we are going to add so where adding a new product what
are the things we are Going to add to the database so now we're just going to copy for example when this product is going to be added we are not going to add this image at that point right so we only going to copy this the product like the the attributes we are going to be adding at that point so we're going to we're going to copy this one this one's right here and meanwhile even this ID is not we're not going to be adding the ID because the ID is going to be generated by
Default right so here we're just going to say let's have all this but the reason why we are copying this because we might use it to do some query later right so here just get the ID along and now let's go here so what I'm just going to do right here is to paste it and now we're going to remove this um this relationship annotations right here and then what we're just going to do here is to get the geta seter so doing that we're just Going to have the at data annotation so you can
see here that we are using at data annotation to get geta and seta but if you go here we are using geta and seta to get geta and seta we don't use data annotation right here so it's not recommended to use the at data annotation right here so we can actually remove this two right here and say at data right at data annotation so right here we see that it's giving an error a kind of warning and if you put your C Right here you see replace it with this because it's not safe it's not
safe to use this one right here because this one right here is actually coming along with equal has code and so many so many other things like that is coming along so it's not good it's not good to use it on a class that is an entity in the database so rather we just control Z to make it we just going to specify the at gor and seter directly right here in the entity class right here but coming here this One is not an entity this one is not a direct entity in a database so
we can just use it right here to actually get our geta and set us right here um so having done this right now we're going to come back here and then instead of first using this one direct to update we're going to have the new class we just created this is add the add product request is what we're just going to have right now and we're just going to leave this one as request and now you can see That having put this one right here now the contract has been broken so we going to copy
this one right here to actually go here to settle the contract that we have signed with this interface right here and so this one is the ad and that we just going to remove this product right here and fix it right here so if we go back right here see that the error is gone and also we still going to instead of void we're going to actually return the product that we just added And once again the contract has been broken one more time so if we go back right here then let's change this one
from void to product to actually ratify the contract so now we've got it we've got it right and for now I'm just going to return I'm just going to return none for now let's return none to avoid this error now the next we're going to do right here we're going to create a helper Method that is going to help us in this process of add product so here I'm going to have the private and I'm going to say product so we're going to say create create product and then we're just going to pass in this
one right here and this time around we're going to also have the category right here we're going to have the category right here so we're going to say category and category right here so let's actually take this One from our model right here so what we're just going to do we're going to go in here then we're just going to return so we're going to return new product new product and inside here we're just going to say request request. get name get name so now we actually we we just just try to pick the attribute
we are going to save to database so now I'm just going to pull this down a couple of Times so now I've got to the name um I would have contr z contr z so let me just put comma here before I pull it down so let's put it down in couple of times so here let's get the brand and the next one here let's get the price let's get the inventory right here okay and then let's get the description get description right here so after that we're going to get the category let's get the
category right Here and let's terminate so now you can see that we have error right here is asking us to actually create this Constructor inside the product so let's go to the product right here and now I'm just going to give a space here and right click and generate a new construct so what we're just going to do right here we're going to take the ones that we're going to save because remember we are not going to enter this this ID Manually so we are just going to take the ones that we going to enter
manually so we just going to click on this one right here and then come here so we're going to take everything right here apart from the ID and the image right here then we going to click on okay right here so now the the the the Constructor has been generated so now that we have gotten our parameterized Constructor so we just want to remove this all AR not actually parameterized that is all AG Constructor right here so we have gotten all AR Constructor right here right so there's no point of using that annotation anymore so
just remove it so now let's go back right here we see that this error is gone so this just all we need to do right here so have we done this right now we're just going to come inside this body so the first thing we're going to do here we're going to find this we're going to find the Category let me just say so the first one we're going to check if the category sorry this is what am I writing if the category is found is found in database right so if yes if yes set
it set it as a new brand as a new product as a new product category category category right here but if no if no then save it so if No then save it save it as a new category Cate save it as a new category then set it as as the new product as a new product category category right here so this is the step we're going to follow right here so the first time we're going to check because this product that is coming actually contains a category right um so now We're going to check
if that category is already in existance in the database if yes we're just going to set it as the as the product category right but if that product if that category is not already in existant it means it's a new category you want to create then we're just going to cut the category repository and save it and then we're going to set it as the new as a new category right so now what the first we going to do right here is to come here to say Category category I'm just going to call this on
category and then we're going to say equals so we're going to call we're going to make this one optional because it might be there it might not be there right so optional. off take the non Lael then here we're going to call the category repository and here we have not created the category repository but now jpa is trying to create it for us is trying to Create it for us using the jpa rep repository and this is what we going to do in the future but now is helping us to do it so now what
we just need to do right here is to check all this one if we actually creating it in in the appropriate package so now the The Entity we have created this repository for is the is the category right here right and the name is category repository so the parent is jpa repository then the directory which is The package we going to create it right here so we're going to choose the package we're going to put it so now we are going to say we are going to put it inside this repository package and we going
to create click on okay right here and then we're going to click on okay so we see that this one it has been created for us so now if we go here um if we see here it's been created so if we go here see that the category has been created for us right here so let's Close it and continue what we're doing so let's come back right here um so now we're going to say category. findind so we're going to find here actually we're going to find by name find by name then we're going
to pass in request uh I'm going to say request. get category get category. get name right get the name of the category and I'm going to zoom this out a little bit then before we move on we're just going to Create this one right here so let's create it inside the category repository right here so let's go back so then we're going to come down right here that okay if this category does not exist then we're going to save it so we're going to say r s then we to say s get say get right
here and inside this one we're just going to call the the category repository to actually save the new category so here we just going to say Category then we're going to get the name then we're going to return category. save right here and then we're going to terminate and after that we're going to say um request this actually a request then we're going to set the category to this one right here and then we're going to save so we're just going to say return um product prodct repository. Save then we're going to call the method
we've created down there so that that the name of that method is create product right so here we're going to pass in the request and we're going to pass in the category right here so this just all we need to do right here to save the new product now we having let me let me remove this space right here we are having an error right here uh what is it required category provided Object uh really um okay let's check the category repository okay here this one supposed to be category right not object so that solve
the problem and so this just all we need to do right here to save the new to save a new product to the database right so now we have done with this uh we have done with this add new product so the next we're going to do right here we're going to move down to actually update to implement the Update but Meanwhile we're to go right here inside this request we're going to create a new class just at the same thing we did last time so this I'm going to say product so this I'm going
to call product update request and we going to hit on enter so the same way we did I'm just going to come right here to copy the same attribute and we're just going to go here and paste it in because these are The attribute we want to update when when a that is going to be updated and at the same way we're going to come here to bring the data annotation so we're going to have the data annotation right here so let's close this and also let's close this so back here back here in the
service class in the prodct service so we're going to have a helper method that is going to help us to do the update so we're going to have Private product so here we're going to say update update existing product we say existing so we going to say update existing product then here we're going to pass in the product this one we're going to say existing product sorry this existing existing product and then we're going to come here to say um product update product update request then this one we're going to say Request right here and
we going to remove this so what we're just going to do right here is to exchange just like the suggestion right here we're just going to exchange we're going to take the new the request the update and replace it and replace the existing product with the update right so for that I'm just going to put this one down a couple of times so we have to set the name and then we have to set the brand Get the brand so we're going to set the price and we'll get the price so we're going to set
inventory and we going to get inventory then after that we going to set the description and get the description right so after that now we're going to just come here to get the category so we're going to say category uh this category we going to Call this category so equals category repository. find by name so if it is found we're just going to set it right here so going to say category right. get name right here and then we're just going to come down right here to actually set it so we're going to say existing
product get the set category that we're just going to set it category then we're going to Return so we're going to return the existing product return existing product right here so have done this right now we're going to come up right here to actually make use of this so here we're just going to say return so we going to say return and meanwhile we're actually going to return this product so for that we're going to just like we did last time we're going to change this one to product product right here and also We're going
to change this one from product to the request right here and now the contract has been broken so we're going to go to the service to reify the contract so this is the update so we're going to pick this one right here and we going to change this one from void to product right here so this sets the contract so let's go back right here to the update so right here we're just going to return Product product repository this one right here so we're going to say product repository. find by ID find by ID sorry
this find by ID and then we're going to pass in the product ID right here now if this product is found we're just going to map it so we're going to call existing product existing so we going to say existing product and then we're going to call the method we create up there is update Existing product this one right there and then we're going to pass in the existing product and we're going to pass in the new product which is the request right there so after that we're going to okay we to come down here
to save so we're going to say map and inside this map we're just going to call the product repository to actually save so we going to say product repository this one right here and we just going to Method reference to Actually save but if the product is not found we're going to come down to throw an error so we're going to say all X throw product not found all right so this is just what we did right here so a quick rundown of what we did right here is that so is a very straightforward thing
we just created a haper method to actually extract we just extract the because we are not actually updating everything inside this product so we just we just Single out the the properties we are updating so actually replace those property we are updating with the existing with the existing product right so after that we just come here to find if that product exist in the database they will actually going to save the update then if the product is not found then we just going to throw this error that product is not found so this just all
we need to do right here to update our product and I think that actually Brings us to the um to the end of this product service implementation so let's move to our next task all right so now we have done with the product service implementation so the next one we're going to do I'm just going to close all I'm going to right click on everything right here and close all so the next task we're going to do in this video is to create is to implement the the categories so here I'm just going to go
Right here so we're going to start implementing the we're going to start creating the category Services right so right here I'm going to um right click on this category and I'm going to create new category sorry I'm going to create a category service so here I'm just going to say category service service and then I'm going to copy this one and then I'm just going to hit on enter um so here I'm going to create an Interface to actually align the contract of the me method contract so here we're going to just say I category
service and this one is going to be an interface here um I'm going to zip this just close it so right here we're going to bring in all the method we're going to use in in our service as usual so I'm going to say category so the first one right here is get category by ID that is correct um Yeah this one is coming from our model but this one here is going to be of type long so the next one we're going to do right here let's see if it tell will give us okay
that's correct so here we get category by name that is also correct and the next one right here is list of category get all categories this is also correct T is doing a very good work so the next one right here is um oh too Much of Praises so now the next we going to do right here is we want to add category so let's say category yeah category and so we're going to say add category then we just going to have the category we're going to add then the next one right here is to
actually update category that is correct and the next one is to delete category this one is also correct and the next one is to get category by name um have we gotten that okay yeah so have 1 2 3 4 5 6 so Here have get category by ID get category by name get all categories then here add a category and update a category and delete a category so but this one right here we're going to include okay so we're going to include anyone that that we we need later that is not here we're going
to come back right here to actually include it right so now let's go back to the category service and Implement That so let's implement the I category service and then we're just going to bring our console right here and implement all the method we've gotten in there all right so the first we going to do right here let's pull in the dependency let's say private remember to make this finer categor um category repository and then we're going to make this one is spr beIN by giving it at service Annotation and here we're going to say
at required let's we going to do Constructor injection right here as we discuss to the past so right here all right so these are just the method we need to implement in our category service so let's start implementing this method one after the other so the first one we're going to implement right here is this one to get category by ID so here just going to Return category repository find by ID right here but Meanwhile we're actually going to return a message we going to say or S throw because we're actually going to return a
message and let's break this down so here we're going to have we're going to throw a new I think we've created something so we just create U throw new uh do we have okay we created product not found right so this one Right here we're just going to say um no category F so we're going to say new um category we're just going to say resource not found so we can use it for we can use it generic um generically with other with other components right with other um entities so we're just going to say
resource not fund resource not fund exception yeah exception right here and then we're just Going to pass in um category not found category not found okay so now we're just going to say resource not found exception so we're going to create this class so let's go right here to actually create this class in our exception package I think we have exception package already so let's check so yeah we have exceptions package right here so we're just going to add it here and then we're going to create so here we're going to Say this one is
the message message and then we're just going to come out right here but Meanwhile we're going to extend this one um run time run time exception and I'm just going to come here to call the super and just add the message right here so this is just all we need for now let's close it so now we are done let me zoom out a little bit so now we are done with this one right here to actually find category by ID so the Next we're going to do right here is to find category by name
so we just going to say category um Repository find by name the one we created last time is out of the way so this one is to get all category so we just going to return category repository. find all this one is out of the way and this one right here is to actually add a category we are going to come to this one later let's finish with the simple ones so this one is delete category by ID so Here we're just going to say um category repository actually find by ID find by ID so
if it is found to pass the ID right here so if it is found just going to delete if present delete it and if it's not present what happens throw message so here we just going to say s throw message then I'm going to break this down so let's go right here to actually throw a message And so we're going to throw new resource not fun so say category not fine this out of the way now the next one we're going to do right here is the one that you actually add category and update category
all right so the next we're going to implement right here is the update category but I noticed a mistake right here from our autogenerate um so let's have this one we're supposed to get the ID of the of the of the category we want to edit so let's have the ID Right here here and now let's go back right here to actually correct this because last time we have Auto generate auto complete but um we didn't check so here we have the update so here I'm just going to have the ID so here just going
to have ID uh sorry this is actually ID yeah okay so um let's go back here to actually get it implemented right here where is it okay this one right here So what we just need to do right here is to find the first we need to do right here is to find this one by ID so let's say return we just going to return optional so we're going to return optional do off of noble so here we're just going to call the um we're going to call the get ID which is this one right
here we we we've created one here get category by ID what is it this one right here okay this one right here so we're just going To get this category by ID right here so we're going to say get by ID um this one right here get category by ID and then we're going to pass in the ID right then so going to come here to map so in here we going to have the old we just going to call it some old old category o category and what we're just going to do Right here
we're going to come down to actually we're just going to get the old category this one right here um here we just going to get the old category to set the name to this new one right here and then we're going to we're going to save so we're just going to return repository do save then after that now the next we're going to do here we're going to if there's any error we're just going to throw so say all throw and then we're going to Go in here to actually um we're going to throw the
new the new error we created last time which is the resource not found exception this one right here so if what we just did right here is that so the first thing we did is to actually get this category by ID right here so if this category actually does exist then we're just going to get the old category so we just to this one is the old category that we've gotten by ID so then we're going to set the name to the new Name we've provided right here and we just going to save then if
the if the category is not found then we're going to throw this error right here so this one is out of the way so the next one which is the last one we're going to create right here is the one to actually add new category so we're going to say optiona optional. off category and um we're going to fil time so so right here we're going to Have sorry not category repository dot exist Exist by name we just going to check this one by name Exist by name then here we're going to call um C.G
name now if this one is not already in existance we're just going to come down right here to actually create it we going to say map then we're going to call the category repository to actually save we going to say um so we're just Going to Method reference to save this then if that is not the case we're just going to throw an error right here r s through so here we're going to create a new exception class called Already exists already exist exception or exist exception then we're going to pass in the message so
we're call the category of there c. get name then we going to say plus already Exist already exist Okay so so let let's break this down let's break this down from here and um so what we're just going to do I'm going to create this one is this one is is going to be a custom method in the repository right here and the next one right here we're going to create it as an exception class so we're going to come down right here to create this class already exist and we're going to choose the exception
package right here I'm going to click on okay and here we going to click on okay so just like what we did last time just going to come here to say this one is a message and then we're going to extend the runtime exception right here and then we're just going to come in here to call the super and pass the message inside so that's just all we need to do right here um so now we have done with the implementation of our category service so let's move to our Next task all right so in
our previous video we have done with the implementation of the category service so in this video we're going to implement the product image service so I'm just going to right click on this and close all tabs then in our project we just going to come right here here in the service right here in the service package so here is the image package right so we're just going to right click on this and create a new class and this One we're going to call it image service so this one is image service and what I'm just
going to do here is to copy this and hit enter so then I'm going to create um service interface for this so I'm just going to call this I image service and this one is an interface and hit on enter so close our project so inside the interface we're Going to bring in some method that we're going to use so the first right here is the method to get image by ID so we're going to say product image yeah this is product um what is the name of this this image right so we're going to
say image um get image so this one is actually we're going to say this one is get image by ID so it will be clearer and I think the ID of the image is long yeah so let's the next one we're Going to do here is the one to um get image delete image by ID so we're going to say um void delete image by ID and the next one right here is want to save image so this one here here we're going to we're going to have the multiart file so we're going to call
file and then this image here we're going to have the product ID because when we saving image we are going to save an image based on Based on an existing product right so when the product has been created then we save image for the product right so for that we're going to have the product ID product ID and the last one we're going to create for now is the one to update the image so this one just going to say void sorry this is void update update image so this one also is going to take
the the ID which is the image ID and um so this update image This one is going to take image ID not product ID all right so these are the four methods we're going to need for now so if we have anyone other later we're going to come back here to actually include it as we go so how is now going to go here to implement this service interface so let's implement this and just put your C here and U Implement all the method we got over there and this one we're going to say is
A service so as to make it a bin spring bin and because we're going to be having some dependencies right here we're just going to pull in the required AR Constructor this one um so the the dependencies we're going to need here is the product service and the image repository so here I'm going to say private I don't think if we have created image Repository I'm to say image repository okay it's not be created but I'm going to see if itj can create this for us um so let's go here yeah that's correct okay so
now we're going to come here to choose the image and um we're going to choose the package that we're going to create this in so this one is going to be inside the repository package and we're going to Click okay and then we're going to click okay again so this is created for us so let's close this and the next we're going to bring in here is the product um sorry so there is the product um product service is that correct it's private right so I'm going to say I product product service this one right
here and then I'm just going to make this to finer because we're going to be Doing Constructor injection as usual um yeah okay so let me give a little space so we can see it clearly so this is the want to actually get so now we're going to start implementing our method one after the other so this we want to get the image by ID so we're just going to come here to say return so we're going to say return image repository find by id. throw Yeah just want to throw something so here we're just
going to say. through this one right here and this one we're just going to go here to throw resource not found I think we created this last time yeah we created it so this just say resource not found and we say image no image found with this ID and the next one we're going to create right here is the one to actually get the image deleted from our database So we're just going to come in here to say image repository find by ID we're going to make sure that this image actually exist so we're going
to find by ID if present we're just going to delete but if not present we're going to throw an error so for that we're just going to use the if present. right here so here we're going to come here to add a function to actually um through this error when the image is not found Right so we just going to say or throw then we're going to come here to say through image. file exception this one right here and is out of the way so the next one we're going to create is the want to
actually um is want to update the image right so this one right here let's go to the update before we come back to the save image so the next we're going to do right here is this save image so here the first thing we're Going to do is to actually find the image so let say image so just going to get this image by ID so notice here that we using this um method that we've declared here this one right here is what we're using so if the image is not found it's going to throw
this exception right here this message right here right um so now let's just come right here if the image is found we're just going to set the image properties so we're going to say image. set file type um file. get Original name so here we're going to say set so we're going to set the going to set the file name and we're going to say file get file name but meanwhile I'm just going to wrap this one here in try and cach so I'm going to press using Windows I'm going to say control and off
then press T to actually bring this one in and just select the try and catch and wrap it with try and catch so in here I'm going to continue so the next I'm Going to bring right here is to get the image the image itself so we're just going to say image do set do set image this one right here then in here we're going to convert this one to Ser to BL to blob because this one the image type is blob right so we're going to say new Sera new seral blob and then in
here here we're going to pass in the f.g by f.g by this one right Here and have done that we're just going to save so to say image repository do save image then we're going to catch I exception right here um our exception yeah this one right here and also then we're going to okay let's see what's happening right here so we're going to add this exception to ex exception then we're just going to throw runtime exception So um actually let's I think it's been there okay we just thr time exception and actually get just
get the message e. get message right here all right so this just all we need to do right here to get our image updated so now we're going to go back to the save image now this save image is going to take another shape uh because we're not going to if we go right here so we see here we actually have um we have the product right here and we also Have the image right here but all these ones right here we are not returning this when the image is saved so we are going to
create a dto and specify what to return back to the client after the image has been saved so for that now we're going to go right here we're going to go to our dto package have we created any dto package so for that I'm going to create a new package right here and then we're going to create we're going to call this Package dto dto right here and now this dto this here we're just going to right click and create a new class so we're going to call this one image so we going to call
this on image dto and um it's a class and now what we just need right here we just need the file sorry so we just need the ID of the file the file name and the download URL right so we're just going to take this one right here but Meanwhile we're going To change this one to long and the next one right here we're going to get the name of the file and then we're going to get the download URL so we're going to say private string then we're going to say download yeah this is
download right so so we going to say download URL yeah this one right here so this just all we need then here we're going to have our lbook Annotation right here let me close this all right so now we're going to we're going to just I'm going to close this right here and let's go back here to the service so this one right here remember that this this um a product actually has multiple images right so we're going to save multiple files for one product so for that this one is not going to be a
single file but meanwhile it's going to be a list of file right here so we're just going to say it's a List of file and sorry and then we just to call this one files so let's go down to the service to actually settle this contract right here so I'm going to say it's a list of file yeah okay this files already okay so here let's go back right here so the first thing we're going to do right here is to get the image is to get the product that we're going to save the the
the image for right so we're going to say product Um just going to make this one clear say product then we're just going to get this product by ID and after that now we're just going to create a new container to say list of so this one going to be the image dto image we going to set it to a new container and right here we just going to have this for Loop to actually iterate to Loop through all the list of files we bring out here then inside this for loop we're going to bring
the try And catch um here just going to come down here to bring the catch let's close it right here okay so now inside here we're just going to have the image we're going to say image image equals New Image so we're going to set so for every image so for every image in this list right here for every image in this file so we're going to set your properties right so then we're just Going to set image. set image do set um we don't set the file name right set file name and we're going
to set the file type then we're going to set the image itself image set image then we're going to have the new serial blob and here we going to say file. get B and after that now we're just going to set the image we're going to set the Product so image do set product to this product right and after that now we're just going to move down right here and we're going to return we're going to actually we're going to um we're going to build the URL that we're going to return for the image download
right so after setting all this right here setting all the properties for this file then we're going to build the URL that we're going to return because we're not going to return the image right so We're going to return a URL through where this um image can be downloaded right so now I'm just going to move down right here so I'm going to say string download um is download URL then we're going to remember last time we have if we go okay we've not created a controller but now we're going to BU we're going to
um we're going to BU the URL with which we're going to create the ENT points later right so here we just going to say slash API slash um V1 this is going to be a pattern of our controller then we're going to say images images right here and then we're going to have image then we're going to have the download right so we have download so what we're doing right here is actually we're actually building a Path that with which we're going to to create we're going to create a URL where the image is going
to be downloaded right then here we're just going to come here to add the image ID that we want to download so we just going to say image. get ID right here uh but meanwhile this one is not necessary just going to remove this and end it here so the next thing we're going to do right here we're going to set the image we're going to say Image. set download URL we're going to set it to this download right here then after that now we're just going to call the repository to actually save this image
to database right so we just going to say image repository do save image so here after saving this image you know remember here actually we set the UR the the download URL right here based on the image ID right that we set the ID right here and save the URL to the database now after the saving after The this image has been saved we also want to get the ID to actually update the URL for download so that we can get the correct ID of the image after being saved so for that now I'm just
going to pass in the saved image right here say saved so this I'm going to call saved image then after when the image is being saved that we're going to get the URL that that is the corrent ID again to actually update the URL right so here I'm just going to say saved Image saved image just SE download URL uh meanwhile I'm going to do a kind of duplicate right here I'm going to copy this one so I'm going to copy this one right here and after that now what we're just going to do here
is to bring in is to get the saved image ID right that's to update it with the saved image so here I'm just going to say plus saved image. get ID right here so this one enables us To get the correct ID of the saved image so that when we want to download it we don't have an error right so after that now we're going to actually call this save again we to say image repository do save the image again but meanwhile since we are having this two right here we having these two um duplicate
right here but it just Within These two places so no need of we can actually make it one string to just okay um just like this to Avo long story I can just say Okay um String build build download sorry this is download URL for example so I can just pass this one right here so I can just say Okay I want to just copy this one right here and put it here right okay so now we're just going to come here remove this one here okay and then we can just okay bu download URL
right plus the ID right so this one will still work the same so Now we're just going to also come here and remove this one here to say bu download URL and we get the the ID just to get it updated right so after all this is been done right now we're going to call the image dto to actually get the properties we want to return back to the the front end right so here we just going to say image image D set image ID then image dto do set file name so here we want
to Say saved saved um saved image. get file name and then we want to get the the download URL right here so we just want to say image dto do set download URL so we're going to set the saved image. get the URL then after that now we're just going to set say save image so after this now we're just going to call the D we have up there so we're Going to say image details this one right here do add this D but meanwhile I'm just going to say um I'm going to change this
one right here okay I want to say this one is a let's differentiate the name saved image dto right so I'm just going to come down here to say this one is saved image dto then we're going to add it okay so after that now we're just going to I'm going to clean off this space right here so in here we're going To say um I going to cat this exception right here and um also so we're just going to catch we're going to catch the SQL exception as well SQL exception this one right here
and we want to say e so just going to come down here to throw runtime exception we say Throw um this actually going to be run time exception new runtime exception this one right here and in here we just Going to call the message it don't get message right here all right so after all this now we're going to return saved image return saved image d right here so now you see that we're having an error because we actually need to this one right here what is it so this one right here is the image
dto right this one right here is the image so um this image this um this um sorry so this one right here need to return the image GTO instead of inad of instead of returning The image right so here we just going to change this one right here to list of because it's actually going to return a list of Saved image right here it's not a single image right so this one here we're just going to say return a list of image dto this one right here right um so for that now we're going to
now this problem is solved but for this right now we're going to go back to the interface and actually return a list of saved images so this one I'm going to Pluralize it to make it clear and here we're going to go back to the um let's go back to the interface right here to actually get this solved so this one is the saved image so we're going to say this one is a list of it's going to be list of um image D sorry image details this one right here and we're going to change
this name also to save the save images so to get everything right right here so this is Just all we need we see that this method is actually big right we can actually break we can find a way to break this to break down this method into into several um chunks right so it will be easier to read okay but for now we're just going to leave it like this um it's clear enough to to read so we're just going to leave it like this but so far so good we have gotten our service set
up ready to accept list of image to save for a product all right so now that we have Done implementing our image service class so in this video and some of the subsequent one we're going to start creating the controllers to actually consume this three um services that we have we have implemented which is the image service um the product service and the category service so what I'm just going to do right here I'm going to right click and close all tabs then we're going to go right here and then we're going to create a
new package Called controller this one is controller and inside this controller we inside the package we're going to create three controllers so we're going to have three um product controller right here then the second one we're going to have the image controller image controller and the last one here is going to be the category controller Category category controller right here so these are the three controllers we're going to have then also I'm going to create another package right here and this package I'm going to call it response we're going to call this package response and
in here we're going to have we're going to create a class called API Response so this is the class we're going to use to response res response so this is a class we're going to use to return data to our front end right so um inside this class here we're just going to have two properties going to have private so this one is going to be the message and we're going to have the object which is the data and right here we just going to Have The lbook annotation right here and also we're just going
to come here to have the at all AR Constructor because we're going to use it right here okay so now we're going to close this so this class is what we're going to use to return data to our front end so let's close this and also what I just going to do right here I'm going to open up our properties file and I'm going to put our API preface right here which is our API Um request mapping preface I'm going to put it right here so I'm just going to say API do prefix and this
one is going to be API slv1 so I'm just going to station this here so that we we'll be invoking this one from here in every API you want to work for want to work in right um so now we're just going to Let's let's close this so the next thing we're going to do right here the first one we're going to implement is the product sorry is the Image the image controller so actually it's a controller we're just going to make it a controller rest controller and we will have request mapping and in here
we're just going to say this one is so now we're going to bring in the value we declare in our properties file then slash so this we going to say images right that's do then that value let's bring it in say API so this one right here API preface so this what we're going to use work through This what we're going to work with throughout all our controllers so now we're going to need a dependency right here we don't have private final sorry this final I image service actually code to interface then now we're going
to Constructor inject right here required Constructor injection uh sorry require all AR Constructor so this one is the m of injecting doing Constructor Injection right okay so now the first one the first end point we're going to create is the one to actually upload the list of images public response entity so make sure you always use response entity because this what we're going to use to return data to the front end because this is the one of the flexible way to actually return data from our restful API down to the front end right and so
we're going to have response entity then We're going to have our sorry not this one right here we're going to have our API response this one right here and then we're just going to say save images save images and remember we're actually saving list of image list of images we're going to say list and we going to have the multipath multiat files going to call it files and also we're going to have the products that we are saving this image for so we going to have Long uh sorry this product product ID and this one
here is going to be at request param so the both of them are going to be at request param request request param and also this one let me just zoom out a little bit and this one also so it's going to be at request param at request param right here so now have done this now we're just going to Go inside the body then we're going to have the list so we're going to have the list of um product um so we going to have so not actually product actually this image what am I doing
so we're going to have image D which is the D created last time and here we're just going to say um images image itos so this one we're just going to say okay image then we're just going to pass in The list of file to the back end to actually submit and we're going to we're going to return back the the list of downloads URL right um so after that now we're just going to return success response entity. okay then we'll have our new API response and we just going to say um upload success upload
upload successful I we just going to say upload success Yes and meanwhile I'm going to I'm going just to highlight I'm going to wrap this one in trach so I'm going to highlight so on Windows press control and off then press T to actually bring up this um this thing right here and just scroll to number six and get TR catch now what we're just going to catch right here is the general exception so should anything go wrong we're just going to throw we're just going to return a message to the front end so We're
just going to say return response entity and this one here we're just going to say um internal server error because that is one um something if anything happen that's just what you want to throw back there so we're going to say internal server error okay so um let's have HTTP HTTP status actually do internal server error right here and let's have the body and just pass in our so I want this One to be constant import import constant uh right here okay right here so now we just return back and the message so we just
going to say upload failed and we going to return back the message while it fails so that's just all we need to do to save our images so the next thing we're going to do right here is to just we just want to men sure that this one is a post a post mapping and then we're just going to say slash sorry so to say SL Upload so in that case now we're just going to have API slv1 SL images SL upload that's what we have right here so now this the next one we're going
to create is the one to actually get the images is they want to download the image so remember last time we created when we created the services where the service this image service right here so remember last time we actually um we we constructed an an endpoint download link um URL right here so this Is the URL we want to go and create right now this the end point we want to create right now so let's go here uh what is it this one and let me close this one for a while close this one
so I don't get confused so let's go here to the image controller so I'm going to say public um response entity so here why do you want to delete Image so here we just want to say download download you sorry download image we say download image right here and now the next St we're going to do is to go in here so this one here we're going to we're going to have the the image ID that we want to download so I'm going to remove this I'm just going to change this one here to add
request param sorry path variable so this is path variable actually and then I'm going to Remove this I'm going to remove this right here all right um so now the next thing we're going to do we're just going to come in here to have image I'm sorry image equals service so we have image service right here not this one this not what I want uh want to say image service U meanwhile let me do this import first let's do this Import yeah so I'm going to say image equals service this image service so we just
want to get this image by ID right so we just want to say image service. get image by ID um what are we do right here get image by ID that we pass in the image ID then after that we're going to create a resource so I'm going to say bite array resource resource then the next we're Going to do right here is to say new new by array Source then inside here we're going to have image image. get image image. get image. get bite then inside here we just going to pass in this and
so we're going to have image image. getet image Mage this one right Here yeah so right here then inside this here we're just going to have actually we don't need this one we don't need to set the bite so we're just going to say length right here so so we're going to let me terate this so we're going to throw this exception and then we're going to cast this to in right here um so what we just this one actually is an assignment let's Assign this and so what we just did right here is to
we we get we create a b array of bite from this blob right here this one is a blob and then from the B AR we created from The Blob we now create image B AR resource which is suitable for us to return F which is one of the suitable suitable method to actually return files in restful apis right here so this the resource will be the resource will be returning to the front End right so have done this right now we're just going to come down here to return so going to say return response
do okay we going to set the content so going to set um the content type say media sorry media type so we going to say medat type. pass medat type then inside here we're just going to bring in the image so we're going to bring in our image getet file type which is the F Type of our image. getet file type this one right here and then we're going to set the header we to say HTTP HTTP headers do content content disposition then we going to have attach attachment um attachment attachment right here and then
we're going to have the F name and then we're going to come sorry This assignment actually is equal and then we're going to come at here to have um so we actually going to have our image ID so we to say not image ID actually image name so we say image do sorry this one I think we need one more here and we're going to have the is so we're going to have image. getet file name get file name and then we're just going to say Plus so here we're just going to we actually need
to escape this right here let me remove one of these okay so have done that now just going to set the body to actually return the the resource so going to say return the resource right here so this just all we need to do and let me I want to break this down from somewhere um okay let me break the down from the body but meanwhile right here we having a kind of error right here Required respones provided this okay so this one here we're just going to change this one to resource resource sorry do
we have it right here let me see yeah this one right here resource so this just all we need to return right here so what we just did right here is we just create a bite array from The Blob and we create we created a bite array of resource then we return the resource as a file which is just set the Attachment everything so this is the one of the flexible way to actually return images in rest API to any front end we are using right here okay so now we just want to say this
one is say get to get mapping this get mapping and now we're going to pass in the we're going to pass in um this one we created last time at the back end but me we going to remove this part right here because this part is what we have here already this is the Part we have already so we just want to say image download let's go here so here we have image download right but meanwhile I'm going to change this one here I'm going to okay it's okay it's okay like that so just going
to go right here then we're going to pass in the ID of the image right so here we just going to say ID which is the image ID we have up there uh sorry the image ID so remember right now last time we're actually passing we appending The ID of the image right to this download link so that is the image ID we are actually appending right here so it's just all we need to do to get our file downloaded all right so the next one we're going to create right here is the want to
update the image AP response and we just going to say API response right here and what we're just going to do right here we going to say update update Image so here we're going to have at request at PA variable actually we're going to say this a PA variable image image ID and then we're going to request body at request body then we're going to say this say multi paath file so to say image so first for the first we going to do here is to actually get the image by ID we're going to check
so if the image Does exist so if image so if image not equal none we going to call the image service image service do we have the service yeah image service dot update image then we're going to pass in the file I'm going to say image what am I doing right here okay so we just going to pass in this one right here then we're going to return so going to return success message this Right here and uh meanwhile what we just going to do right here we don't want to return When the image is
updated we just want to return none right here but um there are some error we need to catch because if you go right here if we go to the update we are actually catching two exception right here this one is thrown is thrown from this the recep that is thrown from here image not found and the second one is The these two right right here so this anything that happened inside the Ser going to return it to the front end so now we're going to return this so to do that now I'm going to wrap
this the whole of this one in here inside Tri cach and then we're going to the first one we're going to handle right here is resource not found resource not found yeah this one right here then we're going to return Response entity do not found actually we're going to return a message so status code not found say body and then we're just going to get the message that is returned so we to say e get message and return n then if any other happen we just going to come here to return um this one this
time around is not actually not found but we're going to say internal server Error this one right here and then we're just going to come right here to actually bring get the message of what actually happened to the front end so this just all we need to do and this one we're going to say that this one is um is a put mapping which is update so we're going to say image slash the image ID then slash update it so here we're just going to pass in the image ID right here so this just all
We need to get our our image updated so the last one we're going to create right here is the one to actually delete the image so this one is that one is going to be exactly the same thing with this with different method with different HTTP U method so let's just put this one right here and then we're going to say delete delete image and here we're just going to change to delete right here and U so This one here the only thing we're just going to remove this one we're going to remove this one
here because we're not going to request the body we only request the ID we want to delete so let's remove this and let's call delete by ID this one right here okay so you see every other thing remain the same but Meanwhile we're just going to say this one is delet success so this just all we need to do um so this one is bit changeed to delete And this one here we going to change this method to delete so we going to say delete mapping all right so this just all we need to do
to actually Implement our delete right here let's make sure we we refactor all the messages to so we can get the appropriate message so now this just all we need to do um to actually get our service our image um controller implemented so now we are done with this image controller implementation so let's Move to the next controller all right so now we have done with the implementation of the image controller so then we're just going to move to the category controller but meanwhile I'm going to grab these three annotations because also we're going to
use them there so now I'm just going to close this and open up our project so here is the category controller I'm just going to go up right here and dump this three annotations Then we are going to change this one here to categories categories that's all so the next thing we're going to need right here is the um a dependency we're going to use so we're going to say private final so I'm going to take the interface so the first one we're going the first end point we're going to implement is the one to
get all categories from database so to say public response Entity and then we're just going to say get all get all categories and right here we're going to have the list of categories right here so let's do the input so now we're just going to return response entity. okay do okay body so we just going to say found and we going to come here to pass In the categories so um we're just going to catch internal server error should anything happen so we're just going to um wrap this in try and catch so that we
can actually catch the error that may happen right if anything happen maybe Network problem so here we're just going to return response entity. status code and in here we have HTTP status right do internal server error This one right here but meanwhile I'm going to just return the body from here and meanwhile also I'm going to do this one static import so let's import static so um here I'm just going to say error and then we're going to come let me Zoom this out a little bit so we're going to come here to say internal
server error this one right here okay so this is just all we need to do so now we just want to Make sure that this one is a is is a get is a get um mapping so let's say get mapping and then we're just going to say get all categories right here I'm going to say get all we just going to say categories slash categories slash all or maybe um we just say all right here yeah let's just leave at order that is okay don't need to write too many State too many grammars there
right so the next we're going to do right here is the one to actually add new category we say public response entity AP response say add add category so going to have at request request body right here so this one we're just going to use this category direct because there are no sensitive data to to to cover from this Category class just name and ID right so we can just return this one direct okay so now we're just going to say right here we're just going to call the category equal category service dot add category
just going to pass in the name right there category name right here and then we're just going to return response entity. okay hope everything is okay then we're Just going to say new so in here we going to pass our message so going to pass success is there any problem right there so we just going to come here and say um the category right okay so um last time we I think we throw an we throw a kind of exception right here let's check the category service so this is the category service okay so this
is the add category so here We we actually throwing a kind of um conflict so maybe if that category is already in existance so we're just going to say this category is already in existant so this message right here this exception right now we're going to cat it at the front end so here we just going to highlight this as usual so press control and o and t to actually wrap this in try and catch so here we're going to say already exist I think that is the name yeah already exist Exception then here we're
just going to get the message we Define in there so we're going to say return response entity dot um so this going to say do status right this one actually we're going to say conflict and we going to have the body sorry body so then we're going to say um actually we're going to get the message That we Define at the back end to get that message right here so now I'm going to say e. get message and then we just going to return none sorry none right here okay so this all we need to
add new category then we're going to say this one is say post request so a post mapping actually then We're going to say this one is um sorry so we say this one is ADD Right add I'm going to say cat we going to say add add is okay right so this all we need to do to actually add a category so the next one we're going to do right here is the want to get category by ID so we're going to come down right here and say public response entity and um in here we're
going to as usual we going to have sorry this are API response so I'm going to say get category by ID why am I not having suggestion get category by ID so here we just going to pass in um this ID is going to be a path variable so we're just going to say at path variable and we're going to say long ID so here we're just going to say category the Category sorry equals service do get category by ID just pass it the ID so then we're going to return response entity. okay body and
I'm going to have new API response and I'm just going to say found and we going to say the category and if the category is not found then we're going to return category not found right but that was The exception we we're catching at the service l so we just going to we're just going to wrap this in try and cash so that we can actually cash and throw the exception so here we just going to say um resource not found so right here and here just going to return resource not found so response entity
dot status code sorry we want to say not found right this one right here going to say body and um we're going to have the Message with declare at the back end so get message I'm just going to return none okay so this one also is going to be a get mapping get mapping and so this one we're just going to say category slash um slash category SL category slash um the ID of the category then slash um Get maybe I going to say category I don't like writing to to Long end point so here
we're just going to pass in the ID as the PA variable right there so this one is to get the category so we have done the one to add we have got the one to add to get all and we have got the one we've done the one to get by ID so the one we're going to do right the remaining ones right here is the one to actually find um by name so let's do the want to find category by Name so this one actually is category is it's going to be the same thing
kind of so just going to say get category by name right here I'm going to pass in string name is that spell of string I going to say spring name sorry string name and um so uh this one actually it's not going to be it's not going to be um it's not going to be uh okay here we need to we need to pass the name right here To settle this case so we have category name and category we're going to have um a Bigos mapping right here because these two right here they are the
same level they they having the same HTTP method and they having the same level right here so we are going to have a Bigos mapping later so for that I'm just going to guide against this one right now so for now I'm going to remove this one one uh what I do okay let's remove one Let's remove one step to make it feel okay um so this one we're just going to say get category by name this one right here and then we're going to pass the name right here name I think this just what
we need because it's basically the same thing all right so the next we're going to create right here is the one to actually delete a category from the database so it's going to look similar to this one right here so I'm just going to copy this one right here and do the Necessary modifications so this one we're going to say delete delete category and here we just um we're going to get the the ID right here so we're going to say ID and then we're going to come here to say ID and this I'm going
to say delete and Meanwhile we're going to pass here going to say Category category ID category so I'm going to say category SL id/ delete and this one here we're going to change to delete delete mapping right here and after that we're going to change this one right here to delete category by ID and just pass in the ID and meanwhile we don't need to return anything because it's void right here so here we're just going to return none so just all we need To do right here and also we are still we we also
cat this error of resource not found right that is what we catched in there so the last one we're going to create right here is the one to actually update the C degree uh this one is slightly different so what I'm just going to do here I'm just going to pull it in so I'm bring it in right here so this we want to actually delete um so here we have the category ID um the category Itself so everything everything is is all right there is no complaint right here so these are the five Ian
how many 1 2 3 4 five 6 so these are the six end points we have in our category so now this brought us to the end of this category um controller implementations so um what I just want to do I want to try something right here let me P this let me just make them follow the same pattern if we have any problem we Actually come here to get it fixed so let's just follow the same pattern we have category SL name/ category category SL id/ delete category SL id/ update and so and so
forth okay so um let's move down to the next task all right so now we are done with the implementation of the category controller and the image controller so the next we're going to do in this video is the product controller so what I'm just going to do here I'm going to grab These three annotations and then I'm just going to right click and close these three tabs so now I'm going to open up the product controller and just dump these three annotations right here and then we're just going to change this one from category
to product so we're just going to say product that's all and then um the next thing we're going to do right here we're going to bring in the dependency we'll Be using which is the I which is the product service um the product service so going to say finer we're going to bring the interface for the product service this one right here so the first one we're going to create the first mod we the first end point we're going to create is the one to to get all the products from the database so we're going
to say response entity and we just want to say get All get all products and then we're going to have a list of products so let's do this UT and then we're just going to return this list of product back to the front end so we going to say return response entity. okay then we're just going to say um we going to say success and we return back the product so it's just all we need to do and then we're going to say this one is a get mapping say get Request so we're going to
say get mapping and then we're just going to say all slash all so this just all we need to do right here to get all our product from the from the back end from our server so the next one we're going to do here is the one to get product by ID so say public response entity as usual so we're going to say get product by ID as suggested by intellig and then we're just going to get this Products and then we're going to return it back to the front end as usual but me why
remember last time let me just open the service of this product so we can compare some things right here when necessary so remember last time when we getting product by ID so this one right here so we said that if the product is not found this exception should be thrown right so that's what we just want to do right here and one thing you need to take note Right here is that this exception right here has been changed from product not fun exception to Resource not fund exception and the reason why it's changed is because
um this resource not found exception is more globally we can use it in every cont for every API we are using but the product not found exception is just like tied to the to the name of the product so that's why we change it so remember take note of this changes right here change the product Not found exception we created last time to Resource not found exception and um in case you forgot how we created that class you can just go to the exception class right here this one right here and you're going to see
that here is the implementation of this product not find exception just exactly the same thing with the product not find exception and also they already exist exception we created last time so these are just the three exception classes we are using so Take note of this so now I'm just going to close this ones again and I'm going to close our project so now let's go so now we are getting a product by ID so we need to throw this we need to cat this exception that we are throwing right from here from the service
layer so we're going to go right here so for us to cat the exception I'm just going to I don't want to do Global exception handling rather I just want to throw the exception directly from the Controll right so right now we're just going to highlight this one right here and you're going to see if you're using in you're going to see this one right here so just um click on this surround right here and just surround with try and catch or you can just um highlight this one right here and press control plus o
plus T this one is going to open so just navigate to number six and Surround we try and catch so Any Which Way You Can Get It surrounded we try and Catch right so now we're going to say um resource not found yeah this is resource not file exception this one right here resource sorry the resour noi exception and then we're just going to return the message that we defined that we have defined at the service layer we going to say return response. entity but meanwhile I actually want to return the message so this one
here I'm going to say do status then we going to say not found so Right here not so say not found and we going to say body then in here we're going to return the message we defin at the back end so we're just going to say E.G message e. getet message right here and the second argument is going to be none yeah so this just all we need to do right here to get our product by ID so now let's do the static input yeah so that is done and this one also this one
is a get Request so we're going to say get mapping and this one this get map we going to say product product slash then we're going to bring in the ID right here which is the uh product ID product ID and after that we're going to slash it again with another product so this is the pattern we're going to use and then we're just going to come right here I'm going to copy this one Right here to make sure it's the same thing with this one right here so make sure this ID right here and
whatever you're using a path variable this one is going to be a path variable so we're going to it's going to be in the path of the request right in the URL so we're going to be getting we're going to be getting this ID from the URL and pass it inside the request down to the body right so now whenever you are using this one right here whenever you're using a Path variable make sure that this variable is exactly the same thing with the path you are passing right here just for example if it is
not the same thing like this you're going to see error right now so let's do it like this so that you're going to see the error right here right you're going to see this one right here so we see that um part variable product is not consumed right which is this one is not consumed because it's not the same thing right Here so doing this now there's there are two ways you can actually do this so you can either come here to do this like this and put product ID right here so you can either
do this one right here and just make sure this one and this one is the same so that um during the interpolation the the the the the the value that is passed in here will come here and will be passed in here right so either you do that or you just remove this one right here and make sure this One right here is exactly the same thing with this one right here so that is the two ways you can actually get a part variable work correctly in your endpoint so having said that right now this
is one to get product by ID so let's move down to create the one to actually add a new product to the database so I'm going to say response entity and this one is create product a nice suggestion from inell but meanwhile I'm going to make this one add product So it's the same thing anyway you can actually leave it create product but I just like add products sounds more clearly and so this one here we're just going to say at request body remember to put this annotation at request body because if you don't put
this annotation you're going to end up with non value in the database you're going to save non field if in case validation did not you didn't validate your input so you're going to have empty field in your data In your table so make sure you request the body using this annotation right here so then we're just going to come inside the body and we're going to we're just going to call the product so we going to say product sorry this is the product equal service do add product and we're just going to pass in the
product and then we're going to um what is happening right here so Okay yeah that is correct uh last time we we created um so remember last time we created something let's go right here so the add product right here where is it so this the no so this one is the add product so remember we are using this one right here we are not using the product directly so that is what it's complaining about so we're just going to bring this one right here and put the ad product and that solve the problem so
Then let's return the result if everything goes well we're just going to return the result back to the front end so we're going to say return response entity. okay and we're going to say success right so we just going to say product added success product product add or we just going to say add product success sorry add product success so there's no point of writing too much of grammar here we just Going to say add product success and um also if there's anything happened if we have any internal server error or anything happen at the
server side we just going to throw the exception so then we're just going to come right here and Surround it with try a cach then we're going to throw this General exception right here then we're just going to say um we're going to say return response entity. status um so this status right here we Just going to say um internet server error this here we going to have the static input and here we just going to go to the body and return the message to the color right here so that is that is gone so
this one is going to be at post mapping and then we're just going to say this one is ADD sorry this one is ADD Right add product okay so three has gone out of the way so The next one we're going to do here is the one to update product so we're going to say h sorry this public actually response enti update product n suggestion uh but me yeah this one is going to be product update the one we created last time product update request and we're going to call this one request and then we're
going to also get the ID of the product we try to update right so we going to say Product product ID and this ID is going to be a path variable have a p varable and also going to request the body of this um of this request right so we're just going to come right here to just um get the product and just get it updated right so going to say product then just going to update and then we're going to return as usual and also if you go right here the update product if we
go to the update product Right here so we are actually finding this product the first thing we do right here is to find this project this product right right here if the product does exist in the database and we said that if the product is not in existant then we should throw this exception so we should throw this message product not found so now we are going to cat this exception at the controller right here so if if that exception is thrown then we're going to actually cat it and send It to the user right
to the browser so here we're going to say resource not found resource not found exception and we're just going to return it back to the front end so going to say return response entity oh sorry I think there's a suggest right here let's make it faster response entity oh my gosh okay do we just going to say do um not found um the same thing we're trying to do so this one is not found and we just get to get the message that we declare at the Back end at the surface layer down to the
front end so this one is going to be a put so it's going to be a put mapping and uh sorry and here we're just going to say um SL product product/ ID which is the product ID and we can just make it we just make it ID to be very easy just ID right um but meanwhile we just going to follow this pattern so we're just going to say product ID then We going to say SL update so this one actually is going to be in craes there it's going to be insert craes right
here so this one is okay sorry uh um so this one is product ID to actually match this one right here so this one is out of the way so the next one we going to create is the one to delete you know we have so many operation in this product API so many things to do so many end point in this Product API so we're going to be patient and get them created gradually step by step so we going to have public response and um and this one is going to be delete delete product
and then we're going to get the ID of the product we're about to delete and we're going to make it also a path variable right so is a straightforward one we're just going to come right here to call the service to actually get it Deleted so you're to say product service delete product and also if the product is not found we still say that if the product will be deleted is not found then we're going to also throw the exception so we're just going to say right here and catch the resource not found exception and
in some cases you can still just catch the general Exception by catching the exception the general one but this one right here we just catching the the Specific exception we have set for this operation so that whenever it throw whenever this exception is thrown we just want to get the message we defined at the service layer to show to the user right so that is what we're just doing right here so then we're just going to return um resource not found again these are the things deleting the product right here I think we have it
up here so this is deleting the product right here so first of find the product by ID right And we said that if the product is not found then we should throw this exception to return this message right so that is the message we actually catching right here to return that's why we say e. get message to actually get the message that we've defined in this service layer right all right so this one is going to be delete annotation it's going to be a delete request so we're going to say at delete delete mapping and
then I'm just Going to say um SL products SL product/ um ID which is product ID right here you say path th say product ID and then we're going to say slash delete SL delete that's all so this just all we need to do right here to get our product delete so the next one we're going to do is the one to get product by brand and name so I'm going to say Public response entity um this one we've deleted already so we're going to say um get product by brand and name and product so
we just want to make sure our our product naming is um is clear enough for the reader to just know what the method is doing um so now we have the brand name and we have the product name good suggestion from t all right so then we're going to remove This extra one right here um so what we just need to do right here is that uh we just going to go here to just send it back we get the list because actually it's a list of products there are list of product are going to
match this request not only one so here we're going to have the list of products so we to say product service do get so let's look at okay this one right here get products by name And by brand and ID uh and name sorry and then we going to pass in the brand name and the product name so we're just going to return this the we're just going to return the the message back to the front end and also if there are no product found then this one right here we can just um we can
just catch this one at the front end so okay if the product is empty if if product is empty we just going to a message but also we can just do it here as well so if Product is empty we're just going to um let's wrap it in try cat so we just throw it as a kind of message to the front end actually it's not the kind of it's not a kind of error anyway it's not an error it's just it's just a normal thing the the the process executed successful but this product is
empty right so it's going to return empty array if no product found so but this to say resource not found actually it's resource not found so if there's no Resource is found that matches this that match that match this criteria then we're just going to say um resource not found right so we're going to return not found I'm just going to get the message resource not found but meanwhile let's go back right here if we actually set this message so let's see um let's see this one right here get product by Branch okay um actually
all this on right here we didn't set exception right Here so there's no exception that's going to be thrown because we we didn't set any exception to be thrown right here so what we're just going to do here is that we're going to remove this one right here we're going to remove this one right here and um are we making any mistake right here okay so now we're just going to return this one right here if product are found we going to return this but Now we're going to come here to check something so if
product is empty yeah so if if if this product is empty we're just going to return not found actually it's not an exception right so if we say if we say we should cash exception it's not going to work the message is not going to show right so this one right here we're just going to say okay um even this one right here we don't have anything we don't have any we don't want to return any message any Product name right here we just want to return a simple message to the user right so just
want to say no no product found for for this sech no product found actually so no need to take all this we just want to say no product found yes so we just want to say no product found because we are not catching any exceptions right so we just want to say no product found so that is just what we Need to do right here so if this product is empty if there are no product that matches this criteria they we just going to return no product found right and if else if if there are
products we just going to return back the result then this try right here we're going to cash this try right here so here we're just going to return the general exception we're talking about so should anything happen we're just going to return inter Ser error so this the general exception We're just holding right we're just catching and throw it back to the to the front end so the user can actually know exactly what happened maybe Network problem or any other issues we going to throw to the front end for the user to actually see exactly
what is happening so now the next one we're going to create here this one is get product so we're just going to come right here to do this mapping before we move on so we just going to say this one is a get Mapping and uh so we're going to say product/ product products SL products SL actually we have products already from the starting so we're just going to say slash um by we're just going to say by brand we say buy product by brand we're just going to say by brand and oh sorry this
is not brand so we're just going to make it as clear as possible going to say by brand And name right so just going to cut this one right here and put it inside this um parenes and just paste it again so this one is going to be SL product/ by brand and name right here so this one solve this problem um so like I said we have a lot of things a lot of a lot of end point to implement and both of them are similar to each other right so here the next one
we're going to find right here is want to find by category and brand so for that now I'm just going to pull in This one this end point right here I'm going to pull this one in so this one is saying product by brand by so meanwhile let's just um let's just um maintain this let's maintain this so let's maintain this pattern right here so this product by brand and name and this one is product by category and brand so it's just the same thing the same implementation we did right but meanwhile we just want
to also check because um it's it's going to fit the Same problem so if this list is empty then we're just going to come here and and also do the same check right and um if that is not the case then we're just going to return the product that is found from the database right just want to come right here and say return this which is the same thing with the product right here that we found in the database um so the next one we're going to do is exactly the same thing but this one
is want to get product by Name so this one is going to get product by name so I'm just going to come right here and also pull it in to save our time so this one is actually getting products by name and uh the same way we are going to come right here to do this check right because they are all list we're going to be expecting from the from the from the result search so here we're just going to replace this one with this because they are all the same thing they products and also
their Products so we just okay if the product is empty then we return this message no product found and if product are found we just going to return this one but if if if something else happened we're going to throw it as the internet server error back to the user right okay so have done that now the next one we're going to create is the one to find product by brand so let let's pull in the one to actually find product by brand so I'm Just pulling this one in one by one because they are
the same thing exactly the same thing if you look at the pattern they exactly the same thing we have been writing over and over again so that's why I'm just pulling them in so there's I've not done any modification here no any changes right just only the name of the methods that change and the head point that change but every other Thing Remains the Same right so here we just get the product right here by brand Which is right here for example we can get all product for Samsung can get all product for Apple can
get all product for for LG and so on and so forth now if the product if there's no LG product in if there's no LG product in our in our for example a user goes to our shop to look for product LG product so if there are no LG products then we just going to um we're just going to return this one right here not found right no product found then if there are product found We're just going to return this one right here product that that we found right so the next one we're going
to create right here is to get all product by category so we're going to come right here to get all product by category so here we have all product by category and also as usual is the same way so I'm going to copy this check right here and also bring it here uh remember this check we can actually do it at the front end but Sometimes I don't really want writing too much of JavaScript at the front end to validate what is coming from the back end although it's a faster sometimes it's faster to actually
check there in some cases but this case right here it's faster checking it from the back end so that if it's empty we just going to return this message to the front end to actually send to our browser our react front end or njs anyone okay so um this one here is the same way um the same way But this one is find product by category by and get all products and so this one right here is a part variable and this one is the category right and then the last one I think the last
one we're going to find right here is the one to actually get product by brand and name have we not done that brand and name this is by brand this is by name so this is U category and brand and this is brand and name so I think we have done with all our implementations right here So how many we have let's count 1 2 3 4 5 6 7 8 9 10 so we have 10 end points right here in our product API in our product controller so I think we have done with our
we are done with all our implementation so if there's anyone we have not implemented we surely come back here to get it implemented as time goes on um there is something I want to call your attention to right here so if we go to this method this end point that is finded product by brand and name so Here you can see that we having a kind of um warning right here right so here we are saying these ones are path variable but actually we didn't say the part right here but this one right here we
don't have to make this one part variable but we have to make them request params so going to say this ones are request param these two they are not part variables so we just going to request This parameter from the body right of the of the request okay so I think this solves the whole problem so if there are any problems we surely come back here to get it fixed so now that we have done with the implementation of the product controller so let's move to the next next task all right so in this video
now that we have finished implementing this product controller so like we said in the beginning we're going to go to Postman we're going to run our project and go to postman to actually testt uh the creation of product images and category right so before we move to the next task but there are few things I would I would like to call your attention to right here now if you see um this method right here this on all this method right here all the services we are not actually we didn't Define any error to throw we
Define any exceptions to be thrown should anything happen Right because this one right here is just um is a combination of jpa queries that we're actually doing right here um so since we did not throw any exception from here so coming here to catch exceptions we are not going to have anything to actually show to the front end because from here you can see that we actually finding the brand by name start finding product by brand and right here um we are returning resource not F exception but if we go here for example Finding product
by brand we did not Define any exception to be returned from here so which means we are not this message we are returning right here we are actually returning nothing to the user right so the user is not going to see any message except this one right here so if this product is is empty so we're going to get this message the user is going to set this message right here but should any exception happen any error happen the user is not going to Get any message right so um for that we are going to
change this one right here to General EX exception right so this one is going to be General exception right here and just change all change all these ones that we did not Define anything to General exception because we did not Define no no resource not find exception right there so we just going to get the message that is thrown from here and so That is just what I want to call your attention to and the second thing right here let's go back here let me quickly go back here to our service um okay so right
here I think this method right here we did not implement this one you see that all the ones are green because we have they are blue because have we have used them already but this one we've not used it so we forgot to implement this so let's go and implement this before we move down to The next task so I'm just going to go down here and scroll down below so I'm just going to pull down and just pull down this method this implementation right here so it's just the same way with every other um
with every other controller with every other end point we have created just that the name is only the name that changes right so this one is Count product by brand and name right so now the count right here this the count but um if if there's If there's a count we're just going to return it back to the just going to return the count right but if nothing to be to be to shown we're just going to return none like this General exception right here right so this is just all we need to do right
here so now um I'm going to let's run our project let's go back to let's check our our resource file our properties properties file so right here we still have this but I'm going to recreate let's create our table From the scratch from the grand right so let's create our table and remember what this thing is doing right um so this one right here we is going to create our table from the scratch this one right here is going to update the table when there's any changes but it's not going to create the table from
the scratch so this one right here is going to create the create and drop means when the when the project is running it's Going to create all the tables the schema so when the project is when the server is stopped it's going to drop the schema right drop all the tables this one here is for production so what we're just doing right here is this one and this one but for now we using the create because we want to create all our tables from the scratch so we just put it right here on the create
and make sure your schema is created right here just like we did last time already um so now I Want I want to run this project and let's go Postman to see what we've got to so far so let's wait a few moment while the project gets run and uh in some cases before running this project you can just after doing a lot of work you can just come here to actually um just um use M to actually test clean your product before actually running it in some cases right so um you can just come
here to say okay you want To run project go M goals you can just clean and install just run this clean and install in some cases right but now um just going to do this one right here but it has already run so which means there were no problems but in some cases running this m Maven go right here actually tells you if there are problems or not before actually running the main project right here so we can see right now that this project is compiling is actually cleaning up it's cleaning and It's actually compiling
and um at the point it's time we're going to see that it's going to finish running so here we can see running test cases but we actually U we don't have any test cases to run right now so now you can see that our our project has built successfully right here right so we're just going to remove this one right here we're just going to remove it and just going to remove this one so now Let's run our project and move to vs code sorry and move to uh Postman and actually text out this API
we've created before we move to the next task so now our project started running all right so now that our project have started let's go let's go to postman so um in Postman what I'm just going to do here I'm going to create a new collection so let's create a new collection right here and uh I'm not Just going to do this I'm just going to create new just add new collection right here um so blank so this one I'm just going to say [Music] e-commerce so we're going to say eCommerce right and just going
to um click outside to be created so right now we see that our collections have be created right here um so the first thing we're going to do here is to um um let's let's let's run a query I'm Just going to close right here so so now we're just going to create we're going to issue the first request for our project so this one is going to be get so we're just going to test get all all all pro pro product from the database so this one right here I'm just going to say I
think it was all right so I'm just going to paste this one in right here so this one have um our our local host and products and all so let's get all Products um so right here we have 200 okay which means everything successfully I will see success message right here but we have um empty list because there's nothing actually in our database right and so now we have created we have created um a collection and we are going to save this request to our collection so let's just go here and save as then we're
going to look for our collection so this our collection right here we're going to choose this collection and we See that this collection is empty right so we're going to create inside the collection we're going to create any folder this one right here and we're going to create this folder product product right and this product right here we're just going to click on Create and inside this product we're going to save this one as get all product so in case you don't know how to use Postman in case you don't know how to Create a
collections and request in Postman this is that the processes for you to actually store this so now we get all product right get all products then we're going to click on save so now that we have done this one right here now we can just close it and we can open right here so you see that we have gotten inside our our collection right here we have a new folder right here and inside the folder we have a new request so next time we can just come Here to click on this and we see here
it's been saved so we just click on send and we get our result so there's no point of writing it over and over again okay so take not of this so now we have we have we're not going to spend much time to actually uh test all this endpoint because we have too many things to do in this end point but meanwhile the only the only operation we're going to test so now we have tested get all so we are going to test create product and We are going to test um create image we going
to add image to product and we're going to add C to product then we're going to get the product along with the images right so that is just the only test we want to do then that time goes on in this project we're going to be testing every other functionalities step by step because if we want to test everything right now we're going to waste too much time testing everything so that's why I just showed you how to Actually get this one done so you can just get all this one and save all the requests
right here and you can also create because we have um images right so we can just also come right here U to create a new folder right here and this one we just call it image image right and we just click outside and you see images have be created and remember we have category right so we can also just come right here to create another Folder and that one we're going to call it category category category right here and we're just going to say this um so now let's close this let's close this so now
in inside our collection right here we have three um we have three end point right here so we have the product endpoint the product API we have the the image API and we have the category API so these are the three apis we have we have created like I said we're not going to Test everything right here we're just going to test the basic implementations and we move to the next task and so now having said this right now we're going to test we're going to create a product so I'm going to make it wi
screen so now let's change this one right here to post and our create product is like um product/ add right because that was what we did last time so we're just going to change this one to add and then we're going to go to We're going to go inside our body let's move this one down so let's go inside the body and let's choose raw so make sure you on Json right and then let's just come here and Define our Json and our product right here so I'm just going to paste in the product right
here so here we have the name we have the brand we have the price we have the inventory we have the description and we have the category so now we are saying okay our first product is going to be TV and it's Going to be Apple TV so we have the price is 400 bucks and right here we have the eventory how many do we have in the store we have 20 uh there we have the description is Apple smart electronics and this one here we just make the category Electronics right so let's save this
and see what we got so right here we have okay 200 okay which means it has been saved so if we go right here we have product we have ADD product success which is the message We defined last time and we have the product that is been added returned back to us so right here you can see that this category has been created for us and this category right here is electronics right and the product has no images yet right here um so if we go here let's go back to vs code uh sorry my
my SQ workbench so let's refresh so right here we see that we hav we've gotten one product in our database and um if we go to the category we're going To see that we have category called Electronics right here but if we go to the image right now we got no image yet right um so let's close this it's a duplicate so these are the three tables we have we we are testing right now so let's go back right here to um Postman so before we move on we're just going to save this one right
here so we're going to come right here make sure you you go to save as because if you hit on this save it's going to override this you see This one is get all so if you click on this one right here it's going to override the settings right there so you're going to have error so now let's go to save as right here so this one we're going to say we are still saving it inside the product folder right so this one we're going to say add new product so this one going to say
add a new product right here and we're going to click on Save right so now if I open our collection we're going to see that We have two end points tested right here already in our product so let me close it back so now we have tested get all add product and get all product so now let's um let's have another one let's create another another product right here so we're just going to go back to the body and this time around we're going to say watch right so here we're just going to say this
watch is um $50 50 bucks and we also have we have 10 of this one in our Warehouse in our store so we're going to say apple Smart Watch right uh I'm going to say apple smart watch and then and we're going to say this watch is a gadget gadget right here is that correct so this one this one is a gadget so let's save this and see what we got so now um what happened what happened okay let's do this again okay we are not having anything We're not having any response you see something's
happening let's go back to vs code what could have been happening um actually okay let's go let's go here let's refresh so this one is not saved this me something is happening all right so actually it's working right now um I didn't do anything I just close I just close uh postm because my postart just updated so I just close it and reopen it so it Started working so nothing actually happened so everything is working as usual so let's go back to our database Let me refresh so right here we have the product we have
the Apple watch we have Apple TV so let's add a couple a couple more so I'm just going to go off screen to add some of this products and then I'll come back right here to continue our testing all right so right here I've added a couple of more products to our database so right here in our database We have we have Apple TV we have apple watches we have LG refrigerators and we have Samsung TV right here in our database um so now let's go back to postman and so I'm just going to save
now now remember we have saved this before so I'm just going to save this one right here to actually keep this one to keep the the input we've entered right here so I'm going to close this so now let's go back here to get all Products so here let's send to get all products so right here we have okay status so here we have a lot of products like three products in a database um so now um what we're just going to do right here let's go back to let's go back to intj so right
here on intj make sure you you put this one from create to upgrade remove it from create to upgrade so that our data will not lost right so uh now let me close this one down so now let's Find by brand so product by brand right so we have the brand name so now let's go to intellig let's go to postman to find Apple product right here so we're just going to say products so we're going to say the brand right what is the what is the Endo again so here we have product and we
have by brand so let's go right here to say by brand product by brand so product by brand right so the Brand actually is it is it a part variable or so this one right here is a request param it's not a part variable right so now request param in Postman so you can see this param right here right so you can just go right here then you come here and tap type exactly exactly the name right here so right here we have brand right so this is the request this is the parameter that is
requested right so just type the brand as it is right here make sure the name Is the same so let's just go right here to say brand right here and then what brand do you need so type the name of the brand let just say we need we need apple right here and let's hit on send but Meanwhile we're going to set let's save this one right let's save it first so here I'm just going to go right here we're going to go to our Collections and here we're going to go to our product and
then we're going to say Get get product get product by brand so this one is actually get product by brand right so let's say products by brand and let's save so now let's hit if we get a any result so right here we have four 44 error um 404 error right here it means um maybe our URL is not correct or something so let's go back to intell so let's go back to intell to see what is happening by brand okay right Here we miss this product right here we're supposed to have product so here
we supposed to have product slash so we supposed to have product/ product by brand and the the param so let's send this again okay good so right here we have okay and we have Apple product so here we have Apple TV and here we have apple watches right here so we actually find it all Apple brand from our warehouse so let's change this one here let's change our param to LG so let's say LG right here and let's send so we have okay so here we have we only have refrigerator of LG in our database
and that is correct and now take note of what we doing right here we actually entering this one in this one is case ignore so this are the jpa finder method we generated last time in our repository right that is actually doing this for us we don't we don't need to write any query to actually get all this done for us than what we've done Last time so now we can actually let's save this one so now we can actually get our product we can save product and we can save by ID but now we
see that the product right now there are no photos right now they have no photos right now so what we're just going to do right now we're going to upload photo for this product now let's go to our uh sorry um um my Square workbench so now we see that we have products um we have product with two Categories so let's go to our category table right here so we have two categories right here one category is electronic one is Gadget right so if we go back to our product we see that we have um
three Electronics three products with with u with the category of electronics and one product with the category of Gadget right here and now let's find product by category right so let's find by category so now I'm just going to we're just going to go back to Post to post my right here before we upload image let's find by category and again let's go back to post to to see the this category right here so this one is find product by category so have product that the category they will say all product I'm just going to
copy this one right here so I'm just going to copy everything right here and go back here so what I'm just going to do here I'm going to remove this and actually paste It here just paste it here and here I'm going to say the category we looking for is electronics so we going to get all Electronics from our project from our warehouse right and this one here I'm going to save this one so I'll just giving you the basics examples so that you actually know how to save all this ones right here so just
we going to say um get product by category category so take note of how I'm saving this one right here so saving it will actually save you the stress of retyping in the request whenever you want to you want to test right um so here I'm just say this one is get by by by category so let's um let's save as get by category so now it's that change get to get by category and now we are getting by Electronics so let's hit and get all the electronics we have in our database so here we
have Electronics we have Apple TV is electronics so we have LG refrigerator is electronics and we have Samsung TV and it's Electronics so right now we're actually getting all our product All Electronics in our database now let's get all gadgets so let's say Gadget so let's get all Gadget from our database so right here we have Apple watch we have only one Gadget in our database which is Apple watch right right here so you can see that our API everything is actually Working as expected right and now you can you get the idea right now
so you can just test every other apis and see how it works just follow this process anyone you put right here you save it to your collection so that it will easy you to test when next right all right so now that I've tested the basic operations for our product API so it's time for us to actually upload images to our products so we have gotten some list of products in our database so but this Product has no images yet so let's test the image API to actually upload image for product so the first we
going to do right here let's go back to um my SQ workbench so right here we have image 1 to 5 and what we're just going to do here is to we are going to upload um a photo we are going to upload a photo for image for a product with ID number three which is the which is the LG LG product right the LG brand so let's pick the image ID number three let's go back to Postman so I'm just going to write I'm going I'm going to close all this now we have saved
all this already I'm going to close all this so I'm going to go here to issue a new request so here I'm just going to pick on this so then we're going to go to images we're going to switch to images the image API then we're going to say upload right upload so let's go back to itell to see our uh let's remove this and um let me just remove let's go Here and close this so where's our image so this is our image controller so this is the product this is the um the API
to upload image so right here we have um request params we have the file as as request params and also we have the product ID as a request param so let's go back right here so the request param uh this one is the this one is the is the um the URL so let's go right here so this in this T this param tab right here is where we're going to put this product ID as a request param so let's say right here we put product ID make sure this product ID is exactly the same
thing with what you have in in your controller right so let's say the product ID is number three right here which is LG and let's go to the body so here we're going to choose the form form data right here and we're going to come here to put in our files so let's have you know remember we are actually having list of files we're Going to upload more than one file for product product has one product has many many many images right so let's say this one is a file and let's select EDG has a
fridge right so I've gotten some images before right here so let's select EDG refrigerator open and let's select the second one open this one right here let's open it and then let's go um let's send and see what actually have this one is a post let's change this to post and Before we send I just want to let's save this one as so this time around we're going to here you see this in in here we go inside this image folder and we're going to um save this one right here as upload image so we're
going to say upload image for product right upload image for product and we're going to save this and and here we've saved so let's actually hit on send and see what we got um so here we are not having Anything just like what happened last time let's hit again we are not having anything let's I'm just going to close this I'm going to close this and run it again so I'm going to close Postman and open it again um I don't know what is happening this my Postman started Mark 4 Shing since since the last
update I don't know what happened okay so now it has opened again let's go back to the body right here and let's Save um so here we have 500 error right here um internal server error so let's go back to let's go to vs code and see what it what what what is happening so right here we have um we have an error that said the request the correct requ is not a multi part right okay U let's go back right here I think we need to let's go back here you know we just restarted
Postman so maybe this images are no longer there Let's remove them let's remove it and let's take the image again let's take fresh ones so the second one also going to take it and let's hit on send again okay so here have 200 okay so that was what exactly what happened so and now we have we see that the images have been have been submitted right and now we got we got back so we got back the um the the the URL to download these images So before we go ahead I just want to save
this right here so now let's check let's click on this download link and let's actually download so here we see that we can able to download the image that we just saved for that LG refrigerator right so let's go back right here and let's download the second one let's hit on send so right here we have the images so these are the two images that we have saved for the product right here right here and let's Go back to vs um my sqare workbench so let's go to the image and let's refresh so right here
we have the image we have image the product ID number three we have image 6 and five 5 or six and we have the U URL download we have the name we have the type and we have the image itself right right so which means everything is actually working as expected but Meanwhile we're just going to save this one right here as get image by ID right so um I think yeah that's Correct because this one is actually getting image by ID and it's working so let's save as let's go right here inside the image
so let's say this one is get image by ID get image by by ID right here and let's let's save it okay so this one is working let's close this let's close this and now let's close this so now we have successfully uploaded image for our product so let's Go and get let's go to the product API so right here we get all product right so let's get all product again let's get all product so here we have eal server error but meanwhile um okay let's get a product by ID let me just close this
so we cannot get all product having a problem let's see what is happening actually so here we're going to have product remember this is the API we send so let's have this is the end point we Set so let's have product with ID number one and product right so let's get a product with ID number one so right here we are able to get a product with ID number one with category with um with with this one right here with Brands which is ID number one product right so now let's get the C the product
with ID number three so right here this is where the error is coming from right because this product Al this product right now has image Already so we need to do some other things before we can get this product by the image but now we can get product without without um images right so um this one brings us to the next task we're going to do so let's go back to in and get this one fixed all right so now coming back toj we're going to we're going to work on the problem on how we
can actually return the image and the product back to the caller the user right and before we Move ahead I just want to call your attention right here so um in the course of your loading images if you're having problems with loading image I just want you to do something here for example if you have any problem like maybe the image you are trying to upload if you have problem of maybe the image is too large you can just come here there are some configurations you can come here to do I'm just going to um
give this space right here and I'm going to pull this in Pull in these two configurations right here these two lines of code right here so this one you can actually set the maximum file you want for example I can just set this one to 5 MB as my maximum and the threshold right here I can just also set it to 5 MB right here so in case you're having problem with image upload maybe the size of images you can actually come here to control the size of image you can upload in your system to
to avoid too much of space Consumption right so that's that so now I'm just going to close this and remember that your once again that this line of code right here this hibernate ddl Auto right here is on update so that you don't get you don't lost all the all the data all the um the product you have in your database right don't let it be on create make sure it's on update okay so now let's close this so the next we're going to do right here now remember last time we have when we doing
The the controller when we implementing the services we created something called d dto right this um right here so we created image dto so if you look here we have the image ID we have the image name we have the the the download which is the URL and that is the reason why we actually getting uh last time that is the reason why we're actually getting all those things right from that's where we getting the URL after the image has been saved and if you go here we see That when the image has been saved
here so when the image was saved you actually return the saved URL using the the image dto so this is the this is the URL we constructed last time so that is what we we that is why we able to actually download the image after the after the Saving right so now we are going to do exactly the same thing we going to create a a product dto and we're going to specify what we going to return back to the user and remember that if we go Right here to the product let's go to the
product model um right here so we can see that this product actually has category and some other things and the product has image it has other things now using the product directly it means is we actually trying to return this one right here and this is not what we trying to return we what we are trying to return actually is the URL of the image we trying to return so that is where we having internal Server error because we need to do some conversion before we can actually return this image back to the user right
so what we're just going to do right now I'm going to copy everything we have here I'm going to copy everything we have right here down to this um last time we created here let's go to the dto so right here I think I created a dto right here product dto last time maybe we created last time together I forgot but this one is a product D so what I'm Just going to do right here I'm going to paste all the attributes we need right here the attributes we need to return to the color and
then I'm going to remove all these annotations and also I'm just going to remove this annotation right here so now instead of us returning this image we're going to return the image dto so we're going to return the image DET right here right so that we are going to return this is the information we are Returning to the user not the real image okay so um now let's go back where is it so I'm just going to let me close some of this let me close this so now let me close this and uh also
let's okay we're going to leave this one for a moment so this is the dto so now let's go right here to have The longb annotation at data right here and then also um I think we're just going to leave this one for now so let's go back to our product Controller actually our product service let's go to our product service um this service right here so right here we have this the product service and let's go to the controller so now you see that this is the end point that is giving us all the
product right and you can see here that we are returning we are returning the the the product directly to the user so that's where we having that problem so now instead of us returning this product Directly we are going to return the product dto so we're going to return the producto but meanwhile this one is not going to work like this rather we are going to convert this product we got from the back end from this from the service layer we are going to convert this product right here to the product dto before we return
it back to the server right to the caller right now for us to be able to convert this product to D so we're going to need a dependency called Model mapper so now let's go to our product service right here so we're going to come up right here to pull in the dependency we going to say private then we're going to bring in a dependency called Model Model mapper this one right here yeah and meanwhile uh we need to get this um we need to get this dependency right in our P file you can See
right here we are not getting it from here so for for that we're going to have we're going to go to our pom file I'm going to open up our pom file so we're going to have a dependency right here called Model mapper right here so you can see that these are the few dependencies we have gotten before right here so now we are going to go get we're going to pull in another dependency so for that we're just going to go let's go to our Browser so right here I'm going to come here to
say model mapper so I'm going to type model mappa spring boo dependency I'm just going to click on enter so right here this is modela website so let's go right here and here the first one we're going to see here we're going to see this um dependency right here right so I'm just going to copy all this one here and then let's move back to our our project so I'm going to just scroll down A kind of scroll down under this web right here so I'm going to bring it in just going to paste it
so after that we're going to but meanwhile I'm going to need the ver of um 2 424 2.4.4 that's what the version I'm going to use so now let's reload let's let's up let's update our P file by reloading just click on that V there and reload so let's wait a while while this one Actually gets downloaded so now we can see that um our dependency is downloaded and everything is okay but now we can just put this one right here to extract this one so let's extract the properties right here and this one of
the best way to actually take care of our property version that the version of our um dependencies so if we go up right here we're going to see that we have um there we're going to have something called property right here so have property Right here and here inside the property right here you're going to see that we have two properties right here already we have Java 17 that jav version we're using 17 and the mod version we're using is 2424 right and that is what we're doing right now so now let's reload let's reload
again so you see that everything is okay so now let's go back I'm just going to close this one right now so let's go back to our product service so I'm going To come here to bring in the model mapper so let's bring in private final so we're going to say model mapper yeah this one right here and then see right here we having a problem right here it said it cannot cannot Auto wire no be found so now we're going to create a be for this model M so we can actually inject it right
so for that let's open up our project and now I'm going to go let's go back to our main package I'm going to Right click and then I'm going to create a new package so this package I'm going to call it config so we're going to call it config and inside this config we're going to create a new class so this one I'm going to call shop config shop so I'm going to call shop conf config right here and I'm going to close this one so the next we're going to do I'm just going to
zoom in a little bit so this one here is going to be a Configuration class where we going to be defining our beans right so let's make it a config so we're going to say add configuration this one right here and then let's come here to actually Define the bean so this Bean remember that every Bean must be public there's no Bean should be defined private right so we will say public model mapper this one right here and then we're just going to return the New object of this return new model Mapp then we're going
to annotate it with the B annotation to make sure it's a be right so this just all we need to do right here so let's go back to our product service so we see that this error has gone away so now we're just going to scroll below we scroll down so down below here we're going to create a service that we actually do this conversion for us I'm going to have Public product dto so I'm going to say this one we're going to call this one I just going to call the name of this method
convert to dto sorry convert to dto and then we're going to get the product object so the first thing we're going to do right here is just to get the product dto and convert all the all this product right here to the product dto right here so the next we're going to do right here we're going to get the list of images That are saved that the list of images that are associated with this product so here we just going to have list um list of images but meanwhile this one we're just going to say
product dto this image dto sorry image repository. find by product ID now this query right here remember last time let's go back to my SQ workbench so now this one is the image table and see that this image table actually consist contain a column called a product ID Right here so when this product is requested um the query is going to come here JP is going to hyet is going to come here to actually download this image based on the product ID that is required right so that is the jpa finder method that we are
writing right now um find by product ID right here that we're writing so here we get this one right here to say images that list of images then we just find it the list of images with the product ID and we going to Download all of them so after downloading them right here then we're going to convert these images to the image GTO remember that the image dto let's let's just go here so I can show you so let's have the list of image dto image this one right here um so this I'm going to
call Image DTS now remember that this image dto right here has just this few attributes that we're actually returning as the image so we're returning the image ID the image name And the image um the image URL actually we can just also come here to return whatever I want to return maybe we just want to return the the the type which is the content type any any information you want to return from the image you just include them right here right but now we just want to return the name of the image and the URL
of the image that the ID right so that that is just what we're returning here right now and so now we're going to convert this images we've Gotten to this image dto because if you remember that this image right here contains the the binary data of the bite data of the image right so we are not going to get this bite data at the so we just going to get the image dto we just going to convert the image to this to the attribute we have in the image dto and return back along with the
product that is been requested for so here we're just going to say images equals images stream then we to say Map so to have image and in here we just going to call the model mapper map and then we're going to pass in the image and we're going to pass the image dto image dto this one right here then will say class class so that's all then the next thing we're going to do right here is just to collect them into a list because we're actually um getting them we actually getting the list of this
images So this just all we need to do right here to convert our image to imageo but I'm just going to break this down somewhere let me break this one down and let and break it down right here so after this now what we just need to do again is to just set we're going to come right here so after this now we just going to come under here and call the product D so to say product D to set images to this image dto right here then we're Just going to return the product dto
return product dto so this just all we need to do right here to actually convert our images to to get our product with our image so now um let's P this one up let's just say override and we're going to pull it up to the interface level so we can get access to R from the controller right now there's something else I want to do right here so I just want to create another um I don't want to use this one Directly at the front end so I just want to create another one right here
so I'm going to say public but meanwhile you can use this one direct but for me I just want to create another one that I want to use right so I'm just public list so this one is going to be list of prod DET so say product product and here I'm going to call it on get converted get Converted get converted um products we some products get converted products and then I'm going to pass the list of products right in here yeah so list of products that's correct and in here we're just going to I'm
just going to return I'm I'm not going to do this so now I'm just going to return so I'm going to return products put stream so we're just going To say map then inside here we're going to call this convert to dto that we have done there so say map this and just going to call this one right here and then we're going to um collect it to to list and then we're just going to terminate and also I'm going to pull this one to the interface level so we can get access to it over
there so I'm just going to say override and then I'm going to pull it up yeah so that's just all we need to do Right here so the next thing we're going to do right here we're going to move to the product controller so now this is the this is the this is the end point that is giving us all the product right and you see that we actually returning the raw product the raw um model back to the front end so now we're just going to come right here to get the list instead of
getting the product the main product we get the list of converted product right so we going to say list of Converted products which is list of product dto so we're going to choose this one right here and we're going to call this converted product converted converted products so I'm going to say equals service um let's just U product service so equals product service. get converted products right here and we're just going to pass in the products that We have done we have we have already gotten right here and then we're going to replace this product
with the converted product right here so that's all we just need to do and after this now we we're just going to move down let's find the method that is the end point that is getting a product by this one right here so get product by ID so instead of us returning this product we're going to return the converted product so let's say um I'm just going To say product or maybe I'm just going to use let just use the V key word right here so I'm going to say um product product reposit sorry product
dto product dto equals product service I know wrong with my keyboard so we're going to say equals product service right here and do um so this one we're going to use right here because actually using get a single a single product right so then We're going to pass in the product right here and so we're going to pass the D right here instead of yeah so we're going to return D um this one right here you can just use it um you can just actually put it you can use it anywhere provided you know the
rules of using this but me I just want to make it clear let me just make it clear so this one is a kind of product d right this is actually product detail right here so let me just make it clear So we don't get confused okay so that's just all we need to do right here so that means this one we are doing right here this conversion we are doing right here we have to we have to paste this ones right here to everywhere we are getting product right so now we actually getting um
we're getting all uh we are deleting product right here so we are getting all product right here so also we're going to convert this one that we've gotten we're also going to convert Them so instead of returning the wrong one that will give us problem we're just going to return the converted one right here right so we're just going to come here and and paste it here that's all so you can just iterate through all this ones right here and actually return the converted product instead of returning this wrong one so just iterate through and
just convert them to the just um return the converted product in all this end point right here so now that we have Getting this done let's run our project again let's rerun it and go to postman to actually test it if it works or not all right so this the project started so let's go to let's go here so now let's get this product with ID number three um so here we ah we having stack of flow somewhere um let's stop our stop our server okay so we actually having um let's see so actually it
works but we have St Overflow so now we get the product and we get the product category and the category is getting the product again okay um I know so here let's remove this so let's go to the product category sorry yeah let's go to the category model so right here this is the product model right here the category model um so this is this is where the program is coming from here so now we are getting this category the Product is getting this category and the category is getting the product so they are getting
each other right so we're going to break the loop right here so just going to say Json ignore we're going to use this annotation right here called Json ignore to actually break the loop so we just telling the category that look when we are when we are calling you don't bother to call this product right here because the product is the one calling you so you don't need To call the product anymore so we just break the loop right here so let's run it again and go back to postman and see what we got and
uh so the project started running again so let's go right here and let's send this again oh yeah so we see that this is broken and now you see that we' gotten our Pro our product with the image so now we get the product so the product ID the name of the product is refrigerator so the brand is LG the Price is 200 which is the unit price and the inventory right we have 15 of them and the description is this so the category is electronic now again you can go to the category um dto
and if you don't want to return this ID you just go to the category you just create a dto for the category and return only the name through the dto so instead of returning the whole category just return only the name right and here we actually getting the images that are Associated with that product right here so we have two images that are uploaded for that product right here and now let's let's save I'm I'm going to save this so now let's get all products oh my I've made a mistake right here this is get
all products actually I have saved it to get uh but meanwhile it's not a problem we can actually get this vertified so let's actually get all products right here and so here we have gotten all Products so you see that this one right here the product with ID number three has images but other products has no images so we have Samsung it has no images so you can see that our now we can actually upload we can we can we can we can save product and we can upload multiple files to one product and we
can actually get the product alongside with your images and also we can just click here and just get this image downloaded right here so we actually See okay so now we see that we have taken a lot of time doing this and uh so let's move to the next task [Music] [Music] [Music] all right um I hope you have replaced all the end points to actually to return the cted images uh the converted products because from me I have done mine because like here for example all the ones that are returning list we just Use
the one we use right here because this one right here that is getting all the product is actually returning a list of converted right so for all for all the ones for all the end points that are returning list here we use the same thing up there then for all the ones that are returning single we actually use this one that is that is returning a single product right here this one is returning a single product and for that is being used right here because this One is r returning a single product after the creation
of the product and this one also is returning a single product after the update of the product right so this one is not returning anything this one is returning a list of pro of of product right so but what I just did right here I'm going to cut it from here and put it here so after the check so this one is going to is going to is going to do this call after the check so if this one is empty there's no Point of making this call we just going to come here but if
it's not empty it's going to make this call right um so that is what I did all through right here so just did just do yours and you can just get the code and just get your comparison right all right so having having said this right now our next task so let's go to our browser and see our next task and so right here we have the cart entity right here which is the cart Entity class so right here is a simple diagram that I just want to use to illustrate so there's no point of
drawing too much of uh too complex a diagram right so just want to make it simple with PowerPoint so right here we have a cat and we have cat item and we have product right here so here we say one cat one cat right there one to many so one cat has a number of items right can have many many items in one cat so that's why we have a cat has a cat items Right here and now a cut item is a product right here so each cut item represent a product might be Samsung
might be um Apple might be LG might be anything right but a cat item is a product right here so um we are going to go back to intell and create this we're going to work with these two classes remember we are finished with this product right here so in this video we are going to work with this cut entity and this cut Item entity so we're going to go back to in and actually model this out and see how it goes so let's move down there so back to intell I just want to right
click on this and close all tabs so let's go inside our project so inside this model this model package right here just going to right click and create we're going to create two new classes so the first one is going to be the cat and the second one is going to be The cat item so going to say cat Cut item sorry this cut item right here all right so now let's go inside the cat so let's close this so here is the cat and then we just going to say the cat is going to
have an ID so we're going to say private L ID and the cat is going to have total price let's say private and this one Going to be big decimal they're going to say total amount we just going to say we're going to call this one amount total amount and now the cut is going to have cut items right so say private um set of cut items cut items and so just all we're going to do and we just going to come up right here and say this one is an entity in the database by
just pulling in these Annotations and now we're going to define the primary key is the ID and we're going to Define the generated strategy right here so we're going to say um strategy just going to say identity this one right here so it just all we need and now what we just need to do right here we're going to say that this card is one to many so one cut to many items so we're going to say at one to Many this one right here and then we're just going to map map by cards we're
going to Define this one in a moment and then we're just going to cascade. all so cascade. all so that when a cut is the when um the cut is deleted all the all the cut items is going to be deleted as well right so then we're going to have the off remover set to True right here so that when if there's any cat items that is not referenced to by any cat is going to be Removed so this just all we need to do here right now but meanwhile I just want to initialize this
one let's give it a the default value of zero this one right here oh sorry um it's a big decimal so let's give it default value of zero right here so now let's move to the cart items to actually um to declare what we need inside this cart item right here um so right here the cart item also is going to have an ID we just going to say this one is is long soft type long long ID and the next thing right here we're going to have the cat is going to have a product
right because each cart item is actually a product so just going to have it as product right here and then the cat is going to have the cat ID and also it's going to have quantity let me just pick this quantity first and the cat is going to have um so let's say private and going to have big Decimal sorry big decimal so it's going to have a unit price and also it's going to have total price so we're going to have private big decimal total price this one right here and then we're going to
have the cat the cat is the cat item is going to have the cat ID so let's say private cat and we're just going to say cat sorry this is cat cat right here so this just all we need to do right here So now let's set up the relationship so we said that one C has many items which means many items has one many items belong to one cat right so this one right here we're going to say at many at many to one and this one right here just going to set the Cascade
also going to set the casket. all and then that just what we just need to do right here then we're going to set the adjoint column the column that is going to join it together with the cat So we're going to say adjoint our joint column right here then we're going to set this name sorry joint column right here so we're going to say name so the name of the column that is going to join them together we're going to just to call it cat ID so going to be cat ID right here so they
just all we need to do and then we're going to come up here to say that the product actually has many products to one cat which is many C Items to one card right so now we're going to say at many to one which means many items to one product and so here we're going to set the relationship between the cat item and a product so we just want to say okay um many cut item one product can belong can can be of many cut items right so this one here is also is going to
have relationship of many to one so let say at many to one right here and then we're just going to go down here to Actually set um we're going to set the Jo column so this one we're going to say the name of this column we're going to give it as product product ID actually product so we going to say the name of this column is going to be product ID so the the C the card is going to have the product ID and the cat ID but meanwhile let me just bring down this relationship
to stay near each other so right here I'm just going to put this one right Here and I'm going to clean this space to move these ones up all right so this is just all we need to do here um then we're just going to go here to just pull in the same annotations just like we have done here so I'm just going to grab these annotations and bring them down here and put them down and so we're going to define the primary key as usual so we're going to say ID and we're going to
we're going to Specify the Generation Um strategy right here so this is just all we need to do for now and then we're going to come down right here so we're just going to calculate we're going to set this total price right here we're going to get a method that actually set this me this total price right here based on the unit and the quantity right here all right so here we're just going to come down right here and say PIV public the public Void then we're going to say set total price set total price
this one right here and meanwhile this one actually we're not going to give any we're not going to give any parameters right here we just going to remove it so this one here we're just going to say okay this do total price just like the suggestion right here this is very correct this is exactly what I wanted to do so here we're just going to say okay This do total price equals um so this one right here we don't really want to get this one right here we just want to get the the unit which
is the um the unit price right here so we're just going to say this do unit price equals this do unit price unit price this one right here right here so we just want to multiply the unit price with the quantity that is picked right to get the total price so we're just setting it Right here so this is how we're going to make use of it later in the course um so now I think we have done with this one right here now we have done with this cart entity right so um let's move
to the next video all right so now that we have done with the card and card item entities implementations so now we're going to start implementing the services so I'm just going to close this to right here and then we're going to scroll down to this service the service package then We're going to create a new package right here and then we're going to call this package card so inside this card we we going to right click and create a new class cut service but meanwhile I'm just going to grab this name and hit on
enter so then I'm going to create an interface for this service um as custom I I I like prefacing my interface with i before the name of the service to denote Interface right so um I'm just going to do the same thing and hit on enter so I'm just going to create the interface which is the the card item card item service and also I'm going to grab this enter and then create the interface for it yeah all right so let's push this down and let's go so now the first one I'm going to close
this down for now so the first one we're going to do right Here we're going to start implementing this um card service so we're going just open up the interface and we're going to register all the methods we're going to be using right here so the first we're going to do right here is the want to get the cut by ID so we're just going to say uh cut we're just going to say get cut this one right here but we're going to have the ID of the card so we to say long ID and
the next one right here is the one To clear the cut so we're going to say void clear cut also we're going to have the ID and then we're going to have the one to calculate the total amount for the cat let's say big decimal gets total price for the card right as um this just the three these are the three methods we need right now so let's move back to this um the cut service and implement the interface we' just created so let's implement it and Pull in all the method that we've gotten over
there all right so now let's make this one a service so as to make it a spring bin and let's bring in The annotation to help us U Get Constructor injection so we're going to bring in the interface which is uh the cut repository that we actually help us to um as a dependency right here um so here this one is the cut um Repository here is it right here and I'm going to close this so we're just going to come here to return um which is the first method we're going to implement right here
is the want to get the cut so we just going to say I'm just going to come right here to the cut equals repository find by ID but right here we're going to we're going we're going to throw a custom message if the cut Doesn't exist we just going to say through a message right here just going to terminate and um I'm going to break this down remember last time we have created our custom class already so we're just going to use our custom resource not found exception class that we've created so this one is
done and after that when getting the cat we're just going to get we're going to to also set the to total price of that card which is um if that Cat contains items then we're also going to pull out the total price that is in that card right so here we're just going to come here to say cut cut do set total amount ount that we're just going to um yeah so this total amount right here um this total amount right here is what we this not what we want to do actually so we have
to create a variable right here to actually get the total amount so we're going to say Total total amount equals cat. get total amount now if we go to the card right here we see last time we have we have a property called total amount and this toal amount right here we actually update we update it right here right it's been updated already so we are just getting the total amount when the card is requested right so we get the total amount then we're going to set cut do set total amount then we're going to
set it to the total amount we Have gotten up there so there's no point of all this I'm just going to remove all this and we're going to just set it to the total amount we have gotten there then after that we're going to um after this all this updates then we're going to save the card before actually getting it right so here we're just going to say return repository. save save the cards right here so this just all we need to do so the next what we're going to do right here is they Want
to clear the card so as usual we just going to come here the first we're going to do is to get the card by ID then when the card is gotten we're going to the first thing we're going to do right here is to clear all the items in the cat right then after that we're going to delete the cat so um let's go up right here to get the card item repository Final Cut item um this one actually okay I think we've not gotten wait let's go this is going to be repository okay we've
not created a repository cut item repository uh let's say if will help us I trust so um this is cut item repository repository repository so let's go here and create okay that's correct so we're just going to say we have created this one here for the cut item and we are going to put this one Inside the repository package right here and going to hit on okay okay and hit on okay one more time and then it's be created for us so I'm just going to close it for now and um so what we the
first thing we going to do right here after getting the cut we're going to clear all the items inside so we're going to say um cut cut item repository do delete all uh this one is a custom um Jpa um finder method that we we try to combine right here so we're going to say going to delete all the item using the cat ID right so we're going to say delete all by cat delete all by card ID then we're going to pass in the card ID we have above and after that now we're going
to call the card to clear from its own site we say cat. get items clear so we going to clear the item then After that now we're going to delete the it we're going to delete the cut by ID so we say cut delete uh Delete by ID uh actually it is cut repository that we're going to pass in the ID up there so this one is going to be C repository yeah the Del by ID then now let's create this let's create this custom method inside this repository Right here so you see that um
there's no noise it's not making noise right here because actually um if we go right here if we go inside this card item we have a property called cart right here and this card right here actually if we go here you see that this cart has an ID so that is the combination we did right here to actually um get it deleted that is the where is it let me close this So to avoid confusion so that was what we did Right here the combination of jpa finder method we actually did right here right so
now we're done with this now the last one we're going to create for now is the one to calculate the total price in the card so as usual the first we're going to do right here is to get the card by ID that's correct and then we're just going to return return card do.get items get items yeah do stream Stream. map and in here we're going to get items which the cut items right here and then we're going to we're just going to I think we're just going to Method reference right here to avoid long
long story so I'm just going to say cut item right here item and we're going to Method reference to get the total price so we to say get total price this one right here and after that now we're going To come here to reduce oh so let me just break let me break it down break down and break it down right here and also going to break it down right here so what we just did right here we just iterate where getting the total price we just iterate through all the items in the cut and
we get all the total price accumulate them together and set them as total price right here and um so all right but now if you take note Right here this total price we're getting right here and if you go up you see somewhere we see here we we also getting the same total amount right here right and that is the same total amount we getting right here now the reason why I'm creating this so that um whenever the total amount of the card is needed somewhere we just we just go to the service to bring
it right but meanwhile remember that the way we get it right here we've already calculated this total Amount right here is the total amount we try to get and here we have already um updated the total amount here already right so this is the same way this this um this this implementation we did right here was the same thing we are doing right here right now in a shorter way right so that is to say we can just replace this implementation with get with the total amount we get up there which is like this one
right here right so for that now we're just going to Replace all this and we're just going to say cut. getet total amount so this one is the same thing right okay so now we are done with this so um let's move before we go to the controller we're going to go to the card item to actually implement the card item services so see you there all right so now that we have done with the cat service implementation so the next thing we're going to do here is to implement the cat item service Um so
I'm just going to let's let me just right click on all this for now and close all tabs so here we're going to come inside the service and open up the card service right here which is the card item um the card item service actually so this one is the interface so what we're just going to register the methods as usual that we're going to use so we're going to have in here we're going to have ADD cat I'm sorry add item to cat we're going to Have remove item from cat and we're going to
to have update item quantity right so these are the three methods we're going to have right here so let's go so going to say add cut item this one okay okay this one is correct actually so I'm just going to make this name like add item item to cut the same thing I just want to make it reverse so here we have we're going to have the card ID the product ID and the quantity we're going To add so here I'm just going to say um long um so I'm going to say cut ID so
we can differentiate between the IDS so we're going to have product product ID and then we're going to have int quantity quantity so these are the three things we need right here um so the next one is the one to remove remove item from cat that's correct so this time around we need the cat ID and the product ID as Itell suggested then the next one here is to update the quantity let's see okay no suggestion this time so let's avoid yes update item quantity then we have the cat ID the product ID and the
quantity that we're going to update so this one is correct so let's go let's go to the service and Implement all this um classes all this methods so right here we're just going to come here and uh so as usual we're going to implement the card item service and pull In all the methods will Define in there so this a service class let's make a service and we are going to need Constructor injector right so we're going to inject by construction so by Constructor so we're going to have this annotation right here to help us
do the injection then we're going to have um the repository cut item repository that is correct and also here we're going to have the product repository this time Around but meanwhile I think I'm going to use the product service so here we're going to have finer i product service this one right here then we're going to call it product service all right so these are just the things we need right here so let's go down and start the implementation one after the other so the first one we going to implement right here is the want
to add item to cut I'm just going to quickly List the steps we're going to take right here so one get the cut then two get get the product and three check if if the product which is the item already exist it's already in the cut then For if yes then increase that increase the quantity the quantity sorry the quantity with with the requested with requested quantity requested quantity but if no if no then Initiate then initiate a new cut item entry a new cut item entry yeah so these are the steps that we're just
going to these are the steps we're going to take to actually add an item to the cat so the first one right here is to get the cut so let's get the cut and meanwhile this not what I want to do so we just want to say cut cat equal course so for that we're going to have we're going to get the cat Service right here so we're going to say cat uh private IAT service we're going to have it right here and so we're just going to um do this import first so we're going
to say um cut service um yeah service yeah we going to say cat service. get cat get cat by ID then after that we're going to have the product to actually get the product by ID just like so then after that now we're going to we're Going to go to the next step so we're going to now check if this product already exist right so here we just going to say cut item um cut item so this I'm just going to say cut item as well equals cut. get items. it it going to stream it
stream. filter we're going to filter and then inside the field tab we're going to get Item just going to call it on item item. getet product so we're going to say item. getet product um so let me let me just break this one down so we see what we're doing clearly stream filter find first so what we did right here is that so we just want to say item. get product but meanwhile we just want to we don't want to compare the Whole of the product because that will take another process so we're going to
just compare the ID of the product so here we're going to say get ID so we're going to compare the product using the ID right here and then after that now we're just going to find first and if the item if if this item is not exist then we're just going to initiate a new cut item right here so we're going to say new cut item right Here and here we are having a kind of result is so what um result is always false okay um so let's go here okay yeah so here we're going
to say product ID that is up there yeah this one right here so we actually comparing the product ID with this one that is up right here so after this now we're going to just okay we're going to check if the product is not found then we're going to come right here so we're Going to say if actually we're going to compare this one with the ID so if get ID yeah get ID equals none then we're just going to initiate a new entry as we said so we're going to just say cut item. set
product then we're going to set the quantity and also we're going to set the cut so say cut item this item actually do set cut sorry this is set cut then the cut but meanwhile I just Want to I want to P this one up let me P this one up right here here so after this now that we're going to set the unit price of this product so to say cut item do set set the unit price then we going to get the product product. getet doget price right here so but if the product
is found we're just going to come right here if the product is found then we're just Going to increase the quantity with one we've gotten before so here we just going to say cut item set quantity and then we're going to come in here to grab the existing quantity and add with the new quantity so here we to say cut item um C item. getet quantity then we're going to add it to the requested quantity which is the one that is up right here this one right here okay so this just all we need to
to add new item then after this now we're Going to come right here come right here to actually set the total price for the cut item right so we're going to say cut item do set sorry um do set total price right here and then we're just going to come right here to add the item the cat item to the cat so we're going to say cat. add cat then we going to add the cat item then after added the item then we're going to save both the cat item And the cat so we're going
to say cut item repository do save then also we're going to save the cut so we're going to say cut repository do save so we're going to save in the cut no this one is cut actually though we don't have it right here we're going to bring it in in a moment so we're just going to say this one is the cut right here save it so now let's go up here to bring in the cat Repository so say private finer cut repository this one so this is just all we need to do right here
to add a new item to the card right here all right so a quick rundown of what we just did right here is what we've explained right here already right so um here we actually we get the cat we get the cut right here and we get the we get the product then after that we iterate we check if the product already exists by comparing the by by comparing all the Items inside the cards with this ID right here so if anyone if if if this ID if we don't find any item that correspond with
this ID then here we're just going to initiate a new cat item right here right but if we find one if we find if this ID already exist in the card then we're just going to come right here to get the quantity that is requested and and add it and get the the existing quantity this one is the existing quantity then we're going to Add it to the quantity that is that is requested then after that we're going to recalculate the price the total price of the item again and remember that this one right here
is the method that we we we have implemented inside this cat entity already so this one right here we we've already set the total price of the item so we get the unit price times the quantity right to get to give us the total price right here so that is what we just did right here so we we Calculate everything to get the new update for the total PR then after that we add the item to the cat then we save the cat item and also we save the cat right so that is just what
we did right here um now the next thing we want to do right here is to implement the one to actually remove item from the product so straight forward the first thing we going to do right here is to actually get this card to make sure this it this cat exist so We're just going to go right here to just do the same thing get the cut and after that we're going to iterate through the product right so we're going to say um cut item remember cut item is a product right don't forget so just
going to say item to remove so we're going to say item to remove equals that we're going to get the we're going to get the um we going to use we're going to iterate through All the product inside the cat so we're going to say cat get items do stream going to iterate through so going to say stream do filter and let's break this one down let's break it down break it down and uh break it down from here so here we just iterate through all this now to actually get to check if that product
if if that ID here we're going to find we're iterating Through through the product right and we are checking the product to get the product that correspond with this ID right here right so if this product is found then we're going to grab it but if the product is not found actually we're just going to declare a message right here we're going to throw a message so for that I'm going to say all s throw because we need something to catch at the front end to to tell the user what is happening right so here
we're Just going to say um sorry that's not what to do so we're going to throw new resource not found so we going to say resource not found exception so we're just going to say um product not found actually so we're going to say product not found now if the product is found we're just going to come here um so if the product is found we're just going to come here to call the cut so we're going to say cat. remove and again This remove right here is what we is the method we Define right
here inside the cat entity right here right there's the cat right here and this is the method we defined to actually remove a product an item from the from the card right so that is the method we are actually calling right here to do the job for us then when that is done we're going to save the cut again to save the update so we're just going to say cut repository cat repository. save then we Going to save sorry then we're going to save back the cat so this is just all we need to do
right here to remove item from the card all right so the next one we're going to right here is the one to update the item quantity so the first step is to get the is to get the cat then we're going to iterate through all the items in the card so we're going to say um sorry this one is get Item get items do stream filter. find first um let's break it down so we can see everything we're doing here and here so now if the item is present we just going to come right here
to say um item. set quantity just like this then going to say item do set item do set so we're going to set the unit price of the item set unit price unit price right sorry Unit price and then we're just going to say item getg product. get price right here and after that we're just going to say um we're going to set the total price again so we're going to say item do set total price and we get the total price right here and but meanwhile uh yeah that's okay so now if the item
does exist we're just going to if it is found that we're just going to set The attribute right here then after that which means if it exist we to set the quantity then we're going to set the price then after that we're going to get we're going to set the total price again remember this is the same method we have we have set right here so the total price is basically the unit price times the quantity right so that is the total price we actually getting right here so we set the the price times quantity
to get the total price right and then we're Just going to come here to end this statement right here then then after that now we're going to get the total amount so we just going to say big decimal then we're going to say total amount total total amount now remember that updating the quantity of the cat means it might be reducing it might be increasing right so if we if we remove item the quantity is going to reduce if we add item the Quantity is going to increase so when time item is been removed we're
going to recalculate the amount and anytime an item is been added we're going to recalculate the amount so that is just what we try to do right here so this one now we're just going to say okay um cat. get so we're just going to get remember we get this amount already calculated in our cat so we're just going to say okay equals cat um sorry this is cat actually do get Total amount which is the one we have gotten inside here already this one right right here this total amount already that we have already
set right here right so that is the same thing we are just doing here right now and after that now we're going to set the total amount to say cut do set total amount then we're going to we're not doing this actually we just tell is doing more than requested so we're just going to say okay total Amount right here and after that now we're going to we're going to save the card again to save the update so we're just going to say cut repository do save and save the cut so this just all we
need to do right here to update the quantity of our product right inside the card so um one thing we I want to do right here is that I want to I want to extract this this uh implementation right here so that anywhere in our project that we actually need this Implementation we're just going to take it from a specific method right a helper method right here um so what I just want to do right here I'm going to come down I'm going to actually copy I'm going to copy this one right here then I'm
going to come down here so I going to say public I'm making it public so that if there is anywhere we're going to need this implementation in any other part of our project later then we just call it From here right so here I'm going to say public I'm going to say cut item and then I'm just going to call this one here get cut item and then we're going to pass in the cut ID and the here so the cut ID and the product ID right here so the first we're going to do here
is to actually get the cat just to get the cut then we're just going to return this implementation so I'm just going to return and then I'm going to just bring This one we copy from here right so we just going to say return card. get items. get all this so this one is a product remember item is a product like I always say item is a product so we just say Okay item not found if it is not found so then we're just going to come up right here now we're going to remove all
this and we're just going to say get cut item and we going to pass in the cat ID and the product ID just like that you see this one actually makes our Our um it makes our method look so small and adhere to the single responsibility um single responsibility principle right here so um that's just all we need to do but meanwhile I would like to pull this one up in case we we want to get access to it from the controller we can actually get access to it so here I'm just going to pull
it up to the interface level right here so this just all we need to do uh I think this brought us to the end of our C item Service implementation so um let's move to the next task all right so now that we have done menting the card service and the card item service so the next one we're going to do here is to implement the controllers so um let's just right click and close all tabs and let's open our project so I'm going to scroll down to the controller um this is the controller package
so here I'm going to right click and create a new controller so the first One we're going to implement right here is the cut controller so I'm going to say cut controller so here we're going to have card controller and hit on enter so I'm going to make this on wi screen so uh let's zoom in a little bit so the first we going to do right here is to bring in the annotations so we going to say rest um require um require Constructor for Constructor Injection I'm going to bring the rest controller and then
we're going to bring in the request mapping this one right here so this one we're just going to say um so this we're just going to say SL Cuts right here and then we're going to bring in the preface is the API prefix right here so the only thing we just going to need right here is the is the service dependency but I'm going to take the I'm Going to take the interface so I'm just going to preface with I and do the import all right so the first end we're going to create is the
one to get cuts by ID so just want to say public um response entity API response then we're going to say get cut get cut and then we going to pass in the cut ID right just going to say cut ID and then we're going to say this one is a path Variable path variable all right so uh we go in here and we're just going to say cut service cut service. get get cat by ID and we're just going to pass in the cat ID then let's do the import and so we said that
if the cat is not found we're just going to throw back exception right so just going to return now we're going to return okay um response entity. okay do okay the Body and here we're just going to say um new oh no no no we're going to Define our message so we just going to say success right and we just going to bring in the card right here and then we said if the card is not found we want to throw an exception right here so we just going to we try and cash as usual
and then we going to cash the resource not find exception here so we're going to say resource resource not find exception and then we're going to Return the message from the back end so we going to say return um not found we're going to say this one is do status status not found not okay let's go not found and then we're going to say body so here we're just going to say um e. get message just for us to get the message we actually Define at the service layer right and the second Argument here is
going to be none so let me zoom out a little bit so we can see it clearly and let's terminate right here and let's do this constant import here okay so we're going to say this one here is a get mapping so we're going to say add get mapping right here and then we're just going to come here to request for the ID of the cat so we to say cat ID cut ID and then we going to say slash My card so this all we just need to do to get the card to get
the card from the database right so the next one we're going to implement is the one to actually clear the card so we're just going to come here to say public rest no no no this public response entity you want to say no this one is this one is Clear clear cut yeah clear cut and then we're just going to get the cut ID right here let's remove this and it's a part variable as well so we're just going to say service which this one right here do clear clear cut and then we're just going
to return success clear cut we're going to say clear cut success clear card clear card success I'm going to put exclamation right so and also we're Going to say this one is a delete it's a delete mapping because actually we are deleting all the items that that is that are inside the card right so we're going to say this one say delete mapping and also we to request for the ID of the card right here and then we're going to say um clear so here we going to pass in the cart ID so this just
all we need to do this all we need to do to clear the cards Right so the last one we're going to do right here is the want to get the total price which is the total amount of the card so let's say public response entity API response so we're going to say um we're just going to say get total amount get total sorry get total amount right here and we're just going to say big Decimal because this amount is actually S Type big decimal will say total total price equals service service dot get total
price pass in the ID and then we just going to return okay and this one also we're going to say this one is a get request so we say get um sorry my bad so here we're just going to say this say get request so we're going To say get mapping and also we're going to request for the ID of the card so we're going to say I say part variable we going to say um cut right I'm going to say slash total so we're going to say/ total price and then we're going to have
the cut ID here as the path variable as usual all right so this is just all we need to do right here this so This these are the three end points we have right here Inside our card controllers but meanwhile um it seems if we go back to this let's go back to the service this cut service right here and we're going to see that we're actually throwing we're actually getting the cut right we get the cut first before any other operation and this get cut actually is throwing a message right here so which means
we need to catch this message right here as well so let's just go here so we just going to Surround this we try and cash and actually cat the message so let's just go here and say resource not found we going to say resource not found exception right here and here we're just going to say return a. get message yeah that's correct and the same thing we're going to do right here just highlight and short cut right here resource not found and we're just going to do same then we're going to return resource not Found
all right so this just all we need to do right here so these are the methods uh the end points we we have in our card controller right now so if there are more we're going to come back right later to actually get them added okay so let's move to the next task all right so now we have done with the implementation of the cart controller so the next we're going to do is the cart item controller so I'm just going to close this one and then we're Going to come back to the controller package
where is the controller package right here and then we're going to create a new controller so I'm going to say cut item controller cut item controller and then I'm just going to close this uh meanwhile I just want to do something right here let's uh let's borrow some from here so it be faster so I'm just going To grab these three annotations right here and I'm going to come here to just just pull them down here and then I'm just going to change this one to cut items that's all so let's bring in the dependency
yeah but Meanwhile we're going to take the interface from here so let's say I C item interface let's do the input so the first we going to create right here is the want to add item to The card so let's say response entity API response and then we're going to say add item add item to uh yeah add item to cut so we going to have long we going to have the the cut ID product ID and the quantity right so here we're just going to say these three are going to be at request param
request params yeah this one so Let's I'm just going to grab this and um I'm I'm going to break this one down actually and let's put this one right here and then I'm going to break this down and just put this one right here just paste it here and uh get space so this just all we need to do so here I'm just going to go in here so we're just going to call the cut service cut item service yeah to just add the item to cut then we're going to return something right so just
going to say return Response entity okay and we're just going to say item added to cut successfully say add item success yeah no need of writing too much grammar here so here we're just going to return um return none from here return none right okay and also so we are catching no no no resource fund exception right here in case the cut does not exist so we're just going to Catch the exception so let's highlight this and uh so let's say resource not fund exception and let's return it so we're going to return this and
uh so we're just going to return resource not found exception this one right here but meanwhile I'm just going to say status the status right here I'm want to say not found this not found status not found right Here body then we just going to say um e. get message because we actually Define this message at the back end already so the service layer already so just going to say e get message and the second argument or the all parameter is going to be known right actually what is the difference between argument and parameter there
are differences anyway yeah so this one right here is like parameter right okay cool so um this is Just all we need to do and we're going to say this one is a post request so we're going to say post and then we're just going to request the item I'm going to come here to say item and then we're going to say add so it's just all we need to do right here so let's move down to the next one that we're going is going to help us to remove item from the card I'm going
to Close this space right here say response entity AP response so we're going to say remove item from card remove remove item from cat and then we're going to request the cat ID and the item which is the product ID right so we're going to say long um cut ID and we're going to say um product ID product is same as the item right and um So this one also we're just going to say these two are going to be path variable at path variable and this one also is going to be a path variable
yeah and I'm going to zoom out a little bit so going in here we just want to call the cat service to do this for us remove item then we're just going to return okay and also because we are also getting the cards before removing the item so we also going to surround we try And catch and just say Okay um the card is not found so we're just going to return the message right here so let's get the message that we've already defined at the back at the service layer this one right here okay
so this just all we need to do but this one here we're going to say it's a delete request because we're actually removing all the item from the card which means we are deleting all the items from the Card so here we're just going to request the cart ID say cart oh sorry this a request part um it's a part variable so we're going to say slash and also going to have another one right here and we're going to say slash right so the first one right here is going to take the cut the cut
ID here and then we're going to come here and say item you say item and Slash then we going to say product ID right Say product ID or can just say item id actually let just make it uniform just say Item ID and for that we're going to change this one right here to Item ID right here and uh also it's going to come here to say Item ID so then here we're just going to say remove so this just all we need to do right here here and so this one have cat SL ID
SL we're just going to say cat right here so we just to say cat Cut ID item Item ID and remove so the last one We're going to do here for now is to want to update the quantity right so let's go here so I'm going to say um this is public actually this public yeah public response entity and and this one here is we're going to say update update um item quantity item qu item quantity and then we going to have um the cut ID and every other one The cut product and quantity and
also we just going to say this ones here uh we're going to pass this one as path variable going to pass this three let me just Comm on let me just do some um do some copy right copy and paste here so I'm going to say copy and paste and this I'm going to say paste but this one here the quantity let me zoom out so the quantity is going to Be request param so let's make the quantity of reest let me break it down so we can see it clearly break it down yeah so
the are these are just the three things we need and straight away we're just going to call the service to actually get it done for us say service. update quantity and then we're just going to return this and also we going to come here to do same thing so we're going to say um resource Not found and then we're just going to return that's correct um so this one here we're going to say is is an update request so it's going to be it's going to be put right so let's put mapping put mapping here
and I just going to say um slash let's double quote we going to say slash cat and then we're going to say cut ID then we're going to say item and then we're going to come here To say Item ID right and then we're going to say slash update so going to say update so we can see that it's actually following this pattern right here right so let's just go here to say cut ID and here we're just going to say item id Item ID and we're just going to come here to get it updated
Item ID and here also we're going to get it updated we're going to say item id all right so this just all we need to do Right here so these are the three points right here we need to Define in our cart um cart item um controller for now as usual if there's any modifications we always come here to get it done all right so so far so good we have got some things done so um let's move to the next task all right so now we have done with the implementation of the cart controller
and the cart item controller so the next we're going to do in this video is actually we're going to run our Project and text all these three ENT points we have created but meanwhile there's something I want to show you right here so if you look this is the this is the end point that is actually adding a new item to card and here we are requesting for the cat ID right so how do we get this cat ID so typically we need to get this cat ID from the authenticated authentication context that when a
user logs in we're going to initialize a new card if there's if that User has no card previously then we're going to initialize a new card and assign that card ID with that user right so that that user can actually add item to its cat but currently we have not implemented the user entity we've not implemented the user API in our project so what we're just going to do here is to we're going to create this cat ID um um manually for now right so for that I'm going to go here we're going to go
to the cat service right Here then up here I'm going to say private final we're going to have Atomic we we have Atomic long um this one here we just going to set the initial value here to zero sorry this is zero but meanwhile I just going to call this one um cut cut ID generator so that it it will correspond what we doing right here so I'm going to say cut ID generator then after that now we're going to come down Right here to create a method that actually do that for us so I
going to say public L initialize initialize new cards and but this one here I don't think we need to have anything right here so I'm just going to go here to create a new object of a cat right here and this one I'm going to call it new card right and then I'm going to come Right here so we're going to have um we're going to get the ID um so this I'm going to say long new cat ID yeah new cat ID then we're going to use this we're going to increment this one we've
created right here so now the initial value is zero so when a new cut is initialized it's going to be one right so here we just going to say um new card increment and get so after that now we're just going to set this new card to this ID right here so Going to say new cut new cut set ID equals this new cut ID then we're going to save so we're just going to call the cut repository do save the new cat and then yeah we're just going to return the the the ID of
the new card we have we have saved right um so now we're going to come back right here to this to this controller then in here we are going to check if this ID is known right if if no ID if there's no um card ID that is provided with the request so here we're Just going to say if so we're going to say if cut ID cut ID equals equals none equals none and actually we just don't want to go all the way here we just want to say equal none if there's no cut
ID then we just want to go right here to call um so for that we're going to need the repository right here which is the service right here we say private Final Cut we going to say we to take from the interface I cut service right here and then we're just going to come here to say cut service do initialize um okay we are not getting it right here so we're going to come here to pull it up to the interface level so we can actually get access to it from the from the controller right
so let's pull it up here and then let's go and get access To it so just going to say do initialize new card right here and then and then we're just going to assign this one here we're just going to assign this um this ID that's is going to be initialized to this one right here so for that we're just going to say um cut ID yeah this cut ID right here we to say equals to this right here so we just assign the ID that is initialized to this so let's let's close it yeah
let's close the space here and So that is how we are going to actually get our ID to include to this place right here so now let's run our project to see what we got so far so but before run our project I'm just going to let's check our property to make sure we don't lost what we got in our database so here this one is on update so that's good so let's close it and now I'm going to clean up our project so let's run M let's run some goals so I'm just going to
run M Clean and install Right here going double click this to actually clean our project for us so let's wait a few seconds while it runs so we can see that our project is being cleaned up it's running let's wait for f to be patient a few seconds okay all right okay so our project has been buil successfully right here so we see buil successfully so let's remove this and uh Let's run let's close this panel and uh let's run our project um so now we still wait for a few seconds while it runs um
so here we have error right here okay this part is already been used so what we're just going to do I'm going to go here um let's change this port to one let's say 91 91 and um so let's run it again so let's wait a few seconds while It runs so here our project has started running so let's go to postman but before we go to postman let's go to my workbench to refresh to see if this table has been created for us so let's refresh okay so we see that our table has been
created cre so let's let's look at this so this one is the cut item right here cut item table so we have the ID we have the quantity the total price unit price cart ID and product ID so let's open up the card Table so here we have the ID and the total amount so let's check the product we have we have some product last time already so right here we still have it and let's check the images and everything still intact right um so now let's go to postman to test add product to to
the cat um so here let's go to postman so here in Postman I'm just going to issue a new request and this request is going to be post Request and uh here we're just going to let's see if we have some caches right here okay I'm going to take this one this one looks similar right so let me just remove everything we have right here so this one we going to have item slash add right and then we're going to go to the param this param tab right here because we're going to we have three
params request params so one of them is the cat ID and one is product ID which is the Item product ID and the last one here is the quantity Quant yeah quantity right here and then we're just going to say we have product ID number one which is Samsung TV right we go here so we have no it's Apple TV product number one right here and we have 20 of it so we are going to take three out of this 20 so let's say we need three of Apple TV right here and this one we're
going to Leave this blank because we're going to generate This One automatically right so um let's run this and see what we got let's run it so right here we have 400 bad request so let's go to intellig to see what why is bad request um let's let's check our conso just going to zoom zoom in right yeah so here we have um required request parameter cut ID for method this of type long is Present but converted to none big grammar so what is actually happening right here is that um it's talking about this one
right here right because we're actually generating this um so what we just need to do right here we need to say this one is not required so we're going to say uh required we want to set it to force at initial state right so we can actually create it will give us chance to Actually um insert okay so let's run it again and see what we got this time all right so it started running let's go back to postman and let's hit on send and see what we got again so let's send yeah so we
got this one 200 okay and we got add item success so let's let's go back to let's go right here let's check our um item cut item let's refresh for okay I'm just close all These and let's refresh and I'm going to get our cut items from here so right here we have cut item with ID number one quantity number three that's three three quantity and you the total price is200 um 1,200 um 1,200 and we have unit price 400 so 4 * 3 it gives us 400 * 3 gives us 12200 right so we
have one and we have one right here so let's go to our card so our card also has 12200 as a Total amount in that cat so let's go and add more quantity so let's just go right here so uh before then I'm going to save this one let's save this one right here so let's save as so we're going to go to I think this this was our collection last time right yeah so inside here we're going to create a new folder and this folder we're going to call it cat call it cat and
we're going to create so inside this cat we're going to we're going to say this one is add item To cut add item to cut so this one is the new request we just issued so let's save it and then we're going to Let's update the quantity from three let's say um put so the update I think the update takes same it take same um structure right so let's just let's go and check so here is the update so have cat cat ID item Item ID right and update so this one is going to be
item id okay let me just remove this to let's Make this let's be fast so here we're just going to have cut right and then we have cut ID which is number one then we have item and we have the item id the item id also is number one right so and the quantity was request param right the quantity was a request param it wasn't a part variable so right here we have two part variables and one request param which is the quantity so which means the quantity is going to be inside here Right so
we're going to say Quantity so the quantity instead of three we're going to say five going to update it to five and let's send it and see what happens um we have nothing coming up I'm going to close this and run it again my postman has been giving me problem since the last updates I don't know um let's open it again and see what happens all right maybe they're waiting for me to upgrade to pay Money okay okay so right here let's let's go let's s it again um 400 not found um it's not found
the set is not found so here we have cat ID we have item let's go back to check let's go and check so this is the update right the cat item so we have cat cat ID item Item ID and update okay so let's go here and say oh right here we supposed to have update so let's say update here right this was supposed to have so let's send it again uh bad Request update oh my gosh what is happening right here so what is what could what could be the problem okay let me just
go off screen and find out what happened and I come back to communicate with you it's my bad come on so here there a misspelling so here we have I don't I can't even pronounce what I wrote here so we're going to have Quant right here this actually is quantity so let's run it Again um so this time we have 200 okay so the product has been updated successfully let's let's go back to here and refresh so now the product has been changed from 3 to 5 and now we have gotten 2,000 bucks from 1200
to 2,000 so 5 * 4 is 2,000 right is two 5 * 4 is 20 actually so here we have 20,000 20 2,000 so let's go to our cat and see if things reflect here so our cat is not reflecting yet but we're going to solve That in a moment but um now before we go to solve this let's just quickly go here to remove to clear let's go and clear the card but before we clear the card I'm just going to save this let's save this one and um so this one is also going
to be in here inside the cat then this one is going to be um update right so you're going to say update card update cat items up card update item quantity maybe Item qu update item quantity that's we have right here so let's save this one and then before we go and fix that problem let's check the delete because these are the three apis we've created so let's check the delete so I think the delete is just let's let's find out again the API uh the Endo so right here we have remove item from cat
so here we have cat Cut ID item Item ID and remove so just the same thing with this one right here So let's just remove this one and let's change this one to remove remove remove and uh let's this one is delete actually so let's remove and see what we got let's hit on on send so here we have 200 okay removed so let's go back to our cat and let's refresh so you see the cat has been the item has been removed let's go to our cat item and refresh so the cat the item
has been removed which means the ENT the the three end points we created is working So the next we're going to do right here we're going to fix the problem why the cat is not updating why why why why the total amount of the cut is not updating but meanwhile what we just going to do right here let's go back and let's save this one right here let's save as U so this time around it's going to be um remove we going to say remove item remove item from or remove Cut item let's remove Cut
item remove Cut item this correct This is shorter right so let's save it okay so now uh um let's go back again let's let's just go right here so let's open up our project so this is the card so I'm going to add um I'm going to add so that we can use it to test so let's add this again success right so let's go back let's go right here and refresh so now we have it back and let's go to the cat so now we have it back so let's go and fix this problem
why it's not updating all right so coming back to Intj um this is the this is the the cut item controller so and this is the method that is updated this end point that is updated the quantity right so let's go to the service of this so this is the add item this is the remove and this one is the add quantity right here and here we said when the quantity is added either remove or or or decrease either decrease or increased then we're going to recalculate the the total price and set it here okay
so here we are Getting the total price and we are setting this one to this and this one is getting the items right okay okay so we say that total price equal of equal this one here do get total price total amount right here and um so this one is not correct it's not working correctly so what we're just going to do we're going to remove this one from here I think we're going to implement this one to recalculate to reiterate through the process because This one right here is actually getting the the is getting
the cat and getting the amount that was in the cat right here um so what we're actually getting right here is the total amount that is getting that is currently inside the cat that is gotten from the service right here because here when the cut is gotten from here where is the the okay here we we got the cut from here what is it here so we are getting the cut from here and we're actually calculating the total am Amount of the card in here to come along with with it right which is the previous
amount so now getting this cut from here getting this cut from here is going to come with the previous amount and now that that that was the same amount we are actually setting where is it so that was the same amount we are setting right here cut. get toal amount so this one is not correct so now what we just need to do right here we're going to remove this one here and then we're going to Recalculate so we're going to say cut so we're going to reiterate through the items again and recalculate the amount
from a fresh so we're going to say cut. get items this one right here do U the stream so we're going to stream and then we're going to map we say map so we just going to say cut item yes cut item here and we just going to Method reference so we're going to say get to price do get total price this one right Here and um after that now we're just going to reduce we're going to accumulate them right so we're going to reduce so let's let me break this down so we can see
exactly what we're doing right here so let's break it down so here we just what we here is that when we get the this cut right here so we just get the cut right here again then we want to reiterate through the items inside the card to get the current price right so we accumulate all the all the All the all the total price of the items right there and we accumulate them right here and set it to the cat right here I think this should work so let's run it again and see what what
we got so let's run this um okay it's running all right so let's let's go back to so let's go back to so this one is our the the the the the the price is still there so this one the item is still there so now let's go let's go back to let's go here so here We have three right so let's change the quantity instead of three let's say we need five let's let's increase by two so let's add five and let's send okay so right here we have success so let's go back here
and refresh so let's see if there's any changes let's refresh yes so here we have 4 * 5 we have 2,000 right so let's go to the cut so let's refresh good so here we have 2,000 so now it's it's is actually updating so let's go back to let's go Back here let's say okay we need only two we just need two so this one is going to be 800 right because 4 * 2 is going to be 800 so let's go right here so let's refresh our cat so here we have 800 and let's
go back here so here we have 800 boom so now um everything is working but now um we we we got we got three end point right right we created three end point right here which is the want to add item to cut then the want to remove Where is it so this one the want to remove they want to add they want to update and they want to add so we have tested everything right so uh let's test again let's test the last time let's remove this item from the card for the last time
so um I'm just going to save this one to what we've got before so I'm going to close this so this is the remove so let's remove one one right so let's remove it so here we have remove Success let's go back here so let's refresh our cut items so the whole product is gone so let's go to our cat and refresh so the whole product is gone but we still have a cat that currently we have an empty cat but we don't have products inside the cat so so far so good our cat has
been created and everything is working successfully so let's move to the next task see you there all right so now we have done with the testing of these three end points We've created right here in the cut item controller so now let's move down here to the cat controller to test the other three that we created here so here we have the one to get the cat by ID so we have the one to clear the card and we have the one to get the total item for the card right um so right here we
have in our database I think we have yeah we still have data right here and we have a card with ID number one um so back here in intelligence J so the one to get all Card is we're just going to have/ card SL card ID and my card so let's go here and then I'm just going to change this one to get to get request and um let's say here okay let's remove this so we're going to have SL cat then we're going to have the cat ID is one then we're going to have
my card so I have my card right here and uh it's a get request so let's send okay so um we are having a stock Overflow right here let's go back to stop our server okay um so now let's go back to see what is actually happening so uh let's go up let's just move up so here we have the total amount of the card is 800 so this is the cut item so we have the item right here total price 800 unit price 400 apple and this card is getting the image and um this
image okay here we have the cat and the Cat is getting the image so I think there's there's some there's a recycling dependency right here so let's check what is happening between the image and the card so is it actually the image or the product so let's see this is the product right here and the product has an image and the product is getting the cut right here and the cut okay yeah the relationship between the cut and the product so we're Going to stop one of them so um let's go let's go back to
intelligent and so we actually getting the card right and uh so the cat is getting the product so what we do right here is that let's go let's go here let's go to the product so right here we have um where is the model right here so this is the product and this is the card right so here we are getting this card and the cut is actually getting the this Cart item right here right it's getting the cart item and the cart item is a product and the cart item is actually getting the product
and the product is getting the cut so in this case we are going to go let's go to the cut item right here and this is the product right so the product is getting the cut so let's go to the product right here and uh um so this is the product and so the product has nothing the product has does not actually is not aware of the card Right here so which means we are going to go right here in this card item and so while getting this card item the cart is getting this cut
item right here and the cart item is getting this cut again so let's break let's try to break the the relationship right here let's say at Json at Json ignor let's see what happens so let's run it again and um let's wait for a few seconds okay it has started so let's Let's fire down again and see what happens okay so yeah so now we get it right so now we able to get the cards with ID number one and the total price in that card is 800 and that card actually has an item item
with number item with ID number three and this item has um um a product two quantity of a product which with with a unit price of 400 right so here the product is Samsung is Apple TV the brand is Apple and the name is TV and so um so now we've gotten A cat so let's save this one right here let's save this one as here um get get cat so let say get cut by ID right and let's save this so the next one we're going to do right here is the want to clear
the card so let's go back to intellig um so let's go back I'm going to close this for now close this and U this is the card controller so we've Tested this one so let's test the one to clear all cart so we have the cart ID and clear right so let's go here we have the cart ID so let's change this one to clear so this one is clear and clear basically is deleting the item terms in that cut right so let's clear everything so here we have um interal server error right here um
let's check what is happening let's get the console and uh here we have Error and here said said the error cannot reliably process removed um no entity manager right here okay um I think we need to make this we need to make this service where is it this service right here so this clear cut we need to make it transactional so let's make it transactional right here transactional so transactional because here we're getting the we're getting we are doing a Lot of operations right here we're getting the cut and we are doing massive delete right
here right so um so we need to make everything a single unit of transaction right here so if one fails everything fails so let's go let's run it again and go back to see what happens and uh so here the the the project started so let's go back to let's go back to postman and let's clear this again okay so we see card cleared Successfully so let's go back here so so you see the item has been cleared so let's go to the cart so the cart also is cleared everything is cleared from the from
the cat right so now we don't have we have empty cat right here and so the last one we're going to text right here is the one to get the total price of the cat so what is the what is the end point to this to get the total price of a cat so uh here this is the controller so this Is the total price so we just get the cut ID the card and total price so I'm just going to copy this one right here um so don't waste too much time so coming back
here but meanwhile we're just going to save this one here as clear we're going to say clear cut so this one is clear cut actually let's save it and now let's have the card ID and I'm just going to I think I'm just going to get this one and paste it here so we have cat ID we have cut And total cut total pricee so let's go and see what happens so here this thing is none because we don't really have anything inside but meanwhile something's happening right here I'm going to start this one again
I think we need to get we need to get something we need to get something back because we actually catching no not um not F exception right here I think we should get something back either bad or good so um here let's do it Again okay so here we have method not allowed actually this one is get right so this is get and let's run again so we say cut not found yes of course we should have something back so cut not found because actually we have deleted everything right here so we don't have any
card right here no ID no nothing so we see that our project our our project everything is actually working our end point all is working as expected so um before we Rand up with did testing I'm Just going to save this one here so this one is like um get to price get toal get total get toal price for cats or get cats total price anyways so um I think we need okay let's take this we are doing this we're saving this inside the cat right here so um we're just going to save this like
this all right so now we are done with our testing with the testing of the six end points that we have created for the cut and cut items so far so good we have Made a progress even though we wasted a lot of time but actually we we learn something from here right so um let's move to the next task see you there all right so in this video we're going to start working on the order API so right here we have the order entity in our slide so right here on the slide we have
exactly the same relationship what we just did with the cart and cart item is exactly what we're going to do right here so right here we Said that one order has many other items right here which is one order to many items and item has item is a product right so we can have many one product ordered many times right here right so here we have order has an order item and other item is a product and the relationship between these entities here we have the B directional so the relationship between the order and the
other item is by directional which means we can access the order from the other Item and we can access the other item from the order right but the relationship between the other item and the product is a unidirectional so the product knows nothing about the other item but the other item knows about the product so that is just the relationship right here that we're going to model in intellig so let's go back to intellig so what I'm just going to do right here we're going to open up our project and then we're Going to come
here to the model package so we're going to right click on this and create two classes so sorry so one is the order and the second one is going to be the other item they have the other item right here that's correct yeah so the the first one we're going to be implementing right here is the order so let's go to the order this one and let's zoom out a Little bit zoom in a little bit rather so here the order is going to have an I order ID and the order is going to have
um we're going to have the date so we're going to say private so we going to have local date we going to have order date and then we're going to have the total price of the order that we to say private so we going to say big decimal and we're going to say total price which Is total amount so here we just going to say total total amount um what am I doing sorry guys what so we're going to have total amount right here and after that we're going to have the order status say private
order status and we're just going to say um order status right here order status now Before we move ahead we're just going to create this one this one is going to be inom so let's create this and um here let's just say we're going to create enom class right here and let's choose the package so here we we don't have that package yet so just going to go to to click on our main package and create a new package and we going to call this one enams okay and okay and okay and also okay for
the last time so here we have In I'm just going just going to go right in here and I'm going to pull in some cant right here so we have the pending we have processing shipped delivered and can so so this is just this are just the um this just the life cycle of our order so let's close it and back here we're going to have we're going to say okay this en right here we're going to just map it with enumerated value and numerated value this one right Here and here we're going to set
the we're going to set the type to string so we say in type then we're just going to set it to string this one right here and let me zoom out a little bit maybe it's too big so the next we're going to do right here we're going to bring in the other items so we to say um this one is going to be private going to be set of other item this correct and we're going to set up the Relationship between these two entities so we're going to say this one is one to many
so one one one um one order to many items right so we going to say one to many so one other to many items and inside here we just going to map it going to map back we going to say order we're going to go to the other side to Define this one in the moment and after that we're going to casc all Cascade then we just going to say um All this one right here and then we're going to set the off hand remover to true as usual so the next thing we're going to
do here we're just going to say this one is an entity class so we're going to bring in the geta seter bring in the set and we're going to bring in um the no AR Constructor no AR Constructor and here we're going to say this one is an entity ntity so right here and then we're going To set the primary key at ID as user and we're going to set the generated value to Identity just like this so after this now we're going to move let's move down to this item um this other items and
here also I'm just going to go right here and grab everything here because also it's going to be a table in our database so it's an entity class so we're just going to paste all this one Right here and then we're just going to have the ID so we going to say ID private long um going to say ID and here we're going to have the order as usual and then we're going to have um the quantity yes also we're going to have the product thank you TJ and also we going to have the quantity
and we're going to have one more one more uh one more um attribute called price so just Want to say oh my gosh sorry mistakely press this cap lock so I'm going to say private then we're going to have the big decimal and going to call Price this going to call it price um just going to say price yeah right here all right so this just all we need to do so now we're going to set up the relationship between these entities so now we're going to set up the relationship between these entities But meanwhile
I'm going to bring this two I'm going to bring this two up here and then I'm going to push down the two that is going to be we're going to have relationship with to declare relationship for so the order right here is going to have at many to one at many many other item to one order and then we're going to set the joint column so you going to say Ad joint column so the name of this column we going to call order ID so Name to say order order ID so they just what we're
going to do right here and the Proto we're going to say so here we're going to say at many at many product to one other item right so we're going to have we can have many products in one other item right here and then we're going to join the column as well we going to say Ad join column and the name of that also we're going to set so we're going to set this One to product ID we say product product I d right here so this just all we need to do here and um
so we're going to set the primary key for this so we're going to say at ID and also we're going to set the generated value or just going to say strategy do identity right here so this just all we need to do right here for now so the next thing we just going to do right here is to I'm just going to initialize this One to new haset and then I'm going to go here we going to go here so what we just going to do right here we're going to generate a Constructor right here
we're going to scroll down right here and then I'm going to right click here and generate a Constructor and this Constructor we just want to take the order take the order we just want to take here except the ID right so we just Going to click on okay so here we have the but meanwhile I'm going to rearrange this one right here doesn't really matter but I just want to rearrange this ones right here and um just going to give space and then go here to remove this so this how I want them to be
ordered right and uh so also I'm just going to this just my own pattern it doesn't mean anything I'm just going to put it Right here yeah just put it here so this is just all we need to do um right here to actually set up our other entity and other items entity um so let's move to the next video where we're going to be talking about but meanwhile okay right here we have the no AR Constructor uh because we've already created a Constructor here already and right here um if we need a Constructor here
we come back right here to actually get it created so let's move to the next video Where we're going to be talking about other task so see you in the next video all right so in this video we're going to start creating the order service so I'm just going to Let's close this and um let's open up our project so we're going to move to the service package right here I'm going to right click on the service package and then we're going to create a new package so this one we're going to say order Order
and inside here we're going to create the order service order service and as usual I'm just going to grab this and hit on enter then I'm going to create the interface for the other service and then we're going to say this is an interface and enter so right now we're just going to I'm going to zip this one I'm going to close it um make It wi screen then currently we just need two methods to work with right now so later we're going to add the other ones so in here we're just going to have
a few method that we're going to need for now so the first one is going to do the one to actually place order so we're going to say place order and um yeah let's take this from our package but this one here is going to be the user ID though we've not created a user we've not implement the user API but we're Going to do that um in in future videos so we're just going to say this one is a user ID so for now we're going to get this ID manually kind of and next
one here is the one to get order so we're going to say order and we going to say get order and we going to say order ID so these are the the two things the two method we're going to work with for the moment so let's compare to implement this and uh let's pull in the method We've created in there as usual so we're just going to say this one is a service so this one is a service and uh we're going to bring in the The annotation for in um for Constructor injection then what
we just need to do right here is to bring in the order repository I think we've not created that but we're going to create it inter is going to create it for us so we're going to say private Finer um order order repository repository uh it's not correct Repository repository what am I doing so here we have other repository I'm going to say other repository so let's allow itell to do this for us okay that's correct so here we're just going to say we are creating this one for the other um model and here We
are going to and here we're going to choose the package so we're just going to say here we are putting this inside the repository package and click on okay and click on okay and it's done for us so let's close this and come back here um so now what what we just need to do right here this order right here let me give a little space so this one is going to be more easier so let's let's first implement This before come here because this one is going to be kind of complex so let's do
this one first so we're just going to come here to say other repository yeah I'm just going to say repository find by ID and uh we're just going to throw a message um to say or SSE through right here and I'm going to throw a specific message just like this and let's break this down a seizure going to break this Down but Meanwhile we're just going to use our own class resource not find exception right here okay so this just what we need to do so now um this order right here this order service is
going to be complex so I'm going to create we're going to break this um we're going to create some helper methods that we're going to assemble right in here to actually place our order right so I'm just going to move down right here create a Space um so the first one we're going to do right here is the one to actually is the one to calculate the total price the total amount of the order so I'm just going to call here to say private so I'm going to say private big decimal this big decimal so
we're going to say calculate total amount calculate total price uh sorry yeah calculate yeah total price this one is okay and now so we're just going to say this one is total amount actually total Amount yeah total amount and then here we're going to have the other items right here so this one is going to be the list of other item because other contain a list of a list of other items right so here sorry so we're going to say a list of other item list of order order items yeah this one here we created
last time and then we just going to say other items other item list and let's do this import Yeah so what we just need to do right here is to iterate through all the other list and get all the price and sum them together right and return and return the sum so here we're just going to say return going to say return order item list do stream stream dot we're going to say map and inside here we're going to have each items to be iterate through so say item. get. get um say item. get price
and we're going to Multiply by by the quanti right so we're going to say item. get price get get price this one right here do multiply and let's break this one down so we can actually see what we have there let's break it down break it down and um Also let's going to break it down from here right so here we're just going to say do multiply by so here we're going to pass in um the quantity of the it item right Here and then we're going to accumulate we're going to accumulate the price of
each items and then we're going to return it to this so we're going to that's just what we are returning with this method right here all right so the next one we're going to create right here is the one to create the the other items for us so let's push down a little bit so I want to say um private so I'm going to say list and This one we going to say other list not other list actually it's other items and then what we're just going to call create other items so I'm going to
say create other items and inside this one here inside this method we're going to have the order and the cut we're going to have the cut as well in here cut so now we're just we just going to iterate through we're just going to get All the items from inside the card right so here we just the return return card.get items stream map and inside this map here we're going to get all the items uh remember that um a product actually is a cat item a cat item actually is a product right now so for
every item in here which is a product we're going to get the product from the cat item and we're going to get the quantity of that Product so what we just going to do right here is that you know we have every product we have in our database has quantity that has inventory so now we're going to subtract the amount that has been ordered from the total inventory then we have the remaining inventory in the database so for example if you're having 10 um Samsung TV in our database and a customer orders for five then
we are going to be left with five in our database right so we're going to Keep track of the inventory when order when any order is being made so in here we're just going to say product so we're going to get product right here and we going to get the item from the card which is the product from the card right so here now we're just going to set the inventory we're going to subtract the inventory so we going to say set inventory here and then we're going to get the product product. get eventory Which
is product product get inventory then we going to say minus cut item. get quantity right here so this just how we're going to um subtract so this is the quantity of the card this the qu quantity of the product that we get from the card right which is get from the product right here so now we are getting this product the the the how many of these products do we have in our Database they we subtract it from how many are we are is how many is the customer ordering right now so we're going to
subtract to have the remaining ones in our database so that what we just do right here so then after the subtraction as we done we're just going to call the product repository to save for us so for that we need to go up right here to bring in the product repository so here I'm going to have private product repository that's Correct so just going to say product repository um this one here do save the product to save the product back to the database then going to return the order we're going to say return new new
order item and just break it down then in here we're going to have the order and then we're going to have the product and then we're going to have um we going to have the quantity is cut do quantity cut item actually do Quantity and then we're going to have the price is the unit price of that item which is cut item. get price cut item dot sorry this get price actually yeah get unit price get the unit price right here then after that now we're just going to collect them to a list H sorry
this is what I want to do so I just want to come here to terminate terminate from here and then we're going to to collect everything to list so we're going to say To list right here so we terminate so let me just let me um format this one a little bit and let me just push this up a little bit so after that now so this is just all we need to do right here right so we are just returning um we just Tred to return the order we just created the other items right
here so we are just iting through the items the product and subtracting the quantity that's being ordered from the total inventory then we save the Product again then we return the other items that we have we have booked right so the next we're going to create right here is the one to create the order for us so going to say sorry this private order so we to call this on create order and inside we going to have the card right there and actually we just need only the card right here we don't need product so
let's remove this and in here we're just Going to create a new order object and we going to say order order do set status. pending say order status pending so the first time the order is booked the status is going to be pending so when the when action is being taken on the pro on the product when the when the order is been processed then the status of the order is going to be changing from one place to the other from one state to the other For example pending processed shipped deliver stuff like that and
then the next we going to do right here we to say order to set the date do set date that's the booking date and then we're going to have though this order also is going to have a user but being that we have not created the user yet we're just going to we're going to come back right here to to um to complete this so here we're just going to say set set the user right here right And um so after that now we just going to return the order so we just going to return
return order right here so let's close this Gap a little bit so now the next one we're going to do right here we going to come here to this we're going to come here to this um to this order which is the main um method where we're going to assemble all these ones we have done but meanwhile we are going to need the user right here so I'm just going to Use this opportunity to create the user so that we just um we just include the user to this order right here so that it will
be covenent for us to test later when we're going to do the testing so for that now I'm going to leave this one first we're going to leave um this method and then we are going to move down to create the user so after the user implementation then we're going to come here to complete this other process all right so in this video we're Going to switch over to implement the user entity before we come back to complete this order service so I'm I'm going to right click and close this temporarily and then we're going
to come here to our project so then in our model package we're going to create a new class we're going to call this class a user so we say user and let's close it um so what I'm just going to do I'm going to zoom this in a little bit so now I'm just going to come right here to Paste in some of the annotations so we say this one is an entity so these are the normal annotations we have been using along right you must have be familiar with these annotations right now so now
we're just going to have a few attribute for the user use private long user ID then the user is going to have a name actually we're going to say this one is a first Name so we're going to say this one is a first name and um so we're going to have the last name and we're going to have the email and the password right here so these are just the things we going to have then now we're going to bring in the cat because the user is going to have relationship with the cat so
we're going to have private and here we're going to have cut so um the relationship between a cut and a user is one and one so one user to One cut right so no two users can use one cut so let's have it one one yeah this one to one relationship and then we're just going to come here to say mapped we're going to map it by the user of which we going to go to the other side to set up then here we're going to Cascade when the user is deleted the cat is deleted
you going to say cascade. all all and then we going to set the off Remover to true so let me Zoom this out a little bit and then the next one we're going to do here is the order so the user um one user can have many ERS right so we're going to say um private um say private order order right and this one is just going to be a list of order because one user can have more than one orders so as far as this order is more than one it's going to be a
list so have a list of order might be list or set of orders Right but actually um many is one to many so here we're going to say at one to many one us are to many orders and then we're just going to also I'm going to also map this by user and then we're just going to also Cascade um right here there's a comma right here so we're going to say cascade. all and offer REM true just as we've done Above So this we going to call it on orders right here so this Just
all we need to do right now um so having setting up this one right now let's go okay before we leave here we're just going to come here to set all the primary key so let's say at ID and let's have the generation value generate value generated value actually um so now let's go to this card to actually set up the relationship between this card and the user so here we're just going to click on this and here is the cut then we're Going to come up right here I'm going to um we're going to
pull down here and um so we're going to have the cut right here sorry not card we have the user right here so we to say private then we're going to say user and we say user so this user is going to have like what we've set up at the other side so the user is going to have oneon-one relationship so one to one relationship and um we're going to come Down here to to join the column so we're going to say Ad joint column and this joint column going to set the name to use
our ID so we're going to say um name equals equals user ID so this just all we need to do right here and um so the next thing we're going to do we're going to let's go back to the user and um so let's go to the order to do the same thing So right here we're just going to scroll down right here to bring in the user so to say private user so we to call this one user as well and this one is going to be at many to one because many ERS to
one user so we're going to say at many to one right here and then we also going to join the column so we going to say at joint column and this one we're going to say um user ID user ID so this just all we need to Do right here to set up the relationship between user and the cat and the cat and the order so let's go back right here we see that all the error is gone okay um so we are going to we're going to before we go to implement the the service
so we are just going to go back to this cut order to actually complete the implementation we've started being that we have gotten a user entity that we are going to go there to Actually get this done all right so now that we have done implementing this user entity so it's time for us to go back to complete the other service so the first we just going to go right here we're going to come here to the other service and then I'm going to remove this one right here and we're going to actually set the
the the user right here so we going to say order set user Yeah I'm going to pass in the card right here do get user because currently the user is Right present is rightly present inside the card so now we're going to come here we're going to create a new jpa finder method to actually get the card to get the card that belongs to this user that is trying to do the order right so have card and we going to say cut service cut service so right here we've it's been Auto imported we to say
Doget going to say get cut yeah get cut but this time around we say get cut by user ID and this one is a custom method we're going to create right inside the the card repository which is um right here just bring it right here inside the card service interface and now the next thing we're going to do after getting this then we're going to actually create the order for the user so we're going to say Order oh sorry this not what we want to doal create order because we've already created this method already then
we're just going to pass in the card to create the other for the user then after this now we're going to get the list of the other items so here just going to say list list of other items say other item list equals create other items that we're going to pass in the order and the cut right here um so I'm just going to give A little space right here so so after this now we're going to set the order so let's say order do set do set order items then we're going to pass in
uh this is going to be new hashmap not actually hashmap hash set and then we're going to pass in this item right here this list of items right here then having done this now we're going to set the total amount so we're going to set the total amount of the card right of the order so we're going To say order do set soorry this is set total amount then we're going to have the calculate amount we have down there this one actually this is not what I want to do we going to have the calculate
this one right here calculate amount we're going to pass in the other the other list right here okay so let me just paste this so after doing this right now we're just Going to save we going to save the order to the database we say order then for that we we actually want to get the order that we have just saved from the database right so we're just going to say order so this we going to call saved order then we're going to say order repository do saved then when this order is being saved then
we're going to clear the card right so here after saving the order to say cut um Service so we're going to clear the cut and then we going to pass in the c. id. get ID so what we after this now we're going to return the order so let's return and I'm just going to let's use this one this return we already have right here so let's return the saved order this one right here okay so this just all we need to do to save the order right to order for the user so what we
just did right here is that so we actually get we make use of all this um Helper method created below right here and then when after the order is been booked it's been saved after when the order is been ordered is been booked so to speak then we just clear the cut right here so clear the cut so in this in at this stage right now so every item inside the cart table is going to be cleared out then the is going to be moved from the cat item to the other table right so the
cut item and the cut table are going to be empty and every Item they are going to be moved to the other table that is being saved with the user ID so this just all we need to do to place an order so now now that gotten a user we're going to come down right here to create another method to actually get order to get the list of the user to get the list of orders for this particular for a particular user so here we're going to say public list so we have list of order
then we're going to say get order We just going to say get user orders and then we're going to pass in the user ID right here long user ID so we're just going to return return other other repository. get so we're going to um say get by user ID right here so get we say find by user ID actually we say find find by user ID and then I'm just going to pull this up to the interface level so we can actually get access to Itate from the controller so let's say override and let's pull
it up all right so now this just all we need to do to actually get all the orders that the user has gotten in the database okay so um remember last time we created a brand new um method right here this one right here so we're going to go this one is inside the cat service right so let's go to the cat service and actually get this implemented so here let's go here Yeah so let's implement the new method we just created so let's just get it implemented and this one we're just going to say
cut repository find find by user ID this one right here and then we're going to pass in the user ID all right so this just all we need to do to create the order for the user so now we have done creating the order we have done placing the order for the user so remember that when the order Is been placed then we're going to clear the card every item is going to move from the card to the order table right okay so now we have done with this implementation so let's move down to the
next task all right so now that we have we've done implementing the order service so in this video we're going to implement we're going to go back to the user service to start implementing it one after the other so the first thing we're Just going to do right here we're going to go to the interface the service interface and register all the methods we're going to use inside the service so the first we're going to do right here is to we're just going to say okay we're going to have a method to actually find a
user by ID so we to say get a user get user by by ID and then we're just going to pass in the ID of the user so we going to say user ID and uh let's do this input yeah so the next we're going to do right here let's have the one to actually create any user so just going to say user we say create create user and inside here we are not going to be working with this user model directly but rather we're going to create a new class that we're going to use
to actually request for this user creation so we're going to say um create user so we're going to call this class Create user request and we're just going to call it request we're going to create this class in a moment so the next one we're going to do here is the want to actually update the US user so we're going to say user update update user and also we're going to have a class that we're going to use to request for the user update so we're going to call this class user update Request and then
we're going to call it request as well and we're going to have the user user ID that is to be updated so we're going to say user ID to be updated right and the next one we're going to create is the one to delete the user as suggested by itell so I think these are just the four method we need we need to get right now we need to have the one to get user by ID to create any user to update user and to delete a user so now Let's create this class let's Cate
this classes so here we're just going to create the class and we're going to choose the package where we're going to create it in we're going to choose this request right here this request package I'm going to click on okay and click on okay and also we're just going to come right here and also create the update request so we're going to also choose this request and click on okay and okay so this just all we need to do right Here um so and so let's go back to let's go back to the service um
let's go back to this other class I think we have the user right here um let's find okay let's just just go back to the user and let's look at what we actually try to create with the user so this is the user this is the user class and these are the attributes of the user so at the time of this user creation we are not going to have this one and we're not going to have this one Right so the only thing we're just going to create for the user are this ones right here
the first name last name email and password and the ID is going to be automatically generated right so we just going to grab this ones right here and go back to this um create user request and just paste them here right so remove The annotation and just make it like this so then we're going to have the geta seter by using at data right right here so this just all we need to Do right here and also we're going to go to this user update right here so what are the things let's go back to
the user entity so what are the things we are going to allow the user to update so now we're not going to allow the user to update the email and also we're not going to allow the user to update the password so if the user wants to update their email or password that should be a special request that should be a special operation right but um the general Operation that that we just want don't want to restrict the user to the names so the user can just edit their names but we should restrict the user
from editing their email or password right and so now I'm just going to grab these two right here and let's go here and just put them in so let's put them in right here and then let's um also we're going to say let's just get the geta and set right here so this is just all the things we Need for now um as time goes on if the request change we're going to come back right here to actually get them included so let's close this right now and also let's close this one and um so
let's go back to the user service and just implement this interface so let's Implement uh sorry this is Implement actually so Implement our user service and let's just pull all the method right here okay um so now we're going to make this class a spring be so we're just Going to say this class is a service class and we're going to bring in The lumb annotation to actually do Constructor injection and then we're going to pull in the user um repository right here just as clearly suggested by intell we just going to P this in
right here so is correct so the most easiest one we're going to implement right here the first one we going to implement is this one right here so we just going to return The repository find by ID so just going to say find by idid and pass in the user ID and we're going to throw a specific re um um a specific message right here oh sorry go what am I doing so let's just go in here and have the Lambda function right here and then we're going to throw the new um new resource not
fund um exception right here so let's break it down and um just no there's no need of Doing this I'm just going to say us are not found as all so I'm just going to say user not found so say found so this is just what we want to do right here all right so the next thing we're going to do right here is to move that here to to do this one right to delete the user we are going to come back right here to the create and update but the first one we're going
to do right here because It's the easiest one this one is easy so we're just going to find the user by ID so we're just going to say sorry Repository find by ID so what we're do here is that so if we're just going to find the user by ID so if the user does exist then we're going to delete but if the user doesn't exist we're going to throw an exception message right so we just to say that if exist and also we're just going to take This one right here so we're just going
to call repository and we're going to Method reference to actually delete right and if that is not the case we're going to throw an error message so if user is not f so just going to say um through error message we going to come down right here to say through resource not found exception so if the user is not found we're going to throw this message but if the user is Found we're just going to delete it right here so this just all we need to do to get our user deleted so the next one
we're going to do right here is the one to actually let's go this update right here and also just as we did right here was also going to find the user to make sure the user does exist so if the user exists we're going to update but if this doesn't exist then we're going to also throw a message right so now just going to say return Repository user repository find by ID find by ID will pass in user ID and then we say map and U so we just to say existing user existing existing user
existing user and then we're just going to say we're just going to come down right here and extract we're going to exchange the attribute we say existing user do set set first name and we're going to get um request. get first name first Name and same way we're going to get the last name so these are just the two things we want to update for the user for now like I said if there's if the request change we're going to come back here to actually get it fixed right but for now these are just the
two request this are just the two things we want to update for the user and then after that we're just going to remove this one right here so if the user is found we're going to update the name but if the user Is not found we're going to throw a message so we're just going to say that um or S throw going to say or S throw this right here and we're just going to come in here to throw our custom um error message new resource not found exception and also going to say user not
found user not found user not found all right so this is just what we want to do um we having error somewhere we Having um error okay we are going to save first we've not saved so we're going to return return repository this one right here do save so we're going to save the ex user right okay so this just all we need to do to actually get our user updated so let's move to the creation of a new user all right so the next thing we're going to do right here is to just come
up here to Actually create the user so here we're going to say um return optional dot off um request uh we're going to say request. filter um actually we're just going to say okay if this if this user does not exist so we going to say user repository not user repository do exist by email right exist or this exist Exist By by email that just going to pass in the request. get Emil request. get email I'm going to break this down let's break this one down right here so now if this request if this user
does not exist then we're just going to come here to map so we going to say map we going to say um we going to call this one request again map request right here actually we just going to this one is RQ and we're just going to say um we're Going to create a new object of user right here so going to say user user equals new user they going to come right here to actually set user email and set the password set the first name and set the last name thank you to itj for
effortless coding now we're just going to return actually this one here we going to return um return rep sorry I made a mistake so I'm going to return what my cap log is on so we going to return Repository repository user repository. save so just going to save the user right and pass in the user then if that is not the case just going to throw an error you say RS through user not find exception all right so this just all we need to do right here to actually get our user created so the first
thing we do here is that we actually find the user by by email so if that email already exist then we just going to throw error Actually we're just going to come right here not this one is not us not found but Meanwhile we're going to we're going to say this one is already exist already exist exception this one right here so we're just going to I'm going to remove this having problem right here so then I'm just going to come here to say this email already exist so we're going to say request request do
get email right then we're going to add the string right here We going to say plus this concatenate already exist right here but meanwhile going to make it fancy let's just do some kind funny things right here I'm just going to do plus so I'm going to say oops so we to say oops this email already exist so this just all we need to do right here to actually get our user a new user created in our database so I think this is just all we need for now um if there are any additions we
Actually come back here to actually get them fixed so let's go back to the next let's move forward to the next task all right so now we have done with the implementation of the user service so the next thing we're going to do right here we're going to start implementing the controllers so I'm going to right click on this and close all tabs and then open up our project so let's go to the controller package so here the first controller we Going to implement is the order controller so let's write click on the pack on
the controller package and create a new class a new new controller and we're going to say cont um other controller so let's close this um I I just want to do something right here so let's go to this some of these controllers we've done in the past I'm just going to grab these three Annotations right here and just close it and just close here then I'm just going to come back here to dump just dump this three annotations right here and then we're going to change this name to ers ERS that's all okay um so
the first we're going to do right here is the one to actually create the order for the user so um let's bring in our other Service as a dependency but meanwhile I'm going to take the interface of the service so let's do the Import so let's say public response entity if you're response so here we're going to say create order so in here we're just going to have the user request sorry um the user ID so user ID for the request right and um so we're just going to call the order to actually place the
we're going to call the service to actually place the order so we're just going to say um order oh no this not what I want to do so we To say equal service place order and then we going to pass in the user ID so after that we're just going to return we're going to return a success story if it happens right so we just going to say um success. okay and new API response then we're going to create inside here we're going to create our success story so we're just going to say Um success
and we're just going to bring in the order right we just going to say order success order order success product order success item actually we're going to say item order success and I'm going to just put a kind of exclamation right here I'm just to pass in the order so that's just all we need to do right here but meanwhile let's be conscious of our exception so let's go to this order are we throwing Any exception from this class from this service is this service throwing any exception if anything happen so let's go there and
check so here this is the service where is the order this is the order service so let's open it up so let's go to the create this is the create order so here we actually um we are having this create no no no not this one right here so this one is the place order right so here we actually getting the card right here and here we are Getting the card by user ID um so this one right here if we go here I I don't think we actually throwing exceptions but um potentially there are
some exception maybe if the user is not found we can just throw exception from here if the user is not found but for this if I go here right now we see that we are not actually throwing any exception right here so we just get it directly so we didn't find the user first but we can just say okay let's find the user if the User does exist they we just get the cut but if the user does not exist we just th exception that user does not exist so you can actually add a condition
right here to actually throw the exception but for this for this demo we just we just want to assume that the user actually exist so we don't want to just um go that way to throw exceptions so now I just want to leave this right here and let's go back to the controller so it's now we see that we Are not actually throwing any exceptions right here so we're just going to leave this one like this but anyways you can still also throw any internal server error right so for example um if something does happen
you can just surr it here we try and cash because we're actually throwing all our exception directly from the controller we didn't create any Global exception Handler we are just trying to handle everything from the controller so you can just Actually create a a generic exception Handler so should anything happen you can just come here to okay return uh return resource um response entity dot so this one right here we can just say the status code and we just going to say internal error right we we can just come here to say HTTP HTTP do
status do status um do we have it right here so just going to say okay no no no sorry it's my bad so we just going to say HTTP Status right here sorry guys my brain just reset so here you're just going to say okay um do internet server error right here so this one right here actually if there's anything if there's anything actually if anything actually happened we just going to say body right here and actually come here to to through a customiz a custom customized message we just going to say e do e
do get exception right here get message rather so this one we just did right Here if anything happen internally we're just going to okay internal Ser error and actually get the message of what actually happened at the front end right if actually we want to throw exception but because I'm not we're not catching any exception because we didn't throw any exception from the from the from the service right so we just going to leave this one like this so let's just tentatively catch General exception here okay so with that being said we're just Going to
say this one here is a post mapping and we're just going to pass in the um the the end point as order so we to see orders SL order right here and it's a post mapping so the next one here we want to do is the want to actually get order by ID so let's let's come right here remember last time in the order service we actually had this is the create order right which is the place order and this one here is get order by ID so this is the one we're going to implement
right now so let's go back right here so going to say public response entity and uh we're going to pass in our API response as usual API response actually and then we going to say get order get order by let's make it clear get order by ID we going to pass in the order ID and then we just going to come here to say Order the order equals service. get order and we just going to pass in the other the order ID right there and just like we explain right here you can always throw General
exception about this one right here we actually throw exception you can see that we actually thr a message other not fun right here so we can just go ahead to catch to catch this one right here because we throw this one right here so we can catch resource not fund exception Right here at the controller so then we can just okay return return is success story if everything is found but if things are not well then return just catch the exception right here so for that now we're going to catch the resource not F exception
resource not found exception right here and then we're just going to return it so we going to say return response entity do resource not found say error occurred and anyways we're just going to Say we can just say oops I don't like writing too much grammar we say oops resource not found which means this message here we are getting is this one here this is the message we are getting so we just oops order not fun no order fund actually we're just going to say no order no order no order found let's just remove this
one right here so that's just what we are doing right here we're actually throwing this message right here by saying e. getet message that We're actually getting the message that we Define right in here so that is out of the way so the next one we're going to do here is the want to actually get order by user ID um so it's just going to be it's just going to follow the same way the same way with this one right here so for that uh but Meanwhile we're just going to let's say this one is
a get mapping so this a get mapping and um so in here we just going to say user um Actually going to say order ID right um this one is inside here so we're going to say order SL order ID actually then slash order so this are the end point we're going to we're going we're going to actually do right here so here we're going to say order so we're going to say this one is order ID and then we're going to say this one is a path variable so we're going to do this one
As a path variable and this one um is actually we're going to make this one right here a PA variable this one but meanwhile I just want to make this one a kind of at request param um at request param in case you don't know the difference between request param you don't know when to use request param in varable we're going to talk about that later in the course so but now just take it that this one is a request param and this one is a part Variable right here okay um so the next thing
we're going to do is actually the one to get the use to get orders for the user so this one just going to come here to say just going to copy this on the paste just going to say get order by user ID oh sorry just going to say get user get user orders get user orders right here and then then we're going to have um so inside here we're going to have the user ID right here so we just to say user ID and I'm going to make this user ID a path variable right
here so we're just going to come here to say user ID user ID user ID right here and we're just going to say orders so right here we also pass in user ID right in here and but meanwhile this one here is going to be a list because we actually getting the user might have more than one order in database so here we just we Just we just getting all the orders with the user ID which means in the database we're going to get every orders that has this user ID right here so it's going
to be a list not a single one right so for that we're going to make we're going to say this one is a list of orders so we just going to say this one right here we're going to set this one to list of orders right here so this one is list of order and then we're going to say user. getet get user orders right here so that Is just all we need to do so um this one also we just say user ID and the orders and this one is order ID then the order
right um so one thing we need to do right here we see that we are actually returning this one directly and if we go here there are some other things that may cause problem right here this is the order ID right so this one right here we have the user right here and we have the other status we have the other items right here so returning this one Directly it means we are returning everything that is in here and if you go to this user right here this user also has other dependencies this us is
also calling the order and the user is calling the card right so we are going to have recycle recy recycl dependencies right here where we are going to run this application so for that now we are going to convert we're going to create a dto for this order right here so we just going to select the attributes we are Going to return back to the user where we are getting all the order of the user so that we don't have uh problems by retrieving product retrieving images retrieving everything that you know this one has dependency
for example this one just like what we see uh this one has this a user and user has other dependencies so if we go right here to the card the cat also has a dependency so we're going to have recycl dependencies like that so for that we're Going to create um a a dto for this one right here so in our next video we're going to see how we're going to customize a dto to actually return properties we want to return for the user when an order is been requested so see you there in the
next video all right so in this video we're going to see how we're going to convert our order to the other D that we're going to return to the user um so if we actually go right here to the order Package that the order entity right here we're going to see that returning this order there are a lot of things we are going to be returning along because this order actually has dependencies that is going to pull along for example the order is going to pull along this order it items and it's going to pull
along the user and in turn if we go inside this other item we see that this other item in turns has its own dependencies that is going to pull along so for Example the other item has a product has product that's going to pull along and also the order is also right in here right and uh so what we're just going to do here we're going to create a dto instead of returning coming right here to the controller so in instead of returning this order directly we are going to return it so remember that a
dto gives us the opportunity or the chance to actually select the attributes that we Wish to return for the for the to the color right um so now let's go right here so inside this dto package I'm going to right click then we're going to create a class so this class we're going to call it order dto so what are the things we actually need in this order D so we're just going to bring in some of these attributes we want to return to the user right here so here I'm going to bring this in
here and Then take note of this one right here that we're actually going to create other item Dil because this order right here this um this other entity right here is actually coming along with this item this other item and this other item is also coming with other dependencies and we not we don't want to return all these dependencies we just want to select what we are going to pull along with the product because actually other item is actually a product right so that Just we just to specify the information we going to take from
the product and some of the information we're going to take from here so now we're just going to come back right here let me let me um close this a little bit so we're going to create another um dto right here this other item dto in the same package which is the dto package right here so I'm just going to click on okay all right so inside this item dto this um Other item dto we just want to come here to say we want to return some few attributes right here but meanwhile I'm going to
rename this I'm going to refactor I just want to make it I want to make this one like um um dto lower lower case and hit on enter did it work let's check it out okay right it works okay so here we're just going to return the product ID the product name the quantity and and the Price because this one right here that's the other item actually um represent a single product right so we're just going to return the product ID the name the quantity of the product and the price right here and then let's
come here to say that we are going to say this one we're going to get the data and seta from this by using the data annotation and then let's close this other items and now let's come back right here and also we're going to have this one as Data annotation for us to get the geta and seta right here and now having done this now we're going to come back to we're going to come back to this service right here and the first thing we're going to do here we're going to bring in the model
mapper um the model dependency so going to say private fin now we're just going to have this model Mapp right here as a dependency and then we're going to come down right here to create a um um a Service that will actually help us to do the conversion so I'm going to we're going to have a private um method right here which is a helper method right here and this we're going to say order dto order D this one right here and then we're just going to say convert convert to dto say convert to D
yeah and then we're just going to pass In the single order and going to convert this one right here to dto so this is the helper method that is going to help us to actually convert to dto so now the first thing we're going to do here so this is the one that we actually getting um getting the order by order ID right here so but meanwhile I'm going to write click and close everything so that our work space will be clear and then let's open let's just have this one right here this one and
the service so let's have The service this other service what is it this one right here so are just the three we need right now and also I think we need the controller as well I should have closed everything so let's go to the controller okay this one right here yeah um so now this one this one is the method that is actually getting um the order by ID right here and you see that we actually returning this order this um the raw order right here but now we are of returning this raw order right
here We're going to say we're going to return the other GTO right here and let's do the UT yeah let's do the import okay right here and then after this now we're going to go in here see here we having an error right now so we're going to go inside the order service so we're going to actually return instead of returning this order right here so we're going to return the order Dto right here to return the order dto right here and then we're going to now the contract has be broken so we're going to
go right here to the service we're going to go here to the service to actually return this dto right here dto right here right so let's do this import let's import it all right so coming back to the service then we're going to do the conversion so instead of returning this I'm just going to remove all these ones for now I'm going remove all the for now and then we just want to say okay we want to return the repository find by ID so we're going to pass in the other ID and then we're going
to map so we just going to map it here and then we're going to say this so we're going to make use of the method we created down below so we just going to say convert to dto Then we just going to say r s through oh sorry this one is map sorry guys yeah so here we just going to say RS through um this one right here but meanwhile let's break this down let's break it down let's breing it down here so we're just going to say Throw no um what say resource not found
right here so we say order not found so that one actually settles this one right here so here we actually returning the Dto instead of the order the raw order so the next we're going to do right here is this list of this list right here so we're actually getting a list of order based on the user ID right here so here of returning the list of order I'm going to return the list of other detto right here right and then let's go back to the service so right here this is the this is the
method that that is actually getting the list of orders for a user right so the first we going to do right Here so instead of returning this instead of returning um returning order we're going to return the order d right here and then let's go to the service so actually the contract has been broken so let's go to the service to fix the contract so so here um this is the order we're going to say order dto right here so we return the other list of other dtos and here let's go back to the service
so coming back here I'm just going to Let's remove this one for now And I'm going to return none for a moment let's return none so in here we're going to have the list of orders so list of order list of order so we're going to call it on orders equals repository. find find by user ID is that correct find by user ID this right here and we just going to pass in user ID we have in there right here so then we're just Going to come here to return orders so I'm going to say
ERS do stream map then inside this map we're just going to call the ref we just just the method sorry we just want to Method reference this and going to call the convert convert to d right here and after that we're going to collect it to list so this just all we need to do to convert the list of um the list of ERS to order dto right Here so using this private method right here and then we're going to let's go back right here to our controller this other controller here so you see here
there's no problem there's no noise right here because we actually returning a list of um other dto right here that are been that that are saved with the user ID right here so this just all we need to do to convert our order to order dto that we can actually return back to the User at the front end um so for now this is just all we need to do so if there's anything to be corrected we surely come back here to correct it when we're going to test our application so I'll see you in
the next video all right so now we have done with the implementation of the other controller so in this video we're going to start implementing um the user controller so I'm just going to right click as usual but meanwhile before I Right click this I'm going to go to the controller and grab these three annotations right here just to grab this three and then I'm going to right click and close all these tabs so let's open up our project and move to the controller so right here we're going to create a controller called user controller
so this one is a user controller and um so let's close this so here I'm just going to drop These three annotations right here and then I'm going to change this one to users that's all and then here we're going to need the dependency which is the user service that's correct this one but Meanwhile we're going to take the interface of the user service right so let's do the import yeah so the first end point we're going to create right here is the one to actually um get the user by ID so I say public
response Entity and we going have the API response and we going to say get user get user by by ID and we to pass in user ID um so just going to say user and then we just going to return the success story let's return the success story if yeah and we're going to say do okay and uh so we just going to say success and uh we're just going To um say user yeah just say success success is okay but if the user is not found remember we we are we are throwing a message
right we are throwing user not found exception so if the user is not found this one right here let's go to the service remember right here we are not here let's go to the Implement um right here I just want to show you so remember right right so remember we actually getting this one right here we are throwing a um we are Throwing exception right here we say that if the user is not found we should throw this message so now let's come here to catch this message so just going just to highlight and S
we try and catch and let's throw resource let's catch resource not F exception yeah and let's return it to the front end so say return rest response entity yeah do Status and we're going to say not found not found and body and so we're going to have e. get message right so we're going to see e do e. get message and then we going to return now none and we just going to terminate so let's do this static input all right so this just all we need to do so this one we're going to say
this one is a get request so let's say Get my ping and let's just pass in the user ID we say user I sorry this the path variable it's going to be a PA variable right so let's say user ID and let's say user so let's come here to say this one is a path variable sorry say path path variable all right so this just all we need to do but meanwhile let's let's balance this and uh let's Balance it right here okay so that's that so the next we're going to do here is the
one to to create the new user right so let's create new user to public response entity get user by email we are not actually getting user by email we just want to create user and so last time we created what is what was the name was it create user um let's go to let's go to this request right here we created a class Last time when we implemented the service layer okay no this is okay here so we have create user request right so that is the class we're going to use so let's come here
and say create user request create user request this one right here and we're just going to say request and then we just going to request the body at request body all Right so then we're just going to say user call user service create user then we to return the success story that's user create user success we going to say create user create user success and we're just going to put exclamation mark right here okay and um so so you can see that here uh we we're actually coming to okay we we're still going to um
remember that here we are we are we are throwing a message right here we said that if the User is already in existant right here we are throwing already exist exception so if this user already in existance we're just going to thr okay this user is already in our database right so that is what we're doing here so we're going to catch this exception right here so that we can actually throw the message for the user to see that this us you try to create is already inexistant in the database so we're just going to
catch already exist exception because that was What that was what we we throw here that that is the exception we are throwing right here right so we just going to come here to catch the same exception right here already exist exception then we just going to return the message we defined at the service layer to the front end so we just to return response entity um no suggestion okay let's go response entity dot um so this time around we're going To say conflict this conflict actually let's say conflict HTTP conflict right and body so we're
just going to get the message will def find at the front end at the back at the service layer this message right here so we oops this IM already exist in the database right so that is the message we're actually getting right here so this one is going to be a post request and we are just going to say Slash add that's all all right so the next one we're going to create right here is the one to update the user the response entity. update user and this one we're going to also take the ID
of this user we say long user ID user ID right so this one is the class we created last time called user update request that is what we're just using here right now equals user. update user and then We're just going to return the success story if everything goes well right but if there's a problem because remember that we actually finding the user before the update right here if you go to the update user here you see that before the user is updated we actually get the US and we are throwing a message right here
so if the user is not found we going to throw this message so let's just come here to catch the error not actually the error it's an exception the difference Between error and exception so let's go here to actually catch the exception right here so we're going to have um this one right here and let's return let's return in case you are wondering the difference between error and exception you can just Google that you know in in in software there's difference between error and Exception yeah okay so um here we have we have this now
um if everything goes well we're going to return this one but if there's a problem then we're going to return this one right here right so um we're going to say this one is a put request put request and so here we're going to just come here to say um we're going to get the user ID as a path variable right so we're going to say path variable what the user ID oh sorry this user ID actually and then we're going to say update that just all need to do user ID is it correct so
why we having problem right here okay yeah so this we want to say this one is a path variable yeah part variable so that solve the problem so the last one I'm going to do here is the want to delete the user this one is a very straightforward one just to say public delete User um actually we're still going to have the response entity so let's say response entity [Music] um I'm getting confused I'm thinking something is let's see okay let me just go straight let me just forget about that so right here we're just
going to have the user ID as a path variable then we're just going to go in here to just call it service to actually delete the User just call the service sorry um what is the name user service right yeah and then we're going to return success story so user deleted successfully we don't need to return any ID just return none all right and if the user is not found as usual we're still going to return resource not found exception so resource not found oh sorry This is resource not found not for exception and we're
just going to return so just going to return get message and this one we're going to say it's a it's a delete mapping so we're going to say add delete mapping and here we're going to get the user ID as a path variable then we going to say slash delete so here let's say user ID all right so this just all we need to do right here so um now we have finished implementation of this of this controller then in in our next video we're going to create the user dto to actually return to convert
this user from this user object to the user D so that's what we're going to do in our next video so see you there all right so in this video we're going to take a closer look at our user controller um so right here this is the User controller that we have implemented in in our previous video right and uh right here we can see that in some of this end point we are actually returning the user back to the front end which is back to the user and here we can see that we're actually
returning the user model directly to the user right and returning this model directly to the user it has a lot of implication because if we look right here this is the user this the user entity directly this is The table rightly in the database and this um entity right here this class right here actually consist of other dependencies so for example we see that this class consist of this cut dependency and also it has the order dependency so if we if for example if we if we getting this user right now the user is is
is going to get this card right here and getting this card right now this let's go here to see what is inside the card so this is the card Right here so this card has card items which is product and this card also refer to the user right so which means getting the card the card is trying to also get the user and get this card items along and now if we go inside this card items we're going to see that the items item this card item it turns has other dependes the cart items also
has product and this this the the cart item also has the reference to the cart right here as well and also getting this card Item this card item is going to make attempt to get the product and and this one but meanwhile we have we have break the loop between the cat item to this cat already so the cat item is not going to get this one anymore right but the cat item actually needs some information from this product right here for example we need a brand we need a name and we need some information
right here and coming here you see that this product is in turns has some other dependencies Right here to to pull along right so what I'm trying to say here is that so getting the user right here there are lot of chains of dependencies that we're going to be pulling along and this one might call um recycle dependency or maybe stack Overflow as time goes on because they are going to be dragging each other and we're not going to get a good result so that is where the dto that we have been talking about that
we have been using so that is the role of The dto in software development so in this case right here we're just going to have a user dto and inside the dto we're going to specify the attributes we want to return to the user right inside the dto instead of returning this raw user to back to the user so what we're just going to do right here is to we're going to go here we're going to go inside this dto package and right click and then we're going to create a new class we're going to
call this class user Dto so this one is the user dto class Here and Now what actually do we need what are we going to return in the user D so we just need the basic uh the basic information of the user to return right so for example we might need the name of the user the ID and so on and so forth so I'm just going to paste all these things right here now apart from this apart from this information right here the we need to return the the we need to see that if
the user has an order in the Database we need to return the order and if the user has a cart we need to return the cart and if the user has an address we need to return the address as well so even though we have not um we have not here we see that we have not gotten we have not work we have not gotten address right we have not bring in anything address right here but in a noral noral situation the user supposed to have an address right of which we're going to include later
so but for now let's work With the one we have right now so getting the user we're going to have the card of the user and the Order of the user so now if I just come right here to say private so if I just say and meanwhile the user can actually have the user can have more than one order right so the user can have a list of order so let's say this is a list and we're going to say order right um this one is order right here so the user can have a
List of order just going to call this one orders right here right and meanwhile this order right here we working with just like we shown this orderers actually has other things to pull along so because of that now we're not going to use this order we're not going to return this raw order to the user but we're going to return the order D to the user like so now if we go to the order dto let's go right here so here we have The order dto and these are the things we are actually returning to
the user from the other dto so these are the things that we actually returning back to the user and now if we go right here let's go to the user d right here so now we having this user other d right here and also we are going to need the card for the user for example the user you know a user can can have some orders and also can have some some product left in the cart waiting to Be ordered right so which means we still going to to check if the user actually has order
in the database that we're just going to pull the order back to the um um um along with the user right so here we're just going to have the card because a user can only have one card a user cannot have two cards right so one card to one user so we're just going to have the cut and just going to have this cut right here as a dependency so here um we're just going to do this one the Input but meanwhile again we are not going to return the raw card because returning this raw
card to the user is going to pull along other things right here right so we are going to create a cut dto that we going to return along to the user so here I'm just going to say cut dto right here and then let's create this dto class for the user that's for the dto class right here cut dto and inside this package of dto we're going to create this class so let's say enter And the next we're going to do right here we're going to come to this cut D to actually select what we
actually going to return to the user from this cart so now inside this c d what we're just going to return right here we're just going to return the cart ID and actually the cat actually consists of cat items which is the product right here right so we're going to return the cat ID and the cat item dto because we cannot return the cart because here we Cannot return directly this cart um this cart item directly so we're going to return the card item dto um so here let's just say we're going to create this
class right here inside dto package so now just look at the chain of what we are returning right now so here this is the cut right we are returning the cart dto and the cart has the cart ID and the the total price that is in the cart and also the cart is returning the product that is that are inside right and this Product are are found inside this cart dto right here this cart item now let if we go back to the original cart item let's go right here so we going to see that
this original this cart item right here actually consist of products and the cart so this cart item if we if we return the original card item it's going to pull along some other things right here so we are not going to return the original one so that is where we we are creating we are returning the cut item Dto right here so that we can have the chance to to select what we are going to return as the product right so right here we also want to select what we are going to return to the
user from the dto right here and remember that the cut the cut item is actually a product right so what we just need right here we're going to need the cut the item id the quantity and the price and also we are going to need the product right here so here we just going To say private and here are sorry um so this a private and we're going to say when actually need the product here and meanwhile we just need a few information because a cut item represent product right so we're just going to say
we need product right here because we actually need the product information so we're going to say product uh sorry this product and we're going to say product right here oh sorry why Um product oh sorry typ right here so we're going to have product right here and let me remove this T so here we have product and also if we return the raw this if we return this raw product we still have problems right so if we go right here we see that this product also has other things for example it's going to return the
photo right and we don't really need some of all these things that we're going to return so what we're just going To do right here let's go back to um I've opened too many too many um too many things right here let me close some of these things so here this cut item instead of returning this product we're going to return the product detail so that we can have the chance to select what we want to return from the product because we are not returning everything in the product right so here let's go to this
product dto so this is the product dto We we we created last time so which means these are the things we are going to return from the product right so that is just what we're going to do that is what what we're trying to do do right here so here what we're just going to do right here let me just I'm just going to grab this one I'm going to take this one right here and I'm going to close this then we are still going to have all this in all the in all the dtos
we have created right uh let me just do so it Will get imported let me do it from the scratch so it will get imported so right here and also we need to have it here so we just need to have it here so what I'm just trying to do right here make sure we have geta sets as necessary um things in all the details we have created right so make sure you have this annotation or you can just specifically have the data set in all the dto we have created so now this is the
user dto we are returning to the user so we are Returning so when we request to get the user so we're going to return the user information which is the name the last name the email notice that we are not actually returning the password right here right because that is secret then we are returning the orders if the user has orders in in the database we're just going to return the list of orders but if the user has no ERS we're going to return an empty list right and also we are returning the cut of
the user so if The user has cut then we're going to return it in the in the database right so these are just the things we want to return as the user dto so having done this right now we're going to go to the user this one right here so right here in the user service class what we just need to do right here I'm going to bring in the model MPP dependency so I'm going to say private final OKAY model mapper right here and now this is the this is the this is the Um
this is the method that is finding the user by ID right that is getting the user by ID so what I'm just going to do right here is to um but meanwhile I'm just going to come down right here we're going to create a new method to actually convert the user to the dto to the user dto right so here we're going to have public um yeah we're going to say public and then we're going to call the we're going to call the user dto this is user dto yeah user dto so We're going to
say get user by ID we just going to say convert convert user to dto convert user to dto this one is not correct so we having we we omitting or right here so we're going to say convert user to dto and here we're going to have the user object right just going to have the user object right here user and so we're just going to return so just going to return mod convert this User to dto right and then we're just going to come right here and where is it so this one that is returning
this user by ID we're just going to come here to say okay uh but because this one right here we might call it we might use it somewhere else right so for that we are not going to we are not going to temper with it but meanwhile we are going to use this one separately at the front end so I'm just going to say I'm going to pull this one up to the the interface Level and then let's pull it up so that we can get access to itate from the controller right so we're just
going to pull this one up and then after getting this what we just need to do right here is to go back to the user controller so instead of returning the raw user right here we're just going to say okay and after getting the user we're going to convert it to to user D so here we just going to say user oh sorry oh my gosh so we're going To say user dto right here and so we just going to say equal um equals service the user service actually do convert to dto and then we're
going to pass in we're going to pass in the user so let's pass in the user right here so that's just all we need to do then instead of instead of returning the user I'm going to return the user D to the front end so currently now this user D actually contains everything we need to Return to the user right so that's just what we're doing so the same way I'm just going to let's let's um let's terminate this and let's go right here just going to copy this one so the same way I'm going
to come here and convert it and then just going to also come there are three so this one and this one also we just going to convert this one too right so now let's actually return the dto here and and also here Yeah so this just all we need to do to convert our user to the user D so um now we have done so we have G we have done so many things right here and we have gone so far so in our next video we're going to run this application and see the update
to what we have done so far to make sure that our system actually runs to this point so far so um see you in the next video all right so now that we have done with the implementation of our Controllers so in this video we're going to run our project to see to make sure that our project actually work up to this stage um so what we're just going to do here the first thing we're going to do is to check let's check our properties to make sure we we have our data preserved in database
so here let's make sure this one is on update right here so we not lost the data we got in the database so now I'm just going to run this project and see what we got so Far um we have some errors somewhere we we have some errors here um so what could be the problem syntax error my SQL workbench okay check syn error near here um anyways let's just go to our SQ workbench and see what we got in there this is the these are the products we gotten in our database already so let's
refresh okay right here we still have it so let's come here to refresh our let's refresh our system so now we see that we have a new table Right here called user right here uh but the order the other table did not create because okay we have the other item created here but the other table did not create because we have an error somewhere so let's go and check what actually happened with the other table because this one right here is the other item where we have the price the ID the price the quantity the
order ID and the product ID but the order the table order is not showing the other table actually So um right here we see that we have error right here I'm just going to go off screen so that um I will come back to communicate with you guys when I get it solved all right so um I got this working but um um what actually happened was that you know we having error regarding to this um the name of this table right here because right here we have the name let me zoom in in a
little bit so you can see so here we have this Name this this name was order right and here we're having order ID and Order date so the error was between this area right here but what I just did because it's a kind of technical error that is really difficult to figure out but what I just did was that I have to use this tag right here thise um this annotations right here to actually specify the table name so I specify the table name here to be tables H sorry the orders because by default this
one is going to be table in A datab like sorry this the table is going to be is going to be name order in a lower case in database by default if we if we don't specify any name right here if we don't specify the table name right here so by default jpa is going to create a table in the database called order and if you look right here we're having this order right here Order ID and Order date and also we are using this order somewhere so there was a conflict between the the attributes
and The name of the table right so that is why I just try to differentiate the name of the table so after doing this so after doing this now we see that everything run successfully so here we can see that the table the T the other table has been created successfully and here we can see the enumeration which is the um the order status everything has been created right here so we have canel delivered pending and so on and so forth right here so um now let's go back to Let's go back to my SQ
workbench and refresh so that we see our update so here I'm just going to refresh and now we see that the the order table has been created which is called orders right here so if we go here we see the order ID the date the status the total amount and the user ID right and these are the information this is the information we actually want from this table and now if we go back to the other item right here which consist of the items that we Ordered from that that we ordered so we see here
we have the the ID we have the price of the item remember that every other item is a product right so this one is the unit price of the product and this one is the quantity of the product this one is the order ID and this one is the product ID right um so this order ID right here is this ID that we are getting right here so which means the item the items that are that that Are named or that are registered with this ID is what we're going to they are the ones that
are going to be reflected right here on this other table right here um so so far so good we have gotten our table created and um so before we test this project I just want us to see the this the structure of our of our database right now you know earlier on when we created three tables this was our structure right this when we have product image and the category but now We've gotten much tables already so I want us to generate the schema again so that we can see the structure of our table right
now um so having said that I just want to let's go back right here so hav said that I just want to go back here to database and we're going to go to R engineering ref engineer actually we'll click on this and then we just click on next so right here we're going to click on next again and then we're going to select our database so let's Let's unselect this one right here because that's not the one we're using our database our schema is dream shops DB right that's our schema we're using right here so
now we're going to click on next and then click on next so right here we're going to see that we have eight total objects which means we have gotten eight tables in our database right so let's click on execute so I'm clicking on this execute right here then let's click on next and Let's click on finish so right here we got um we've got eight tables right here in our database but meanwhile I'm going to go off screen to arrange these tables the way they supposed to be then we're going to come back all right
so um currently this is the structure of our database these are the tables we've gotten in our database right and right here we see the product that we've gotten last time the product and image and the category um now we've gotten 1 2 3 4 five five more tables to add to these three tables we've gotten um the initial State we run this project so we see that the user actually has relationship with the order and the cat and the cat has relationship with the items and cut items actually are products just like the way
um other items are products cut items are product now between these two here we have other items and the others and the other items right here you see that exactly they Exactly the same thing with the cat and Cat item right here now what the difference between this is that a user can actually have a cat a user can go to the website can go to the shop and pick some product inside the cat but they might not order at that moment right so when they log out of the system what they keep in the
cat will still remain in the card so when they come back the next time they should be able to see what they keep the product they keep in The cart right and so in that case they going to the cat is going to be persisting is going to be there until they come back and on the other hand when this product when the user or place an order the product in this cart are going to be cleared off to this order right here but in case the user plac an order and left some of the
product inside the cart which means the card also is still going to be there until the user clear off all the all the items Inside because a user can can pick maybe like six products right and order for two at the moment and the the the rest of them will still remain in the card right so in that case we just want to make sure the item that the user keep in the card are still there until they come back the next time so this just the two right here so this one here is to
keep the the cut items and this one here is to show the orders of the user so when we get the user if there is any order That the user has has purchased in the past or currently we're just going to see them all right here So currently this is what we just have this is the structure of our table right now right so um in our next video we're going to go to postman to actually test this out to see how it works so see you in the next video all right so in this
video we're going to test our project to see how it works up to this point um so in our Previous video we have run our project and we see that we have gotten additional tables created for us such as the user the order the order item card and cut items right and so so right now we just going to go back to inj and I have cre let me take you back to to inside our project so here I have created a new package right here called data right here in the main package I created
a new package called data right here and inside this package I've Created a new class called data initializer right and if we open up this data initializer you're going to see that this data initializer actually implements the application listener interface and then we pass in the par we pass in the application ready event class as a parameter right here and in here we have we have um we have over reading we have override the a method right here called on application event right so pass in The application ready right here so in here we have
we are running we are running a method right here inside this application event method right and then below right here we have defined a class or sorry a method to actually create five users for us when the application is run so we're going to have default users in our database so that we can actually test without wasting much time and um so which mean this class you see that this class is a is a is um it's a Being it's a component so it's a spring beIN and here we actually doing Constructor injection right here
using the user repository um so what we just doing right here is that when this project is run when our project is ready everything is run and all the dependencies have been fixed the project is ready then this class is going to be is going to be called so and when this class is called this this method here is going to run And when this method runs it's going to create it's going to create this user this default users in our database so that we can have user to test with so before we go on
let's quickly let's run our project and see and right here we see that we have gotten um we have gotten some queries right here on the screen so let's go back to let's go back to my SQ workbench and we just going to refresh let's refresh and let's go to the user right Here and here we have five users created for US 1 2 3 4 five default users created for us with user one user two user two 3 four and five and the password right here to6 right so these are the default users that
we're going to be testing with and now let's go back to the cart so right here you see that our cart has been modified now you know the previous one we run there was no there was no user ID here but because we have created we have in we have Initiated the user API so now we have gotten the user ID attached user ID as a foreign key inside the card table right and also if we go to our card item we're going to see that actually we have the cat ID right here previously right
but the changes right here is that we now we've gotten a user right here which means we can find a card by the user ID right and when the when this user is is is requested when the user is retrieved so we can easily retrieve the card using The user ID right okay so um now let's go back to and also if we go to our order let's go to our order right here so here we see that we still have the user ID in our order so we can actually find we actually retrieve every
orders right here um by querying with the user ID right right and now let's go back to intelligent so there's something I want to show you right here so now before we I'm just going to now I'm going to close this class now I'm going to close this File right here now let's go back to the card item controller right here so we see that when adding a cart when adding an item to the cart we we are actually requesting the cart ID right here and in our previous video because we have not gotten the
user so we actually manually um implementing we are manually generating a cat ID so that we can actually have a cat ID to to add data and if we go here if we go to this cat service right here so last time you see That we actually generating the cat ID manually from here right manually from here and we are initializing a new cat when the user itend to add a new item to a cat so we are generating a new cat right here so now that we have gotten a user remember here we we've
implemented a method to actually find a cat by user ID right so what we're just going to do in this video before we test we are going to modify this this um implementation we did right here so we Are going to actually pass in the user so in this case when the when the when the the client or when the customer wants to add a user want to add an item to the card so we are going to check if there's a user right so if there's a user we're going to find the Repository if
there's a user then we're going to query the card using the user ID to actually get the card for the user so if the user gotten a cat in in the database the cat is going to be retrieved for the User to add item but if the user has not gotten a cat then we're going to initialize a new card for the user using the user that we just passed in um this one we're doing right here is still manually because actually uh let's go back to the let's go back to the here so we
can see here that we actually getting this card initializing initialized right here right but in a real scenario in a real world scenario we need to check when the user um Attempts to add an item to the card we need to check the login uh context we need to check the authentication context if there's a user that is logged in so if a user is logged in then we're going to check the user take the user ID to actually um do this process to find the card repository to check Che if that user already has
a card in the repository in the database but if the user has not gotten a card then going to create a new one but because we have not Gotten um security system integrated to our project so now we just want to get the we just want to add the user ID right here to actually get this one initialized for us when the user wants to add an item to a cat right um so we're just going to do it manually right now until because in in in our in our future videos we're going to actually
implement the user um the user security where we're going to have the user to actually log in then we're going to Actually use the normal way to get the card but for now we are going to get it manually for the user so um having said that right now the first we're going to do right here we are going to we are going to come right here to this we are going to come here to this um cut item controller and then we are going to modify this um this end point right here all right
so now we're going to start implementing this endo so the first we're going to do right here we're going To remove this one here and I'm just going to come here to remove this if statement right here so the first thing we going to do right here is to actually check is to find the user is to check is to get the user and then use the user to query the cut repository to check if that user got an A cat in a database so if there's a cat we going to retrieve it just as
we've discussed before so I'm just going to come right here to say user user equals so here we're going to Come up right here to get the user repository dependency to say final uh want to say I user rep I user service this one right here I don't know my Auto Import is not importing come on okay it has many right okay so um here we just want to say user service user service. getet user by ID and we're going to manually pass one user ID us user with ID number one because right here in
our database we Actually have a user with ID number one number two number three to number five right so now we just going to check we just going to we just we just want to see that the user with ID number one wants to add an item to the card so we are quaring our database with user number one right so then this one is actually a long value so we're going to come here to add L to come it down right okay um so how done this right now we're just going to come here
to get a new Card create a card so we're going to say okay uh why what happening right here okay so then we're going to pass in this user right here for the card initialization right here right and then let's go back to our cat item service or maybe our cat service because this one is actually the cat service so let's go right here so here we have this is the method that is actually that in that is initializing the new card so what we just going to do right to do right here Is to
clear off we're going to modify this method so now we're going to change this one from long to cut and then we're going to pass in the user object right in there right we going to say user we just pass in user object right here then what we're just going to do we're going to use this method that we've already created right here to actually query the cut repository to check if this user already got a cut in database just as we Explained earlier so now I'm just going to return return optional this one is
going to be optional actually because um in case the cut does not exist so we're going to say optional off nullable then we're going to say get cut by user ID right here then we're just going to pass in at user do get ID then we're just going to come right here okay if the cat does not exist then we're going to create a new cat right We're going to say or SSE or S get and in here we're just going to we're just going to say okay if the user does not exist then we're
going to we're going to create a new card for the user that's if the card does not exist if the user has not gotten a card then want to create a new card for the user so we just going to come here to just break it down right here to create a new card then we're going to set this card to the User then we're going to save the card to the database right so we say card repository do save the card to the dat database and then we're just going to come here to terminate
so this just all we need to do right here to actually initialize a new card for the user so now let's put this one because now the contract has changed so let's go back to this service let's just um let's just go back to the service and actually get it vertified let's go here and this one is The method so let's pass in a user instead so here we have a user and let's do the input yeah so just all we need to do right here to get our card initialized right okay so um here
we are having um initialize a cut return type um what is happening right here wait excuse me so here we said make initialize [Music] return okay um we are going to go Here I think we having a problem somewhere um this is the service let's go back to the service yeah right here we forgot to we forgot to change the return type right here um I think this settles the case okay cool this settles the case okay so now we have this is what we just need to do so let's go back to our controller
and see the error we having right here so here we are actually having required long provided card so what we're just Going to do right here is to get this cat. getet ID so we're going to call this one cat here and then we're going to say cat. getet id cat. getet id right here so this just all we need to do um so now let's test our application and see if we can actually um create a new card for the user and um add item to the cat so let's run our project so let
wa for a few moment while it Runs okay so now it has run so let's so we have seen all this before so what what we're just going to do right here let's go to postman so here in Postman we're going to open up our our project and um I think we have I forgot the name of our collection I think our collection is um okay it's e-commerce right this one right here and then let's open up this is the cut right here so we're going to add a new item to the card so let's
go Here to add a new item um So currently what we just have right here let's put this down so now we don't have this card ID anymore right here so we just have product ID and the quantity right so let's go let let's go back to int to my SQ workbench to check our product if we have so let's refresh so actually we have product ID from 1 to 5 okay and um also we have um okay yeah so let's go back to let's go back to here so then we're just Going to put
we have the product ID number one and quantity number three but meanwhile before we send this let's check our cat items so everything is empty right so let's go and add the product let's see what happens if it works or not okay here we have okay we have success item add add item success so let's go back here to our cat and refresh okay so now we have a cat with ID number two and we have a product this Product we we have three we pick three we pick um a product with ID number one
and we say we need three of this product which is the quantity is three and 4 * three we get 12200 right so now let's go to the card let's refresh the card so right here in the card we see that we have a user with ID number one which is the user we set right so now this user with ID number one actually has a card already um so now let's go back here here uh let's let's save this One let's save it okay so now that we have gotten all this one right here
we have gotten the user has gotten a card so let's go and find a user by ID and let's see if the user will come out with the cat and and everything it has right so let's go back right here we are going to create right here we have not gotten a user before but now we we are going to get a user right so let's come here to get a user by ID so we just going to modify this one right Here so this one is going to be users sorry this is users slash
um let's go and check the end point so let's go to user controller let's open up the controller so have a user controller right here and then this is getting a user by ID get user by ID so have users user ID and user right so have users user ID is one then slash users oh sorry slash sorry this one say user SL user and then let's just close this I'm going to save this let's save This as so we're going to this time around we're going to create a new folder and we're going to
call this folder a user so let's say user and we're going to create and inside this folder we're going to say this one get user by ID get user by ID and we're going to save and so now let's get this user by ID as usual let's hit on send so here we don't have anything um I think okay let's close this I think My post man have started his problem again let's open it again please just bear with me for a few seconds while it opens again Postman keeps giving me problems since the last
update I did anyways I soon upgrade I'll upgrade soon okay um let's just allowed so now let's hit this one again and see the error we got uh so right here we see that we actually got this but it seems okay so we got 200 okay and now let's See the data that is returned so now we return success so this is the user information right here and the user has gotten a cat you see here the user has gotten a cut now the cut ID of the user is one is two then inside the
cart there's an item right here so we have item the item ID um this the cart Item ID is number four then the quantity is three then this then this is the sorry this the cat item right because the product is actually inside the cat item So the cat item is number four that the quantity inside that cat item is three then the unit price is is 400 right now this is the product information inside the card item so the product ID is one the name is TV the brand is Apple just like that and
the category is one and it has no images then the total price of the card is 1,200 right here so we can see that we we have a complex data actually return for us by just getting a user right by ID which means when the User logs in the user is the user should be able to see all the card to see the product he has added to the card right here now the next we're going to do right here so let's as let's let's pretend that the user wants to wants to order right so
let's order the user um so for that we're just going to let's go let's go and check let's go to the other controller and see the endpoint point so this one is order create order right so this order actually needs the User ID so we have orderers slash order right and the user ID is a request param which is user number one which is user ID number one so now let's come here we have already saved this one already so let's come here to say orders so now this user want to this user want to
want to place order right so let's say order they will have and the user id/ order actually then user ID is a request param of which we're going to Come here to say user ID user ID and let's put one right here so this user this user with ID number one wants to place order now let's check the method the the the HTTP method of the order we see here that the HTTP method is post right so we TR try to save the order for the user so let's go back right here to postman and
um let's see let's change this one to post actually and before we hit on send I'm just going to save this one As so again um we're going to go back here because a new it's a new it's a new operation so we're going to create inside the e-commerce we're going to create a new folder and this time around we're going to call this one order right and we're going to save this oh no sorry we're going to create this and then we're going to say uh place order uh sorry I'm say place order place
order right then we're Going to save this one so now we're going to hit on send and let's see what we got so let's send so here we have um we have 200 okay let's see but here we are having let's stop we're having a recycling dependency right here um let's stop our server and figure out what is happening so let's go back to postman scroll up so here we have item order success and here we have the order ID the total amount and the order is pending right so this is the order items right
here um so this is the order the order items right here and this is the order inside the order items and um so the the order again is getting the other items then the other items is getting the order so we're going to break there's a loop right here between the other items and the order so right Here what we just actually try to do right now we're going we're going to go to the other items to stop calling the order because the order is the one calling it so it should call the order so
let's go back to the other items um let's go back to our entity class our model right here so here we have the order item and we have the order right so here this order is actually we are getting this order and this order right here is actually Calling this other item so we're going to stop this other item from calling the order so here this is the order right here so we're going to ignore this Let's ignore so let's say Json ignore actually Json ignore so so that the other shouldn't call this other item
shouldn't call this one right here but rather just what you need to call we might also break this one right here but meanwhile let's run it again and see so let's wait a few seconds while it Runs okay so now let's go back right here to to place order again so now we have placed order and let's see so now we see that we are still having stack Overflow so let's go back again and stop our server so this time around let's see the one we are going to break so now um the order is
calling the user right so the order is calling the user and the user is calling so here this is the order right the order item is NN for Now um then the the the user right here the other is calling the user and the user is calling the cat and the cat is calling so here you see the so the user is calling the cat the card is calling the user right so um there's something we need to do so let's go back to the card and user so why call it the item let's let's
go to the fundamental of calling this of ordering what we actually returning so when this order when this order is be Where is the order right here so this the order controller so when this order is being done you see right here we actually returning this order right here right and we are returning you see the the problem we are facing last time so if we go right here now so this is where the problem is coming from right so the order is actually calling all these and all these dependencies are being the chain of
these dependencies are be try they trying to pull along with the order So in and that actually brings in again um where is it this is the controller so that actually breaks again the idea of a dto so for us to stop all this instead of using Json to Json ignore to ignore all this we just want to create we just want to convert this order that is being returned to an order dto where we going to specify the things we need to return When after the order is been made so in our next video
we're going to create the order dto and solve the problem of this U recycling dependencies right here so see you in the next video all right so now we are going to convert this order here to the order detail and now let's remember that in some of our previous videos we have actually created the other dto and if we go back to the other dto we see that these are the things that we just want to return from the other dto right so all those chain that we are trying to break we actually we have
actually break Them right here by actually creating the other dto and selecting what we actually want right here and if you look at this um other item D we're going to see that we also select the things to actually return right here so returning the product ID the product name the quantity and the price so these are just the things we are returning right here we are not actually returning the main product that was causing the problem and also we are not returning the user that Was actually causing the problem right um so what we
just need to do here we're going to go back to the other controller so instead of returning this one right here we're just going to convert it to the other dto right and now let's go to the let's go to our service class and see the implementation of what we have done before so here we're going to see that we actually created a heper method that is actually doing the conversion for us right here um so the first thing I'm going to do I'm going to convert this one from private to public so we can
actually get access to it in other places right um so number two I'm going to pull this one to the interface level so we can get access to it from the controller through the interface right and then let's pull it up okay this one is done um so what we're just going to do right here you see that this one here is where we are placing the order Right so this where we placing the order right and we actually returning the order right here so we're going to leave this one like this but now we're
going to come here to the controller and we're going to convert the return order to the order dto so here we just going to say order dto um this right here but we're just going to remove this one and then we're going to call the service the other service to convert to GTO and then we're Going to pass in the order that we have returned from the back end right here so that's just all we need to do here and then we are going to return this one here instead and then we are going to
come back here so there's no point of using this one here we just going to remove this one here so we're just going to remove this one here because now we are not we are not returning this one directly but we are returning the detail Of this one right here of which we have selected some items to actually return right here right okay so um let's run our project let's go back to the controller so let's run our project and see what we got before we move to the next task so let's wait a few
let's wait a moment while it gets run okay so now the project is started so let's go back here to postman so right here in Postman so this is the Place order right but meanwhile I'm just going to let's add another let's do another adding let's add other um product to cat because this one actually has already placed but um it didn't come it has placed just the chain of dependency we are breaking so for the for the because this one has been placed already so the cat must have been empty so now let's add
a new product to the cat so here I'm just going to say we're going to add um three products to the Cat of the same ID number one and then let's hit on enter so here we have added a new item to the card so um let's go let's go back here so this one is the cut item let's refresh so here we have three items right three items right here of ID of em number one then the unit the unit price is 400 which make it 12200 just like before and if we go here
and refresh so we're going to get the same thing so now let's go back to postman and actually place this Order so now we are going to place this order this time around with user number one as well and uh so let's just place this order right here here okay so this time around we see that we didn't have that that uh chain of dependencies anymore but rather we just have this is the order we've placed with ID with the order ID number 10 so we have the user number one right here and so the
total amount in that order is 12 12 12 um 1200 and here the date of The order is August 2nd which is today and now here we have the items in that order is this so this is the item product ID number one and we have the quantity three three items which is we are ordering three TVs right and the the unit price is 400 but what I just want to do right here I want us to as well return the brand of this TV right here right so I'm going to go here to this
item and we're going to return the brand as Well um so let's go back here let's go back um here then we're going to go to this item dto this one let's go to this other dto and let's go to this other item DT so here I'm going to return the brand of the product right it doesn't matter but just for clarity purpose so I'm just going to return the brand right here and then let's let's run this Again let's run this again and currently if we uh okay it has started so currently let's go
back again so if we place this order right now we're going to see that we're going to get empty because uh there's nothing in the cart anymore because the order has been placed already so the cart must have been cleared by now so but meanwhile let's send this order so right here we see that nothing comes out we only have the A order date total item total amount is zero and there's no item because the card has been cleared already so let's add a new item to our card so the user wants to add other
items so this time around the user wants to add four items for Samsung TV to the cat and let's let's order so right here we have order success and now let's place this order again let's place it and here we have we have it back so this is the order so this time around we have the the the Quant the total price of the of the order is 1,00 is600 and we have the Samsung TV that's the Apple TV right here that is the of the importance of adding the brand so that we can actually
know the kind of TV we are ordering so here we have the Apple TV and the price is 400 and then we have four quantity of feet so 4 * 4 we have 16 right here which is the total amount so now we can see that we have broken that we have broken the loop of That but meanwhile um what I just want to do right now um let's try to get this user with ID number one let's run this again and here we have all the orders that the user have gotten in the database
right but right now we having a flick of orders but what I just want to do right now I want us to create this to create our schema from our Fresh So that we have fresh data to actually do this testing so for that we're just going to Go back let's go back to um intellig so I'm going to come here and um so let's go to our resource and let's create all our table from from our fresh so I'm going to say create right here and then let's um let's create this table let's run
this so that the whole table will be empty so that we're going to add fresh you see that all our table has been recreated you see here that all our Table has been recreated and our default user has been created so let's go back right here and refresh so we see every is empty let's refresh every it's empty right and I'm just going to right click and close all so now the only thing we have right now is the user we have the user but now we don't have product right um so like we did
before we're going to go to postman and add a few product right here and this time around I'm going to open Here and let's go to our product here and so let's post new product here so this is the product right so we're going to have we're going to add Samsung TV to our database so let's hit on this and um this one is not coming the same problem uh we have the same problem maybe okay maybe we've change okay last time I think we change this um let's see okay yes we changed this port
already so we Chang this port already so It's it's not 91 91 so let's post okay so now we have this order this are item being added so let's go back to here and refresh so that we have one item so I'm going to go off screen to add more products and so after that we're going to come back to continue our testing all right so coming back here you see that I've added a couple of products right here and um so also I have added images to this product right here so we see that
we have product ID One has two images product ID four has two images and product ID 2 has three images right here so these are the images is right here for the products and these are the products and here we have the user so our shop is ready for user to order right so now let's go to postman let's go and clear off Postman so I'm just going to save save this and close it then also save and close and also close this and place order save this to and close it and also I'm going
To save this and close it so let's clear our environment so what the first we're going to do right now now we're going to test our project from end to end right so the first thing we're going to do now we have we have products we have um we have um we have items that is product that we have users already so now let's go here so the first thing we're going to do right here is to add is to add product to cat so let's go to the cat right where is the cat okay
this one is The cat so let's add item to cat and now we're going to get a user with so here is the quantity right here so now we have users in the database already so um here we are still for remember that we are still having our user here manually but meanwhile before any other thing I want to change this one to update so that we don't lost our data we don't lose all the data we have in the database if we mistak run this project Again so just change this one to update and
so then let's close this and also I'm just going to write click on all this and close all these tabs so now uh the next thing I want us to do right here is to um let's go to this let's go to our controllers right here because now we're going to working with controllers so this our card item controller this is our cat controller and this is our user controller right so now we want to add item to the user so We want to make use of this one right here so we are still having
user ID number one right here that we are still add that we are still pulling in manually so now let's go back here let's go back to postman and now we're going to say um we're going to add a product with ID number one and we're going to take the quantity of we're going to take four of it right so let's send this and here we have add item success which means this User has gotten item right and also um going to add so let's change let's let's change this um ID so this this user
has gotten has gotten a cat has gotten items so let's log in let's assume we want to log in another user so let's have user with ID number four and then let's run it again please make sure you change your properties to update because if you don't change it as we run it right now all your product all your all your data In the database are going to be are going to lost so now we have gotten another user so let's go right here let's go to our cat So currently our cat has a user
with ID number one and it has total amount of 600 so let's go to the cart item so here this is the cart item with user ID number one right cart ID number one so now we are going to initiate a new user so let's say we want to add a new item to another user right remember we have Pulled in a user with ID number two so let's say we are going this user needs five of these items and this one is going to be be item number let's go right here now let's say
this user needs um a iPhone iPhone i iPhone 20 Pro Max right here so which is ID which is ID number three so here this one is going to this product ID is going to be number three right here and we need five of it we need five IPhone right here so now it's added so let's go back here and let's go back to our cat so here we have our cat now we have another user with ID number four and we have a cat with ID number two this been generated so if we go
to the cart item right here we're going to see the product we've added right so we have a product with ID number three and we have five of this product and the unit price is 2,000 and now we having 10,000 bucks of this product right and now the Item number three the product number three right here is iPhone is iPhone 20 Pro Max right here that is the item number three and item number one is Samsung TV right here so now let's say now that we have added now that we have added item to our
card so let's say that the user with ID number one wants to place an order right want to check out so let's go right here so now I'm going to save this one and so let's remove it so let's go back here and go to the Order so a user want wants to place an order so a user with ID number one wants to place an order so let's just place this order right here and now we have item order success so here we have the information of this user with it number one so this
is the date of ordering this is the total amount and this other is pending so these are the items in the card right here so here we have the the information of the of the of the item we are ordering and with the information of This right now so now that we have got to this let's go back to check the cat or with to check the cat if the cart still contain item for user number one so right here let's refresh the card so here this card still contain the order this order has not
been cleared we're going to fix this one in a moment but meanwhile let's go to this cart items and refresh so we can see that this cart item the the cart item with ID number one has been cleared it has been Ordered but meanwhile the information is still here but we going to fix this this is just a little thing we're going to fix it in a moment so now let's go to our order let's open our order table to see if any user has ordered so if we refresh right here so we can see
that a user with ID number one has ordered right on this date right here and let's go to the other items right here and let's refresh so we see that here we actually get the price the the the the Information for this product right here and for the order we get the accumulation of the total item of the total amount that is being ordered and the ordered ID right here and so now um this cut right here this order this order right here is supposed to be cleared out since this user has gone right but
we're going to fix that so now that this user ID number one has been has ordered already so let's go here let's go back I'm going to so now let's Close this for now so let's find a user with ID number one to see what we got so let's find this user right here with ID number one so right here we got a user with ID number one and this user this is this is the user information and user has got an order right here so the this this is the other information right here and
these are the other items so the user has gotten other he has ordered for uh Samsung TV four of it and the total Amount is 600 because one TV is 150 so 150 * 4 we got 600 right here and here the cut item right here is been cleared you see right here so the cart item is being cleared off because now the user has ordered so the cart has not the user has nothing in the cart anymore which is cat ID number one right so that is just what we so we can see that
everything is actually working as we expected right now and now uh the next thing we're going to do so let's find um let's find Um a user with ID number two right here so let's get a user with ID number two so now we got a user with ID number two here but now the user has not gotten an order right but the user has gotten a card right here I think user ID number two got has got a card right here user ID okay the user ID number two has has not gotten a card
so let's find a user with ID number four so user with ID number four here good so now we have a user with ID Number four right here and user with ID number four actually got a card with ID number two and this is the and this are the cut items right here and these are the product inside the cut items right here right and the user number four actually ordered for for iPhone Pro Max iPhone iPhone 20 Pro Max right which is 2001 and user ordered for how many the user ordered for how many
for five or Fe quantity is 5 5 * 2,000 that we get total amount of 10,000 bucks right here And so you can actually fine tune this one with your you can fune this one right here with your product dto to actually ulate some of this U some of this um some of this thing right here but meanwhile we actually um returning this just for testing purpose but you can fune this dto to select what you actually want to return now we have a user number one let's go back to user number one right here
let's find user number One so we have a user number one that has ordered for Samsung TV and ordered four of Samsung TV so now Samsung TV this product with ID number one supposed to because if we go back to our database we added 20 right it was 20 before let's go to this product right here so Samsung TV was 20 it was 15 but now I think we've ordered we four has been ordered so let's refresh and see what we got so now Four has been ordered the balance here is 11 right because right
here we actually subtract that when a user ordered we should subtract the total inventory from the amount that was ordered from the quantity that was ordered then this is the balance in our inventory so with this right now we can actually track how many orders we are we we have remaining in the database and we can actually return when this product is out of stock we can actually return the Information to the front end that this product is out of stock stock right and now we can see actually get the information to the front end
how many product we have in stock with this uh with this item how many of the this item we actually have remaining in stock so we can see that everything is actually working to an extent to what we have actually done right now right um so um the next thing we're going to do you can just take some time to actually test Everything we have done so far and um I'm going to see you in the next video video so see you there all right so now that we have done clearing the cat so the
next thing we're going to do right here let's go back to my Square walk bench and now let's refresh so we see that the card has been cleared except this one we have the previous one right so um now let's go back to our product and see the remaining items because um these are these are the items that we Have before and now let's refresh so now we have we see that this one has been some of them has been reduced right so we see that when we ordered they will be reduced so here um
let's go here what I just want to do right now I want to add so here we have um iPhone iPhone 20 Pro Max right so I I want to add let's add five quantity right here so that we have 15 in our database so let's go right here I'm going to this time around I'm going to Close let's close this one right here so let's go this item right here and let's add new product so right here we have um okay we're just going to have let's check the watches we have so I just
add that so here we have watches let say we have um five how many watches 12 watches right here so let's add eight watches so it will be 20 right so this one is item number that the ID here is number U number four right so this one here is an Update so this one calls for updates so now we're going to update this one right here but now let's try to add something like this if it we go because here we have the brand as apple and we have the name as watch right so
let's go right here so now let's just pretend that we want to add more quantity let's add it right here so it be 20 right here so um just going to hit on this let's click and so we see 200 okay right here so the product was added successfully so let's Go back to our database and let's refresh um so here we see that a new entry of the same product was entered right here so we have the same product here already so we're supposed to just uh we don't we don't supposed to we don't
supposed to add anymore we're supposed to update this one right here uh but now this addition we did supposed not to go through because this one is already in existant so we're having duplicate product entry right here so The next tax right now we are going to we are going to go back to inter and and guide against a duplicate like this so if this if this item is already in existant as it is then we're just going to say okay this item is already inexistant right so then we just have to update the item
and just add the quantity to this one right here right instead of having a new entry so let's go back to in and get this done so coming back to intell we're just going To let's go to the product service so we're going to go to product where is the product this one right here so let's go to the service so this one here is the product service and if we scroll down right here here we're going to so this is the method that is actually adding a new product right and um so right here
we just checking for the for the category right here but now we are going to have another helper method that is going to help us to validate to check The name at the brand if it already exist in the database right so for that now I'm just going to push down here and create another hyper method for us so let's say private bullan so it's going to be bullan so we're going to say product exist product exist exist right and then we're going to pass in the name of the product we say string name and
we have string the branch of the product so to say brand right Here and um so we're just going to go down right here and return so now we're going to create a new um custom query a custom jpa fer method to actually do this query for us so we going to say return product repository do exist uh this exists by so we're going to say Exist by name and brand and then we're going to pass in the name and we're going to pass in the Brand right here so let's create this product sorry let's
create this um method in our repository okay right here and then let's go back to our service this one right here so for we're just going to come up right here to say if products so we're going to say ifu exist then we're just going to pass in the request.get name request.get name and then we're going to get Request.get branch. get Branch now if product exists we're going to throw a message right so I'm going to say Throw new through new already exist exceptions and in here we going to say request request. get name uh
sorry to get bran we get the bran and um plus request. get Name get name this one is nam this time around and then we're going to say plus already exist so just do right here but meanwhile I'm going to give let give a kind of space right here let give it space kind of so this just all we need to do right here so um let's run this and actually test it out before we move on so just going to run it so let's wait a while while it runs all right so now our
product is Started so let's go back to postman and test it out so now this is the same product we're just going to send now remember that we have this product in our system in the database already so we just going to send it to see what's up so now let's send it to see um so right here we actually we got this one Apple watch already exist right so so we just have the brand that is already exist so if we go to our database we're going to see that actually Apple watch already Exists
right so let's refresh so we see that Apple watch already exist in our database so that I cannot submit right and let's go back again and check out something let's s it again and we have the same thing let's go back here and refresh um okay cool it didn't submit I was thinking it submitted it didn't submit these are the data we have already in the database so but now notice something right here that we are having 500 error because we are throwing Ital server error so now we're going to go back to it to
actually we're going to throw the actual exception right here so um let's go to the let's go to the product controller right here this one and this is the method let's zoom out so this is the method that is actually adding here where is it to add a new product where is it let's scroll up uh okay here so this one is adding new product right and if you look here we are actually catching um General Exception so now we're going to catch already exist I'm already already exist exception right here and then we just
going to say e. get message so here instead of server error we're going to say conflict this going to say conflict yeah this one right here and so that's just all we need to do but meanwhile if you just if you if you wish you can just also if you can include the General exception but now this is the the one that is more important we want just to tell the user right now so let's run this again and see what we got let's run it and uh let's wait for a while okay so now
the product started the project has started actually um so let's go right here so let's send this again to see what what happens okay cool so now in of instead of inter server error we have conflict right here because Apple watch already exist right so let's go back here and refresh and see so we see that it wasn't added anymore so because it already exists right um so in this case right now um the user instead of adding a new one is just going to update so so then um in that case we can just
come here to give because we have a separate method for the update right so we can just come here to say so instead of just in of just throwing this message right here we're Going to say you may update you you may you may update product instead update this product you may update this product instead so we just going to give a kind of information to the user so instead of adding a new one we just going to update that instead is a correct um we're having typo error instead is it spell instead of course
It's correct is crazy sometimes so right here we can just okay um Apple watch already exist you may update this product instead right so let's run it again and see if we if we get that message at the front end um so let's wait a little while while it runs all right so let's go here and let's send it again cool so now we got Apple watch already already exist you may update This product instead so that is it so this one we've gotten this one already so let's move to the next task all right
so um in in this video we're going to start bringing in Spring Security uh we can see that up to this time we are still pulling in the user manually from the database to actually add item to the card so in this video and some of the upcoming videos we're going to start integrating Spring Security so that we Can actually have a log a login system where the user can actually log in and then we get the user from the um from the authentication context right so what I'm just going to do here we're going
to right click and close all these tabs so let's open up our project so now we're going to create a new package called security so this one is security and inside the security I'm going to create another package called user just going To enter and um so this is the package right here so I'm going to right click again and create a new package and this package we're going to call it config this time around I'm going to just put it right here so this one we're going to call it config F and just hit
on enter so now we're going to create two classes right in here the user details and user Detail Service so we're going to say shop user shop user details and I'm going to grab this just going to copy and hit on enter and then I'm going to right click again and create a service for that class right so just going to paste this in in and we're going to add in service and hit on enter that's all so I'm just going to um make it wide screen so the first we going to do right here
we're going to actually Implement um we're going to implement the um right now there's something we forgot to do so we're actually going to Let's Open Up Our pum file right here so what we're just going to do right here we're going to bring in this security dependency right here so that we can actually get access to what we are trying to implement right here because currently we've not gotten our dependency in here so what I'm just going to do here we're going to go to Our browser and I'm going to go to we're going
to go to Spring initializer where we generated our project right here though I'm not going to create a project though you can still go to the m Central repository to actually get this dependency but I just like I just want to get it from here that will be faster so so now I just want to come here to say I want to add dependency and I want to say I need Spring Security actually I Need security and hit on enter so then I'm going to explore so if we explore here we're going to see that
we actually have all these dependencies right here but now we are not have we are not going to do anything testing so we're just going to grab only this one right here let's grab only this so let's copy and let's go back to our project and paste it in our P file so this is the web this is the Validation so I'm just going to come below here below the web and paste it here then I'm going to reload so let's reload our dependencies and uh let me just close this make it wide screen so
it seems our dependencies has finished reloading so let's close this so now let's come here to say we are implementing um the user details user details service this one right here from Spring and then we're going to just come Here to implement the only method in there yeah so we're going to come back to this in a moment so let's move down right here and the same way we're going to Let's Zoom this in so the same way we're going to implement the details user TS from here and we're just going to implement we're going
to pull in all the methods in there so we just going to click on shift and click on this and hold on shift and just okay um so now we see that all this ones has Been implemented for us by default from the interface right so they'll be implemented so if we go right here right now we're going to see that you see they have been implemented by default so by default just need to use them for free we don't need to implement them anymore so the only one we're going to implement are this three
right here right okay so let's close this so the next thing we're going to do Before we come here to start working here we're going to come here to create we're going to go go to our model our model package here we're going to create a new class called row so we're going to have the row class right here there's the row and we're just going to say enter right all right so um we're going to implement the row before we move down to the user right because we're going to make use of this class
inside here inside the um shop User details so now let's say the row we just need the ID of the row so uh this one actually we're going to say a long type ID so we're going to have the the row name and then I'm just going to say this one is a name actually because we are inside the row domain so and here I'm going to specifically create a a AR Constructor right here that Consists of only the name of the r right here and also just come here to say that this one is
an entity in our database so I'm going to say at um at entity and then we're going to have the geta seter set right here and then because we have generated because we have generated our row here so I'm going sorry not row so we have generated our Constructor right here our AG Constructor right here so we are going to have a no AR Constructor right here because anytime you create you create AR Constructor for yourself you need to also create no AR Constructor right so now let's specify the ID of this so we're going
to say that the ID is the primary key and the generated value is going to be identity so let's say the strategy sorry so we going to say strategy and then we're going to say identity identity right here all right so this all we need to do but Meanwhile We're going to say that this row is going to have a relationship with the user so we're going to set up a many to many relationship between the row and the user right so we're just going to come here to say um private uh sorry so we're
going to have collection of user user and here we're just going to say users just like this and now we're going to say this one is going to have a Many to many relationship with the user right so many row belongs to many many rows belong to many users and here we're just going to say map by going to say um rows of which we're going to set up in a moment sorry what am I typing so this one actually is row right so this just all we need to do right here uh but meanwhile
I would like to change this implementation to set so going to say set because we going To be using set you see the beauty of using of cod to interface we can actually switch the implementor right so for example now we can we can use list I list here or we can use set right here so it's easier to swing to switch um Implement right here so now let's go let's go to the user to actually set up this relationship at the other side so this is the user right here so here we're just going
to come down here also to say we have we need a list of rowes For the user so we going to say private this one actually we're going to say collection collection of row and uh yeah is coming from us so this one we're just going to say equals we also going to um in um initialize it to New haset let's remove this one right here doesn't matter and this one is an assignment see the assignment operator right here and then we're going to set up the relationship right here we going To say add many
to many as well right all right so now we are going to go right here to set up some things so we going to set up the fetch. eard and then we're going to we're going to set up the Cascade say Cascade sorry this is cascade. all right here and but now we're going to look at something right here so here we see that we have a warning right here so we're just going To follow this warning um I'm going to break this down so the warning actually what is saying is that um it's not
safe to use cascade. all because just like we use right here so right here we have order right and we say cascade. all right so which means um anything that happens to the user is going to happen to the to the so this one is the user right so say okay when the user is deleted or the orders are going to be deleted except Otherwise so if we have a requirement that said okay if a user is deleted we should also keep the the history of the of the user order in the database then we're
going to change this one but now in our requirement we just okay if the user is deleted then it should be deleted too so anything that happens to the user should happen to the row to the order that's what we just do right here so doing that in right here with the row it means that if the user is deleted the Row should be deleted but that shouldn't be it because here now we having many to many right and um so when a user is deleted a row is a stand is a standalone entity a
row does not the row class this row entity does not depend on the user and the user does not depend on the row which means if there is no row there is still there should be a user too and if there's no user there should be a row so if there is no row there's a user if there's no user there's a row so they Don't depend on each other okay so we just want to select because casc all this one actually include remove so here we don't want to say okay we don't want to
delete the row when the user is being deleted so we just want to mge uh detach mge persist and refresh as suggested by TJ but you can actually pick anyone you need you just remove all of this for example you can just take M or you can just take only persist and what it depends on the operation you want to do Right here that you depends on the operation you want to Cascade down from the user to the row right but meanwhile in this demo we're just going to leave it like this so the next
we're going to do here because we are having many to many we are going to have a third table that is going to join the row with the user right because many we can have many users can actually have many rows right and so now we're going to come here to say we need a third table so We're going to call this one ad joint table just as have suggested right there and what is the name of the table we're going to say the name of the table is going to be user rule right so
we're going to say name and then we're going to say user user rules right so this is going to be the name of the table and what are the columns that are supposed to be in this table so we just going to say okay we want to have a column called user ID And a column called um row ID so here we're just going to say a join column sorry this one is actually join column join columns then we're going to say equals ad join column and now we going to set the name of this
column right here to user ID so we say this one is user ID and this user ID what is what column of the user is he referencing so we're going to say this user ID is actually referencing the user ID right here so We're going to say now let's check this user this is the user right here so let's check the ID so we're going to say that it's actually referencing this one is the primary key right so this primary key is going to become the foreign key right here in this table which is going
to be this foreign key right here right so this foreign key is actually referencing the ID of the user so this just all we need to do right here and having done this now we're going to Define the inverse of the relationship inverse means the other side of the relationship which is inside here so now let's break it down from here and so just going to break it down so now we're just going to say inverse we're going to break the inverse column right here now the inverse side of f then we're going to say
equals ad joint column sorry sorry this is AD joint column actually joint column and now what is The name of this column so we're going to say this column is going to be called row ID so we're going to call this column row ID and again what column of the row is this is this um key referencing so if we go back to the row right here we're going to see that this row also has an ID right so we're just going to reference this ID right here so we're going to say okay the row
also is referencing the ID in the row column Right so we going to say reference column and then we're just going to set the name to the same ID so this one is actually referencing the ID inside this row entity why this one right here is referencing the ID this one right here so that's just all we need to do to set up the the third table to actually keep track of the user of the user rules in a separate table okay so now that we have done all this right now we're going to go
back and start implementing our user Details in the next video all right so in this video we're going to start implementing the user details so let's move down here um so here we're just going to pull in some properties um so let's create a space right here so we're going to have the ID and we're going to have the email so we're going to have email and then we're going to have the password and then we're going to have The collection of row [Music] collection and this one is going to be collection of granted authorities
right here I'm going to call it authorities so the next we're going to do here is to actually is to create a method that will help us build the user details so here we're going to have a public method right here so say public static shop Details um okay let's take this just um imported this from our from our package but this one right here I'm just going to say build user details so we going to say build user this is be user detail user user details then we're going to pass in this right here
as dependency as um yeah pass in user object right here so the next the first thing we going to do right here is to actually get all the is to get all the rows all the Authorities that belongs to the user right so we're just going to say um list so this I'm going to call granted authorities just as suggested by but Meanwhile we're just going to grab all the ones that belong to the user so we're going to say user. get roles oh sorry this is dots actually rules oh my gosh what am I
doing user. getet Rose stream and map it so here let's pull it down and let's pull that From here and so what we're just doing here is that so we just trying to um get all the rows that the user has and we stream there which means we iterate through all the rows so for every row we map it to we convert it to simple granted Authority right so after that we're just going to collect so we're just going to collect it to to a list so I'm going to break this one down as well
break it down but meanwhile I'm just going to have a Simple list here say to list right here so then just going to come here to return so going to say return my cap lock is on so return new shop user new shop user details then we're going to come in here to specify what we are going to return right so say user. getet get ID user. get emails like get email actually user. getet Password and then we're going to pass in the authority so we're just going to pass in authorities we have update that
we've already that we've already um gotten from the user right so let's bring this closer bring it closer all right so now we having the kind of error right here and so what we just need to do we going to see that it's asking us to create a Constructor right so so what we just need to do right here is to bring in our annotations to actually Create Constructor for us so let's have at all add Constructor and we're going to have ad known our Constructor and then we're going to come here to have also
have the geta and seter seter right here so just all we need to do we see that the error is gone all right so the next thing we're going to do right here we're just going to come here to say we're going to return return authorities And here we're just going to return the password and then we're going to return the email right here so we see that all these ones have been created for us for free they have been implemented for us so with our requirement right now we don't need to change this one
anymore with with our requirement but if the requirement changes we just we can we can actually override the default implementation Maybe by doing some other Things right here instead of using the default implementations but right now in our project we just want to make use of the default implementations right here okay so now I think we have done with let let me close this Gap right here let me clean this from here so I think for now we have done with this we have done with this with the implementation of our user details so let's
move to implement the user Detail Service this one right here so the next we going to do right The first thing we're going to do right here is to bring in the user repository as a dependency this one right here and we are going to do Constructor injection so we're going to make it f Fin and then we're going to bring in this annotation right here that will enable us to do Constructor injection right here and also we're going to say this class is a spring Bean by saying it's a service okay so the next
thing we're going to do here we're just going to go Inside here to say user equals um we're just going to say course let's do this input before continue this input here okay let's do it again just let's get it from our own here let's scroll up right here I think this our model right so let's get it from here so we're just going to say it was this one we're going to make it optional because the user might be present or not so we're going to say Repository find so here we're going to create
a new custom jpa finder method to actually find a user by the email so we going to pass in the email right here but Meanwhile we're going to change this one here to email and we're going to say o throw here so let's before we move on let's let's do this um import right here and um so let's go back here and we're just going to Return shop details userb then we going to pass in the user so build the user details right here all right um so we're going to break this one down we're
going to actually throw a message if the user is not found we're just going to say username not found exception so we're going to say username not find exception and we're just going to come in here to pass in a message we're going to say user not found user not Found all right so they just all we need to do right here but here we are having an error um is oh sorry forgot to put new here thank you to okay now we're having an error here too requires required user provided object um okay let's
check out this um okay this one supposed to be user right here yeah so this will get okay cool set so this just all we need to do right Here to implement our user details so let's move to the next video all right so now that we have done implementing our user Detail Service and our user details so before we move to the to the next task we're just going to run our system to make sure everything works to this point um so the first thing I'm going to do right here I'm going to run
some M goals right here I'm going to run clean and install this one right here I'm just going to double click this one Right here to actually clean our project to make sure everything um we don't we are not having any error any errors anywhere so let's wait a moment while it cleans up all right so okay so we see that okay it's still cleaning okay so we see that um the system has cleaned up success sucessfully so we can see right here that it has built successfully and we can see that um some things
some tables Have been created you can see that this road table right here has been created for us right and um this user rows also has been created which means we have two new tables that are created for us right now in the database uh so now let's um let's remove this one right here and uh let's close this panel and let's actually run this project so let's wait a few seconds while it runs okay so now we see that our projector started so let's go back to my SEC workbench and refresh so let's refresh
our database and here we have um a new table that is created so here we have the user Row the user the user ID and the row ID which is this the table that is joining the user and the together and here we have have the row right here so we have the ID of the row and we have the name of the row so now let's go back to the user to see the modifications that has been there um so right here we see that There are no modifications even though we there's we we
map a relationship between the user and the row if we see here there are nothing this table is not affected right so the thing is that we just have a table right here that is tracking the user table and the road table so I'm just want to right click on this and close all the tables right here so we open the ones we actually we actually need so here we have the user row we have the row and we have the user So these are the these are the most important thing right here so this
is the user table and this one is the road table and this one is the is the the table that is actually joining the user and the RO together um so one more time let's go back and see our database structure so let's go to reverse engineer to see our database structure at this time that's our table the table the structures of our table and let's just go next and let's Unselect this one right here we are having dream shops DB and let's next next and now we have 10 table total object right here so
we have t 10 tables you know we having eight before but now it has become 10 so let's next and let's click on finish so I'm going to go off screen to arrange this then I'll come back to show you all right so uh currently this is the structure of our tables in in the database so right here we have the user And here we can see that we have a new table called row and we have another table right here that is joining the user and the row together so this is the user row
and here we have the user ID and the row ID right here right so every other table Remains the Same right so so right now we have 10 tables in our database and this is the structure of our tables of our project right now so this is the flow of our data in the database um so haven't done this right Now let's go back right here let's go back right here so this one is the is the playground that is our tables um then let's go back here to to in so the next thing we're
going to do right here if let's scroll up and see um okay yeah all right so now um let's for example let's go out let's let's go to postman right here let's go to postman and let's say we want to we want to get get a user for example let's just go Here to find a user by ID and let's hit on enter and so right here we have unauthorized right here so now um Spring Security has loged down our project so here you are not authorized to find this user so now you have to
log in right here right so that is to say that Spring Security has taken over our project so now let's go back to vs code uh sorry not vs code itell so so the next thing we're going to do before we start Configuring the class before we start the the the class configuration which is the main security configuration we are just going to bring in we're going to create because we are going to be using JWT in this in this um project right so we just going to configure the JWT so after the JWT configuration
then we now create the main configure class configuration class to configure the whole security to put everything together in that configuration class Then before we start logging in and testing right okay so um let's go here so let's go here um inside this package inside this service this security right here I'm going to create another package right here so this package we're going to call it JWT J WT right here inside here but meanwhile I want to call attention to something if you remember last time we've created a config class right here but now I
just want to I want to Move maybe I just want to cut it I want to cut this one right here and then I want to come here to put it here I just want to put it inside here so let's do the refactor okay so we want everything we want every security to stay here because this is the this is the um configuration class this is where we are going to do the security configuration to actually take care of our system now let's go back right here Let's go back to this config so here
now we see that this folder is empty so we're just going to eliminate this just going to remove this folder from our system right here all right so that is gone and now we have our configuration inside the security class inside the Security package and also we have created a JWT package right here and uh so the next thing we're going to do I'm just going to Let's right click and close all These so the next thing we're going to do here is to we're going to go to our pom file then we're to bring
in the JWT dependencies to enable that is going to enable us to actually work with JWT um so to save our time I'm just going to move down here so this are this are my SQ connector the database connector this lbook and this the the the test stter so I'm just going to break it here I'm going to break it Down here then I'm going to pull in three dependencies of which you can just pause the video and get this dependencies right here these three dependencies so um here we have um we have the JWT
API and we have the JWT implementor and here we have the JWT Jackson right here so these are the three dependencies we need to actually work with um JWT properly in our system um I'm just going to extract this version let's extract this properties Right here and you take note that they all are the same version right we are using the same version all through uniform version so let's scroll up right here to to the properties so if we go here we see that we have extracted the properties right here so this are properties so
here we have the JJ WT Ino JWT sorry this API this Ino and this is Jackson and they all are using the same version right so you can just pause this video and just Type this or you can just go to Maven the the the M repository centrer to get all this exactly like this right uh but the best suggestion just pause this video and type exactly what you see on the screen right here so you can just type this one exactly as it is like this okay so let's move to it before we leave
here we're going to Let's reload our let's reload our our dep M dependency so you see that it has been Reloaded right you see here is preloaded okay so let's just keep this aside so um let's move to the next video when we going to start implementing the classes we going to start creating and implementing the classes one after the other so see you there all right so in this video we're going to start creating and implementing the JWT classes we are going to work with so let's close this pom file and let's open up
our project so right here We're going to right click on this JWT package and create a new class so this one I'm going to call it J WTS and hit on enter so let's make this one white screen so in here we're going to have some properties so let's have private so we're going to say JWT secret right here and the next one we're going to have right here is we're going to have the expiration time of the token so we're going to say in expiration time Expiration expiration time so these are the two properties
we need right here then have done this right now the first method we're going to create right here is the one to generate token for the user let's say public string generate token generate sorry generate token for for user and then we're going to pass in the authentication object right here get it from Spring core Spring Security core And U let me just make this smaller a little bit so the first thing we going to do right here is to get the login user so we're going to say shop shop user details so we're going
to call this one user so going to say user principal so um now what we did right here is that we just try to get the user that is logged in because the token is going to be generated for the user for For the logged in user right so that's why the first thing do in this method right now is to get the user that is logged in then the next thing we're going to do right here is to just come down and now that we have gotten this login user right here we're just going
to get the list of the row that this user has right so here we're going to say list so we're going to have list of string and this one we going to call Rows so we say equals user principal. get authorities do stream so we're going to map and inside here we're just going to call the granted authority to actually convert the rows so let's break this one down and then let's put them inside a list so what we did right here is that we just get the login user and we extract all the rows
that the user has right and then and we start them here so now we're going to Get the information that we're going to encode inside the the the token for the user so here we're just going to say um return J WTS do builder then now we're going to what are we going to get from the user so the first we're going to get right here is the user the user username so we get the email which is the email is the username right here and then we're going to get a claim claim actually means
the information you Want to pass in for the user right that the user information so the we want to get the ID of the user as well so get ID then the next one we're going to have we need the rows of the user right so we're going to pass in here as a claim as well so this do claim so we're going to pass in rows and then we're going to just bring in the list of rows we have extracted up there and then we're going to set the ISS time the time this token
is this token was Generated and then we're going to come here to set the time that is going to be expired the expiration time and here we're just going to say. get time get time plus expiration time all right and then we're going to come down here to say do do sign with key now this is a private method that we going to create in a moment this key right here and then we're going to say Um choose this algorithm right here and then we're going to come out to compact compact so this just all
we need to do right here um so before we explain let's just create this private method right here we're going to say private key yeah this one right here we're getting from the user Java security right here and then what do we need to return from there we're just going to say return Return keys so we going to say return keys right here and then we're going to have we're going to have the decode we're going to have decoders right is that correct yes we're going to have the coders do base yeah and that we're
just going to say do the code right here they going to pass in the secret we have up there so what we just did right here is that we this this this is the information we Actually want to encode inside the token for the user so here we have the ID we have the email we have the ID we have the row so any information you want to encode for the user you just pass it as a claim right here you can you can pass the first name the last name anything you want to pass
just passing the pass it here as a claim for the user right and in that case you have to include it to this you have to include the information inside here right so that You can actually get them to include when the user logs in um so now the next thing we're going to do we just going to come down right here to create the method that is going to extract the user name from The Token as a public string we're going to say get get username from token right here so now we're just going
to say JW J WTS do pass so um let's just follow follow this um this suggestion that is Given to us right here and let's come down break it down break it down so this just what we want but meanwhile if we look here we see that this one is giving us warning and it said is duplicated right so instead of using the pass we're going to use the pass Builder right here and then before here we're going to build I'm going to say do build right here so this will solve the problem so what
we're just doing right Here is that we just try to extract we extracting the user name from The Token that was given right here because this one is the token and we try to extract the user name out of it then after that now we're just going to actually we're just going to return this one we just going to return it right here all right so the next we're just going to do right here is to say do um get subject yeah this one right here so it just all we need to do To actually
extract the username from the token so the the next one we're going to do is the want to actually validate the token so we come here to say public so this one is going to be bulling and then we're just going to I'm just going to say validate token validate validate token then we're going to pass in the token right here then we're just going to come inside here to return actually we not returning we're Just going to call um JWT J WTS this one right here and then we're going to set the I'm just
going to take this suggestion right here so let's break it down and see what we got break it down break it down and break it down right here so what we're just doing right here is to actually validate the token so this just the CH the chain of method we need to validate this token right here Then after that we're just going to return true we return true right here and um if that is not case we just going but what we just want to do right here I'm going to surround this with try and
catch so that we can actually throw exceptions yes so uh these are the four exceptions that are likely to be thrown from this from this uh method right here but meanwhile I'm going to I'm going to pipe all this exception to one line so Let's pipe them I'm going to P right here and come here also just pipe it and paste it here and we have this one too so let's pipe it put it here oh sorry yeah so this one is actually piping we are doing so let's put it here and the last one
is this one right here so we're still going to pipe it uh let me make it small a little bit so we can see the full length of what we're doing So we just going to come here and pipe it then I'm going to remove all this going to remove all this on from here remove this and then we're just going to throw the new R time but actually here we're going to throw um new JWT exception right here so in that case now so we're going to say e. get message actually e get message
here so what we just did right here is that so whatever error that is thr from here if don't Throw error we're going to catch it here so any message that you throw by any one of this one is the message is what we're getting right here because these are the likely messages these are the messages that we likely to get from here um so here we are having um um okay we are missing maybe I think we are missing craes right here okay yeah Miss craes okay so I think this just all we need
to do right here so the next thing we're going to do Is to come up right here to say this one is a spring Bean just make it a component now the next we're going to do right here is to set the properties of this one right here is to set values for this properties right here so now we're just going to open up our project so let's go to our properties file here so here I'm just going to come here to bring in some information right here so I'm going to make this full screen
so this one is the JWT secret I've gotten so you can just use this one as well and also we are setting the expiration time to 1 hour right here so let's go back to our to this class right here to actually wire this one to inject this properties inject this value right in here so for us to inject the values inside this properties we're just going to say here we're going to get we're going to say the at Value getting from Spring framework right Here and inside here we're going to say we just going
to so this one is the secret right the first one is the secret right here so we just try to inject the value right in here and now let's inject the second value Val right here so we going to say add value so this one have Au that's authentic authentication and here we actually um passing in the expiration time right here so this just all we need to do to actually inject the values Inside these properties right here so I think we have done with the implementation of this one right now so let's move to
the next one all right so now that we have we've done implementing this w utils so in this video we're going to move to the next class so let's close this and go here let's go back to this package and now we're going to create a new class so this one we're going to call it All token filter and let's enter so let's close this one um so what we just going to do right here is to bring in some dependency I'm going to say J WT utils so we're going to make use of this
class right here and also we're going to make use of our user service right here so we're going to say um that class is shop right it's actually shop user Detail Service this one right here But meanwhile I'm just going to shorten this I just want to say user Detail Service all right and the next thing let's extend so going to extend the on per request filter this one right here and then we're going to overwrite the method going to implement the method in there all right so this just all we need to do but
meanwhile let's break this down let's break it down so there's a warning right here it says um Nonannotated parameter overr in non non API so many English right so what we're just going to do right here we're going to come here to say at not none so at least let's remove that warning so I think we going to take this one from the spring framework let's take this and also I'm just going to copy and let's move down and paste it there move down and paste it here so that's all right so uh before we
start implementing this we're going to the First method we're going to create is the one to actually capture the request and extract the token from the header private string um pass JWT then we going to have the HT HTTP salet request and in here we're just going to say string token but meanwhile I'm going to just change to header head authorization and then just Going to get the header we're going to get this tab from the from the authorization from the request right then we're going to check we going to come right here to say
if so say if string. util this one is correct this one right here this is very correct and what we're just going to do here we're going to come down and return the substring of this and if there is nothing like that we're just going to come here to return now so we're going To say return none there's none actually so what we did right here is that so we captured the request right from the client then we check the header we request for the header right here this authorization right here so when we get
a header with this authorization then we're going to check so if there's if there's a token right here so that's why this one right here we say header or right so okay if this or if there is if there is any string right here that Start with Bea right there then we're going to extract this Bea right here which is seven characters here which is 1 2 3 4 5 6 plus the empty space seven so we just going to remove this space and and grab the other the rest of the token right so that
is just what we are doing here all right so the next thing we're going to do right here is to just go inside this method and start the implementation so say string the WT Equals pass JWT we get the request so we're going to say if so if this one. has text and and J yeah this one validate so what we just try to do here is that so we try to grab this header right here so if there is text if it if is not empty and if is validated then here we're just going
to come here to say string string Emil because our username actually is an email right so we just going to say uh mwh we just going to go Here let's see the suggestion so just like the suggestion inell is giv us right there so we're just going to grabb this one right here we're going to get the username actually from um from the JWT right here so if if the JWT is not empty and if it is been validated then we're going to extract the username right here so these are the functions we created last
time inside this um JWT UIL right here then after that now we just going to come down here to call our user Details we're going to say user details user details user details then we're going to call the service to actually load the user right we load that user by username that we did last time so we actually extracting the username from the token then we find the database if that username actually exist right so that's what we just did right here load by username remember this this is this is the implementation we did last
time inside here right and now After this now the next thing we're going to do here we're going to come back right here to actually um authenticate this user right so we're going to say um that we're just going to use the keyword right here let's just make it clearly so let's just say authentication equals Earth and then let's get this one from Spring Security core right here so let's make this import um so because of this long name I just want to make it shorter I want to Use the VY word right here so
a little bit shorter so what we just did right here is that so um if this token is not empty and is validated then what we just did right here is that we extract the username which is the email from the token right and then we take the username and we call the user service details um our user deta service the implementation last time so we call it to actually load this user to check the Database using the email of the user which is the user um usern name right so now we just load the
user and put it right here and then we validate we try to authenticate the user using the username password authentication so we pass in the credentials of the user right here which is the user details right here and we get the role of the user right here just for authenication so that is what we just did right here then after that we're going to come down To set the security context so we're going to say security security context holder this one right here then we're going to call this one um we going to say get
context get context and then we're going to set the authentication then we're going to put in the we're going to set the the authenticated user right here as the current authenticated user that's just what we did right here all right so now that we have done with this um now If you take note we see that we are actually using this method right here the validation method right here and if we go back here we're going to see that we are throwing a lot of exceptions right here inside this method so this method is actually
throwing exceptions if the validation um depending on the validation status right here um so now we're going to go right we're going to go here to actually catch this messages and actually display to the user right So for that now we're going to come here to wrap this one this one inside try and catch so that we can actually cat and return the message for the user so here we're just going to say JWT JWT exception this one right here is what we're going to catch and then we're just going to come here to get
the response so to say response. set status uh this one actually we're going To set the status so set status then we're going to just get let's remove this one right here we not actually doing this then after this now we're going to come down right here to get the writer to actually write the message for the user right so we to say response get writer get writer. write going to write and inside here we're just going to um Define a kind of Message that the user can read um meanwhile we we just we just
want to come here to say get message so actually get the message that is returned from from those exception the message they through then also we're going to attach our own message right so we're going to say plus right here so just going to say um youra um now just going to say um invalid Invalid invalid or expired or expired token right then we're going to say here so just to say you may log in sorry guys get a little bit confused so going to say you may log in and try again you may log
in and try again again yeah again so we're going to say you may log in and try again so because um possibly we're going to have invalid invalid token right here or Expired token right here so we to say invalid or expired token you may log in and try again right so it's um clear enough for the user to read then after that now we're just going to come right here and return and now we're going to catch General exception if anything happen apart from this so we're going to catch General exception so now we're
just going to bring in another another catch Block right here and in this one here we're just going to say we should catch General exception let's go right here to say exception C exception and then let's go right here then we're going to response with um this one right here but this one this time we're not going to return with this but rather we're going to return with um internal server error right so we're just going to say this one here might be internal server Error internal server error this one right here so this one
here is actually throwing the messages that consigns um actually the validation this one if anything if any error happens internally we just going to throw a kind of General exception right so this just all we need to do right here then we're just going to return just return the message that is been thrown to the user which is e get message right here so that's just all we Need to do then after that now we just going to return so this just all we need to do then then we're going to come out right here
to call the filter to just um just uh pass in the request and the response right here all right so this is just all we need to implement to get our token get uh filtered okay so now that you have done with this now let's move to the next class implementation all right so now that we have done with The implementation of this o filter and this um utils so the next one we're going to do is the entry points so let's go back to our project and right click on this and create a new
class so this new class we're just going to call it JWT or entry point and just hit on enter so I'm going to make this on the full screen and then the first thing we're going to do right here we're going to implement the entry point interface which is this application Entry point interface and then we're just going to implement the method in there and um we I'm going to break let's break this down so we can actually see the ending so the only thing just going to do right here I'm just going to go
in here and dump this some codes um so as the name suggests um this is the entry point which means when a request is issued this is the this is the class that is going to receive that request first right there is the F that is the Entry point of the JWT token um authentication so it's going to this class is going to pick the request and actually um check if that request has been authenticated or not so we're just going to say this one here is the is a component is it's actually a spring
Bean so this is just all we need to do here right now this all we need to do here right now if there's anything we need to do we're going to come back here to get it fixed later all right so have I done This now I'm just going to close it down and the next thing we're going to do is here so this is the configuration class so now we are going to declare all our be and the proper configuration to actually take care of our system right in here inside this class so the
first thing we're going to do right here is to bring in some dependencies so we're going to have uh we're going to have our user our user service dependency right here so we say Finer it is f actually and we're going to say shop this is shop shop user service details this one is going to be service yeah sorry user service user Detail Service actually I'm going to say user service user Detail Service and the next one we're going to bring right here is the is the entry point so we're going to say private private
final we're going to say J WT um o which is the class we just Created the entry point right here so then we're going to say Earth entry point and then we're just going to come up here to enable the um the web security so we going to say at enable web security at enabled um this one actually is not MVC but we're going to say security web security this one right here and then we're going to bring The annotation for us to actually do the construct Constructor injection so see at required act Constructor this
one actually is require a Constructor this one right here so this just all we need to do so now we're going to start creating our be one after the other so up to now the only be we have in this class is this model MPP right here we created earlier on so now we are going to create more beans right here so the next one we're going to create right here is the we just going to bring in the password encoder right so we going To say public password encode that yeah this one right here
and then we're going to return a new Big Crypt so this one is a be so we're just going to come here to annotate it with Bean a ban then after this we're going to create a ban of the all token filter so we want to say public um Earth or token filter this is the class we created last time we just going to say um o token filter this one right Here here and we're just going to return also we're going to return the new instance of the filter right and then we're just going
to come here to actually annotate it as a be so we're going to say it's a bean here so the next one we're going to create here is the alt manager is the authentication manager being so we're just going to say public we going to say authentication manager and we're going to call this Authentication manager but now this one is going to have we're going to we're going to have um a kind of argument right here kind of parameter so this we going to say authentication configuration then we're going to call it on o config
config and we're just going to go here to return so let's say return config this one here. Get getet tication manager here and then we're just going to come here to throw the exception right here so this just all we need to do then we're going to annotate this one also as a b so it's a bean as well all right so the next we're going to create right here is the authentication provider so we're going to say public so this one we going to say Dao provider Dao um want to say Dao authentication provider
sorry this the AO or provider yeah this one is correct okay um this one also we're just going to return we're going to come here to say um we just going to call it on o sorry is Earth provider so we going to say o provider we're just going to create a new instance of this Dao provider and here we're going to set the Provider right here and then we're going to set it to our user service user data service and then we're going to set the password encoder right here we going to come right
here to set the password encoder to the be we created up there and after that we just going to return the provider right so they just all we need to do right here um so this one also we're going to annotate as a be all right so this just all we need to do this all the these are all the beans That we need to create for now and um so the next thing we're going to do right here we're going to create the last bin where we're going to actually do the configuration to give
permission to our apis right so here let's say public so this I'm going to call it security security chain this security actually security chain filter filter chain actually so just going to call this on Filter chain and then we're going to have the htdp security object inside there and just going to go in here all right so in here um to save our time I'm just going to go here and pull in some code then we discuss it so this is just the configuration we need to um to actually get our our security working right
so here we see we just disable right here the C right here and here we handle the exception right here and in Here we just pass in the entry point the class we the class we've created and then we we we we come here to configure the section policy so we say the section is stateless which means when the JWT is being generated it's not going to track um um our back end is not going to track the JWT so on once that token is generated it's not going to keep it in section it's not
going to have memory of it right and then right here we just try to specify the UR that actually match Matches this this one right here then we're going to allow right and we're going to create this one in a moment this one is a Conant that is holding all the URL that we have actually going to secure right here you see right here just say authenticate so we say okay every URL that is inside here should be authenticated then apart from the that are in here every other one should be permit be permitted right
and coming here we just set the Dao authentication Provider right here and then we come here to use the filter we created before using the user name password filter so this just all we need to do right here so we're going to annotate this one as a be actually a be we're going to say this one is a be all right so now we're going to scroll up to actually declare this constant right there so we're just going to come here below this one here we're Going to bring in a constant so this is the
constant right here we just going to have a list of view of end points that we're going to secure so in our next video we're going to come right here to actually put in we're going to bring we're going to list all the end points we are going to secure right here in our next video so see you in the next video all right so in this video we are going to test our project from end to end so we can see that we have already um set Up our security security uh configurations and um
everything is ready to be tested but before then there are some things we need to be we need to go inside here there are some things we need to do so for example right now if we go here um let's let's go to the controller um let's okay there's the controller right here so let's go to the um the cut item so now right here we see that we are actually getting this user manually from The database right but now that we have gotten our security set up already so we actually going to we're going
to create a method we are going to get this user actually from the user from the um from the authentication context right so what we just going to do right here I'm going to turn this one right here I'm going to convert this one to um a new method we're going to create so here I'm just going to say get authenticated user so we're going to say get undedicated User and then we're just going to create this method inside um we made the mistake right here this authenticated so then we're just going to create this
method inside our user service so let's do that first let's make sure the name is correct so here is get authenticated user yeah that's correct so now let's create this method inside our user service so right here this is the method So so let's go to let's go and implement it let's open our service let's go to our user service and uh this is the service package right here and here is our user and here so let's implement this Method All right so what we're just going to do right here we're going to get the
we're going to check the authentication context if there's a login user so if there's a user we're just going to return it and going to Create the card for that user so now we're just going to come here we're going to have authentication uh this one right here and then we're just going to call this one um authentication right equals security you call security context context folder doget um doget context. getet authentication doget authentication Right here so then we're just going to come to this string email then we're going to get the the the email
if there's if there's a user right here that is undedicated then we're going to get the email of that user then here we're just going to find that user in database so we're going to say return um user repository. find so we going to say that find by Emil this one right here and we're going to Pass in the email right so this just all we need to do right here and um so every implementation at the front ENT made the same this one right here where is it uh where is the controller let me
close this one right here so don't get confused this one right here so you see that this this one see remain the same so now instead of getting the user manually we actually trying to get the login user To actually um we can actually create the card for the user so that is what we just did right here and so the next thing we're going to do here so the next thing we're going to do here we're going to come here we're going to add another catch block right here so this time around we're going
to cat remember last time when we were implementing um one of this this one right here let's go back to JWT so this Is the JWT and and um here we have the U right so last time we actually throwing exception right here this exception right here right so now we want to cat this exception in that controller because that is where this authentication is going to take place right so now we're going to cat this one right here to actually return the message to the user so here we're going to say um JWT want
to catch JWT exception right Here and then we going to return response entity but this one is not going to be bad request rather this one is going to be unauthorized so we're going to say um the status then here we going to say unauthorized unauthorized so because because this is the message we are this is the this is exception we are throwing from inside the service so we're going to catch this exception right here so whatever message Being expired JWT JWT not found invalided JWT so we're going to just return the message right here
to the user right so this just the addition we need to do right here inside this add item controller and then the other thing we're going to do here we're going to go to our user service again so now that we have gotten um user security so we're going to encode the user password right here right so because we've created a Bin already remember we've created a spring password encoder been here already so we're just going to use it right here so now we're going to come here and bring in the ban as a dependency
so we going to say private okay yeah good suggestion right here so then we're going to come to the password right here set where is okay here set password that we just going to say um let's remove this one right here so we're going to say password Encoder do encode password right here so we're just going to encode the password before we submit it and last time we have so this one is done so last time we have a class A util class where we actually creating default users right so we're just going to go
this one right here so we're going to go here and um something's happening so we make a mistake right here so um we're just going to go right here to also bring in this password encoder dependency right Here so we're just going to encode this password before it's been saved right so going to say password encoder do encode then we're going to pass in 1 2 3 4 5 6 so we just going to encode this password before it's been saved and number two the next we're going to do right here we're going to create
some default rows in the database right here so i' I've set up a method right here to actually create a default Row for us right here so this one is When the method runs just like this just like this one that is creating the user so we're just going to create some default rows in a data database so now um here I've set up two rows here one is for admin and one is for customer right so and now we're going to call this create default row right here and then we're going to pass in
the default row default rows right here so that we're going to have rows in the database to test with right so now the next thing We're going to do right here I'm going we're going to create a default admin right so here we're just going to I'm going to come here to copy this method I'm going to copy this one and then we're going to push down and paste it here right so we're going to change this name from default user to default admin default admin right here and then we're just going to create um
let's just create to admin and now we're just going To say admin email so we're going to say admin email and um so we're just going to ignore this one right here let's just Let's ignore this um yeah let just just leave it don't mind this so now we're just going to set this one we just set the name to admin admin and also admin we just going to have admin admin right so here we're just going to leave everything as the Same so here we just going to say admin user this a mistake copy
and paste mistake so we say admin user so um so right here we have we have to admin we're going to have to admin in the database then we're going to have yeah the admin one and the admin two right okay so um the next we're going to do here we're just going to come up here to also create this admin so we're going to create admin yeah default admin this one right here And now we're going to set the r right here we're going to set the admin row so this one here we're going
to set customer row because here we have two rows right here I have customer and I have admin right so this one here are going to be us these users here are going to be um let's just say users right let's just say this one is row users user right user row and the admin row right so now we're going to set this this row to this one so that to this Users freay here so that when they are created they can actually have a row by default so for that we're just going to come
right here so what we're just going to do right here we're going to we're going to set the row because here we have two rows that are going to be present in the database when the application runs right so now we're going to set row we're going to set rows default Row for this user when it's be Created so we're just going to say row so this we going to call it user row equals row repository. find find by name then we're going to pass in the user row we going to say row row user
and then we're going to say do get it's an option right so now we're just going to come right here to actually set the row for the user so we just going to come here to say user do Set row sorry so say user set rows and then we're just going to pass in the set of row right here and uh so the same way we're going to just copy this one and we're going to come right to the admin and then we're going to change this one to admin so we say admin right here
and also we're just going to say this we're going to call it admin row admin row right so we're just going To come right here to do the same thing so we're going to set the row we going to say user do set row pass in the admin row right so just all we need to do right here um then what we need to do before we run this project we're going to we we just want to make sure the Row first the rows are created first so for that I'm going to silence this and
we going to silent this so that the row will be the first to be created so after the we're going to come back here to to Release all this to be created so they can actually so that this row will be present in the database before we create the user right now the next thing we're going to do right here I'm going to where we going to create our database from the scratch so we're going to go here so we're going to go right here and I'm going to Let's close it so I'm going to
change this one to create want change to create and also I'm going to go to my SQL workbench We're going to drop our schema I'm just going to come here to drop the schema so we're going to create this scha from the scratch okay so that everything will be fresh we're going to have fresh data fresh schema fresh database to test with so coming back to intell I'm just going to copy the name of this schema where is our schema okay this one right here and then we're going to create it again so let's copy
and just here I'm going to create a New schema and just paste it here and then we're going to hit on apply apply finish right and let's refresh all right so let's make it default so now we have a brand new schema without any data without any table um so um I think that's all we need to do right now and uh let's go here let's go back to I think I've opened too many windows right here let me just close okay let's close this one maybe let's close users Let's close this one too and
uh let's close this now coming here we are going to specify um what we just need to do right now let's go back to intelligent so we just going to specify one of this controller right here so this is the add item controller this one right here this this cut item controller that's where we're going to go oh sorry not here the controller sorry guys get it confused Right here so here we're going to secure this this uh this API this this cut item API because this is the API that is performing add item update
item remove item because all this on right here actually requires the user right so that we can know um the cat that we are dealing with so we are going to secure this cat item and cut item cut item um API and the cat API um so here we're just going to S this one is actually API let's go to our Control our properties right here so this one is our preface so API V1 so let's go here so go say um/ API slash V1 right then here we're going to have Cuts have cut slash
all right so we're just going to secure everything this API and then we're going to come here to have another one so just going to I'm just going to copy this One and um going to remove this so let's paste it in and then um let's let's actually I think let's break it down maybe let's break it down here so then we're going to say this one is cut item cut items right and this one is cut because this is our end point so we're going to secure this to endpoint for now and um so
having done this let's go back let's take let's confirm it so this one is yes cut items and also we have the Cat right here which is this one is cards all right um this is the wrong file so but this is card items and we have the cards right so what we're just going to do right here is just that we try to secure we try to we try to lock down the cart API and the card item API right so that before any user before a user can access this card API that user
must loged in so we can actually get a hold of the user ID to do this operation right so we're going to lck this two Down for now and um having done this um is there other thing that we've not done okay so let's clean up our project and see what we got so just going to run M go to say um clean install M clean install so let's wait and see until this one is is cleaned up so um let's wait for a while while it cleans up so I'm just going to go off
screen so when it finish cleaning we're going to come back here all right so you see um it has Finished cleaning up we see BU success yes and if we scroll up right here we're going to see that tables have been created and um we see all the tables have been created again in our database right so let's go to my S work page and refresh um I'm just going to Let's refresh and let's remove this for now so we see that we have gotten our tables again and now everywhere is empty right so no
nothing no nothing the product everywhere is empty right now so what I Just want to do right here I will encourage you let's go offline at least I have shown you how to we have seen how to insert data right so let's go right here let me just go right here so make sure you have this class make sure you have this class set up like this right so let's try to insert data so before we go offline I'm just going to let's run this application to see if if this one if the rows are
going to be created or not so let's run this To start with but make sure you have this class you have data right here because we're going to set up default data right here in our database so we easy our testing right so um let's run our project let's wait a little why why it runs okay um it's been created but meanwhile uh we're going to go here let's let's let's check first let's go here and so this one is our row right let's check the Row okay so now you see that we have two
rows user rows and admin rows now um to avoid problems let's go here to change this one from create to update so we're going to put this on to update so that our data we not going to lose our data in the database anymore so let's close this for now now it's time for us to it's time for us to release these two guys right here so let's release them right here and let's run it and let's see what happens Um we have error somewhere so let's see what is happening so here we have a
detached entity okay um this one right here so we need to save I think we need to do something let's go let me just go quick go offline um and get this solved I go come back here to communicate with you all right so now everything works and what I just did here is to come here you see this annotation right here so I just decided to make this class at transaction so I made Every operation right in here to be a single unit transaction right so that is what I just did there's no other
thing I do I did I just put this annotation right here to make the whole class a single to make all the operations right here a single unit of operation right a single unit transaction right so now if we go back here let's go back to our MyQ you see that everything has been created so let's go here let's go to users so here we have users and we see all our Password has been encoded right here with Spring Security and U so we have users we have rows so user rows and then we have
um so here we have two rows we have two users with um with admin admin rows right right here and uh so now we got nothing in our database we only have users and rows everything has been created so the next thing we're going to do here are just going to we're going to go offline off screen to create some product so remember last time in post Man how we created product right so we just going to go here this is product right here so we just going to post some product and now we can
see that let's just try now um let's try one before we go offline so this one right here I'm just let me just try to hit send and see what happens so here we have okay so we created one product so let's go back to my SQ workbench and and refresh where is the product so this one is the product Right here so here we have one product right so I just want you to go off screen to create a couple of product then we come back here to test all right so now we've seen
that we have added more products to our database so um here let's go back to postman so there is the API that is this the end point to actually add the product and uh you see that our validation is is actually working everything is still in place so now um let let's go back right here Let's go back to intell so right here in itell um remember that what is it U let's go back to our config right here so remember that we have locked down these two apis right here this cut item API and
uh this cat API so which means any user that is accessing these two these two apis right here must be an authenticated users right this one right here and now this product API has not been locked down right we didn't Lo down anything right There so let's go back here let's go and text this one right here um so let's for example we want to add item to cat let's go to this um where's the cat this product uh this one is the cat folder right here so here is add item to cat right so
now let's say we want to add item with ID number three which is a product with ID number three and we want to add five of it to the card so um let's hit on send so right here we've got a message that full Authentication is required to access this resource right um which means uh the security is actually working but meanwhile this message that is displaying right here is not friendly enough right um I just want a friendly message that all the lay man user right there can actually use which is every user can
actually understand what is happening um straight away right so um I want us to go back to intell to customize this message before we move on So right here in intell J we are going to go here this is the the entry point of our application of our authentication just like the name right here the J WT or entry point so which means every request that is sent to our system this is the first place it land so this is the class that check if that author if that um access if the user that is
requesting that access if the user that is sending that request is authenticated or not so the message we Are seeing on Postman this is the message we actually seeing right there right so now what I'm just going to do here we're going to remove this one right here and we're going to say um we're just going to Define a simple message just for testing right so we're going to say you may log in you may log in and try again so we're going to say you may log in and try again and meanwhile I'm going
to remove that path this is the path Right here that is showing that the end point right so I'm going to silent it and also I'm going to silent this status code right here oh sorry this one I'm just going to silent this so let's run it again and see what we got this time around okay um so here our project is running so let's go back to postman so let's hit on the same send so right here we see that this message has been customized so that everyone that is looking at this Can okay
you may log in oh I've not logged in let me log in and before I can add item to cat so the idea here is that I just want to show you that that is the place where you can customize this message so that you can actually return a friendly message to our front end for the user to see now have done this right now let's go back to let's go back to this product right here so if we let's go back to this product in intellig so right here in intellig this is the Product
controller right here and if we look at this product controller it consist of so many operations right here searing product by brand by category by different things and also the same API we have delete product we have add new product and we have update product and now um despite that we give access despite the access we give to this API for everybody to access does not mean that everybody can access all endpoint inside this API So there are some a there are some endpoints in here that only the admin should access for example the add
new product should be only someone that is an admin and for someone to delete a product or update a product that person must be an admin and for someone to delete a product that person also must be an admin right so for that now we cannot just because because because of the other operations that are that are still here inside this API we cannot log Down this full API just like the way we lock down this one right here so we cannot lock it down right so what we just want to do in this state
and we don't want to flood here with different pattern of of end point so what we just want to do in this this case we're going to come down to Method level security so we're going to use method level security to secure these three end points right here so now what we just need to do here is to come here we're going to use this Annotation called preauthorize so we're going to say pre-authorize and in here we're going to specify the role of the person that is allowed to to access this endpoint so here going
to say has Ro and in here we're going to have single quote and we going to say row and we going to say admin so what we're say here is that only admin only a user with the row of admin can access that can add a product To database and also we're just going to copy this one and we're also going to say that only admin user can update a product and also only admin user can delete a product right here and then for this annotation to work for this action to take place to take
effect we need to go back to our configuration class so up here we need to bring in an annotation so we're going to say at enabled method level Sorry at enable so we're going to say method level security this one right here so we're just going to say at enabled method level security and we are going to say prepost enabl we say true right so this just so this just The annotation we need here for that for our action to take place in the controller right here so we just want to set prepost to enabled
right um so having done this right now let's go let's let's run our Project again and go back to postma to text this out so our product is our project is running let's wait a while so now the project has started let's go back to postman so right here in Postman this is the add product right so now let's hit on this again so now we see that this message has changed to unauthorized you may log in and try again right which means um the user must log in for us to determine whether the user
has it's it's A user or it's an admin right but now inside this product API let's go back right here let's say we want to do other operation this the product API so let's try to find product by brand right here so here let's say we want to find all Apple brand we want to find all Apple brand in our database so let's send this um let's send it again um okay okay we have changed this Ser Port last time I'm just going to save this let's save it so let's send it Again all right
so now we see that we are able to get all the brand that belongs to Apple in our database right here so if we scroll down all the brand of Apple we actually get them but now remember that this the same end point is inside the API is inside the product API as well so now we can actually operate all these ones but the but the the end point that we have secured for example like the ad product that we have secured we cannot access That not everybody can access that endpoint so now you get
the idea so uh what we just want to do right here remember that we have not setting up our API to actually authenticate a user right so now the first thing we're going to do right now before we move on it's going to go back to intellig and create the API to actually authenticate a user whereby we can actually log in a user to determine the role of that user right so after that we're going to come back here To continue with the testing so see you in the next video all right so in this
video we're going to create our login controller so let's open up our package our project and U inside this controller package so we're going to create a new a new a new class called um o controller so this one I'm just going to call it o controller and going to enter so now let's um and also before we close this let's go here inside this request Package this one here I'm going to create a new class so this one I'm going to call it login request EST login request and inside this response class I'm going
to create yeah inside this response class I'm going to create a new class called JWT response response JWT response yeah so let's make it white screen so this JWT response what just need right here we need an ID we need a token but meanwhile I just want the ID to come first so going to have private long ID uh sorry this ID and right here we're going to have the data to get um data seter that we're going to have all AR Constructor this one right here and also going to have no AR Constructor so
I'm going to close this then for this login request we're going to have the the email and The password so actually the email is the username so we're just going to say email and here we're going to say password and then we're going to come up right here to actually um get our geta set by using the data as usual but this one we're going to validate this we're going to try to validate this so we're going to say at not at not blank so we're going to validate the input to make sure that um
The user does not submit empty empty Fields right okay so this just all we need to do so let's close this so let's move down right here to this um Earth controller so um as usual like every other controller I'm just going to come right here I'm going to come here to copy this one and I'm going to close it also close this then I'm just going to come here to dump these three Annotations right here and and then I'm just going to change this one to Earth we just going to say Earth all right
so here we need some we need a few um a few dependencies right here so the first one is going to be the Earth manager there's the authentication manager being recreated last time so we're going to say um private final authentication oh this one is fine actually so we're going to say authentication Manager and then we're going to need um as a private final J WTS the one we created last time so these are the two dependencies we need here all right so now let's move down to create the endpoint we will say public response
entity and this one here I'm just going to say let's replace this one right here with our API response and These two right here we're going to replace it with the class we just created so we're going to say login login request sorry so we're going to say login request and we're just going to say this one is um let's just make it short call it request so here we're going to request the body at request body and then we're going to we're going to actually validate we're going to use A new annotation called at
valid because we actually validating the input right here right this one right here so since we are validating the input so we actually need this one right here to make sure that this classes be validated all right so now let's go inside so going to have o and um in here we just going to have U new user password token validation I watch the name I don't know what I'm saying so let Me just I just want to break this one down right here and also let's make it small a little bit then I'm going
to break this one down right here so we're just going to say user password authentication token right here and then we're going to pass in the user credentials then after that we're going to call the security context holder we say security SEC create context holder this one right here. get context do set authentication So after that we're just going to set the authenticated user inside this security context right here right and then um so what we're just going to do here is that after this one now we're going to generate we're going to generate a
token JT token for this user right here so now we going to say string J I'm going to call this on JWT equal JWT utils do generate token for user this one right here and we're just going to pass in the authenticated User right here then after this now the next we're going to do we're going to call our user details so I'm going to say shop user details this one right here um we call this user details and then we're just going to get the principal which is the authenticated user right then after that
we're just going to return the ID and the token back to the front end so we're just going to come here to say JWT Response and then we just going to pass in the token this one right here and we're going to get the principal ID right here so we're going to say um um us get user what's name user details so we to say user details. get ID get ID and we pass in JWT all right so after this now we're just going to return this to the authenticated user we just going to return
this to the front end so we're Going to say return response entity. okay and here we're just going to pass in um we're going to pass in a message right so we just going to say login Success login success then we going to pass in the body the the response right here okay so now we're going to catch we're going to catch the the error that is the authentication um exception right here So what we just need to do here I'm going to wrap all this in a try and catch then we're going to throw
the you see this the suggestion right here so we need to throw this exception right here so in case anything go wrong so here we just going to say return response entity but this one right here we're not going to say B request we're going to say um unauthorized so this I'm going to say status the status then here I'm going to Say an authorized and I'm going to say HTT HTTP HTTP status actually do unauthorized okay um so here we have uh we have this one right here and so this just all we need
to do to authenticate our user so um meanwhile just going to come here we're just going to return the message that is actually return from the back end so we don't need to pass extra message here so we're Just going to return this message right here but Meanwhile we're just going to um say here we're going to return none right there so this just all we need to do to authenticate our user so we're just going to say this one is is a post request so let's say at post mapping and then inside here we're
going to say login so this just all we need to do so in in the next video we're going to run our application and go to postman to Continue our testing see you there all right so now we have done with the with the login API implementation so now we're going to run our application so let's rerun it and then we're going to go to postman to continue with our testing um so we see that this project started running so let's go back to postman so right here in Postman we trying to you know last
time we trying to add a product right but the product said um the API said I must log in so Now let's go here let's go to our this our users right here so here we have eight users of which two of them are admin users right so this one is admin and this one also is admin so we have two admin users right here okay um so now we're just going to log in as as a user that is not an admin right so let's log in with user user number five or maybe let's
say user number four any any of the users um so here we're just going to We're going to have a new endpoint so this one is going to be let's choose this one right here and change let's change this to login so this one is going to be or or/ login this login and we're going to make on the post so we're going to pass in the r Json um so inside the body we just need the the the email and the password right so here we're going to say um Email email so the email
is um user user for at.com yeah that was the email and the password is is 1 2 3 4 5 6 so we're going to say 1 2 3 4 5 6 and yeah we're going to put a comma right here so this just all we need so now let's log in let's um yeah let's log in so here we have bad request bad request um why are we having bad request okay um here I miss spell the Email so let's try if that is the cause all right so that is the cause so now
we have logged in and um we're loging successfully so let's take this JWT right here let's copy this JWT right here um copy so here you see that we have the user ID and the token that is been generated for the user so now we are going let's go to the browser to actually see what what we have inside this token so here I'm going to say J um JWT Doio and then let's come down right here remove this one and let's put our own so we see that this user right here this user has
the row of a user right that you seeing right here and it has the ID and the email here um so let's go back right here so we're going to to take this token to the to here let's this add product right so let's take this one right here and let's go to the authorization and here we're going to select um be token and let's remove this Existing one right here and put this one okay so now we have gotten a token right here so let's try this L loog let's try to add this product
um so right here once again you see that the story has left you must log in to forbidden right so if we go here we're going to see that the request was a legal request but the server is refusing to respond to it unlike 401 authorized response authenticating will make no difference so which means We have already authenticated right so this is not unauthorized but we don't have the access we don't have the permission to do this operation um so right now what we just need to do before we before we solve this problem before
we solve this problem let's go back to the log where is the login let's go back to the login we are going to login a user that has the admin row so here we're going to log in a user admin user one right here and Let's send so now um let's copy this one right here let's copy and let's go back to the browser and see what is inside this one so let's replace this one here with this so we're going to see that this one actually is is an admin user right so let's go
back to let's go back to postman and we're going to come back here and replace this token with the one of admin right so let's send the same request add new Product okay so now you see that the request actually went through but this this product already exist in our database so let's change this product to make sure we submit a new product so let's say um Samsung let's change to Samsung Samsung watch right Samsung watch and this one is Samsung watch so let's send it all right so we see that now we are able
to sub to add a product to our database as an admin um so again let's Go back right here um this is the login uh so before we move forward I just want to save this one as a login so let's save it as so here inside our e-commerce um here we have uh we're going to create I saw a mirage so we're going to create a folder called Earth let's create and then inside this Earth we're just going to say login so we're going to say login user And then we're going to save all
right um so now we are going to log in another user just for us to test what we want to do so we're going to log in another user that is not an admin so remember we have tested the admin and everything is working actually so here we're just going to say user one I mean let's just say user two that is not an admin right so let's log in so now we've logged in and I've gotten our token right here so Let's copy this token and let's go back to the ad product and replace
the admit token with the user Tok toen so let's remove the admit token right here and let's put this in okay so let's send this again all right so now we have this forbidden error you know this message is not friendly right so what we're just going to do we are going to go inside our system to customize this message to make It friendly so let's go back to intelligent so right here we see that we actually um we are actually throwing the authentication exception right here uh and this one here is the is the
login is the login controller right and that is the error we are throwing right here now for us to see for us to customize a message for this one right here we're going to create um we are not just going to create um um because this one right here right here this one right here is Already in existant right this the um a builtin authentication exception right here so we are going to create something called Global exception right that is going to catch all exception globally so let's go right here you know remember last time
we've gotten some exception classes right here but they are not Global right so but this time around we're going to create the one that is global so here I'm just going to right click on this exception package and Let's create a new class so we're going to say Global so we're going to say Global exception Handler Handler right here I'm going to say enter all right so the first thing we're going to do right here we're going to come up right here to use annotation called at controller advice so we going to say at controller
advice so this one right here Means for every request that is going to any controller so this is the this is the advisor this is the class that is going to advise that controller so that class is going to that request is going to pass here going to check if that controller is going to be authenticated or not right so if that controller is going to be authenticated if there's any error exception that is been thrown in there and that exception is been declared right here so automatically That exception is going to be picked from
here and actually through so just as the name implies this is the controller advisor which means it advises the controller before the controller does anything right okay so for that now we're just going to go in here so we're going to create a class sorry a method so we say public um this one right here then this one we to say response entity as usual response entity but this time Around we're going to have the string we going to say string actually we could we could just use our own API stuff but here we just
want to make it simple right here so we just want to make it string and this time around we say handle um access deny exception let's just call this access deny handle access access denied this access actually s access den9 exception and let me make this one small Then inside here and inside this one right here we're just going to bring in the class let's remove this one from here and we're going to bring we're going to bring in the class that we actually creating right that the the the kind of exception that we want
to want to handle and remember that this exception we're trying to handle right here this one is actually access deny forbiding right so the user is logged in But he doesn't have the permission to do this operation so this access of this user is denied right so now we're going to come here to say access Denine access Denine exception this one right here but I'm not going to yeah let's let's take this one right here but I'm not going to take this one there's no point of taking this one right here for now let's remove
this one for now and then what we're just going to do we're Going to come up here to say at exception hand then we're going to set the value to the same class right here right so we could do it like this but anyways we can just also do it this way just remove this one right here and everything will still work right here it's look like this right so this is the exception that we try to actually throw we try to handle right now having done this right I'm just going to come in Here
to say string so this one I'm going to say string message then we're going to say um we're going to set the kind of message we want right here so here we're just going to say um you are not allowed or maybe you don't have access just let's just set a kind of yeah you don't have permission to access this resources or you don't have permission to this to this action to This action maybe or you don't have permission to maybe just anything anything you can just write anything there but um you get you get
the you get the idea what we're trying to do right here so after this right now we're just going to return a message so we're going to say return response entity here and this one here we just going to say um so this one we're just going to say Actually this one is a new response entity new response entity then inside this one we're going to bring in the message right and then we're just going to bring in the the um the the token oh is sorry not the token the the status code sorry guys
sometimes I do St so just going to say HTTP sorry this HTTP do status to say status then going to say forbidding forbiding this one right here so just all we need to do right here okay so now have done this Right now we're just going to run our project and let's see what happens so let's go right here and run this okay so now let's go back right here so we are going to perform this op this the same operation so let's hit on this so we see that you do not have permission to
this action right so you see that we have actually de Define a global exception that will give a general message that will advise the controllers to before they perform any Operation so this message is clear enough for a user to say oh the access is denied so in this case right now this this user might be directed to a page to access the page right to get this message right here all right so I hope you get the idea of what we try to do right here so this one is called Global exception Handler so
you can Define any exception even the exception right last time all the exception we be throwing from the controller they all can be Defined right here and you throw all the exception right from here right um so I think um now that we have gotten this right so let's go back remember last time we have already um tested some apis already so um now we're just going to make um a quick end to end test then we round up this tutorial so see you in the next video all right so finally we have gradually come
to the testy point of our application so in this video we are Going to uh we're going to be let me just close everything right here because let me just close everything and close this and then let's go to postman I'm just going to open this save it and close it close this one too and also close it just close everything right here let's save this one and close it all right so now we are going to test our application from end to end we're going to test our shop from end to end so now
we're going to have a user visit Our shop and um search for product book products and actually um add product to cat then actually order product so um the first we're just going to do right here is go we're going to come here to to our product so let's say a customer wants to get all product we get we have in a shop so we're just going to come here to pull out all products um okay we actually changed this part last time but we didn't update our saved URL so let's pull out all the
Products we've got in the database um so here we got um we got a list of products so we got Samsung we got um Apple we got so these are all the products we have gotten in our database right and so now a user wants to add a product to our database so meanwhile let's go to our M workbench and let's refresh our product so that we can see what we're going to add um so right here we have um let's say um a customer wants to buy iPhone 24 Pro Max right and now Right
here we have 20 of this iPhone in our inventory right and the price is 150 so a customer wants to buy this phone maybe wants to buy five of these and the ID of this product is uh where's the ID so the ID of this iPhone is 4 right so let's go back here to let's go back to um let's go back here what's the name Postman and then I'm going to close this one so let's go to our cat this our cat right here so let's add item to Cat uh remember let's quickly go
back to here and let's check what we have in our cat so this our cat the cat is empty and let's check the cut item so the cut item is also empty so let's check the order the order is empty the other item is empty right okay so let's go back right here so let's add a product to C so a product with ID number four so let's put this down here so the product we want to add is number four and we need five of iPhone iPhone 24 Pro Max so let's hit on Send
okay so here we need to log in right so let's go to Let's log in let's come right here and um meanwhile we have a login already right let's go to O right here so let's bring this out so now we're going to log in a user user number two right and let's log in right here and now a user is been logged in let's take the token and um let's close this for now so let's go back here to the card item so let's go to authorization and let's Choose here be token right here
and I'm going to put this one down a little bit so let's remove this one right here and put in a new token yeah okay so let's add this item to let's add this item to our card so right here we we see add item success so let's go to our database and here let's go to our cut items and let's refresh so here we have a we have a cat number one and a product ID number one quantity four and four * this 150 * 4 we Have um 750 is that correct I think
150 * supposed to be 600 right why is it 750 anyways um we're going to check this later but [Music] um what actually happened right here we're going to find out but I think 150 times oh oh sorry guys I'm looking at the product ID I wasn't looking at the quantity please forgive me so right here we have 150 * 5 give us 650 that is correct and here let's go to Our cat and see what we got so right here we have the total amount of this card is 750 then the user ID is
number three right we have a user ID with number three and we have um the cut ID number one so now let's say um this user wants to order so let's order this user so let's go back right here so now we have added a product to cat so let's save this one right here so let's remove this and let's remove this now let's go to the order let's go to order right Here where is the order so let's go to order and let's place order so right here the user ID is number three right
I think that was the user ID we saw right there in the database yeah the user ID was three so now the user ID user with ID number three wants to um place an order so let's hit on send and so right here we see that this order has been has been placed successfully right here and now that the order has been placed Successfully um then let's go back right here let's refresh our cat so we see that our cat has become empty and let's check the cat item and the cat item has become empty
let's go to to our order and this order has is pending so the user has ordered this the date that the user has ordered this item and let's check here the the ordered items so here we see the ordered items right here with ID number four and uh so um this is the end I think this is the end end go of Our of of our our demo right so now we have booked a product we we add a product to cat and we ordered a product and for every other things right here for every
you know we have been testing gradually since we have been creating right so we have systematically test tested every most of these end points right here um so you can just go ahead test to test all this API that we gotten right here because we actually have a lot of apis a lot of endpoints to testt Right here of which say if we're going to do that one right now it's going to take cost a lot of time doing that so I just encourage you to go ahead test all your apis if you have any
problems you can meet me on daily code work on Facebook our Facebook page is daily code work uh the link is below this video so you drop your comment and you can meet me on daily code work on Facebook so that we can actually trash it out so so far so good we have come to the end of Our shopping cart back end development um I hope this application we we will be helpful to you and you learn a lot of things here and also you can Al you can build upon this you can add
any functionalities you can build upon this this just a foundation that you can actually build upon you can create your front end to actually get this run maybe with nextjs reactjs view you can create any front end to actually work with this back end okay guys thank You um for staying with me this far um I really appreciate your effort and I appreciate your patience and also I appreciate your patronage please don't forget to subscribe to my to my Channel please help me that just the only thing you can that is one of the things
you can one of the area you can actually help me right help me to subscribe to my channel share my channel to your friends and introduce my channel to your friends as well thank you and I hope to see you In our next demo goodbye and happy coding