public Customer RegisterCustomer_Success(string countryCode) { var options = new RegisterCustomerOptions() { Firstname = "Lefteris", Lastname = "Xidis", Type = Constants.CustomerType.PhysicalEntity, CountryCode = countryCode, VatNumber = GenerateVat(countryCode) }; var result = _customers.Register(options); Assert.True(result.IsSuccessful()); Assert.NotNull(result.Data); var customer = result.Data; Assert.Equal(options.Firstname, customer.Firstname); Assert.Equal(options.Lastname, customer.Lastname); Assert.Equal(options.Type, customer.Type); Assert.Equal(options.VatNumber, customer.VatNumber); Assert.True(customer.IsActive); return(customer); }
//[Route("Register")] public async Task <IActionResult> Register( [FromBody] RegisterCustomerOptions options) { var customer = await _customer.RegisterAsync(options); return(Json(customer)); }
public async Task <IActionResult> Register( [FromBody] RegisterCustomerOptions options) { var result = await _customers.RegisterAsync(options); if (result.Code != ResultCode.Success) { return(StatusCode(result.Code, result.ErrorMessage)); } return(Json(result)); }
public async Task <Result <Customer> > RegisterAsync(RegisterCustomerOptions options) { if (string.IsNullOrWhiteSpace(options?.VatNumber)) { return(new Result <Customer>() { ErrorMessage = "VatNumber is empty", Code = Constants.ResultCode.BadRequest }); } if (options.VatNumber.Length != 9) { return(new Result <Customer>() { ErrorMessage = "VatNumber requires 9 digits", Code = Constants.ResultCode.BadRequest }); } if (string.IsNullOrWhiteSpace(options.Surname) || string.IsNullOrWhiteSpace(options.Name)) { return(null); } var customer = new Customer() { Name = options.Name, Surname = options.Surname, VatNumber = options.VatNumber }; _dbContext.Add(customer); try { await _dbContext.SaveChangesAsync(); } catch (Exception) { return(new Result <Customer>() { Code = Constants.ResultCode.InternalServerError, ErrorMessage = "Customer could not be saved" }); } return(new Result <Customer>() { Code = Constants.ResultCode.Success, Data = customer }); }
public async void Test_RegisterCustomerAsync() { var options = new RegisterCustomerOptions() { Name = "Sofia", SurName = "Ourolidou", VatNumber = "065252238", CustType = CustomerType.Personal, Address = "Irakleitou 17" }; var customer = await _customer.RegisterCustomerAsync(options); Assert.NotNull(customer); }
public async Task Update_Customer_Success_with_Async() { var options = new RegisterCustomerOptions() { CustomerBankID = "CUSTBNK0219", Name = "Melitini", SureName = "Parginou", VATNumber = "186493164", CustType = CustomerType.Personal, Address = "Aigyptou 84 - Glyfada" }; var result = await _customer.UpdateCustomerAsync(2007, options); Assert.Equal(ResultCodes.Success, result.Code); }
private static void RunCustomerService(string[] args) { if (args.Length == 1) { Console.WriteLine(Usage()); } else { var _customer = Scope.ServiceProvider.GetRequiredService <ICustomerService>(); switch (args[1]) { case "-a": var options = new RegisterCustomerOptions() { Name = args[2].Split(',')[0], SureName = args[2].Split(',')[1], VATNumber = args[2].Split(',')[2], CustomerBankID = args[2].Split(',')[3], CustType = (CustomerType)int.Parse(args[2].Split(',')[4]), Address = args[2].Split(',')[5] }; _customer.Register(options); break; case "-q": var result = _customer.GetCustomerbyID(int.Parse(args[2])); if (result.Code == ResultCodes.Success) { var cust = result.Data; Console.WriteLine( $"Customer ID: {cust.CustomerId}\n" + $"Customer Name: {cust.Name}\n" + $"Customer Sure Name: {cust.SureName}\n" + $"Customer Bank ID: {cust.CustBankID}\n" + $"Customer VAT Number: {cust.VatNumber}\n" + $"Customer Address: {cust.Address}\n" + $"Created On: {cust.AuditInfo.Created}\n" + $"Customer Active: {cust.Active}"); } else { Console.WriteLine("Customer ID not found !"); } break; } } }
/// <summary> /// Updates a given customer Id with Async support /// </summary> /// <param name="customerID">The customer Id to updated</param> /// <param name="options">The information to be updated as type of RegisterCustomerOptions </param> /// <returns> /// Result.Code should be Success(200) /// Check Result.Code and Result.Message to get more details about possible errors /// </returns> public async Task <Result <Customer> > UpdateCustomerAsync(int customerID, RegisterCustomerOptions options) { if (options == null) { return(new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Options must be specified" }); } var result = await GetCustomerbyIDAsync(customerID); if (result.Code == ResultCodes.Success) { var customer = result.Data; customer.Address = options.Address; customer.CustBankID = options.CustomerBankID; customer.CustType = options.CustType; customer.Name = options.Name; customer.SureName = options.SureName; customer.VatNumber = options.VATNumber; _dbContext.Update(customer); await _dbContext.SaveChangesAsync(); return(new Result <Customer>() { Code = ResultCodes.Success, Message = $"Customer ID {customerID} updated successfully", Data = customer }); } else { return(new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Failed to update information for Customer ID {customerID}" }); } }
public void RegisterCustomer_Fail_Customer_Exists() { var customer = RegisterCustomer_Success(Constants.Country.GreekCountryCode); Assert.NotNull(customer); var options = new RegisterCustomerOptions() { CountryCode = customer.CountryCode, VatNumber = customer.VatNumber, Firstname = "Name", Lastname = "Lastname", Type = Constants.CustomerType.PhysicalEntity }; var result = _customers.Register(options); Assert.False(result.IsSuccessful()); Assert.Equal(Constants.ApiResultCode.Conflict, result.Code); }
public Customer Add_New_Customer_With_DI(string countryCode) { var vatNumber = GenerateVat(countryCode); Assert.NotNull(vatNumber); var options = new RegisterCustomerOptions() { CustomerBankID = "CUSTBNK0209", Name = "Iliana", SureName = "Panagiotopoulou", VATNumber = vatNumber, CustType = CustomerType.Personal, Address = "Aigyptou 84", CountryCode = countryCode }; var customer = _customer.Register(options); Assert.Equal(ResultCodes.Success, customer.Code); return(customer.Data); }
public void RegisterCustomer_Fail_InvalidOptions() { var options = new RegisterCustomerOptions() { Lastname = "Pnevmatikos", Type = Constants.CustomerType.PhysicalEntity, CountryCode = Constants.Country.GreekCountryCode, VatNumber = "1111111" }; // Firstname var result = _customers.Register(options); Assert.False(result.IsSuccessful()); Assert.Equal(Constants.ApiResultCode.BadRequest, result.Code); // lastname options.Firstname = "Dimitris"; options.Lastname = null; result = _customers.Register(options); Assert.False(result.IsSuccessful()); Assert.Equal(Constants.ApiResultCode.BadRequest, result.Code); }
public ApiResult <Customer> Register(RegisterCustomerOptions options) { if (options == null) { return(new ApiResult <Customer>() { Code = ApiResultCode.BadRequest, ErrorText = $"Null {nameof(options)}" }); } if (string.IsNullOrWhiteSpace(options.Firstname)) { return(new ApiResult <Customer>() { Code = ApiResultCode.BadRequest, ErrorText = $"Null or empty {nameof(options.Firstname)}" }); } if (string.IsNullOrWhiteSpace(options.Lastname)) { return(new ApiResult <Customer>() { Code = ApiResultCode.BadRequest, ErrorText = $"Null or empty {nameof(options.Lastname)}" }); } if (options.Type == Constants.CustomerType.Undefined) { return(new ApiResult <Customer>() { Code = ApiResultCode.BadRequest, ErrorText = $"Invalid customer type {nameof(options.Type)}" }); } if (!IsValidVatNumber(options.CountryCode, options.VatNumber)) { return(new ApiResult <Customer>() { Code = ApiResultCode.BadRequest, ErrorText = $"Invalid Vat number {options.VatNumber}" }); } var customerExists = Exists(options.VatNumber); if (customerExists) { return(new ApiResult <Customer>() { Code = ApiResultCode.Conflict, ErrorText = $"Customer with Vat number {options.VatNumber} already exists" }); } var customer = _mapper.Map <Customer>(options); customer.IsActive = true; _dbContext.Add(customer); try { _dbContext.SaveChanges(); } catch (Exception ex) { // log Console.WriteLine(ex); return(new ApiResult <Customer>() { Code = ApiResultCode.InternalServerError, ErrorText = $"Customer could not be saved" }); } return(new ApiResult <Customer>() { Data = customer }); }
public IActionResult Register( [FromBody] RegisterCustomerOptions options) { return(Ok(options)); }
public async Task <Result <Customer> > RegisterCustomerAsync(RegisterCustomerOptions options) { if (string.IsNullOrWhiteSpace(options.Name)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer name is empty!" } } ; if (string.IsNullOrWhiteSpace(options.SurName)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer sure name is empty!" } } ; if (options.VatNumber.Length != 9) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"VAT Number length is invalid!" } } ; long vatNumber = 0; if (!long.TryParse(options.VatNumber, out vatNumber)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"VAT number must be numeric!" } } ; var customer = new Customer() { Name = options.Name, SurName = options.SurName, VatNumber = options.VatNumber, CustType = options.CustType, Address = options.Address }; await _dbContext.AddAsync <Customer>(customer); await _dbContext.SaveChangesAsync(); return(new Result <Customer>() { Code = ResultCodes.Success, Data = customer }); }
/// <summary> /// Adds a new customer /// </summary> /// <param name="options">RegisterCustomerOptions</param> /// <returns> /// Result.Code should be Success(200) /// Check Result.Code and Result.Message to get more details about possible errors /// </returns> public Result <Customer> Register(RegisterCustomerOptions options) { if (string.IsNullOrWhiteSpace(options.Name)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer name is empty!" } } ; if (string.IsNullOrWhiteSpace(options.SureName)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Customer sure name is empty!" } } ; var validVatNumber = IsValidVatNumber(options.CountryCode, options.VATNumber); if (!validVatNumber.IsSuccess()) { return(new Result <Customer>() { Code = validVatNumber.Code, Message = validVatNumber.Message }); } if (_dbContext.Set <Customer>() .Any(c => c.VatNumber == options.VATNumber)) { return(new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Specific Vat Number {options.VATNumber} already exists!" }); } var customer = new Customer() { Name = options.Name, SureName = options.SureName, VatNumber = options.VATNumber, CustBankID = options.CustomerBankID, CustType = options.CustType, Address = options.Address }; _dbContext.Add <Customer>(customer); _dbContext.SaveChanges(); return(new Result <Customer>() { Code = ResultCodes.Success, Data = customer }); }
/// <summary> /// Adds a new customer using with Async support /// </summary> /// <param name="options">RegisterCustomerOptions</param> /// <returns> /// Result.Code should be Success(200) /// Check Result.Code and Result.Message to get more details about possible errors /// </returns> public async Task <Result <Customer> > RegisterAsync(RegisterCustomerOptions options) { if (string.IsNullOrWhiteSpace(options.Name)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = "Customer name is empty" } } ; if (string.IsNullOrWhiteSpace(options.SureName)) { return new Result <Customer>() { Code = ResultCodes.BadRequest, Message = "Customer sure name is empty" } } ; var validVatNumber = IsValidVatNumber(options.CountryCode, options.VATNumber); if (!validVatNumber.IsSuccess()) { return(new Result <Customer>() { Code = validVatNumber.Code, Message = validVatNumber.Message }); } if (await _dbContext.Set <Customer>() .AnyAsync(c => c.VatNumber == options.VATNumber)) { return(new Result <Customer>() { Code = ResultCodes.BadRequest, Message = $"Specific Vat Number {options.VATNumber} already exists!" }); } var customer = new Customer() { Name = options.Name, SureName = options.SureName, VatNumber = options.VATNumber, CustBankID = options.CustomerBankID, CustType = options.CustType, Address = options.Address }; try { await _dbContext.AddAsync <Customer>(customer); await _dbContext.SaveChangesAsync(); } catch (Exception ex) { return(new Result <Customer>() { Code = ResultCodes.InternalServerError, Message = $"Cannot save customer info. Details: {ex.Message}" }); } return(new Result <Customer>() { Code = ResultCodes.Success, Data = customer }); }