Ejemplo n.º 1
0
        public void CreateCustomer_Success()
        {
            var options = new CreateCustomerOptions()
            {
                Firstname = "Dimitris",
                Lastname  = "Pnevmatikos",
                Vatnumber = "123456789"
            };

            var result = customers_.CreateCustomer(options);

            Assert.NotNull(result);
            Assert.True(result.Success);
            Assert.NotNull(result.Data);

            var customer = customers_.SearchCustomers(
                new SearchCustomerOptions()
            {
                CustomerId = result.Data.CustomerId
            }).SingleOrDefault();

            Assert.NotNull(customer);
            Assert.Equal(options.Firstname, customer.Firstname);
            Assert.Equal(options.Vatnumber, customer.VatNumber);
            Assert.Equal(0, customer.TotalGross);
        }
        public Customer CreateCustomer(
            CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.Vatnumber))
            {
                return(null);
            }

            var customer = new Customer()
            {
                Lastname  = options.Lastname,
                Firstname = options.Firstname,
                VatNumber = options.Vatnumber
            };

            context_.Add(customer);

            if (context_.SaveChanges() > 0)
            {
                return(customer);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public ApiResult <Customer> Update(int id,
                                           CreateCustomerOptions options)
        {
            ApiResult <Customer> res = GetCustomerById(id);

            if (!res.Success)
            {
                return(res);
            }

            if (options == null)
            {
                return(ApiResult <Customer> .CreateUnsuccessful
                           (StatusCode.BadRequest, "null options"));
            }
            Customer c = res.Data;

            if (!string.IsNullOrWhiteSpace(options.Email) &&
                options.Email.Contains("@"))
            {
                c.Email = options.Email;
            }

            context.SaveChanges();

            return(ApiResult <Customer> .CreateSuccessful(c));
        }
Ejemplo n.º 4
0
        public void CreateCustomer_Success()
        {
            var options = new CreateCustomerOptions()
            {
                VatNumber = $"123{DateTime.UtcNow.Millisecond:D6}",
                Email     = "dsadas",
                FirstName = "Alex",
                LastName  = "ath",
                Phone     = "344234",
            };

            var result = csvc_.CreateCustomer(options);

            Assert.NotNull(result);

            var customer = csvc_.SearchCustomers(
                new SearchCustomerOptions()
            {
                VatNumber = options.VatNumber
            }).SingleOrDefault();

            Assert.NotNull(customer);
            Assert.Equal(options.Email, customer.Email);
            Assert.Equal(options.Phone, customer.Phone);
            Assert.True(customer.IsActive);
        }
Ejemplo n.º 5
0
        public bool CreateCustomer(CreateCustomerOptions opt)
        {
            if (opt == null)
            {
                return(false);
            }

            var customer = new Customer()
            {
                FirstName = opt.FirstName,
                LastName  = opt.LastName,
                Email     = opt.Email,
                VatNumber = opt.VatNumber,
                Created   = DateTime.Now
            };

            db_.Add(customer);
            //db_.SaveChanges();

            if (db_.SaveChanges() > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 6
0
        public void CreateCustomer_Success()
        {
            ICustomerService customerService =
                new CustomerService(context_);

            var optionsCreate = new CreateCustomerOptions()
            {
                Email     = "*****@*****.**",
                FirstName = "Dimitris",
                VatNumber = 1234567889
            };

            var customer = customerService.Create(optionsCreate);

            var optionsSearch = new SearchCustomerOptions()
            {
                Email     = optionsCreate.Email,
                FirstName = optionsCreate.FirstName,
                VatNumber = optionsCreate.VatNumber
            };

            var customers = customerService.Search(optionsSearch);

            Assert.NotNull(customer);
            Assert.Equal(optionsSearch.Email, customer.Email);
            Assert.Equal(optionsSearch.VatNumber, customer.VatNumber);
            Assert.Equal(optionsSearch.FirstName, customer.FirstName);
        }
Ejemplo n.º 7
0
        public ApiResult <Customer> Create(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.Email) ||
                !options.Email.Contains("@") ||
                string.IsNullOrWhiteSpace(options.VatNumber))
            {
                return(null);
            }

            var customer = new Customer();

            customer.VatNumber = options.VatNumber;
            customer.Email     = options.Email;
            customer.FirstName = options.FirstName;

            context.Set <Customer>().Add(customer);
            context.SaveChanges();

            return(ApiResult <Customer> .CreateSuccessful(customer));
        }
Ejemplo n.º 8
0
        public async Task <ApiResult <Customer> > CreateCustomerAsync(
            CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(new ApiResult <Customer>(
                           StatusCode.BadRequest, "Null options"));
            }

            if (string.IsNullOrWhiteSpace(options.VatNumber) ||
                string.IsNullOrWhiteSpace(options.Email))
            {
                return(new ApiResult <Customer>(
                           StatusCode.BadRequest, "Mail or Vat is empty"));
            }

            if (options.VatNumber.Length > 9)
            {
                return(new ApiResult <Customer>(
                           StatusCode.BadRequest, "Invalid vat"));
            }

            var exists = await SearchCustomers(
                new SearchCustomerOptions()
            {
                VatNumber = options.VatNumber
            }).AnyAsync();

            if (exists)
            {
                return(new ApiResult <Customer>(
                           StatusCode.Conflict,
                           $"Customer with {options.VatNumber} already exists"));
            }

            var customer = new Customer()
            {
                VatNumber = options.VatNumber,
                Firstname = options.FirstName,
                Lastname  = options.LastName,
                Email     = options.Email,
                Phone     = options.Phone,
                IsActive  = true
            };

            await context_.AddAsync(customer);

            try {
                await context_.SaveChangesAsync();
            }catch (Exception ex) {
                return(new ApiResult <Customer>(
                           StatusCode.InternalServerError, "Could not save customer"));
            }

            return(ApiResult <Customer> .CreateSuccess(customer));
        }
        public IActionResult Create([FromBody] CreateCustomerOptions options)
        {
            var customer = customerService_.CreateCustomer(options);

            if (customer == null)
            {
                return(BadRequest());
            }
            return(Json(customer));
        }
