Пример #1
0
 public IActionResult Add([FromBody] Customer customer)
 {
     if (ModelState.IsValid)
     {
         _customersService.CreateCustomerAsync(customer);
         return(Json(customer));
     }
     return(Json("Model is not valid"));
 }
Пример #2
0
        public async Task <IActionResult> Create([FromBody] CreateCustomerRequest customerRequest)
        {
            var customer = _mapper.Map <Customer>(customerRequest);

            customer = await customersService.CreateCustomerAsync(customer);

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUrl = $"{baseUrl}/{ApiRoutes.Customers.Get.Replace("{customerId}", customer.Id.ToString())}";

            return(Created(locationUrl, _mapper.Map <CustomerResponse>(customer)));
        }
Пример #3
0
        public async Task <IActionResult> Signup([FromBody] CustomerSignupRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new AuthFailedResponse
                {
                    Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                }));
            }
            var customer = _mapper.Map <Customer>(model);

            var authResponse = await _customersService.CreateCustomerAsync(customer, model.Password);

            if (!authResponse.Success)
            {
                return(BadRequest(new AuthFailedResponse {
                    Errors = authResponse.Errors
                }));
            }
            return(Ok(new AuthSuccessResponse {
                Token = authResponse.Token
            }));
        }
Пример #4
0
        public async Task <ServiceResponse <GetBasketDto> > CreateBasketAsync(CreateBasketDto createBasketDto)
        {
            var response = new ServiceResponse <GetBasketDto>();

            try
            {
                var basket = _mapper.Map <Basket>(createBasketDto);
                // Check for customer if email exist
                if (createBasketDto.Email != null)
                {
                    var customer = _context.Customers.FirstOrDefault(c => c.Email == createBasketDto.Email);
                    if (customer == null)
                    {
                        // Tạo khách hàng (bỏ sau)
                        var customerDto = new CreateCustomerDto
                        {
                            Email    = createBasketDto.Email,
                            FullName = createBasketDto.FullName,
                            Gender   = Gender.Unknown,
                            Address  = createBasketDto.Address
                        };
                        var createNewCustomerResult = await _customerService.CreateCustomerAsync(customerDto);

                        if (!createNewCustomerResult.Success)
                        {
                            response.Success = false;
                            response.Message = createNewCustomerResult.Message;

                            return(response);
                        }
                        customer = await _context.Customers.FirstOrDefaultAsync(c => c.Id == createNewCustomerResult.Data.Id);
                    }
                    basket.Customer = customer;
                }
                // Add basket item

                await _context.Baskets.AddAsync(basket);

                var getBasketDto = _mapper.Map <GetBasketDto>(basket);

                var TotalPrice = 0;

                foreach (CreateBasketItemDto createBasketItem in createBasketDto.Items)
                {
                    var newBasketItem = await _basketItemService.CreateBasketItemAsync(createBasketItem, basket);

                    TotalPrice += newBasketItem.Data.Price;
                }

                basket.TotalPrice = TotalPrice;
                basket.IsPaid     = false;
                basket.Status     = BasketStatus.Ordering;

                _context.BasketLogs.Add(new BasketLog()
                {
                    BasketId = basket.Id,
                    Status   = basket.Status,
                });

                await _context.SaveChangeWithValidationAsync();

                response.Data = getBasketDto;

                response.Data.TotalPrice = TotalPrice;

                return(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                response.Code    = ErrorCode.BASKET_UNEXPECTED_ERROR;

                _logger.LogError(ex.Message, ex.StackTrace);
                return(response);
            }
        }