Пример #1
0
        public async Task <IActionResult> CreateCustomer(CreateCustomerDto model)
        {
            var result = await _command.ExecuteAsync(model);

            return(Ok(result));
        }
Пример #2
0
        public static void ValidateData(this IHDSContext context, CreateCustomerDto dto)
        {
            var errors = new StringBuilder();

            // FirstName
            errors.AddIfExists(dto.FirstName.ValidateRequired(ValidationMessages.FirstNameRequired));
            errors.AddIfExists(dto.FirstName.ValidateLength(100, ValidationMessages.FirstNameLength));
            // LastName
            errors.AddIfExists(dto.LastName.ValidateRequired(ValidationMessages.LastNameRequired));
            errors.AddIfExists(dto.LastName.ValidateLength(100, ValidationMessages.LastNameLength));
            if (dto.Addresses != null && dto.Addresses.Any())
            {
                // Addresses
                for (int i = 0; i < dto.Addresses.Count; i++)
                {
                    if (dto.Addresses.Select(o => o.Primary).Count() > 1)
                    {
                        errors.AddIfExists(ValidationMessages.PrimaryAddressSingle);
                    }
                    // AddressTypeID
                    errors.AddIfExists(dto.Addresses[i].AddressTypeID.ValidateRequired($"Address [{i}]:{ValidationMessages.AddressTypeIDRequired}"));
                    errors.AddIfExists(context.KeyExists <AddressType>(dto.Addresses[i].AddressTypeID, $"Address [{i}]:{ValidationMessages.AddressTypeIDNotFound}"));
                    // Address
                    if (dto.Addresses[i].Address != null)
                    {
                        // Street Address
                        errors.AddIfExists(dto.Addresses[i].Address?.StreetAddress.ValidateRequired($"Address [{i}]:{ValidationMessages.StreetAddressRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.StreetAddress.ValidateLength(100, $"Address [{i}]:{ValidationMessages.StreetAddressRequired}"));
                        // City
                        errors.AddIfExists(dto.Addresses[i].Address?.City.ValidateRequired($"Address [{i}]:{ValidationMessages.CityRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.City.ValidateLength(50, $"Address [{i}]:{ValidationMessages.CityLength}"));
                        // State
                        errors.AddIfExists(dto.Addresses[i].Address?.State.ValidateRequired($"Address [{i}]:{ValidationMessages.StateRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.State.ValidateLength(50, $"Address [{i}]:{ValidationMessages.StateLength}"));
                        // PostCode
                        errors.AddIfExists(dto.Addresses[i].Address?.PostalCode.ValidateRequired($"Address [{i}]:{ValidationMessages.PostalCodeRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.PostalCode.ValidateLength(10, $"Address [{i}]:{ValidationMessages.PostalCodeLength}"));
                    }
                    else
                    {
                        errors.AddIfExists($"Address [{i}]:{ValidationMessages.AddressRequired}");
                    }
                }
            }
            if (dto.ContactMethods != null && dto.ContactMethods.Any())
            {
                // ContactMethods
                for (int i = 0; i < dto.ContactMethods.Count; i++)
                {
                    // ContactMethodTypeID
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodTypeID.ValidateRequired($"ContactMethod [{i}]:{ValidationMessages.ContactMethodTypeIDRequired}"));
                    errors.AddIfExists(context.KeyExists <ContactMethodType>(dto.ContactMethods[i].ContactMethodTypeID, $"ContactMethod [{i}]:{ValidationMessages.ContactMethodTypeIDNotFound}"));
                    // ContactMethod Value
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodValue.ValidateRequired($"ContactMethod [{i}]:{ValidationMessages.ContactMethodValueRequired}"));
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodValue.ValidateLength(100, $"ContactMethod [{i}]:{ValidationMessages.ContactMethodValueLength}"));
                }
            }

            if (errors.Length > 0)
            {
                throw new ValidationException(errors.ToString());
            }
        }
Пример #3
0
 public static Customer ToEntity(this CreateCustomerDto dto)
 {
     Init();
     return(Mapper.Map <Customer>(dto));
 }
Пример #4
0
 public async Task Post([FromBody] CreateCustomerDto dto)
 {
     //var dto = new CreateCustomerDto() { LoginName = "abc", Password = "******", Name = "ABC", Email = "*****@*****.**" };
     await _customerService.AddCustomerAsync(dto);
 }
Пример #5
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);
            }
        }
Пример #6
0
 public IActionResult Post([FromBody] CreateCustomerDto createModel) =>
 _mapper.Map <CustomerInsertModel>(createModel)
 .Map(_commandRepo.Insert)
 .Map(x => Created(new { id = x }))
 .Reduce(_ => BadRequest(), error => error is ArgumentNotSet)
 .Reduce(_ => InternalServerError(), x => _logger.LogError(x.ToString()));
Пример #7
0
 public async Task <IActionResult> Create([FromBody] CreateCustomerDto dto)
 => await GetResponse(async() =>
                      new ApiResponseViewModel(true, "Customer Created Successfully",
                                               await _customerService.Create(dto, UserId)));
Пример #8
0
 public async Task <ValidationResult> Create(CreateCustomerDto dto)
 => await _mediator.SendCommand(new RegisterNewCustomerCommand(dto.Name, dto.Email, dto.BirthDate));
 public async Task <IActionResult> Create(CreateCustomerDto createUserDto)
 {
     return(ActionResultInstance(await _userService.CreateUserAsync(createUserDto)));
 }