public IActionResult AddCart(AppCartDto appCartDto) { try { var UserId = _context.User.Where(i => i.OpenId == appCartDto.OpenId).FirstOrDefault().UserId; var checkObj = _context.AppCarts.Where(x => x.GoodsId == appCartDto.GoodsId && x.UserId == UserId).FirstOrDefault(); //判断该人是否购物车里添加过该商品 if (checkObj == null) { var AppCartObj = new AppCart(); AppCartObj.Num = appCartDto.Num; AppCartObj.Seclect = true; AppCartObj.GoodsId = appCartDto.GoodsId; AppCartObj.UserId = UserId; AppCartObj.CreatDate = DateTime.Now; AppCartObj.UpDate = DateTime.Now; _context.AppCarts.Add(AppCartObj); } else { checkObj.Num = appCartDto.Num + checkObj.Num; checkObj.UpDate = DateTime.Now; } _context.SaveChanges(); return(Ok("添加成功")); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public IActionResult GetCartAllNum(AppCartDto appCartDto) { try { var number = 0; var UserId = _context.User.Where(i => i.OpenId == appCartDto.OpenId).FirstOrDefault().UserId; var cartlist = _context.AppCarts.Where(i => i.UserId == UserId).ToList(); foreach (var p in cartlist) { number += p.Num; } return(Ok(number)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public IActionResult ModifyCart(AppCartDto appCartDto) { try { var AppCartObj = _context.AppCarts.Where(x => x.Id == appCartDto.Id).FirstOrDefault(); if (AppCartObj == null) { return(BadRequest("没有该购物车")); } AppCartObj.Num = appCartDto.Num; AppCartObj.UpDate = DateTime.Now; _context.SaveChanges(); return(Ok("修改成功")); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public IActionResult GetCartNum(AppCartDto appCartDto) { try { var UserId = _context.User.Where(i => i.OpenId == appCartDto.OpenId).FirstOrDefault().UserId; var shoppingCart = _context.AppCarts.Where(i => i.UserId == UserId && i.GoodsId == appCartDto.GoodsId).FirstOrDefault(); if (shoppingCart == null) { return(Ok("0")); } else { return(Ok(shoppingCart.Num)); } } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }