using Cloud_Manager; using Cloud_Manager.Models.Enums; using Cloud_Manager.Models.Interfaces; using Cloud_Manager.Models.POSModels; using Microsoft.EntityFrameworkCore; using Microsoft.Net.Http.Headers; using MySqlConnector; using System.Text.Json; namespace Cloud_Manager.Services { public class UserService : IUser { private readonly BiskAcdbContext m_context; private readonly ITokenService m_tokenService; private readonly HttpContext m_httpContext; public UserService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor) { m_context = a_context; m_tokenService = a_tokenService; m_httpContext = a_httpContextAccessor?.HttpContext; } public IEnumerable FetchUsers() { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { IEnumerable accessiblebranches = m_tokenService.BranchIds(token); return m_context.Tblusers.Where(b => accessiblebranches.Contains(b.BranchId)); } return new List(); } public Task> GetUsers() { throw new NotImplementedException(); } public async Task SyncUserAsync(List a_users) { string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) { string jsonString = JsonSerializer.Serialize(a_users); using (var command = m_context.Database.GetDbConnection().CreateCommand()) { m_context.Database.OpenConnection(); command.CommandText = "CALL UsersSync(@p0)"; command.Parameters.Add(new MySqlParameter("@p0", jsonString)); command.ExecuteNonQuery(); } } } } }