| | | 1 | | using System.Data; |
| | | 2 | | using System.Data.SqlClient; |
| | | 3 | | using ConsoleApp1; |
| | | 4 | | using Dapper; |
| | | 5 | | using Microsoft.Data.SqlClient; |
| | | 6 | | using Microsoft.Extensions.Configuration; |
| | | 7 | | using TreeGrouping.Application.DbService; |
| | | 8 | | using TreeGrouping.Application.DbService.Models; |
| | | 9 | | |
| | | 10 | | public class DatabaseService:IDatabaseService |
| | | 11 | | { |
| | | 12 | | private readonly string _connectionString; |
| | | 13 | | |
| | 0 | 14 | | public DatabaseService(IConfiguration configuration) |
| | | 15 | | { |
| | 0 | 16 | | _connectionString = configuration.GetConnectionString("DefaultTreeConnection") |
| | 0 | 17 | | ?? throw new InvalidOperationException("Connection string not found"); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<IEnumerable<CategoryModel>> ExecuteStoredProcedureAsync(StoredProcedureType type, object parameter |
| | | 21 | | { |
| | 0 | 22 | | using var connection = new SqlConnection(_connectionString); |
| | | 23 | | |
| | 0 | 24 | | var procedureName = type.GetProcedureName(); |
| | 0 | 25 | | var paramDict = type.GetParameter(parameters); |
| | | 26 | | |
| | 0 | 27 | | return await connection.QueryAsync<CategoryModel>( |
| | 0 | 28 | | procedureName, paramDict, commandType: CommandType.StoredProcedure |
| | 0 | 29 | | ); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public IEnumerable<CategoryModel> ExecuteStoredProcedure(StoredProcedureType type, object? parameters = null) |
| | | 33 | | { |
| | 0 | 34 | | using var connection = new SqlConnection(_connectionString); |
| | | 35 | | |
| | 0 | 36 | | var procedureName = type.GetProcedureName(); |
| | 0 | 37 | | var paramDict = type.GetParameter(parameters); |
| | | 38 | | |
| | 0 | 39 | | return connection.Query<CategoryModel>( |
| | 0 | 40 | | procedureName, paramDict, commandType: CommandType.StoredProcedure |
| | 0 | 41 | | ); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | |
| | | 45 | | public async Task<IEnumerable<CategoryLinkModel>> GetAllCategoryLinksAsync() |
| | | 46 | | { |
| | 0 | 47 | | using var connection = new SqlConnection(_connectionString); |
| | | 48 | | |
| | 0 | 49 | | return await connection.QueryAsync<CategoryLinkModel>( |
| | 0 | 50 | | "GetAllCategoryLinks", |
| | 0 | 51 | | commandType: CommandType.StoredProcedure |
| | 0 | 52 | | ); |
| | 0 | 53 | | } |
| | | 54 | | } |