using Biskilog_Accounting.Server.POSModels; using Biskilog_Accounting.Shared.CustomModels; using Biskilog_Accounting.Shared.Enums; using Biskilog_Accounting.Shared.Interfaces; using Biskilog_Accounting.Shared.POSModels; using Microsoft.EntityFrameworkCore; using Microsoft.Net.Http.Headers; using MySqlConnector; using System.Text.Json; namespace Biskilog_Accounting.Server.Services { public class CustomerService : ICustomer { private readonly BiskAcdbContext m_context; private readonly ITokenService m_tokenService; private readonly HttpContext m_httpContext; public CustomerService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; m_httpContext = a_httpContextAccessor?.HttpContext; } public IEnumerable FetchCustomers() { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { IEnumerable accessiblebranches = m_tokenService.BranchIds(token); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { command.CommandText = "CALL GetCustomers(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", string.Join(", ", accessiblebranches.ToArray()))); m_context.Database.OpenConnection(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { yield return new CustomerAccounts { Customer = new Shared.POSModels.Tblcustomer { CustomerId = reader.GetString(0), BranchId = reader.GetString(1), Firstname = reader.GetString(2), Surname = reader.GetString(3), Address = reader.GetString(4), Telephone = reader.GetString(5), DateAdded = reader.GetDateTime(6), Status = reader.GetString(7), Email = reader.GetString(8), FinancialStatus = reader.GetString(9), }, Debt = reader.GetDecimal(10) }; } } } } } public Task> GetCustomers() { throw new NotImplementedException(); } public async Task SyncCustomers(List a_details) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_details); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL CustomerSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } } }