Compare commits

...

10 Commits

Author SHA1 Message Date
Michał Zieliński
6d2c46d971 App name refactor
Some checks failed
Build Bimix / Build WebAPI and WebUI (push) Failing after 12s
2025-10-11 11:33:46 +02:00
Michał Zieliński
b4edaf007e Cleanup before refactor 2025-10-10 16:00:52 +02:00
Michał Zieliński
569245c296 Google login is working 2025-08-22 11:30:09 +02:00
Michał Zieliński
14c61ca1ee Implement Google authentication (for Web) and user management system 2025-07-19 22:50:38 +02:00
Michał Zieliński
b673fd2da3 Scan ean code on iOS app 2025-07-17 19:17:27 +02:00
Michał Zieliński
2a42f16daf ProductList 2025-07-17 14:29:02 +02:00
Michał Zieliński
518eff0ec7 Import products 2025-06-28 18:54:08 +01:00
Michał Zieliński
eb4b2efbff Update build.yml 2025-06-24 09:31:31 +02:00
Michał Zieliński
1e823ab4d3 Update build.yml 2025-06-24 09:29:37 +02:00
Michał Zieliński
bbea0741a3 yodatebuild filename 2025-06-24 09:26:59 +02:00
96 changed files with 2441 additions and 454 deletions

View File

@@ -22,11 +22,17 @@ jobs:
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Restore dependencies
run: dotnet restore
- name: Restore WebAPI
run: dotnet restore Bimix.API/Bimix.API.csproj
- name: Build solution
run: dotnet build --configuration Release --no-restore
- name: Restore WebUI
run: dotnet restore Bimix.UI.Web/Bimix.UI.Web.csproj
- name: Build WebAPI
run: dotnet build Bimix.API/Bimix.API.csproj --configuration Release --no-restore
- name: Build WebUI
run: dotnet build Bimix.UI.Web/Bimix.UI.Web.csproj --configuration Release --no-restore
- name: Publish WebAPI
run: |

View File

@@ -7,18 +7,20 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.70.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.17">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.12.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
<ProjectReference Include="..\Bimix.Application\Bimix.Application.csproj" />
<ProjectReference Include="..\Bimix.Infrastructure\Bimix.Infrastructure.csproj" />
<ProjectReference Include="..\BimAI.Application\BimAI.Application.csproj" />
<ProjectReference Include="..\BimAI.Infrastructure\BimAI.Infrastructure.csproj" />
</ItemGroup>
</Project>

6
BimAI.API/BimAI.API.http Normal file
View File

@@ -0,0 +1,6 @@
@BimAI.API_HostAddress = http://localhost:5090
GET {{BimAI.API_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,96 @@
using System.Security.Claims;
using BimAI.API.Services;
using BimAI.Application.DTOModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BimAI.API.Controllers;
public class AuthController(
GoogleAuthService googleAuthService,
JwtTokenService jwtTokenService,
ILogger<AuthController> logger)
: ControllerBase
{
[HttpPost("google")]
public async Task<IActionResult> GoogleAuth([FromBody] GoogleAuthRequest request)
{
try
{
if (string.IsNullOrEmpty(request.IdToken))
{
return BadRequest(new GoogleAuthResponse
{
Success = false,
Error = "IdToken is required"
});
}
var (isValid, user, error) = await googleAuthService.ValidateGoogleTokenAsync(request.IdToken);
if (!isValid || user == null)
{
var statusCode = error switch
{
"User not authorized to access this application" => 403,
"User account is not active" => 403,
"Invalid Google token" => 401,
_ => 401
};
return StatusCode(statusCode, new GoogleAuthResponse
{
Success = false,
Error = error ?? "Authentication failed"
});
}
var jwt = jwtTokenService.GenerateToken(user);
return Ok(new GoogleAuthResponse
{
Success = true,
Token = jwt,
User = new UserDto
{
Id = user.Id,
Email = user.Email,
FullName = user.FullName,
IsActive = user.IsActive,
LastLoginAt = user.LastLoginAt
}
});
}
catch (Exception ex)
{
logger.LogError(ex, "Error during Google authentication");
return StatusCode(500, new GoogleAuthResponse
{
Success = false,
Error = "Internal server error"
});
}
}
[HttpGet("me")]
[Authorize]
public IActionResult GetCurrentUser()
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
var emailClaim = User.FindFirst(ClaimTypes.Email);
var nameClaim = User.FindFirst(ClaimTypes.Name);
if (userIdClaim == null || emailClaim == null || nameClaim == null)
{
return Unauthorized();
}
return Ok(new UserDto
{
Id = Guid.Parse(userIdClaim.Value),
Email = emailClaim.Value,
FullName = nameClaim.Value,
IsActive = true,
});
}
}

View File

@@ -0,0 +1,71 @@
using BimAI.Application.DTOModels;
using BimAI.Application.DTOModels.Common;
using BimAI.Infrastructure.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BimAI.API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ProductsController(BimAIDbContext context) : ControllerBase
{
private readonly BimAIDbContext _context = context;
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts([FromQuery] ProductFilterRequest request)
{
var query = _context.Products.AsQueryable();
if (!string.IsNullOrWhiteSpace(request.Search))
{
var searchTerm = request.Search.ToLower();
query = query.Where(x =>
x.Name.ToLower().Contains(searchTerm) ||
(x.Code != null && x.Code.ToLower().Contains(searchTerm)) ||
(x.Ean != null && x.Ean.ToLower().Contains(searchTerm))
);
}
if (!string.IsNullOrWhiteSpace(request.Name))
{
query = query.Where(x => x.Name.ToLower().Contains(request.Name.ToLower()));
}
if (!string.IsNullOrWhiteSpace(request.Code))
{
query = query.Where(x => x.Code != null && x.Code.ToLower().Contains(request.Code.ToLower()));
}
if (!string.IsNullOrWhiteSpace(request.Ean))
{
query = query.Where(x => x.Ean != null && x.Ean.ToLower().Contains(request.Ean.ToLower()));
}
var totalCount = await query.CountAsync();
var items = await query
.OrderBy(x => x.Name)
.Skip((request.Page -1) * request.PageSize)
.Take(request.PageSize)
.Select(x => new ProductDto
{
Id = x.Id,
Name = x.Name,
Code = x.Code ?? string.Empty,
Ean = x.Ean ?? string.Empty,
StockAddresses = x.StockAddresses ?? string.Empty,
CreatedAt = x.CreatedAt,
UpdatedAt = x.UpdatedAt
})
.ToListAsync();
return Ok(new PagedResult<ProductDto>
{
Items = items,
TotalCount = totalCount,
Page = request.Page,
PageSize = request.PageSize,
});
}
}

View File

@@ -1,7 +1,7 @@
using Bimix.Infrastructure.Sync;
using BimAI.Infrastructure.Sync;
using Microsoft.AspNetCore.Mvc;
namespace Bimix.API.Controllers;
namespace BimAI.API.Controllers;
[ApiController]
[Route("api/[controller]")]

71
BimAI.API/Program.cs Normal file
View File

@@ -0,0 +1,71 @@
using System.Text;
using BimAI.API.Services;
using BimAI.Infrastructure.Data;
using BimAI.Infrastructure.Sync;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<BimAIDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped<ProductSyncService>();
builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Start auth section
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
var secretKey = jwtSettings["SecretKey"];
var issuer = jwtSettings["Issuer"];
var audience = jwtSettings["Audience"];
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
ClockSkew = TimeSpan.Zero,
};
});
builder.Services.AddAuthentication();
builder.Services.AddScoped<GoogleAuthService>();
builder.Services.AddScoped<JwtTokenService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// End auth section
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAll");
app.UseAuthorization();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -5,7 +5,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7142;http://localhost:5142",
"applicationUrl": "http://localhost:7142;http://0.0.0.0:7142",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@@ -0,0 +1,72 @@
using BimAI.Domain.Entities;
using BimAI.Infrastructure.Data;
using Google.Apis.Auth;
using Microsoft.EntityFrameworkCore;
namespace BimAI.API.Services;
public class GoogleAuthService(BimAIDbContext context, IConfiguration configuration, ILogger<GoogleAuthService> logger)
{
private readonly BimAIDbContext _context = context;
private readonly IConfiguration _configuration = configuration;
private readonly ILogger<GoogleAuthService> _logger = logger;
public async Task<(bool IsValid, User? user, string? error)> ValidateGoogleTokenAsync(string idToken)
{
try
{
var clientId = _configuration["GoogleAuth:ClientId"];
if (string.IsNullOrEmpty(clientId))
{
_logger.LogError("Google Auth Client Id is not configured");
return (false, null, "Google Auth Client Id is not configured");
}
var payload = await GoogleJsonWebSignature.ValidateAsync(idToken,
new GoogleJsonWebSignature.ValidationSettings
{
Audience = new[] { clientId }
});
_logger.LogInformation("Google token validated for user: {Email}", payload.Email);
var user = await _context.Users
.FirstOrDefaultAsync(x => x.GoogleId == payload.Subject || x.Email == payload.Email);
if (user == null)
{
_logger.LogError("User not found in BimAI database: {Email}", payload.Email);
return (false, null, "User not found in BimAI database");
}
if (!user.IsActive)
{
_logger.LogError("User is not active: {Email}", payload.Email);
return (false, null, "User is not active");
}
user.LastLoginAt = DateTime.UtcNow;
user.FullName = payload.Name;
if (user.GoogleId != payload.Subject)
{
user.GoogleId = payload.Subject;
}
await _context.SaveChangesAsync();
_logger.LogInformation("User logged in: {Email}", payload.Email);
return (true, user, null);
}
catch (InvalidJwtException ex)
{
_logger.LogError(ex, "Invalid JWT token");
return (false, null, "Invalid JWT token");
} catch (Exception ex)
{
_logger.LogError(ex, "Error validating Google token");
return (false, null, "Error validating Google token");
}
}
}

View File

@@ -0,0 +1,86 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using BimAI.Domain.Entities;
using Microsoft.IdentityModel.Tokens;
namespace BimAI.API.Services;
public class JwtTokenService(IConfiguration configuration, ILogger<JwtTokenService> logger)
{
private readonly IConfiguration _configuration = configuration;
private readonly ILogger<JwtTokenService> _logger = logger;
public string GenerateToken(User user)
{
var jwtSettings = _configuration.GetSection("JwtSettings");
var securityKey = jwtSettings["SecurityKey"];
var issuer = jwtSettings["Issuer"];
var audience = jwtSettings["Audience"];
var expiryDays = int.Parse(jwtSettings["ExpiryDays"] ?? "7");
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.FullName),
new Claim("google_id", user.GoogleId),
new Claim("is_active", user.IsActive.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(),
ClaimValueTypes.Integer64)
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: issuer,
audience: audience,
claims: claims,
expires: DateTime.UtcNow.AddDays(expiryDays),
signingCredentials: creds
);
var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
_logger.LogInformation("Generated JWT token for user: {Email}", user.Email);
return tokenString;
}
public ClaimsPrincipal? ValidateToken(string token)
{
try
{
var jwtSettings = _configuration.GetSection("JwtSettings");
var secretKey = jwtSettings["SecretKey"];
var issuer = jwtSettings["Issuer"];
var audience = jwtSettings["Audience"];
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(secretKey);
var validationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey = new SymmetricSecurityKey(key),
ClockSkew = TimeSpan.Zero
};
var principal = tokenHandler.ValidateToken(token, validationParameters, out _);
return principal;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error validating JWT token");
return null;
}
}
}

View File

@@ -0,0 +1,27 @@
{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=bimai;User Id=sa;Password=9832&^*&huihj;TrustServerCertificate=True;"
},
"E5_CRM": {
"ApiKey": "7e50a8a5-f01f-4fbc-8c1b-59f3fc474bb5"
},
"GoogleAuth": {
"ClientId": "1037727384847-t1l2au6du34kdckamro81guklk17cjah.apps.googleusercontent.com"
},
"JwtSettings": {
"SecretKey": "BimAISuperSecretKeyThatMustBeAtLeast32CharactersLong123456789",
"Issuer": "BimAI.API",
"Audience": "BimAI.Clients",
"ExpiryDays": 7
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Warning",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning"
}
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
<ProjectReference Include="..\BimAI.Domain\BimAI.Domain.csproj" />
</ItemGroup>
<PropertyGroup>

View File

@@ -0,0 +1,22 @@
namespace BimAI.Application.DTOModels;
public class GoogleAuthRequest
{
public string? IdToken { get; set; }
}
public class GoogleAuthResponse
{
public bool Success { get; set; }
public string? Token { get; set; }
public UserDto? User { get; set; }
public string? Error { get; set; }
}
public class UserDto {
public Guid Id { get; set; }
public string Email { get; set; } = default!;
public string FullName { get; set; } = default!;
public bool IsActive { get; set; }
public DateTime? LastLoginAt { get; set; }
}

View File

@@ -0,0 +1,12 @@
namespace BimAI.Application.DTOModels.Common;
public class PagedResult<T>
{
public List<T> Items { get; set; } = new();
public int TotalCount { get; set; }
public int PageSize { get; set; }
public int Page { get; set; }
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
public bool HasPreviousPage => Page > 1;
public bool HasNextPage => Page < TotalPages;
}

View File

@@ -0,0 +1,22 @@
namespace BimAI.Application.DTOModels;
public class ProductDto
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Code { get; set; } = string.Empty;
public string Ean { get; set; } = string.Empty;
public string StockAddresses { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class ProductFilterRequest
{
public string? Search { get; set; }
public string? Name { get; set; }
public string? Code { get; set; }
public string? Ean { get; set; }
public int Page { get; set; } = 1;
public int PageSize { get; set; } = 20;
}

View File

@@ -1,4 +1,4 @@
namespace Bimix.Domain.Entities;
namespace BimAI.Domain.Entities;
public abstract class BaseEntity
{

View File

@@ -0,0 +1,9 @@
namespace BimAI.Domain.Entities;
public class Product : BaseEntity
{
public required string Name { get; set; }
public string? Code { get; set; }
public string? Ean { get; set; }
public string? StockAddresses { get; set; }
}

View File

@@ -1,7 +1,7 @@
namespace Bimix.Domain.Entities;
namespace BimAI.Domain.Entities;
public class SyncState
{
public required string Entity { get; set; }
public required string Entity { get; init; }
public required long LastSynced { get; set; } // UnixTimestamp
}

View File

@@ -0,0 +1,10 @@
namespace BimAI.Domain.Entities;
public class User : BaseEntity
{
public string GoogleId { get; set; } = default!;
public string Email { get; set; } = default!;
public string FullName { get; set; } = default!;
public bool IsActive { get; set; } = false;
public DateTime? LastLoginAt { get; set; }
}

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
<ProjectReference Include="..\Bimix.Application\Bimix.Application.csproj" />
<ProjectReference Include="..\BimAI.Domain\BimAI.Domain.csproj" />
<ProjectReference Include="..\BimAI.Application\BimAI.Application.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,90 @@
using Microsoft.EntityFrameworkCore;
using BimAI.Domain.Entities;
namespace BimAI.Infrastructure.Data;
public class BimAIDbContext(DbContextOptions<BimAIDbContext> options) : DbContext(options)
{
public DbSet<Product> Products { get; set; }
public DbSet<SyncState> SyncStates { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Product properties
modelBuilder.Entity<Product>().HasKey(x => x.Id);
modelBuilder.Entity<Product>().Property(x => x.Name).IsRequired().HasMaxLength(512);
modelBuilder.Entity<Product>().Property(x => x.Code).IsRequired().HasMaxLength(40);
modelBuilder.Entity<Product>().Property(x => x.Ean).IsRequired().HasMaxLength(50);
modelBuilder.Entity<Product>().Property(x => x.StockAddresses).IsRequired().HasMaxLength(512);
// SyncState properties
modelBuilder.Entity<SyncState>().HasKey((x => x.Entity));
// User properties
modelBuilder.Entity<User>().HasKey(x => x.Id);
modelBuilder.Entity<User>().Property(x => x.GoogleId).IsRequired().HasMaxLength(100);
modelBuilder.Entity<User>().Property(x => x.Email).IsRequired().HasMaxLength(255);
modelBuilder.Entity<User>().Property(x => x.FullName).IsRequired().HasMaxLength(255);
modelBuilder.Entity<User>().Property(x => x.IsActive).IsRequired().HasDefaultValue(false);
modelBuilder.Entity<User>().Property(x => x.LastLoginAt).IsRequired(false);
// User indexes
modelBuilder.Entity<User>().HasIndex(x => x.GoogleId).IsUnique().HasDatabaseName("IX_Users_GoogleId");
modelBuilder.Entity<User>().HasIndex(x => x.Email).IsUnique().HasDatabaseName("IX_Users_Email");
// Configure defaults for all CreatedAt and UpdatedAt in entities
ConfigureBaseEntity(modelBuilder);
}
private void ConfigureBaseEntity(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
{
modelBuilder.Entity(entityType.ClrType)
.Property(nameof(BaseEntity.CreatedAt))
.HasDefaultValueSql("GETUTCDATE()");
modelBuilder.Entity(entityType.ClrType)
.Property(nameof(BaseEntity.UpdatedAt))
.HasDefaultValueSql("GETUTCDATE()");
}
}
}
public override int SaveChanges()
{
UpdateTimestamps();
return base.SaveChanges();
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
UpdateTimestamps();
return base.SaveChangesAsync(cancellationToken);
}
private void UpdateTimestamps()
{
var entities = ChangeTracker.Entries<BaseEntity>();
foreach (var entity in entities)
{
if (entity.State == EntityState.Added)
{
entity.Entity.CreatedAt = DateTime.UtcNow;
entity.Entity.UpdatedAt = DateTime.UtcNow;
break;
}
else if (entity.State == EntityState.Modified)
{
entity.Entity.UpdatedAt = DateTime.UtcNow;
break;
}
}
}
}

View File

@@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Bimix.Infrastructure.Data;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
@@ -9,9 +9,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimixDbContext))]
[DbContext(typeof(BimAIDbContext))]
[Migration("20250619185202_InitDatabase")]
partial class InitDatabase
{
@@ -25,7 +25,7 @@ namespace Bimix.Infrastructure.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Bimix.Domain.Entities.Product", b =>
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()

View File

@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class InitDatabase : Migration

View File

@@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Bimix.Infrastructure.Data;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
@@ -9,9 +9,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimixDbContext))]
[DbContext(typeof(BimAIDbContext))]
[Migration("20250623184943_AddSyncState")]
partial class AddSyncState
{
@@ -25,7 +25,7 @@ namespace Bimix.Infrastructure.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Bimix.Domain.Entities.Product", b =>
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -47,7 +47,7 @@ namespace Bimix.Infrastructure.Migrations
b.ToTable("Products");
});
modelBuilder.Entity("Bimix.Domain.Entities.SyncState", b =>
modelBuilder.Entity("BimAI.Domain.Entities.SyncState", b =>
{
b.Property<string>("Entity")
.HasColumnType("nvarchar(450)");

View File

@@ -2,7 +2,7 @@
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddSyncState : Migration

View File

@@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Bimix.Infrastructure.Data;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
@@ -9,9 +9,9 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimixDbContext))]
[DbContext(typeof(BimAIDbContext))]
[Migration("20250623194653_ResizeProductName")]
partial class ResizeProductName
{
@@ -25,7 +25,7 @@ namespace Bimix.Infrastructure.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Bimix.Domain.Entities.Product", b =>
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -47,7 +47,7 @@ namespace Bimix.Infrastructure.Migrations
b.ToTable("Products");
});
modelBuilder.Entity("Bimix.Domain.Entities.SyncState", b =>
modelBuilder.Entity("BimAI.Domain.Entities.SyncState", b =>
{
b.Property<string>("Entity")
.HasColumnType("nvarchar(450)");

View File

@@ -2,7 +2,7 @@
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class ResizeProductName : Migration

View File

@@ -1,19 +1,22 @@
// <auto-generated />
using System;
using Bimix.Infrastructure.Data;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Bimix.Infrastructure.Migrations
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimixDbContext))]
partial class BimixDbContextModelSnapshot : ModelSnapshot
[DbContext(typeof(BimAIDbContext))]
[Migration("20250624193445_Products-NewFields")]
partial class ProductsNewFields
{
protected override void BuildModel(ModelBuilder modelBuilder)
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
@@ -22,20 +25,35 @@ namespace Bimix.Infrastructure.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Bimix.Domain.Entities.Product", b =>
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Code")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("nvarchar(40)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<string>("Ean")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<string>("StockAddresses")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime2");
@@ -44,7 +62,7 @@ namespace Bimix.Infrastructure.Migrations
b.ToTable("Products");
});
modelBuilder.Entity("Bimix.Domain.Entities.SyncState", b =>
modelBuilder.Entity("BimAI.Domain.Entities.SyncState", b =>
{
b.Property<string>("Entity")
.HasColumnType("nvarchar(450)");

View File

@@ -0,0 +1,54 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BimAI.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class ProductsNewFields : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Code",
table: "Products",
type: "nvarchar(40)",
maxLength: 40,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<string>(
name: "Ean",
table: "Products",
type: "nvarchar(50)",
maxLength: 50,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<string>(
name: "StockAddresses",
table: "Products",
type: "nvarchar(512)",
maxLength: 512,
nullable: false,
defaultValue: "");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Code",
table: "Products");
migrationBuilder.DropColumn(
name: "Ean",
table: "Products");
migrationBuilder.DropColumn(
name: "StockAddresses",
table: "Products");
}
}
}

View File

@@ -0,0 +1,136 @@
// <auto-generated />
using System;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimAIDbContext))]
[Migration("20250718162313_AddUsersTable")]
partial class AddUsersTable
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.17")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Code")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("nvarchar(40)");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.Property<string>("Ean")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<string>("StockAddresses")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<DateTime>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.HasKey("Id");
b.ToTable("Products");
});
modelBuilder.Entity("BimAI.Domain.Entities.SyncState", b =>
{
b.Property<string>("Entity")
.HasColumnType("nvarchar(450)");
b.Property<long>("LastSynced")
.HasColumnType("bigint");
b.HasKey("Entity");
b.ToTable("SyncStates");
});
modelBuilder.Entity("BimAI.Domain.Entities.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("FullName")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("GoogleId")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<bool>("IsActive")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property<DateTime?>("LastLoginAt")
.HasColumnType("datetime2");
b.Property<DateTime>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.HasKey("Id");
b.HasIndex("Email")
.IsUnique()
.HasDatabaseName("IX_Users_Email");
b.HasIndex("GoogleId")
.IsUnique()
.HasDatabaseName("IX_Users_GoogleId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,88 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BimAI.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddUsersTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
name: "UpdatedAt",
table: "Products",
type: "datetime2",
nullable: false,
defaultValueSql: "GETUTCDATE()",
oldClrType: typeof(DateTime),
oldType: "datetime2");
migrationBuilder.AlterColumn<DateTime>(
name: "CreatedAt",
table: "Products",
type: "datetime2",
nullable: false,
defaultValueSql: "GETUTCDATE()",
oldClrType: typeof(DateTime),
oldType: "datetime2");
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
GoogleId = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Email = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
FullName = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
LastLoginAt = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETUTCDATE()"),
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETUTCDATE()")
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Users_Email",
table: "Users",
column: "Email",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Users_GoogleId",
table: "Users",
column: "GoogleId",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
migrationBuilder.AlterColumn<DateTime>(
name: "UpdatedAt",
table: "Products",
type: "datetime2",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "datetime2",
oldDefaultValueSql: "GETUTCDATE()");
migrationBuilder.AlterColumn<DateTime>(
name: "CreatedAt",
table: "Products",
type: "datetime2",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "datetime2",
oldDefaultValueSql: "GETUTCDATE()");
}
}
}

View File

@@ -0,0 +1,133 @@
// <auto-generated />
using System;
using BimAI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BimAI.Infrastructure.Migrations
{
[DbContext(typeof(BimAIDbContext))]
partial class BimAIDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.17")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BimAI.Domain.Entities.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Code")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("nvarchar(40)");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.Property<string>("Ean")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<string>("StockAddresses")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("nvarchar(512)");
b.Property<DateTime>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.HasKey("Id");
b.ToTable("Products");
});
modelBuilder.Entity("BimAI.Domain.Entities.SyncState", b =>
{
b.Property<string>("Entity")
.HasColumnType("nvarchar(450)");
b.Property<long>("LastSynced")
.HasColumnType("bigint");
b.HasKey("Entity");
b.ToTable("SyncStates");
});
modelBuilder.Entity("BimAI.Domain.Entities.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("FullName")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("GoogleId")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<bool>("IsActive")
.ValueGeneratedOnAdd()
.HasColumnType("bit")
.HasDefaultValue(false);
b.Property<DateTime?>("LastLoginAt")
.HasColumnType("datetime2");
b.Property<DateTime>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("GETUTCDATE()");
b.HasKey("Id");
b.HasIndex("Email")
.IsUnique()
.HasDatabaseName("IX_Users_Email");
b.HasIndex("GoogleId")
.IsUnique()
.HasDatabaseName("IX_Users_GoogleId");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -1,23 +1,30 @@
using System.Text.Json;
using Bimix.Domain.Entities;
using Bimix.Infrastructure.Data;
using System.Web;
using BimAI.Domain.Entities;
using BimAI.Infrastructure.Data;
using Microsoft.Extensions.Configuration;
namespace Bimix.Infrastructure.Sync;
namespace BimAI.Infrastructure.Sync;
public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfiguration configuration)
public class ProductSyncService(HttpClient httpClient, BimAIDbContext db, IConfiguration configuration)
{
private readonly HttpClient _httpClient = httpClient;
private readonly BimixDbContext _db = db;
private readonly IConfiguration _configuration = configuration;
/// <summary>
/// Dekoduje encje HTML w ciągu znaków (np. &quot; na ")
/// </summary>
private string DecodeHtmlEntities(string text)
{
if (string.IsNullOrEmpty(text))
return text;
return HttpUtility.HtmlDecode(text);
}
public async Task RunAsync()
{
var apiKey = _configuration["E5_CRM:ApiKey"];
var syncState = _db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
var apiKey = configuration["E5_CRM:ApiKey"];
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
var response = await _httpClient.GetStringAsync(url);
var response = await httpClient.GetStringAsync(url);
var products = JsonSerializer.Deserialize<List<JsonElement>>(response);
if (products == null) return;
@@ -27,7 +34,10 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
foreach (var p in products)
{
var idStr = p.GetProperty("id").GetString() ?? "";
var name = p.GetProperty("name").GetString() ?? "";
var name = DecodeHtmlEntities(p.GetProperty("name").GetString() ?? "");
var code = DecodeHtmlEntities(p.GetProperty("code").GetString() ?? "");
var stockAddresses = DecodeHtmlEntities(p.GetProperty("stock_addresses").GetString() ?? "");
var ean = DecodeHtmlEntities(p.GetProperty("ean").GetString() ?? "");
if (!Guid.TryParse(idStr, out Guid id))
{
@@ -35,7 +45,7 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
continue;
}
var existing = _db.Products.FirstOrDefault(x => x.Id == id);
var existing = db.Products.FirstOrDefault(x => x.Id == id);
if (existing == null)
{
@@ -43,10 +53,13 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
{
Id = id,
Name = name,
Ean = ean,
Code = code,
StockAddresses = stockAddresses,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
_db.Products.Add(product);
db.Products.Add(product);
}
else
{
@@ -58,17 +71,17 @@ public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfi
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
await _httpClient.GetAsync(updateUrl);
await httpClient.GetAsync(updateUrl);
}
syncState.LastSynced = now;
if (_db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
{
_db.SyncStates.Add(syncState);
db.SyncStates.Add(syncState);
}
else
{
_db.SyncStates.Update(syncState);
db.SyncStates.Update(syncState);
}
await _db.SaveChangesAsync();
await db.SaveChangesAsync();
}
}

View File

@@ -1,4 +1,4 @@
namespace Bimix.UI.Mobile;
namespace BimAI.UI.Mobile;
public partial class App : Microsoft.Maui.Controls.Application
{

View File

@@ -2,6 +2,8 @@
<PropertyGroup>
<TargetFramework>net8.0-ios</TargetFramework>
<GenerateXcodeProject>true</GenerateXcodeProject>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
@@ -13,7 +15,7 @@
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
<OutputType>Exe</OutputType>
<RootNamespace>Bimix.UI.Mobile</RootNamespace>
<RootNamespace>BimAI.UI.Mobile</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
@@ -21,10 +23,10 @@
<Nullable>enable</Nullable>
<!-- Display name -->
<ApplicationTitle>Bimix.UI.Mobile</ApplicationTitle>
<ApplicationTitle>BimAI</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.companyname.bimix.ui.mobile</ApplicationId>
<ApplicationId></ApplicationId>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
@@ -38,6 +40,13 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)' == 'iPhone'">
<ApplicationId></ApplicationId>
<CodesignKey>Apple Development: Michal Zielinski (2F35ZHMBTB)</CodesignKey>
<CodesignProvision>bimai-local</CodesignProvision>
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4"/>
@@ -56,20 +65,26 @@
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)"/>
<!-- Dodaj pliki wwwroot jako MAUI assets -->
<MauiAsset Include="wwwroot\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<MauiAsset Include="wwwroot\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.9"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.9"/>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.9"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.9"/>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1"/>
<PackageReference Include="MudBlazor" Version="8.8.0"/>
<PackageReference Include="ZXing.Net.MAUI" Version="0.4.0"/>
<PackageReference Include="ZXing.Net.MAUI.Controls" Version="0.4.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bimix.UI.Shared\Bimix.UI.Shared.csproj"/>
<ProjectReference Include="..\BimAI.UI.Shared\BimAI.UI.Shared.csproj"/>
</ItemGroup>
</Project>

View File

@@ -1,4 +1,4 @@
namespace Bimix.UI.Mobile;
namespace BimAI.UI.Mobile;
public partial class MainPage : ContentPage
{

View File

@@ -0,0 +1,67 @@
using System.Reflection;
using BimAI.UI.Mobile.Services;
using BimAI.UI.Shared.Extensions;
using BimAI.UI.Shared.Interfaces;
using BimAI.UI.Shared.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Hosting;
using MudBlazor.Services;
using ZXing.Net.Maui.Controls;
namespace BimAI.UI.Mobile;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseBarcodeReader()
.ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });
builder.Configuration
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
builder.Services.AddMauiBlazorWebView();
builder.Services.AddMudServices();
if (DeviceInfo.Platform == DevicePlatform.iOS)
{
builder.Services.AddSingleton<IScannerService, ScannerService>();
}
else
{
builder.Services.AddSingleton<IScannerService, NoOpScannerService>();
}
builder.Services.AddScoped<AuthService>();
var baseUrl = GetApiBaseUrl();
builder.Services.AddSharedServices(baseUrl);
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
builder.Logging.SetMinimumLevel(LogLevel.Debug);
#endif
return builder.Build();
}
private static string GetApiBaseUrl()
{
#if IOS
// iOS symulator
return "http://192.168.29.140:7142/";
#else
return "https://localhost:7142/";
#endif
}
}

View File

Before

Width:  |  Height:  |  Size: 228 B

After

Width:  |  Height:  |  Size: 228 B

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,140 @@
using BimAI.UI.Shared.Interfaces;
using ZXing.Net.Maui;
using ZXing.Net.Maui.Controls;
namespace BimAI.UI.Mobile.Services;
public class ScannerService : IScannerService
{
public bool IsAvailable => true;
public async Task<string?> ScanBarcodeAsync()
{
try
{
if (!IsAvailable)
return null;
var hasPermission = await RequestCameraPermissionsAsync();
if (!hasPermission)
return null;
var tcs = new TaskCompletionSource<string?>();
await MainThread.InvokeOnMainThreadAsync(async () =>
{
var scanner = new CameraBarcodeReaderView
{
Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.OneDimensional | BarcodeFormats.TwoDimensional,
AutoRotate = true,
Multiple = false
},
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Colors.Black
};
scanner.BarcodesDetected += async (sender, e) =>
{
if (e.Results?.Any() == true)
{
var barcode = e.Results.First();
// Wykonaj operacje UI na głównym wątku
await MainThread.InvokeOnMainThreadAsync(async () =>
{
try
{
await Microsoft.Maui.Controls.Application.Current?.MainPage?.Navigation.PopModalAsync()!;
tcs.TrySetResult(barcode.Value);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error closing modal: {ex.Message}");
tcs.TrySetException(ex);
}
});
}
};
var cancelButton = new Button
{
Text = "Anuluj",
BackgroundColor = Colors.Red,
TextColor = Colors.White,
Margin = new Thickness(20),
HorizontalOptions = LayoutOptions.Center
};
cancelButton.Clicked += async (sender, e) =>
{
try
{
await Microsoft.Maui.Controls.Application.Current?.MainPage?.Navigation.PopModalAsync()!;
tcs.TrySetResult(null);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error closing modal: {ex.Message}");
tcs.TrySetException(ex);
}
};
var stackLayout = new StackLayout
{
Children =
{
new Label
{
Text = "Skieruj kamerę na kod kreskowy",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Start,
Margin = new Thickness(20),
FontSize = 18,
TextColor = Colors.White
},
scanner,
cancelButton
},
BackgroundColor = Colors.Black,
Spacing = 0
};
var scannerPage = new ContentPage
{
Title = "Skanuj kod",
Content = stackLayout,
BackgroundColor = Colors.Black
};
await Microsoft.Maui.Controls.Application.Current?.MainPage?.Navigation.PushModalAsync(scannerPage)!;
});
var result = await tcs.Task;
System.Diagnostics.Debug.WriteLine($"Scanner returned: {result}");
return result;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine($"Scanner error: {e.Message}");
return null;
}
}
public async Task<bool> RequestCameraPermissionsAsync()
{
try
{
var status = await Permissions.RequestAsync<Permissions.Camera>();
return status == PermissionStatus.Granted;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine($"Permission error: {e.Message}");
return false;
}
}
}

View File

@@ -0,0 +1,5 @@
{
"GoogleAuth": {
"ClientId": "1037727384847-t1l2au6du34kdckamro81guklk17cjah.apps.googleusercontent.com"
}
}

View File

@@ -0,0 +1,5 @@
{
"GoogleAuth": {
"ClientId": "1037727384847-t1l2au6du34kdckamro81guklk17cjah.apps.googleusercontent.com"
}
}

View File

@@ -14,15 +14,17 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.17" />
<PackageReference Include="MudBlazor" Version="8.8.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bimix.Domain\Bimix.Domain.csproj" />
<ProjectReference Include="..\Bimix.Application\Bimix.Application.csproj" />
<ProjectReference Include="..\BimAI.Domain\BimAI.Domain.csproj" />
<ProjectReference Include="..\BimAI.Application\BimAI.Application.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
<Folder Include="wwwroot\images\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,42 @@
@using BimAI.UI.Shared.Services
@inject AuthService AuthService
@inject NavigationManager Navigation
@if (_isLoading)
{
<div class="d-flex justify-center align-center" style="height: 100vh;">
<MudProgressCircular Indeterminate="true" Size="Size.Large" />
</div>
}
else if (_isAuthenticated)
{
@ChildContent
}
@code {
[Parameter] public RenderFragment? ChildContent { get; set; }
private bool _isLoading = true;
private bool _isAuthenticated = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
Console.WriteLine("AuthGuard: Checking authentication...");
_isAuthenticated = await AuthService.CheckAuthenticationAsync();
_isLoading = false;
Console.WriteLine($"AuthGuard: isAuthenticated={_isAuthenticated}");
if (!_isAuthenticated)
{
Console.WriteLine("AuthGuard: Redirecting to /login");
Navigation.NavigateTo("/login", replace: true);
}
StateHasChanged();
}
}
}

View File

@@ -0,0 +1,142 @@
@using BimAI.UI.Shared.Services
@using Microsoft.Extensions.Configuration
@inject IJSRuntime JS
@inject IConfiguration Configuration
@inject AuthService AuthService
@inject NavigationManager NavigationManager
<MudCard Class="login-card" Elevation="8">
<MudCardContent Class="pa-8 d-flex flex-column align-center">
<MudText Typo="Typo.h4" Class="mb-4">Witaj w BimAI</MudText>
<MudText Typo="Typo.body1" Class="mb-6 text-center">
Zaloguj się używając konta Google
</MudText>
<MudButton
Variant="Variant.Filled"
StartIcon="@Icons.Custom.Brands.Google"
Size="Size.Large"
OnClick="HandleGoogleSignIn"
Disabled="@_isLoading">
@if (_isLoading)
{
<MudProgressCircular Class="mr-3" Size="Size.Small" Indeterminate="true"></MudProgressCircular>
}
else
{
<span>Zaloguj z Google</span>
}
</MudButton>
@if (!string.IsNullOrEmpty(_errorMessage))
{
<MudAlert Severity="Severity.Error" Class="mt-4">
@_errorMessage
</MudAlert>
}
</MudCardContent>
</MudCard>
@code {
private bool _isLoading = false;
private string _errorMessage = string.Empty;
private static LoginCard? _instance;
private bool _isInitialized = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_instance = this;
await InitializeGoogleSignIn();
}
}
private async Task InitializeGoogleSignIn()
{
try
{
if (_isInitialized) return;
var clientId = Configuration["GoogleAuth:ClientId"];
if (string.IsNullOrEmpty(clientId))
{
throw new Exception("Google ClientId is not configured.");
}
await JS.InvokeVoidAsync("initGoogleSignIn", clientId);
_isInitialized = true;
}
catch (Exception ex)
{
_errorMessage = "Błąd inicjalizacji Google Sign-In.";
Console.Error.WriteLine($"Google Sign-In initialization error: {ex.Message}");
}
}
private async Task HandleGoogleSignIn()
{
try
{
_isLoading = true;
_errorMessage = string.Empty;
StateHasChanged();
await JS.InvokeVoidAsync("requestGoogleSignIn");
}
catch (Exception ex)
{
_errorMessage = "Błąd podczas logowania. Spróbuj ponownie";
_isLoading = false;
StateHasChanged();
}
}
[JSInvokable]
public static async Task OnGoogleSignInSuccess(string accessToken, string fullName, string email, string avatarUrl)
{
Console.WriteLine($"Google Sign-In Success: {email}");
if (_instance != null)
{
var userInfo = new UserInfo
{
FullName = fullName,
Email = email,
AvatarUrl = avatarUrl
};
await _instance.AuthService.SetAuthenticationAsync(accessToken, userInfo);
_instance._isLoading = false;
_instance._errorMessage = string.Empty;
_instance.NavigationManager.NavigateTo("/dashboard", replace: true);
await _instance.InvokeAsync(() => _instance.StateHasChanged());
}
}
[JSInvokable]
public static async Task OnGoogleSignInError(string error)
{
Console.WriteLine($"Google SignIn Error: {error}");
if (_instance != null)
{
_instance._isLoading = false;
_instance._errorMessage = "Błąd logowania Google. Spróbuj ponownie";
await _instance.InvokeAsync(() => _instance.StateHasChanged());
}
}
}
<style>
.login-card {
max-width: 400px;
width: 100%;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
}
</style>

View File

@@ -0,0 +1,124 @@
@using MudBlazor.Internal
<MudText Typo="Typo.h4" Class="mb-4">Lista Produktów</MudText>
<MudExpansionPanels Class="mb-4">
<MudExpansionPanel Icon="@Icons.Material.Filled.FilterList"
Text="Filtry"
Expanded="true">
<MudGrid>
<MudItem xs="12" sm="6" md="4">
<MudTextField @bind-Value="filterRequest.Search"
Label="Szukaj"
Placeholder="Nazwa, Kod, EAN..."
Immediate="true"
DebounceInterval="500"
OnDebounceIntervalElapsed="SearchProducts"
Clearable="true"/>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<MudTextField @bind-Value="filterRequest.Name"
Label="Nazwa produktu"
Immediate="true"
DebounceInterval="500"
OnDebounceIntervalElapsed="SearchProducts"
Clearable="true"/>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<MudTextField @bind-Value="filterRequest.Code"
Label="Kod produktu"
Immediate="true"
DebounceInterval="500"
OnDebounceIntervalElapsed="SearchProducts"
Clearable="true"/>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<div style="display: flex; gap: 8px; align-items: flex-end;">
<div style="flex: 1;">
<MudTextField @bind-Value="filterRequest.Ean"
Label="EAN"
Immediate="true"
DebounceInterval="500"
OnDebounceIntervalElapsed="SearchProducts"
Clearable="true"/>
</div>
@if (ScannerService.IsAvailable)
{
<MudIconButton Icon="@Icons.Material.Filled.CameraAlt"
Color="Color.Primary"
OnClick="OnScannerClick"
Variant="Variant.Filled"
Size="Size.Medium"
Title="Skanuj kod EAN"/>
}
</div>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<MudButton Variant="Variant.Outlined"
OnClick="ClearFilters"
StartIcon="Icons.Material.Filled.Clear">
Wyczyść filtry
</MudButton>
</MudItem>
</MudGrid>
</MudExpansionPanel>
</MudExpansionPanels>
<MudDivider Class="my-4"></MudDivider>
<MudTable Items="products.Items"
Dense="true"
Hover="true"
Loading="isLoading"
LoadingProgressColor="Color.Info">
<HeaderContent>
<MudTh>Nazwa</MudTh>
<MudTh>Kod</MudTh>
<MudTh>EAN</MudTh>
<MudTh>Akcje</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Nazwa">@context.Name</MudTd>
<MudTd DataLabel="Kod">@context.Code.Trim()</MudTd>
<MudTd DataLabel="EAN"></MudTd>
<MudTd DataLabel="Akcje">
<MudIconButton Icon="@Icons.Material.Filled.Edit"
Size="Size.Small"
OnClick="() => EditProduct(context.Id)"/>
<MudIconButton Icon="@Icons.Material.Filled.Delete"
Size="Size.Small"
Color="Color.Error"
OnClick="() => DeleteProduct(context.Id)"/>
</MudTd>
</RowTemplate>
<NoRecordsContent>
<MudText>Brak produktów do wyświetlenia</MudText>
</NoRecordsContent>
<LoadingContent>
Ładowanie...
</LoadingContent>
</MudTable>
@if (products.TotalCount > 0)
{
<MudGrid Class="mt-4" AlignItems="Center.Center">
<MudItem xs="12" sm="6">
<MudText Typo="Typo.body2">
Wyniki @((products.Page - 1) * products.PageSize + 1) - @Math.Min(products.Page * products.PageSize, products.TotalCount)
z @products.TotalCount
</MudText>
</MudItem>
<MudItem xs="12" sm="6" Class="d-flex justify-end">
<MudPagination Count="products.TotalPages"
Selected="products.Page"
SelectedChanged="OnPageChanged"
ShowFirstButton="true"
ShowLastButton="true"/>
</MudItem>
</MudGrid>
}

View File

@@ -0,0 +1,104 @@
using BimAI.UI.Shared.Interfaces;
using BimAI.UI.Shared.Services;
using Microsoft.AspNetCore.Components;
using BimAI.Application.DTOModels;
using BimAI.Application.DTOModels.Common;
using MudBlazor;
namespace BimAI.UI.Shared.Components;
public partial class ProductListComponent : ComponentBase
{
[Inject] private ProductService ProductService { get; set; } = default!;
[Inject] private IScannerService ScannerService { get; set; } = default!;
[Inject] private ISnackbar Snackbar { get; set; } = default!;
private PagedResult<ProductDto> products = new();
private ProductFilterRequest filterRequest = new();
private bool isLoading = false;
protected override async Task OnInitializedAsync()
{
await LoadProducts();
}
private async Task LoadProducts()
{
isLoading = true;
try
{
products = await ProductService.GetProductsAsync(filterRequest);
}
catch (Exception ex)
{
Console.WriteLine($"Loading products failed: {ex.Message}");
}
finally
{
isLoading = false;
}
}
private async Task SearchProducts()
{
filterRequest.Page = 1;
await LoadProducts();
}
private async Task OnPageChanged(int page)
{
filterRequest.Page = page;
await LoadProducts();
}
private async Task ClearFilters()
{
filterRequest = new ProductFilterRequest();
await LoadProducts();
}
private async Task EditProduct(Guid productId)
{
// TODO
Console.WriteLine($"Edytuj produkt: {productId}");
}
private async Task DeleteProduct(Guid productId)
{
// TODO
Console.WriteLine($"Usuń produkt: {productId}");
}
private string GetScannerIcon()
{
return ScannerService.IsAvailable ? Icons.Material.Filled.CameraAlt : "";
}
private async Task OnScannerClick()
{
if (!ScannerService.IsAvailable)
{
Snackbar.Add("Skaner nie jest dostępny na tej platformie", Severity.Warning);
return;
}
try
{
var scannedCode = await ScannerService.ScanBarcodeAsync();
if (!string.IsNullOrEmpty(scannedCode))
{
filterRequest.Ean = scannedCode;
await SearchProducts();
Snackbar.Add($"Zeskanowano kod: {scannedCode}", Severity.Success);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Scanner error: {ex.Message}");
Snackbar.Add("Błąd podczas skanowania", Severity.Error);
}
}
}

View File

@@ -0,0 +1,8 @@
@inherits LayoutComponentBase
<MudThemeProvider/>
<MudDialogProvider/>
<MudSnackbarProvider/>
@Body

View File

@@ -0,0 +1,17 @@
using BimAI.UI.Shared.Services;
using Microsoft.Extensions.DependencyInjection;
namespace BimAI.UI.Shared.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSharedServices(this IServiceCollection services, string apiBaseUrl)
{
services.AddHttpClient<ProductService>(client =>
{
client.BaseAddress = new Uri(apiBaseUrl);
});
return services;
}
}

View File

@@ -0,0 +1,9 @@
namespace BimAI.UI.Shared.Interfaces;
public interface IScannerService
{
bool IsAvailable { get; }
Task<bool> RequestCameraPermissionsAsync();
Task<string?> ScanBarcodeAsync();
}

View File

@@ -0,0 +1,69 @@
@using MudBlazor
@inherits LayoutComponentBase
<AuthGuard>
<MudThemeProvider/>
<MudDialogProvider/>
<MudSnackbarProvider/>
<MudLayout>
<MudBreakpointProvider OnBreakpointChanged="OnBreakpointChanged"></MudBreakpointProvider>
<MudAppBar Elevation="0">
<MudIconButton
Icon="@Icons.Material.Filled.Menu"
Color="Color.Inherit"
Edge="Edge.Start"
OnClick="ToggleDrawer"
Class="mud-hidden-md-up"/>
<MudSpacer/>
<MudText Typo="Typo.h6">BimAI</MudText>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen"
Anchor="Anchor.Start"
Variant="@_drawerVariant"
Elevation="1"
ClipMode="DrawerClipMode.Always"
Class="mud-width-250">
<MudNavMenu>
<MudNavLink Href="/dashboard" Icon="@Icons.Material.Filled.Dashboard">Dashboard</MudNavLink>
<MudNavLink Href="/products" Icon="@Icons.Material.Filled.Inventory">Products</MudNavLink>
</MudNavMenu>
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge" Class="my-4">
@Body
</MudContainer>
</MudMainContent>
</MudLayout>
</AuthGuard>
@code {
private bool _drawerOpen = true;
private DrawerVariant _drawerVariant = DrawerVariant.Persistent;
void ToggleDrawer()
{
Console.WriteLine($"ToogleDrawer clickkk {DateTime.Now}");
_drawerOpen = !_drawerOpen;
}
private void OnBreakpointChanged(Breakpoint breakpoint)
{
if (breakpoint < Breakpoint.Md)
{
_drawerVariant = DrawerVariant.Temporary;
_drawerOpen = false;
}
else
{
_drawerVariant = DrawerVariant.Persistent;
_drawerOpen = true;
}
StateHasChanged();
}
}

View File

@@ -0,0 +1,46 @@
@page "/login"
@layout EmptyLayout
<div class="login-page">
<div class="login-container">
<LoginCard />
</div>
</div>
<style>
html, body {
margin: 0 !important;
padding: 0 !important;
height: 100% !important;
overflow: hidden !important;
}
#app {
height: 100% !important;
}
.login-page {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url('_content/BimAI.UI.Shared/images/login-background.jpg') no-repeat center;
background-size: cover;
display: flex;
align-items: center;
justify-content: center;
}
.login-container {
padding: 20px;
width: 100%;
max-width: 450px;
}
</style>