Ejemplo n.º 10
0
        public Result <Customer> CreateCustomer(
            CreateCustomerOptions options)
        {
            var result = new Result <Customer>();//Result typou customer

            //result.Data.CustomerId = 1;////Gia paradeigma

            if (options == null)
            {
                return(Result <Customer> .CreateFailed(StatusCode.BadRequest, "Null options"));

                // ^
                //result.ErrorCode = StatusCode.BadRequest;
                //result.ErrorText = "Null options";
                //return result;
            }

            if (string.IsNullOrWhiteSpace(options.VatNumber))
            {
                return(Result <Customer> .CreateFailed(StatusCode.BadRequest, "Null or Empty VatNumber"));

                //result.ErrorCode = StatusCode.BadRequest;
                //result.ErrorText = "Null or Empty VatNumber";
                //return result;
            }

            var customer = new Customer()
            {
                FirstName = options.FirstName,
                LastName  = options.LastName,
                Email     = options.Email,
                VatNumber = options.VatNumber,
                Phone     = options.Phone,
                IsActive  = true
            };

            context_.Add(customer);

            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
                if (rows <= 0)
                {
                    return(Result <Customer> .CreateFailed(StatusCode.InternalServerError, "Customer could not be updated"));
                }
            }
            catch (Exception ex)
            {
                return(Result <Customer> .CreateFailed(StatusCode.InternalServerError, ex.ToString()));
            }

            return(Result <Customer> .CreateSuccessful(customer));
        }
Ejemplo n.º 11
0
        public IActionResult Create([FromBody] CreateCustomerOptions options)
        {
            var result = customerService.CreateCustomer(options);

            if (!result.Success)
            {
                return(StatusCode((int)result.ErrorCode, result.ErrorText));
            }

            return(Json(result.Data));
        }
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,VatNumber,Address")] CustomerDto customer)
        {
            if (ModelState.IsValid)
            {
                await _customerService.CreateCustomerAsync(CreateCustomerOptions.MapFromCustomerDto(customer));

                return(RedirectToAction(nameof(Index)));
            }

            return(View(customer));
        }
        public async Task <Result <CustomerDto> > CreateCustomerAsync(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(new Result <CustomerDto>(ErrorCode.BadRequest, "Null options."));
            }

            if (string.IsNullOrWhiteSpace(options.FirstName) ||
                string.IsNullOrWhiteSpace(options.LastName) ||
                string.IsNullOrWhiteSpace(options.Address) ||
                string.IsNullOrWhiteSpace(options.VatNumber))
            {
                return(new Result <CustomerDto>(ErrorCode.BadRequest, "Not all required customer options provided."));
            }

            if (options.VatNumber.Length > 9)
            {
                return(new Result <CustomerDto>(ErrorCode.BadRequest, "Invalid vat number."));
            }

            var customerWithSameVat = await _context.Customers.SingleOrDefaultAsync(cus => cus.VatNumber == options.VatNumber);

            if (customerWithSameVat != null)
            {
                return(new Result <CustomerDto>(ErrorCode.Conflict, $"Customer with #{options.VatNumber} already exists."));
            }

            var newCustomer = new Customer
            {
                VatNumber = options.VatNumber,
                FirstName = options.FirstName,
                LastName  = options.LastName,
                Address   = options.Address
            };

            await _context.Customers.AddAsync(newCustomer);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);

                return(new Result <CustomerDto>(ErrorCode.InternalServerError, "Could not save customer."));
            }

            return(new Result <CustomerDto>
            {
                Data = CustomerDto.MapFromCustomer(newCustomer)
            });
        }
        public async Task CreateCustomer_Fail_VatNumber()
        {
            var options = new CreateCustomerOptions()
            {
                Email     = "dsadas",
                FirstName = "Alex",
                LastName  = "ath",
                Phone     = "344234",
            };
            var result = await csvc_.CreateCustomerAsync(options);

            Assert.Equal(StatusCode.BadRequest, result.ErrorCode);
        }
Ejemplo n.º 15
0
        public void CreateCustomer_Fail_Null_Email()
        {
            ICustomerService customerService = new CustomerService(context_);

            var options = new CreateCustomerOptions()
            {
                Email     = "",
                FirstName = "Dimitris",
                VatNumber = 0
            };

            var customer = customerService.Create(options);

            Assert.Null(customer);
        }
Ejemplo n.º 16
0
        public Customer CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.VatNumber) ||
                string.IsNullOrWhiteSpace(options.Email))
            {
                return(null);
            }

            if (options.VatNumber.Length > 9)
            {
                return(null);
            }

            var exists = SearchCustomers(
                new SearchCustomerOptions()
            {
                VatNumber = options.VatNumber
            }).Any();

            if (exists)
            {
                return(null);
            }

            var customer = new Customer()
            {
                VatNumber = options.VatNumber,
                Firstname = options.FirstName,
                Lastname  = options.LastName,
                Email     = options.Email,
                Phone     = options.Phone,
                IsActive  = true
            };

            context_.Add(customer);
            try {
                context_.SaveChanges();
            }catch (Exception ex) {
                return(null);
            }

            return(customer);
        }
Ejemplo n.º 17
0
        public IActionResult Create([FromBody] CreateCustomerOptions opt)
        {
            if (opt == null)
            {
                return(BadRequest());
            }

            var result = customerService_.CreateCustomer(opt);

            if (result == false)
            {
                return(BadRequest());
            }

            return(Json(result));
        }
Ejemplo n.º 18
0
        public Result <Customer> CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(Result <Customer>
                       .CreateFailed(StatusCode.BadRequest, "Null options"));
            }

            if (string.IsNullOrWhiteSpace(options.Vatnumber))
            {
                return(Result <Customer>
                       .CreateFailed(StatusCode.BadRequest, "Null or empty Vatnumber"));
            }

            var customer = new Customer()
            {
                Firstname = options.Firstname,
                Lastname  = options.Lastname,
                Email     = options.Email,
                VatNumber = options.Vatnumber,
                Phone     = options.Phone,
                IsActive  = true
            };

            dbContext.Add(customer);

            try
            {
                if (dbContext.SaveChanges() > 0)
                {
                    return(Result <Customer>
                           .CreateSuccessful(customer));
                }
                else
                {
                    return(Result <Customer>
                           .CreateFailed(StatusCode.InternalServerError, "Customer could not be updated"));
                }
            }
            catch (Exception ex)
            {
                return(Result <Customer>
                       .CreateFailed(StatusCode.InternalServerError, ex.ToString()));
            }
        }
Ejemplo n.º 19
0
        public Customer Create(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.Email) ||
                options.VatNumber == 0)
            {
                return(null);
            }

            var customer = new Customer();

            var vat = customer.VatNumber.ToString();

            if (vat.Length == 9)
            {
                customer.VatNumber = options.VatNumber;
            }
            else
            {
                return(null);
            }

            if (customer.Email.Contains("@"))
            {
                customer.Email = options.Email;
            }
            else
            {
                return(null);
            }

            customer.FirstName = options.FirstName;
            customer.LastName  = options.LastName;
            customer.Phone     = options.Phone;
            customer.Age       = options.Age;

            context.Set <Customer>().Add(customer);
            context.SaveChanges();
            return(customer);
        }
Ejemplo n.º 20
0
        public Result <Customer> CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(Result <Customer> .CreateFailed(
                           StatusCode.BadRequest, "Null options"));
            }

            if (string.IsNullOrWhiteSpace(options.Vatnumber))
            {
                return(Result <Customer> .CreateFailed(
                           StatusCode.BadRequest, "Null or empty VatNumber"));
            }

            var customer = new Customer()
            {
                Lastname  = options.LastName,
                Firstname = options.Firstname,
                VatNumber = options.Vatnumber
            };

            context_.Add(customer);

            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Customer> .CreateFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Customer> .CreateFailed(
                           StatusCode.InternalServerError,
                           "Customer could not be created"));
            }

            return(Result <Customer> .CreateSuccessful(customer));
        }
Ejemplo n.º 21
0
        public Customer CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = new Customer()
            {
                LastName  = options.Lastname,
                FirstName = options.Firstname,
                VatNumber = options.Vatnumber,
                Email     = options.Email,
                Phone     = options.Phone,
            };

            context_.Add(customer);
            return(context_.SaveChanges() > 0 ? customer : null);
        }
Ejemplo n.º 22
0
        public Customer CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = new Customer()
            {
                FirstName = options.FirstName,
                LastName  = options.LastName,
                Email     = options.Email,
                Phone     = options.Phone
            };

            context_.Add(customer);
            if (context_.SaveChanges() > 0)
            {
                return(customer);
            }
            return(null);
        }
Ejemplo n.º 23
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public Customer CreateCustomer(CreateCustomerOptions options)
 {
     if (options == null)
     {
         return(default);
 public ApiResult <Customer> UpdateCustomer([FromRoute] int id,
                                            [FromBody] CreateCustomerOptions options)
 {
     return(custService.Update(id, options));
 }
 public ApiResult <Customer> CreateCustomer(
     [FromBody] CreateCustomerOptions options)
 {
     return(custService.Create(options));
 }