Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views5 pages

Update

This lesson covers the implementation of the update cart method in a shopping cart repository class, which updates the shopping cart based on product and user IDs. It includes functionality to increase, decrease, or delete items in the cart using a switch statement to handle different actions. The method checks for existing items, updates quantities and total amounts, and saves changes asynchronously.

Uploaded by

Asfend Yar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Update

This lesson covers the implementation of the update cart method in a shopping cart repository class, which updates the shopping cart based on product and user IDs. It includes functionality to increase, decrease, or delete items in the cart using a switch statement to handle different actions. The method checks for existing items, updates quantities and total amounts, and saves changes asynchronously.

Uploaded by

Asfend Yar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In this lesson we’re going to start our

work with the update cart method of


shopping cart repository class.
This method will take the product id and
user id and will update the shopping
cart.
Let me tell you what this method does.
First we’ll await and async then use db
context dot shopping cart items dot first
or default method and there we’ll use
the lambda expression to check whether
the product id of shopping cart item
table is equal to the product id which
we’ll provide and user id of the shopping
cart item table is equal to the user id
which we’ll provide in this method. Then
store the result in a variable and name it
existing shopping cart item.
Right after that we’ll check if the existing
cart item is null then return false.
Otherwise we’ll increase , decrease or
delete the shopping cart item.
For example if the user has some item in
his shopping cart and user will tap this
add button then we’ll increase the
quantity and update the total amount.
Similalrly if the user will tap this minus
option then we’ll decrease the quantity
and update the total amount and if the
user will press this delete option then
we’ll delete that specific product from
our shopping cart item.
So first of all we’ll add a parameter of
type string inside our method and name
it action. Then we’ll also add this inside
our interface,
Now we’ll use the switch statement and
there we’ll pass this action and then add
three cases , increase , decrease and
delete.
In the increase we’ll increment the
quantity of the existing cart item and
then we’ll multiply the existing cart item
price and existing cart item quantity and
update the existing cart item total
amount.
Then in the decrease we’ll use the
reverse of this but let’s say if there’s only
one product quantity and if we’ll press
this minus option then this will goes in
negative number. To overcome this
problem we’ll check if the existing cart
item dot quantity is greater than zero
then we’ll copy these two line and paste
it here but this time we’ll use the
decrement sign.
Else if there only one product quantity
then tapping this minus option will
remove that product from the shopping
cart so we’ll use db context dot shopping
cart items dot remove method and pass
the existing shopping cart item.
Now if the user will press this delete
option then we’ll remove the product
from our shopping cart so we’ll use the
same line of code which is db context
dot shopping cart items dot remove
method and pass the existing cart item.
In the default case we’ll pass false and
outside the switch statement we’ll call
the save changes async method and
return true,
Alright this method has been completed.
Now in the next lesson we’ll write our
controller and then call these methods
from our controller. I’ll see you in the
next lesson.

You might also like