View File

@@ -0,0 +1,8 @@
@page "/products"
@using BimAI.UI.Shared.Components
<PageTitle>Produkty</PageTitle>
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge">
<ProductListComponent />
</MudContainer>

View File

@@ -0,0 +1,113 @@
using System.Text.Json;
using Microsoft.JSInterop;
namespace BimAI.UI.Shared.Services;
public class UserInfo
{
public string FullName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string AvatarUrl { get; set; } = string.Empty;
}
public class AuthService
{
private readonly IJSRuntime _jsRuntime;
private bool? _isAuthenticated;
private UserInfo? _userInfo = null;
public event Action<bool>? AuthenticationStateChanged;
public AuthService(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
public bool IsAuthenticated => _isAuthenticated ?? false;
public UserInfo? CurrentUser => _userInfo;
public async Task<bool> CheckAuthenticationAsync()
{
try
{
var token = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "google_token");
var userInfoJson = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "user_info");
_isAuthenticated = !string.IsNullOrEmpty(token);
if (_isAuthenticated.Value && !string.IsNullOrEmpty(userInfoJson))
{
_userInfo = JsonSerializer.Deserialize<UserInfo>(userInfoJson);
}
Console.WriteLine($"AuthService.CheckAuthentication: token={(!string.IsNullOrEmpty(token) ? "EXISTS" : "NULL")}, isAuth={_isAuthenticated}");
return _isAuthenticated.Value;
}
catch (Exception ex)
{
Console.WriteLine($"AuthService.CheckAuthentication ERROR: {ex.Message}");
_isAuthenticated = false;
_userInfo = null;
return false;
}
}
public async Task SetAuthenticationAsync(string token, UserInfo? userInfo = null)
{
try
{
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "google_token", token);
if (userInfo != null)
{
_userInfo = userInfo;
var userInfoJson = JsonSerializer.Serialize(userInfo);
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "user_info", userInfoJson);
}
_isAuthenticated = true;
Console.WriteLine($"AuthService.SetAuthentication: token saved, user={_userInfo?.Email}");
AuthenticationStateChanged?.Invoke(true);
}
catch (Exception ex)
{
Console.WriteLine($"AuthService.SetAuthentication ERROR: {ex.Message}");
}
}
public async Task ClearAuthenticationAsync()
{
try
{
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", "google_token");
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", "user_info");
_isAuthenticated = false;
_userInfo = null;
Console.WriteLine($"AuthService.ClearAuthentication: token and user ingfo removed");
AuthenticationStateChanged?.Invoke(false);
}
catch (Exception ex)
{
Console.WriteLine($"AuthService.ClearAuthentication ERROR: {ex.Message}");
}
}
public async Task<string?> GetTokenAsync()
{
if (_isAuthenticated != true)
{
await CheckAuthenticationAsync();
}
if (_isAuthenticated != true) return null;
try
{
return await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", "google_token");
}
catch
{
return null;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace BimAI.UI.Shared.Services;
// TODO it's a good place for this file?
public class GoogleAuthConfig
{
public string ClientId { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,18 @@
using BimAI.UI.Shared.Interfaces;
namespace BimAI.UI.Shared.Services;
public class NoOpScannerService : IScannerService
{
public bool IsAvailable => false;
public Task<string?> ScanBarcodeAsync()
{
return Task.FromResult<string?>(null);
}
public Task<bool> RequestCameraPermissionsAsync()
{
return Task.FromResult(false);
}
}

View File

@@ -0,0 +1,52 @@
using System.Text.Json;
using BimAI.Application.DTOModels;
using BimAI.Application.DTOModels.Common;
using Microsoft.AspNetCore.WebUtilities;
namespace BimAI.UI.Shared.Services;
public class ProductService(HttpClient httpClient)
{
private readonly HttpClient _httpClient = httpClient;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
public async Task<PagedResult<ProductDto>> GetProductsAsync(ProductFilterRequest request)
{
var queryParams = new Dictionary<string, string?>
{
["page"] = request.Page.ToString(),
["pageSize"] = request.PageSize.ToString(),
};
if (!string.IsNullOrWhiteSpace(request.Search))
{
queryParams["search"] = request.Search;
}
if (!string.IsNullOrWhiteSpace(request.Name))
{
queryParams["name"] = request.Name;
}
if (!string.IsNullOrWhiteSpace(request.Code))
{
queryParams["code"] = request.Code;
}
if (!string.IsNullOrWhiteSpace(request.Ean))
{
queryParams["ean"] = request.Ean;
}
var uri = QueryHelpers.AddQueryString("api/products", queryParams);
var response = await _httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<PagedResult<ProductDto>>(json, _jsonOptions);
return result ?? new PagedResult<ProductDto>();
}
}

View File

@@ -6,8 +6,7 @@
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Bimix.UI.Web
@using Bimix.UI.Web.Components
@using Bimix.UI.Shared
@using Bimix.UI.Shared.Components
@using MudBlazor
@using BimAI.UI.Shared
@using MudBlazor@using BimAI.Application.DTOModels
@using BimAI.Application.DTOModels.Common
@using BimAI.UI.Shared.Components

Binary file not shown.

After

Width:  |  Height:  |  Size: 965 KiB

View File

@@ -0,0 +1,119 @@
let googleClient = null;
let isSigningIn = false;
function waitForGoogleApi() {
return new Promise((resolve, reject) => {
if (window.google?.accounts?.oauth2) {
resolve(window.google);
return;
}
const maxAttempts = 20;
let attempts = 0;
const checkGoogle = setInterval(() => {
attempts++;
if (window.google?.accounts?.oauth2) {
clearInterval(checkGoogle);
resolve(window.google);
} else if (attempts >= maxAttempts) {
clearInterval(checkGoogle);
reject(new Error('Google OAuth2 API failed to load within the timeout period'));
}
}, 100);
});
}
async function handleAuthError(error, context = '') {
const errorMessage = error?.message || error?.type || error?.toString() || 'Unknown error';
const fullError = `${context}: ${errorMessage}`;
console.error('Google Auth Error:', { context, error, fullError });
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError', fullError);
}
async function fetchUserInfo(accessToken) {
const response = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
headers: { 'Authorization': `Bearer ${accessToken}` }
});
if (!response.ok) {
const errorText = await response.text();
console.error('Failed to fetch user info:', errorText);
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
`Failed to fetch user info: HTTP ${response.status}`);
return null;
}
return await response.json();
}
window.initGoogleSignIn = async function(clientId) {
if (googleClient) {
return googleClient;
}
try {
const google = await waitForGoogleApi();
googleClient = google.accounts.oauth2.initTokenClient({
client_id: clientId,
scope: 'email profile',
callback: async (tokenResponse) => {
try {
if (tokenResponse.error) {
console.error('Token response error:', tokenResponse.error);
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
tokenResponse.error);
return;
}
const userInfo = await fetchUserInfo(tokenResponse.access_token);
if (!userInfo) return;
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInSuccess',
tokenResponse.access_token,
userInfo.name || '',
userInfo.email || '',
userInfo.picture || ''
);
} catch (error) {
console.error('Callback error:', error);
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
error.message || 'Unknown callback error');
} finally {
isSigningIn = false;
}
},
error_callback: async (error) => {
console.error('OAuth flow error:', error);
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
error.type || 'OAuth flow error');
isSigningIn = false;
}
});
return googleClient;
} catch (error) {
console.error('Initiaxcrun xctrace list deviceslization error:', error);
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
error.message || 'Failed to initialize Google Sign-In');
isSigningIn = false;
}
};
window.requestGoogleSignIn = async function() {
if (isSigningIn) {
console.log('Sign-in already in progress');
return;
}
if (!googleClient) {
console.error('Google Sign-In not initialized');
await DotNet.invokeMethodAsync('Bimai.UI.Shared', 'OnGoogleSignInError',
'Google Sign-In not initialized. Call initGoogleSignIn first.');
return;
}
isSigningIn = true;
googleClient.requestAccessToken();
};

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Bimix.UI.Shared\Bimix.UI.Shared.csproj" />
<ProjectReference Include="..\BimAI.UI.Shared\BimAI.UI.Shared.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -8,13 +8,13 @@
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link href="Bimix.UI.Web.styles.css" rel="stylesheet" />
<link href="BimAI.UI.Web.styles.css" rel="stylesheet" />
<script src="https://accounts.google.com/gsi/client" async defer></script>
<HeadOutlet />
</head>
<body>
<Bimix.UI.Shared.Components.Routes @rendermode="InteractiveServer" />
<BimAI.UI.Shared.Components.Routes @rendermode="InteractiveServer" />
<div id="blazor-error-ui">
@@ -30,6 +30,7 @@
<script src="_framework/blazor.web.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
<script src="_content/BimAI.UI.Shared/js/auth.js"></script>
</body>
</html>

