Created
September 22, 2022 14:30
-
-
Save chaudarywajid/52e66ebb9999f51eceef824dbfdb4e1f to your computer and use it in GitHub Desktop.
Web API to Add Item into Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost("addcart")] | |
public IActionResult AddCart(int userId, int productId) | |
{ | |
try | |
{ | |
var isProductAlreadyAdded = userCartEntity | |
.FindBy(a => a.UserId == userId && a.ProductId == productId && !a.IsDeleted).Any(); | |
if (!isProductAlreadyAdded) | |
{ | |
var userCart = new UserCartEntity | |
{ | |
UserId = userId, | |
TotalQuantity = 1, | |
AddedDateTime = DateTime.Now, | |
IsDeleted = false, | |
ProductId = productId | |
}; | |
userCartEntity.Add(userCart); | |
return Ok(new Status {IsSuccess = true, Message = "Item added to cart successfully."}); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex); | |
return Ok(new Status { IsSuccess = false, Message = ex.Message }); | |
} | |
return Ok(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment