isList handling

This commit is contained in:
2021-11-30 19:09:22 +01:00
parent 2a3910a7cf
commit 8519da26a8
3 changed files with 47 additions and 11 deletions

View File

@ -15,6 +15,7 @@ namespace de.hottis.genericdatabaseapiservice.Services {
public class DbServiceException : Exception {}
public class NotDataFoundException : DbServiceException {}
public class TooMuchDataFoundException : DbServiceException {}
public class DbService : IDbService {
private readonly IConfiguration Configuration;
@ -30,7 +31,7 @@ namespace de.hottis.genericdatabaseapiservice.Services {
Configuration["Database:Name"]);
}
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, TIN input) {
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input) {
var itemList = new List<TOUT>();
Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
@ -63,14 +64,12 @@ namespace de.hottis.genericdatabaseapiservice.Services {
var dma = (DataMemberAttribute)attributes[0];
int ordinal = reader.GetOrdinal(dma.Name);
Console.WriteLine("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
if (propertyInfo.PropertyType == typeof(System.String)) {
if (reader.IsDBNull(ordinal)) {
propertyInfo.SetValue(item, null);
Console.WriteLine("Output Value: null");
} else {
propertyInfo.SetValue(item, reader.GetString(ordinal));
Console.WriteLine("Output Value:{0}", reader.GetString(ordinal));
}
if (reader.IsDBNull(ordinal)) {
propertyInfo.SetValue(item, null);
Console.WriteLine("Output Value: null");
} else if (propertyInfo.PropertyType == typeof(System.String)) {
propertyInfo.SetValue(item, reader.GetString(ordinal));
Console.WriteLine("Output Value:{0}", reader.GetString(ordinal));
} else if (propertyInfo.PropertyType == typeof(System.Int32)) {
propertyInfo.SetValue(item, reader.GetInt32(ordinal));
Console.WriteLine("Output Value:{0}", reader.GetInt32(ordinal));
@ -85,6 +84,9 @@ namespace de.hottis.genericdatabaseapiservice.Services {
if (itemList.Count == 0) {
throw new NotDataFoundException();
}
if (justOne && itemList.Count > 1) {
throw new TooMuchDataFoundException();
}
return itemList;
}