public async Task <IActionResult> AddShoppingCartItem([FromBody] AddShoppingCartItemDto shoppingCartItemDto) { var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; var shoppingCart = await touristRouteRepository.GetShoppingCartByUserId(userId); var touristRoute = await touristRouteRepository.GetTouristRouteAsync(shoppingCartItemDto.TouristRouteId); if (touristRoute == null) { return(NotFound("旅游路线不存在")); } var lineItem = new LineItem() { TouristRouteId = shoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingCart.Id, OriginalPrice = touristRoute.OriginalPrice, DiscountPresent = touristRoute.DiscountPresent }; await touristRouteRepository.ShoppingCartItem(lineItem); await touristRouteRepository.SaveAsync(); return(Ok(mapper.Map <ShoppingCartDto>(shoppingCart))); }
public async Task <IActionResult> AddShoppingCartItem( [FromBody] AddShoppingCartItemDto addShoppingCartItemDto ) { //1.get the current user var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; //2 use the userId to get shoppingcart var shoppingCart = await _touristRouteRepository.GetShoppingCartByUserId(userId); //3. create line item var touristRoute = await _touristRouteRepository.GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId); if (touristRoute == null) { return(NotFound("tourist route does not exist")); } var lineItem = new LineItem() { TouristRouteId = addShoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingCart.Id, OriginalPrice = touristRoute.OriginalPrice, DiscountPresent = touristRoute.DiscountPrice }; //4. add lineItem, save data to database await _touristRouteRepository.AddShoppingCartItem(lineItem); await _touristRouteRepository.SaveAsync(); return(Ok(_mapper.Map <ShoppingCartDto>(shoppingCart))); }
public async Task <IActionResult> AddShoppingCartItem( [FromBody] AddShoppingCartItemDto addShoppingCartItemDto) { //1. 取得當前用戶 var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; //2. 使用userid獲得購物車 var shoppingCart = await _touristRouteRepository.GetShoppingCartByUserId(userId); //3. 創建lineItem var touristRoute = await _touristRouteRepository .GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId); if (touristRoute == null) { return(NotFound("旅遊路線不存在!")); } var lineItem = new LineItem() { TouristRouteId = addShoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingCart.Id, OriginPrice = touristRoute.OriginPrice, DiscountPresent = touristRoute.DiscountPresent }; // 4. 添加lineItem,並保存至DB await _touristRouteRepository.AddShoppingCartItem(lineItem); await _touristRouteRepository.SaveAsync(); var shoppingCartDto = _mapper.Map <ShoppingCartDto>(shoppingCart); return(Ok(shoppingCartDto)); }
public async Task <IActionResult> AddShoppingCartItem([FromBody] AddShoppingCartItemDto shoppingCartItemDto) { try { var result = await _shoppingCartServices.AddShoppingCartItem(shoppingCartItemDto); return(Ok(result)); } catch (NotFoundException ex) { return(NotFound(ex.Message)); } catch (ShoppingCartValidationException ex) { return(BadRequest(ex.Message)); } catch (BadRequestException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { _logger.LogCritical($"AddShoppingCartItem() Error: {ex}"); return(StatusCode(500, "Internal Server Error")); } }
public async Task <IActionResult> AddShoppingCartItem([FromBody] AddShoppingCartItemDto addShoppingCartItemDto) { // get user var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // get shoppingCart by userId var shoppingCart = await _productRepository.GetShoppingCartByUserId(userId); //create lineItem var product = await _productRepository.GetProductAsync(addShoppingCartItemDto.ProductId); if (product == null) { return(NotFound("product is not existed")); } var lineItem = new LineItem() { ProductId = addShoppingCartItemDto.ProductId, ShoppingCartId = shoppingCart.Id, OriginalPrice = product.OriginalPrice, DiscountPercentage = product.DiscountPercentage }; //add lineItem and save it in the database await _productRepository.AddShoppingCartItem(lineItem); await _productRepository.SaveAsync(); return(Ok(_mapper.Map <ShoppingCartDto>(shoppingCart))); }
public async Task <IActionResult> AddShoppingCartItem([FromBody] AddShoppingCartItemDto addShoppingCartItemDto) { // 1 获取当前用户 var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // 2 使用 userid 获得购物车 var shoppingcart = await _touristRouteRepository.GetShoppingCartByUserId(userId); // 3 创建 lineItem var touristRoute = await _touristRouteRepository.GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId); if (touristRoute == null) { return(NotFound("路由路线不存在")); } var lineItem = new LineItem() { TouristRouteId = addShoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingcart.Id, OriginalPrice = touristRoute.OriginalPrice, DiscountPresent = touristRoute.DiscountPresent }; // 4 添加 lineitem,并保存数据库 await _touristRouteRepository.AddShoppingCartItem(lineItem); await _touristRouteRepository.SaveAsync(); return(Ok(_mapper.Map <ShoppingCartDto>(shoppingcart))); }
public async Task <IActionResult> AddShoppingCartItem([FromBody] AddShoppingCartItemDto addShoppingCartItemDto) { var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; var shoppingCart = await _touristRouteRepository.GetShoppingCartByUserIdAsync(userId); var touristRoute = await _touristRouteRepository.GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId); if (touristRoute == null) { return(NotFound($"Tourist route {addShoppingCartItemDto.TouristRouteId} not found")); } var lineItem = new LineItem { TouristRouteId = addShoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingCart.Id, OriginalPrice = touristRoute.OriginalPrice, DiscountPercent = touristRoute.DiscountPercent }; await _touristRouteRepository.AddShoppingCartItemAsync(lineItem); await _touristRouteRepository.SaveAsync(); var shoppingCartDto = _mapper.Map <ShoppingCartDto>(shoppingCart); return(Ok(shoppingCartDto)); }
public async Task <IActionResult> Post([FromBody] AddShoppingCartItemDto addShoppingCartItemDto) { var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; var shoppingCart = await _touristRouteRepository.GetUserShoppingCart(userId); var touristRouteFromRepo = await _touristRouteRepository.GetTouristRouteAsync(addShoppingCartItemDto.TouristRouteId); if (touristRouteFromRepo == null) { return(NotFound($"旅游路线{addShoppingCartItemDto.TouristRouteId}不存在")); } var lineItem = new LineItem() { TouristRouteId = addShoppingCartItemDto.TouristRouteId, ShoppingCartId = shoppingCart.Id, OriginalPrice = touristRouteFromRepo.OriginalPrice, DiscountPresent = touristRouteFromRepo.DiscountPresent }; // 4 添加LineItem,并保存数据库 await _touristRouteRepository.AddShoppingCartItem(lineItem); await _touristRouteRepository.SaveAsync(); return(Ok(_mapper.Map <ShoppingCartDto>(shoppingCart))); }
public async Task <ShoppingCartDto> AddShoppingCartItem(AddShoppingCartItemDto itemDto) { var user = await GetCurrentUser(); var shoppingCart = await _shoppingCartRepository.GetShoppingCartByUserIdAsync(user.Id); if (shoppingCart == null) { throw new NotFoundException("ShoppingCart not found"); } var course = await _courseRepository.GetCourseAsync(itemDto.CourseId); if (course == null) { throw new NotFoundException("Course not found."); } shoppingCart.AddCourse(itemDto.Quantity, course, user); await _shoppingCartRepository.SaveAsync(); return(Mapper.Map <ShoppingCartDto>(shoppingCart)); }