This commit is contained in:
Riccardo Usardi committed 2025-07-09 22:23:37 +02:00
1 parent 2db9525570
commit 5aca82244e
21 files changed
+545 -126

No files matched your search

@@ -0,0 +1,46 @@
using Microsoft.Extensions.Configuration;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using uWine.Core.Domain.Entities;
using uWine.Core.Domain.Repositories;
namespace uWine.Core.Infrastructure.Repository
{
public class MariaDbUserRepository : MariaDbServerRepository, IUserRepository
{
public MariaDbUserRepository(IConfiguration configuration) : base(configuration)
{
}
public User UserById(int userId)
{
using (var conn = Connection())
{
conn.Open();
var cmd = new MySqlCommand($@"SELECT u.id,
username,
u.role as role_id,
r.role as name,
description
FROM t_users u
JOIN t_role r on r.id = u.role
WHERE u.id = @userId ", conn);
cmd.Parameters.AddWithValue("userId", userId);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
return User.LoginUser(reader);
}
}
}
return new User();
}
}
}
@@ -113,5 +113,23 @@ namespace uWine.Core.Infrastructure.Repository
}
return new Owner();
}
public List<Owner> FromUser(int userId)
{
var list = new List<Owner>();
using (var connection = Connection()) {
connection.Open();
var cmd = new MySqlCommand("SELECT owner_id as id FROM t_user_owner WHERE user_id = @userId",connection);
cmd.Parameters.AddWithValue("userId", userId);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Simple(reader.GetInt32("id")));
}
}
}
return list;
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using uWine.Core.Domain.Entities;
using uWine.Core.Domain.Repositories;
namespace uWine.Core.Infrastructure.Services
{
public class UserService
{
public IUserRepository _userRepository;
readonly OwnerService _ownerService;
readonly IOwnerRepository _ownerRepository;
public UserService(
IUserRepository userRepository, OwnerService ownerService, IOwnerRepository ownerRepository)
{
_userRepository = userRepository;
_ownerService = ownerService;
_ownerRepository = ownerRepository;
}
public User Process(int userId)
{
var user = _userRepository.UserById(userId);
if (user == null)
throw new ArgumentException($"User with ID {userId} not found.");
var owners = _ownerRepository.FromUser(userId)
?.Select(owner => _ownerService.FillData(owner))
.ToList();
user.Owners = owners ?? new List<Owner>();
return user;
}
}
}