File tree Expand file tree Collapse file tree
Cherry.Services.ShoppingCartAPI/Controllers Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -190,10 +190,41 @@ public async Task<ResponseDto> RemoveCart([FromBody]int cartDetailsId)
190190 if ( totalCountofCartItem == 1 )
191191 {
192192 var cartHeaderToRemove = await _db . CartHeaders . FirstOrDefaultAsync (
193- u=> u . CartHeaderId == cartDetails . CartHeaderId ) ;
193+ u=> u . CartHeaderId == cartDetails . CartHeaderId ) ;
194194 _db . CartHeaders . Remove ( cartHeaderToRemove ) ;
195195 }
196- await _db . SaveChangesAsync ( ) ;
196+ await _db . SaveChangesAsync ( ) ;
197+ _response . Result = true ;
198+ }
199+ catch ( Exception ex )
200+ {
201+ _response . Message = ex . Message . ToString ( ) ;
202+ _response . IsSuccess = false ;
203+ }
204+ return _response ;
205+ }
206+
207+ [ HttpDelete ( "ClearCart/{userId}" ) ]
208+ public async Task < ResponseDto > ClearCart ( string userId )
209+ {
210+ try
211+ {
212+ var cartHeader = await _db . CartHeaders
213+ . FirstOrDefaultAsync ( u => u . UserId == userId ) ;
214+
215+ if ( cartHeader != null )
216+ {
217+ // Remove all cart details associated with this cart header
218+ var cartDetails = _db . CartDetails
219+ . Where ( u => u . CartHeaderId == cartHeader . CartHeaderId ) ;
220+ _db . CartDetails . RemoveRange ( cartDetails ) ;
221+
222+ // Remove the cart header
223+ _db . CartHeaders . Remove ( cartHeader ) ;
224+
225+ await _db . SaveChangesAsync ( ) ;
226+ }
227+
197228 _response . Result = true ;
198229 }
199230 catch ( Exception ex )
Original file line number Diff line number Diff line change @@ -97,6 +97,12 @@ public async Task<IActionResult> Confirmation(int orderId)
9797 OrderHeaderDto orderHeader = JsonConvert . DeserializeObject < OrderHeaderDto > ( Convert . ToString ( response . Result ) ) ;
9898 if ( orderHeader . Status == SD . Status_Approved )
9999 {
100+ // Clear the cart after successful payment
101+ var userId = User . Claims . Where ( u => u . Type == JwtRegisteredClaimNames . Sub ) ? . FirstOrDefault ( ) ? . Value ;
102+ if ( ! string . IsNullOrEmpty ( userId ) )
103+ {
104+ await _cartService . ClearCartAsync ( userId ) ;
105+ }
100106 return View ( orderId ) ;
101107 }
102108 }
Original file line number Diff line number Diff line change @@ -59,5 +59,14 @@ public CartService(IBaseService baseService)
5959 Url = SD . ShoppingCartAPIBase + "/api/cart/CartUpsert"
6060 } ) ;
6161 }
62+
63+ public async Task < ResponseDto ? > ClearCartAsync ( string userId )
64+ {
65+ return await _baseService . SendAsync ( new RequestDto ( )
66+ {
67+ ApiType = SD . ApiType . DELETE ,
68+ Url = SD . ShoppingCartAPIBase + "/api/cart/ClearCart/" + userId
69+ } ) ;
70+ }
6271 }
6372}
Original file line number Diff line number Diff line change @@ -9,5 +9,6 @@ public interface ICartService
99 Task < ResponseDto ? > RemoveFromCartAsync ( int cartDetailsId ) ;
1010 Task < ResponseDto ? > ApplyCouponAsync ( CartDto cartDto ) ;
1111 Task < ResponseDto ? > EmailCart ( CartDto cartDto ) ;
12+ Task < ResponseDto ? > ClearCartAsync ( string userId ) ;
1213 }
1314}
Original file line number Diff line number Diff line change 55 {
66 <input asp-for =" CartHeader.UserId" hidden />
77 <input asp-for =" CartHeader.CartHeaderId" hidden />
8+ <input asp-for =" CartHeader.CouponCode" hidden />
89 <input asp-for =" CartHeader.Discount" hidden />
910 <input asp-for =" CartHeader.CartTotal" hidden />
1011 }
You can’t perform that action at this time.
0 commit comments