using Microsoft.AspNetCore.Mvc;
using System.Data.SqlClient;
using System.Net.Http.Headers;
namespace Capgemini.Interview.WebAPI.Controllers
[Route("customer controller")]
public class CustomerController : ControllerBase
private IConfiguration _configuration;
private string _customersDbConnectionString;
public CustomerController(ILogger logger, IConfiguration configuration)
_configuration = configuration;
_customersDbConnectionString = _configuration.GetConnectionString("CustomersDb");
[HttpPost, Route("read")]
public IActionResult ReadAllCustomers()
_logger.LogDebug("ReadAllCustomers starting");
var page = int.Parse(Request.Query["page"]);
var size = int.Parse(Request.Query["size"]);
CustomersDbContext context = new CustomersDbContext();
var customers = context.Customers.Skip(page * size).Take(size);
_logger.LogDebug("ReadAllCustomers Finished", customers);
[HttpGet, Route("search/{filter}/{isAscending}")]
public IActionResult FindCustomers(string filter, bool isAscending)
IDbConnection db = new SqlConnection(_customersDbConnectionString);
var customers = db.QueryAsync<CustomerModel>(
$@"SELECT * FROM Customer WHERE [name] like '%{filter}%'"
return isAscending switch
true => Ok(customers.OrderBy(customer => customer.Name)),
_ => Ok(customers.OrderByDescending(customer => customer.Name))
[HttpPost, Route("notifications/{customerId}")]
public async IActionResult NotifyCustomerAsync(Guid customerId, [FromBody]string message)
CustomersDbContext context = new CustomersDbContext();
var customer = context.Customers.Single(c => c.Id == customerId);
HttpClient notificationApiClient = new HttpClient();
notificationApiClient.BaseAddress = new Uri(_configuration["Notification:BaseUrl"]);
string basicAuth = $"{_configuration["Notification:UserName"]}:{_configuration["Notification:Password"]}";
notificationApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
var notificationPayload = new NotificationPayload(customer.NotificationUserId, $"Notification: {message}");
await notificationApiClient.PostAsJsonAsync(_configuration["Notification:NotifyPath"], notificationPayload);
public record NotificationPayload (Guid UserId, string Message);