123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using Supabase.Postgrest;
- using Supabase.Postgrest.Attributes;
- using Supabase.Postgrest.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using static Supabase.Postgrest.Constants;
- namespace ConsoleAppForUniqWear
- {
- public interface IDatabaseClient
- {
- Task<User> GetUserById(Guid id);
- Task<List<User>> GetAllUsers();
- }
- public interface IReadyMadeClothingDatabaseClient
- {
- Task<ReadyMadeClothing> GetReadyMadeClothingById(Guid id);
- Task<List<ReadyMadeClothing>> GetAllReadyMadeClothing();
- }
- public interface IClothingTypeDatabaseClient
- {
- Task<ClothingType> GetClothingTypeById(Guid id);
- Task<List<ClothingType>> GetAllClothingTypes();
- }
- public class UserService
- {
- private readonly IDatabaseClient _dbClient; // Изменено на интерфейс
- public UserService(IDatabaseClient dbClient)
- {
- _dbClient = dbClient;
- }
- public async Task<User> GetUserByIdAsync(Guid id)
- {
- var result = await _dbClient.GetUserById(id);
- return result;
- }
- public async Task<List<User>> GetAllUsersAsync()
- {
- var result = await _dbClient.GetAllUsers();
- return result;
- }
- }
- public class ClothingTypeService
- {
- private readonly IClothingTypeDatabaseClient _dbClient;
- public ClothingTypeService(IClothingTypeDatabaseClient dbClient)
- {
- _dbClient = dbClient;
- }
- public async Task<ClothingType> GetClothingTypeByIdAsync(Guid id)
- {
- return await _dbClient.GetClothingTypeById(id);
- }
- public async Task<List<ClothingType>> GetAllClothingTypesAsync()
- {
- return await _dbClient.GetAllClothingTypes();
- }
- }
- public class ReadyMadeClothingService
- {
- private readonly IReadyMadeClothingDatabaseClient _dbClient;
- public ReadyMadeClothingService(IReadyMadeClothingDatabaseClient dbClient)
- {
- _dbClient = dbClient;
- }
- public async Task<ReadyMadeClothing> GetReadyMadeClothingByIdAsync(Guid id)
- {
- return await _dbClient.GetReadyMadeClothingById(id);
- }
- public async Task<List<ReadyMadeClothing>> GetAllReadyMadeClothingAsync()
- {
- return await _dbClient.GetAllReadyMadeClothing();
- }
- }
- public class TestReadyMadeClothingDatabaseClient : IReadyMadeClothingDatabaseClient
- {
- private readonly List<ReadyMadeClothing> _testReadyMadeClothing;
- public TestReadyMadeClothingDatabaseClient()
- {
- // Инициализация тестовых данных
- _testReadyMadeClothing = new List<ReadyMadeClothing>
- {
- new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Классическая футболка", Description = "Удобная и стильная", ImageUrl = "image_url_1", Price = 19.99m },
- new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Спортивная кофта", Description = "Для активных людей", ImageUrl = "image_url_2", Price = 29.99m },
- new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Летние джинсы", Description = "Легкие и удобные", ImageUrl = "image_url_3", Price = 39.99m }
- };
- }
- public Task<ReadyMadeClothing> GetReadyMadeClothingById(Guid id)
- {
- var clothing = _testReadyMadeClothing.FirstOrDefault(c => c.Id == id);
- return Task.FromResult(clothing);
- }
- public Task<List<ReadyMadeClothing>> GetAllReadyMadeClothing()
- {
- return Task.FromResult(_testReadyMadeClothing);
- }
- }
- public class TestDatabaseClient : IDatabaseClient
- {
- private readonly List<User> _testUsers;
- public TestDatabaseClient()
- {
- // Инициализация тестовых данных
- _testUsers = new List<User>
- {
- new User { Id = Guid.NewGuid(), FirstName = "John", LastName = "Doe" },
- new User { Id = Guid.NewGuid(), FirstName = "Jane", LastName = "Smith" },
- new User { Id = Guid.NewGuid(), FirstName = "Alice", LastName = "Johnson" }
- };
- }
- public Task<User> GetUserById(Guid id)
- {
- var user = _testUsers.FirstOrDefault(u => u.Id == id);
- return Task.FromResult(user);
- }
- public Task<List<User>> GetAllUsers()
- {
- return Task.FromResult(_testUsers);
- }
- }
- public class TestClothingTypeDatabaseClient : IClothingTypeDatabaseClient
- {
- private readonly List<ClothingType> _testClothingTypes;
- public TestClothingTypeDatabaseClient()
- {
- // Инициализация тестовых данных
- _testClothingTypes = new List<ClothingType>
- {
- new ClothingType { Id = Guid.NewGuid(), Name = "Футболка", Description = "Комфортная футболка" },
- new ClothingType { Id = Guid.NewGuid(), Name = "Кофта", Description = "Теплая кофта" },
- new ClothingType { Id = Guid.NewGuid(), Name = "Джинсы", Description = "Удобные джинсы" }
- };
- }
- public Task<ClothingType> GetClothingTypeById(Guid id)
- {
- var clothingType = _testClothingTypes.FirstOrDefault(ct => ct.Id == id);
- return Task.FromResult(clothingType);
- }
- public Task<List<ClothingType>> GetAllClothingTypes()
- {
- return Task.FromResult(_testClothingTypes);
- }
- }
- // Модель ReadyMadeClothing
- public class ReadyMadeClothing
- {
- [Column("id")]
- public Guid Id { get; set; }
- [Column("clothing_type_id")]
- public Guid ClothingTypeId { get; set; }
- [Column("name")]
- public string Name { get; set; }
- [Column("description")]
- public string Description { get; set; }
- [Column("image_url")]
- public string ImageUrl { get; set; }
- [Column("price")]
- public decimal Price { get; set; }
- [Column("created_at")]
- public DateTime CreatedAt { get; set; }
- }
- // Модель ClothingType
- public class ClothingType
- {
- [Column("id")]
- public Guid Id { get; set; }
- [Column("name")]
- public string Name { get; set; }
- [Column("description")]
- public string Description { get; set; }
- [Column("created_at")]
- public DateTime CreatedAt { get; set; }
- }
- // Модель User
- public class User : BaseModel
- {
- [Column("id")]
- public Guid Id { get; set; }
- [Column("uid")]
- public string Uid { get; set; }
- [Column("first_name")]
- public string FirstName { get; set; }
- [Column("last_name")]
- public string LastName { get; set; }
- [Column("middle_name")]
- public string MiddleName { get; set; }
- [Column("photo_url")]
- public string PhotoUrl { get; set; }
- [Column("created_at")]
- public DateTime CreatedAt { get; set; }
- [Column("updated_at")]
- public DateTime UpdatedAt { get; set; }
- }
-
- class Program
- {
- static async Task Main(string[] args)
- {
- // Инициализация тестового клиента для работы с базой данных
- var testClient = new TestDatabaseClient();
- var userService = new UserService(testClient); // Используем TestDatabaseClient
- // Пример получения пользователя по ID
- Guid userId = Guid.Parse("вставьте_здесь_id_пользователя"); // Замените на фактический ID
- var user = await userService.GetUserByIdAsync(userId);
- Console.WriteLine($"Retrieved user: {user?.FirstName}");
- // Пример получения всех пользователей
- var allUsers = await userService.GetAllUsersAsync();
- Console.WriteLine($"Total users: {allUsers.Count}");
- foreach (var u in allUsers)
- {
- Console.WriteLine($"User: {u.FirstName} {u.LastName}");
- }
- }
- }
- }
|