| | | 1 | | using Snacks.DAL.Abstractions; |
| | | 2 | | using Snacks.DAL.Common; |
| | | 3 | | using Snacks.Domain.Abstractions; |
| | | 4 | | |
| | | 5 | | namespace Snacks.DAL.Entities; |
| | | 6 | | |
| | | 7 | | public class SnacksDao<T> : ISnacksDao<T> where T : IDomainModels |
| | | 8 | | { |
| | | 9 | | protected IAdoTemplate template; |
| | 256 | 10 | | protected QueryStore? queryStore = QueryStore.Instance; |
| | | 11 | | protected List<QueryStore.JQuery>? queries; |
| | | 12 | | |
| | 256 | 13 | | public SnacksDao(IConnectionFactory connectionFactory) |
| | 256 | 14 | | { |
| | 256 | 15 | | this.template = new AdoTemplate(connectionFactory); |
| | 256 | 16 | | if (queryStore!.Root is not null && queryStore.Root.Entities is not null) |
| | 256 | 17 | | { |
| | 256 | 18 | | queries = queryStore.Root.Entities[typeof(T).Name].Queries; |
| | 256 | 19 | | } |
| | 256 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<bool> DeleteAsync(T input) |
| | 82 | 23 | | { |
| | 302 | 24 | | var query = queries?.Find(x => x.Name == "DeleteAsync"); |
| | 82 | 25 | | var parameter = query is null? throw new ArgumentNullException(): query.Parameters; |
| | 78 | 26 | | if (query is null || query.Statement is null || parameter is null) |
| | 8 | 27 | | { throw new ArgumentNullException(); } |
| | 74 | 28 | | return 1 == await template.ExecuteAsync(query.Statement, FillParametersWithValues(input, parameter)); |
| | 74 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<IEnumerable<T>> SelectAsync(T input) |
| | 200 | 32 | | { |
| | 396 | 33 | | var query = queries is null? throw new ArgumentNullException(): queries.Find(x => x.Name == "SelectAsync"); |
| | 198 | 34 | | var parameter = query is null ? throw new ArgumentNullException() : query.Parameters; |
| | 196 | 35 | | var mapper = queryStore!.Root is null ? throw new ArgumentNullException() : (RowMapper<T>)queryStore.Root.Entiti |
| | 194 | 36 | | if (query.Statement is null || parameter is null || mapper is null) |
| | 8 | 37 | | { throw new ArgumentNullException(); } |
| | 190 | 38 | | return await template.QueryAsync(query.Statement, mapper, FillParametersWithValues(input, parameter)); |
| | 190 | 39 | | } |
| | | 40 | | |
| | | 41 | | public async Task<bool> InsertAsync(T input) |
| | 38 | 42 | | { |
| | 118 | 43 | | var query = queries is null? throw new ArgumentNullException() : queries.Find(x => x.Name == "InsertAsync"); |
| | 36 | 44 | | var par = query is null? throw new ArgumentNullException(): query.Parameters; |
| | 34 | 45 | | if (query.Statement is null || par is null) |
| | 8 | 46 | | { throw new ArgumentNullException(); } |
| | 30 | 47 | | return 1 == await template.ExecuteAsync(query.Statement, FillParametersWithValues(input, par)); |
| | 28 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task<bool> UpdateAsync(T input) |
| | 60 | 51 | | { |
| | 240 | 52 | | var query = queries is null? throw new ArgumentNullException(): queries.Find(x => x.Name == "UpdateAsync"); |
| | 58 | 53 | | var par = query is null? throw new ArgumentNullException(): query.Parameters; |
| | 56 | 54 | | if (query.Statement is null || par is null) |
| | 8 | 55 | | { throw new ArgumentNullException();} |
| | 52 | 56 | | return 1 == await template.ExecuteAsync(query.Statement, FillParametersWithValues(input, par)); |
| | | 57 | | |
| | 52 | 58 | | } |
| | | 59 | | |
| | | 60 | | private QueryParameter[] FillParametersWithValues<T>(T input, QueryStore.JParameter[] par) |
| | 346 | 61 | | { |
| | 346 | 62 | | var parameters = new List<QueryParameter>(); |
| | | 63 | | |
| | 3680 | 64 | | foreach (var parameter in par) |
| | 1322 | 65 | | { |
| | | 66 | | |
| | 1322 | 67 | | var parameterName = parameter.Name is null? throw new ArgumentNullException(): parameter.Name.TrimStart('@') |
| | 1320 | 68 | | var propertyInfo = typeof(T).GetProperties() |
| | 5538 | 69 | | .FirstOrDefault(p => p.Name.ToLower() == parameterName); |
| | | 70 | | |
| | 1320 | 71 | | if (propertyInfo != null) |
| | 1320 | 72 | | { |
| | 1320 | 73 | | var propertyValue = propertyInfo.GetValue(input); |
| | 1320 | 74 | | parameters.Add(new QueryParameter(parameter.Name, propertyValue)); |
| | 1320 | 75 | | } |
| | 1320 | 76 | | } |
| | 344 | 77 | | return parameters.ToArray(); |
| | 344 | 78 | | } |
| | | 79 | | |
| | | 80 | | |
| | | 81 | | } |