Skip to content

Instantly share code, notes, and snippets.

@chaudarywajid
Created September 22, 2022 14:30
Show Gist options
  • Save chaudarywajid/52e66ebb9999f51eceef824dbfdb4e1f to your computer and use it in GitHub Desktop.
Save chaudarywajid/52e66ebb9999f51eceef824dbfdb4e1f to your computer and use it in GitHub Desktop.
Web API to Add Item into Cart
[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