Program.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Supabase.Postgrest;
  2. using Supabase.Postgrest.Attributes;
  3. using Supabase.Postgrest.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using static Supabase.Postgrest.Constants;
  9. namespace ConsoleAppForUniqWear
  10. {
  11. public interface IDatabaseClient
  12. {
  13. Task<User> GetUserById(Guid id);
  14. Task<List<User>> GetAllUsers();
  15. }
  16. public interface IReadyMadeClothingDatabaseClient
  17. {
  18. Task<ReadyMadeClothing> GetReadyMadeClothingById(Guid id);
  19. Task<List<ReadyMadeClothing>> GetAllReadyMadeClothing();
  20. }
  21. public interface IClothingTypeDatabaseClient
  22. {
  23. Task<ClothingType> GetClothingTypeById(Guid id);
  24. Task<List<ClothingType>> GetAllClothingTypes();
  25. }
  26. public class UserService
  27. {
  28. private readonly IDatabaseClient _dbClient; // Изменено на интерфейс
  29. public UserService(IDatabaseClient dbClient)
  30. {
  31. _dbClient = dbClient;
  32. }
  33. public async Task<User> GetUserByIdAsync(Guid id)
  34. {
  35. var result = await _dbClient.GetUserById(id);
  36. return result;
  37. }
  38. public async Task<List<User>> GetAllUsersAsync()
  39. {
  40. var result = await _dbClient.GetAllUsers();
  41. return result;
  42. }
  43. }
  44. public class ClothingTypeService
  45. {
  46. private readonly IClothingTypeDatabaseClient _dbClient;
  47. public ClothingTypeService(IClothingTypeDatabaseClient dbClient)
  48. {
  49. _dbClient = dbClient;
  50. }
  51. public async Task<ClothingType> GetClothingTypeByIdAsync(Guid id)
  52. {
  53. return await _dbClient.GetClothingTypeById(id);
  54. }
  55. public async Task<List<ClothingType>> GetAllClothingTypesAsync()
  56. {
  57. return await _dbClient.GetAllClothingTypes();
  58. }
  59. }
  60. public class ReadyMadeClothingService
  61. {
  62. private readonly IReadyMadeClothingDatabaseClient _dbClient;
  63. public ReadyMadeClothingService(IReadyMadeClothingDatabaseClient dbClient)
  64. {
  65. _dbClient = dbClient;
  66. }
  67. public async Task<ReadyMadeClothing> GetReadyMadeClothingByIdAsync(Guid id)
  68. {
  69. return await _dbClient.GetReadyMadeClothingById(id);
  70. }
  71. public async Task<List<ReadyMadeClothing>> GetAllReadyMadeClothingAsync()
  72. {
  73. return await _dbClient.GetAllReadyMadeClothing();
  74. }
  75. }
  76. public class TestReadyMadeClothingDatabaseClient : IReadyMadeClothingDatabaseClient
  77. {
  78. private readonly List<ReadyMadeClothing> _testReadyMadeClothing;
  79. public TestReadyMadeClothingDatabaseClient()
  80. {
  81. // Инициализация тестовых данных
  82. _testReadyMadeClothing = new List<ReadyMadeClothing>
  83. {
  84. new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Классическая футболка", Description = "Удобная и стильная", ImageUrl = "image_url_1", Price = 19.99m },
  85. new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Спортивная кофта", Description = "Для активных людей", ImageUrl = "image_url_2", Price = 29.99m },
  86. new ReadyMadeClothing { Id = Guid.NewGuid(), ClothingTypeId = Guid.NewGuid(), Name = "Летние джинсы", Description = "Легкие и удобные", ImageUrl = "image_url_3", Price = 39.99m }
  87. };
  88. }
  89. public Task<ReadyMadeClothing> GetReadyMadeClothingById(Guid id)
  90. {
  91. var clothing = _testReadyMadeClothing.FirstOrDefault(c => c.Id == id);
  92. return Task.FromResult(clothing);
  93. }
  94. public Task<List<ReadyMadeClothing>> GetAllReadyMadeClothing()
  95. {
  96. return Task.FromResult(_testReadyMadeClothing);
  97. }
  98. }
  99. public class TestDatabaseClient : IDatabaseClient
  100. {
  101. private readonly List<User> _testUsers;
  102. public TestDatabaseClient()
  103. {
  104. // Инициализация тестовых данных
  105. _testUsers = new List<User>
  106. {
  107. new User { Id = Guid.NewGuid(), FirstName = "John", LastName = "Doe" },
  108. new User { Id = Guid.NewGuid(), FirstName = "Jane", LastName = "Smith" },
  109. new User { Id = Guid.NewGuid(), FirstName = "Alice", LastName = "Johnson" }
  110. };
  111. }
  112. public Task<User> GetUserById(Guid id)
  113. {
  114. var user = _testUsers.FirstOrDefault(u => u.Id == id);
  115. return Task.FromResult(user);
  116. }
  117. public Task<List<User>> GetAllUsers()
  118. {
  119. return Task.FromResult(_testUsers);
  120. }
  121. }
  122. public class TestClothingTypeDatabaseClient : IClothingTypeDatabaseClient
  123. {
  124. private readonly List<ClothingType> _testClothingTypes;
  125. public TestClothingTypeDatabaseClient()
  126. {
  127. // Инициализация тестовых данных
  128. _testClothingTypes = new List<ClothingType>
  129. {
  130. new ClothingType { Id = Guid.NewGuid(), Name = "Футболка", Description = "Комфортная футболка" },
  131. new ClothingType { Id = Guid.NewGuid(), Name = "Кофта", Description = "Теплая кофта" },
  132. new ClothingType { Id = Guid.NewGuid(), Name = "Джинсы", Description = "Удобные джинсы" }
  133. };
  134. }
  135. public Task<ClothingType> GetClothingTypeById(Guid id)
  136. {
  137. var clothingType = _testClothingTypes.FirstOrDefault(ct => ct.Id == id);
  138. return Task.FromResult(clothingType);
  139. }
  140. public Task<List<ClothingType>> GetAllClothingTypes()
  141. {
  142. return Task.FromResult(_testClothingTypes);
  143. }
  144. }
  145. // Модель ReadyMadeClothing
  146. public class ReadyMadeClothing
  147. {
  148. [Column("id")]
  149. public Guid Id { get; set; }
  150. [Column("clothing_type_id")]
  151. public Guid ClothingTypeId { get; set; }
  152. [Column("name")]
  153. public string Name { get; set; }
  154. [Column("description")]
  155. public string Description { get; set; }
  156. [Column("image_url")]
  157. public string ImageUrl { get; set; }
  158. [Column("price")]
  159. public decimal Price { get; set; }
  160. [Column("created_at")]
  161. public DateTime CreatedAt { get; set; }
  162. }
  163. // Модель ClothingType
  164. public class ClothingType
  165. {
  166. [Column("id")]
  167. public Guid Id { get; set; }
  168. [Column("name")]
  169. public string Name { get; set; }
  170. [Column("description")]
  171. public string Description { get; set; }
  172. [Column("created_at")]
  173. public DateTime CreatedAt { get; set; }
  174. }
  175. // Модель User
  176. public class User : BaseModel
  177. {
  178. [Column("id")]
  179. public Guid Id { get; set; }
  180. [Column("uid")]
  181. public string Uid { get; set; }
  182. [Column("first_name")]
  183. public string FirstName { get; set; }
  184. [Column("last_name")]
  185. public string LastName { get; set; }
  186. [Column("middle_name")]
  187. public string MiddleName { get; set; }
  188. [Column("photo_url")]
  189. public string PhotoUrl { get; set; }
  190. [Column("created_at")]
  191. public DateTime CreatedAt { get; set; }
  192. [Column("updated_at")]
  193. public DateTime UpdatedAt { get; set; }
  194. }
  195. class Program
  196. {
  197. static async Task Main(string[] args)
  198. {
  199. // Инициализация тестового клиента для работы с базой данных
  200. var testClient = new TestDatabaseClient();
  201. var userService = new UserService(testClient); // Используем TestDatabaseClient
  202. // Пример получения пользователя по ID
  203. Guid userId = Guid.Parse("вставьте_здесь_id_пользователя"); // Замените на фактический ID
  204. var user = await userService.GetUserByIdAsync(userId);
  205. Console.WriteLine($"Retrieved user: {user?.FirstName}");
  206. // Пример получения всех пользователей
  207. var allUsers = await userService.GetAllUsersAsync();
  208. Console.WriteLine($"Total users: {allUsers.Count}");
  209. foreach (var u in allUsers)
  210. {
  211. Console.WriteLine($"User: {u.FirstName} {u.LastName}");
  212. }
  213. }
  214. }
  215. }