View File

@@ -6,6 +6,8 @@
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Bimix.UI.Shared
@using Bimix.UI.Shared.Components
@using BimAI.UI.Web
@using BimAI.UI.Web.Components
@using BimAI.UI.Shared
@using BimAI.UI.Shared.Components
@using MudBlazor

View File

@@ -1,5 +1,8 @@
using Bimix.UI.Shared;
using Bimix.UI.Web.Components;
using BimAI.UI.Shared;
using BimAI.UI.Shared.Extensions;
using BimAI.UI.Shared.Interfaces;
using BimAI.UI.Shared.Services;
using BimAI.UI.Web.Components;
using MudBlazor.Services;
var builder = WebApplication.CreateBuilder(args);
@@ -7,10 +10,13 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddMudServices();
builder.Services.AddHttpClient("BimixAPI", client =>
{
client.BaseAddress = new Uri("https://localhost:7071");
});
builder.Services.AddSharedServices("http://localhost:7142");
builder.Services.AddSingleton<IScannerService, NoOpScannerService>();
builder.Services.AddScoped<AuthService>();
var app = builder.Build();

View File

@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"GoogleAuth": {
"ClientId": "1037727384847-t1l2au6du34kdckamro81guklk17cjah.apps.googleusercontent.com"
}
}

View File

@@ -3,19 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.Domain", "Bimix.Domain\Bimix.Domain.csproj", "{190E3B1F-C91F-430F-BE32-4E7221574D36}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.Domain", "BimAI.Domain\BimAI.Domain.csproj", "{190E3B1F-C91F-430F-BE32-4E7221574D36}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.Application", "Bimix.Application\Bimix.Application.csproj", "{2E61A11C-851F-47D6-A8B6-329078CF1AFC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.Application", "BimAI.Application\BimAI.Application.csproj", "{2E61A11C-851F-47D6-A8B6-329078CF1AFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.Infrastructure", "Bimix.Infrastructure\Bimix.Infrastructure.csproj", "{1049E8B5-6965-4CCD-A989-88E44D40BF48}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.Infrastructure", "BimAI.Infrastructure\BimAI.Infrastructure.csproj", "{1049E8B5-6965-4CCD-A989-88E44D40BF48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.API", "Bimix.API\Bimix.API.csproj", "{02FF9A1C-6D22-4CD1-8FE6-DD5BCDD621DA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.API", "BimAI.API\BimAI.API.csproj", "{02FF9A1C-6D22-4CD1-8FE6-DD5BCDD621DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.UI.Shared", "Bimix.UI.Shared\Bimix.UI.Shared.csproj", "{0EB8CFFF-97BA-48D1-BEC1-2DFD6C934946}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.UI.Shared", "BimAI.UI.Shared\BimAI.UI.Shared.csproj", "{0EB8CFFF-97BA-48D1-BEC1-2DFD6C934946}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.UI.Web", "Bimix.UI.Web\Bimix.UI.Web.csproj", "{7ACBFE74-E72C-4033-9172-30512233A518}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.UI.Web", "BimAI.UI.Web\BimAI.UI.Web.csproj", "{7ACBFE74-E72C-4033-9172-30512233A518}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bimix.UI.Mobile", "Bimix.UI.Mobile\Bimix.UI.Mobile.csproj", "{12FB8E56-08C1-47CF-B0FC-4BE9F01F020A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BimAI.UI.Mobile", "BimAI.UI.Mobile\BimAI.UI.Mobile.csproj", "{12FB8E56-08C1-47CF-B0FC-4BE9F01F020A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@@ -1,6 +0,0 @@
@Bimix.API_HostAddress = http://localhost:5090
GET {{Bimix.API_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -1,19 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Bimix.Infrastructure.Data;
using Bimix.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace Bimix.API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ProductsController(BimixDbContext context) : ControllerBase
{
private readonly BimixDbContext _context = context;
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
return await _context.Products.ToListAsync();
}
}

View File

@@ -1,27 +0,0 @@
using Bimix.Infrastructure.Data;
using Bimix.Infrastructure.Sync;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<BimixDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped<ProductSyncService>();
builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -1,6 +0,0 @@
namespace Bimix.Domain.Entities;
public class Product : BaseEntity
{
public required string Name { get; set; }
}

View File

@@ -1,20 +0,0 @@
using Bimix.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace Bimix.Infrastructure.Data;
public class BimixDbContext(DbContextOptions<BimixDbContext> options) : DbContext(options)
{
public DbSet<Product> Products { get; set; }
public DbSet<SyncState> SyncStates { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Product>().HasKey(x => x.Id);
modelBuilder.Entity<Product>().Property(x => x.Name).IsRequired().HasMaxLength(512);
modelBuilder.Entity<SyncState>().HasKey((x => x.Entity));
}
}

View File

@@ -1,39 +0,0 @@
using Microsoft.Extensions.Logging;
using MudBlazor.Services;
namespace Bimix.UI.Mobile;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });
builder.Services.AddMauiBlazorWebView();
builder.Services.AddMudServices();
var baseUrl = GetApiBaseUrl();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseUrl) });
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
return builder.Build();
}
private static string GetApiBaseUrl()
{
#if IOS
// iOS symulator - użyj swojego IP
return "http://192.168.1.100:5015/"; // Zastąp swoim IP
#else
return "https://localhost:7015/";
#endif
}
}

View File

@@ -1,5 +0,0 @@
@page "/products"
@using MudBlazor
<MudText Typo="Typo.h4">Produkty</MudText>
<p>Lista produktów zostanie tutaj zaimplementowana</p>

View File

@@ -1,66 +0,0 @@
@using MudBlazor
@inherits LayoutComponentBase
<MudThemeProvider/>
<MudDialogProvider/>
<MudSnackbarProvider/>
<MudLayout>
<MudBreakpointProvider OnBreakpointChanged="OnBreakpointChanged"></MudBreakpointProvider>
<MudAppBar Elevation="0">
<MudIconButton
Icon="@Icons.Material.Filled.Menu"
Color="Color.Inherit"
Edge="Edge.Start"
OnClick="ToggleDrawer"
Class="mud-hidden-md-up"/>
<MudSpacer/>
<MudText Typo="Typo.h6">Bimix</MudText>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen"
Anchor="Anchor.Start"
Variant="@_drawerVariant"
Elevation="1"
ClipMode="DrawerClipMode.Always"
Class="mud-width-250">
<MudNavMenu>
<MudNavLink Href="/dashboard" Icon="@Icons.Material.Filled.Dashboard">Dashboard</MudNavLink>
<MudNavLink Href="/products" Icon="@Icons.Material.Filled.Inventory">Products</MudNavLink>
</MudNavMenu>
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="my-4">
@Body
</MudContainer>
</MudMainContent>
</MudLayout>
@code {
private bool _drawerOpen = true;
private DrawerVariant _drawerVariant = DrawerVariant.Persistent;
void ToggleDrawer()
{
Console.WriteLine($"ToogleDrawer clickkk {DateTime.Now}");
_drawerOpen = !_drawerOpen;
}
private void OnBreakpointChanged(Breakpoint breakpoint)
{
if (breakpoint < Breakpoint.Md)
{
_drawerVariant = DrawerVariant.Temporary;
_drawerOpen = false;
}
else
{
_drawerVariant = DrawerVariant.Persistent;
_drawerOpen = true;
}
StateHasChanged();
}
}