255 lines
8.7 KiB
C#
255 lines
8.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using CarParkValidationAPI.Entities;
|
|
using CarParkValidationAPI.Models;
|
|
using Microsoft.Extensions.Options;
|
|
//using Microsoft.Data.SqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
using Microsoft.OpenApi.Reader;
|
|
|
|
namespace CarParkValidationAPI.Datas;
|
|
|
|
public class DataContext : DbContext
|
|
{
|
|
// public DbSet<User> Users { get; set; }
|
|
public DbSet<AdminUser> AdminUsers { get; set; }
|
|
public DbSet<ValidationPoint> Validations { get; set; }
|
|
public DbSet<ConcessionValidation> ConcessionValidations { get; set; }
|
|
public DbSet<ConcessionType> ConcessionTypes { get; set; }
|
|
private readonly IConfiguration Configuration;
|
|
private IOptions<AppSettings> _appSettings;
|
|
|
|
public DataContext(IConfiguration configuration, IOptions<AppSettings> appSettings, DbContextOptions<DataContext> options): base(options)
|
|
{
|
|
Configuration = configuration;
|
|
_appSettings = appSettings;
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<ValidationPoint>().ToTable("ValidationPoint");
|
|
modelBuilder.Entity<ConcessionValidation>().ToTable("ConcessionValidation");
|
|
modelBuilder.Entity<ConcessionType>().ToTable("ConcessionType");
|
|
modelBuilder.Entity<AdminUser>().ToTable("AdminUser");
|
|
// Forces EF Core to map all DateTime properties to 'timestamp without time zone'
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
var properties = entityType.GetProperties()
|
|
.Where(p => p.ClrType == typeof(DateTime) || p.ClrType == typeof(DateTime?));
|
|
|
|
foreach (var property in properties)
|
|
{
|
|
property.SetColumnType("timestamp without time zone");
|
|
}
|
|
}
|
|
}
|
|
|
|
// protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|
//{
|
|
// in memory database used for simplicity, change to a real db for production applications
|
|
// options.UseInMemoryDatabase("TestDb");
|
|
// options.UseSqlServer(_appSettings.Value.SQLConnectionString);
|
|
// options.UseSqlServer(Configuration.GetValue<string>("AppSettings:SQLConnectionString"));
|
|
//}
|
|
|
|
private ConcessionValidationExtDto PopulateItem(IDataReader reader)
|
|
{
|
|
ConcessionValidationExtDto item = new ConcessionValidationExtDto();
|
|
object myobj;
|
|
myobj = reader["ValidationID"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.Id = (int)myobj;
|
|
|
|
myobj = reader["ValidateDate"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ValidateDate = (DateTime)myobj;
|
|
|
|
myobj = reader["ConcessionCard"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ConcessionCard = (string)myobj;
|
|
|
|
myobj = reader["ConcessionHolder"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ConcessionHolder = (string)myobj;
|
|
|
|
myobj = reader["ConcessionExpiry"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ConcessionExpiry = (bool)myobj;
|
|
/*
|
|
myobj = reader["ConcessionTypeID"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ConcessionTypeId = (int)myobj;
|
|
*/
|
|
myobj = reader["StafflinkNo"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.StafflinkNo = (string)myobj;
|
|
|
|
myobj = reader["ConcessionType"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ConcessionType = (string)myobj;
|
|
myobj = reader["ValidationPoint"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ValidationPoint = (string)myobj;
|
|
|
|
myobj = reader["StaffFirstName"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.StaffFirstName = (string)myobj;
|
|
|
|
myobj = reader["StaffLastName"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.StaffLastName = (string)myobj;
|
|
|
|
myobj = reader["StaffEmail"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.StaffEmail = (string)myobj;
|
|
/*
|
|
myobj = reader["ValidationPointID"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.ValidationPointId = (int)myobj;
|
|
*/
|
|
myobj = reader["Active"];
|
|
if (myobj != System.DBNull.Value)
|
|
item.Active = (bool)myobj;
|
|
|
|
return item;
|
|
}
|
|
|
|
public async Task<List<ConcessionValidationExtDto>> SelectConcessionValidationByStaffId(bool activeOnly,
|
|
string stafflinkNo, DateTime fromDate, DateTime toDate)
|
|
{
|
|
using (var command = this.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
var pfromDate = command.CreateParameter();
|
|
|
|
pfromDate.ParameterName = "FromDate";
|
|
pfromDate.Value = fromDate.Date;
|
|
|
|
var pToDate = command.CreateParameter();
|
|
|
|
pToDate.ParameterName = "toDate";
|
|
pToDate.Value = toDate.Date;
|
|
|
|
var pStaffLink = command.CreateParameter();
|
|
|
|
pStaffLink.ParameterName = "StaffLinkNo";
|
|
pStaffLink.Value = stafflinkNo;
|
|
|
|
|
|
|
|
ConcessionValidationExtDto item;
|
|
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
|
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
command.CommandText = ("capv_ConValidation_Sel");
|
|
command.Parameters.Add(pfromDate);
|
|
command.Parameters.Add(pToDate);
|
|
command.Parameters.Add(pStaffLink);
|
|
this.Database.OpenConnection();
|
|
using (DbDataReader reader = await command.ExecuteReaderAsync())
|
|
{
|
|
while (await reader.ReadAsync())
|
|
{
|
|
item = PopulateItem(reader);
|
|
result.Add(item);
|
|
}
|
|
}
|
|
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public async Task<List<int>> SelectCheckConcessionValidationAsync(DateTime fromDate, string concessionCard,
|
|
string staffLinkNo)
|
|
{
|
|
using (var command = this.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
var pfromDate = command.CreateParameter();
|
|
|
|
pfromDate.ParameterName = "FromDate";
|
|
pfromDate.Value = fromDate.Date;
|
|
|
|
|
|
var pStaffLink = command.CreateParameter();
|
|
|
|
pStaffLink.ParameterName = "StaffLinkNo";
|
|
pStaffLink.Value = staffLinkNo;
|
|
|
|
|
|
var pConcessionCard = command.CreateParameter();
|
|
pConcessionCard.ParameterName = "ConcessionCard";
|
|
pConcessionCard.Value = concessionCard;
|
|
|
|
|
|
ConcessionValidationExtDto item;
|
|
List<int> result = new List<int>();
|
|
object myobj;
|
|
int id;
|
|
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
command.CommandText = ("capv_CheckConValidation_Sel");
|
|
command.Parameters.Add(pfromDate);
|
|
command.Parameters.Add(pStaffLink);
|
|
command.Parameters.Add(pConcessionCard);
|
|
this.Database.OpenConnection();
|
|
using (DbDataReader reader = await command.ExecuteReaderAsync())
|
|
{
|
|
while (await reader.ReadAsync())
|
|
{
|
|
myobj = reader["ValidationID"];
|
|
if (myobj != System.DBNull.Value)
|
|
{
|
|
id = (int)myobj;
|
|
result.Add(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<List<ConcessionValidationExtDto>> SelectConcessionValidationByDate(bool activeOnly,
|
|
DateTime fromDate, DateTime toDate)
|
|
{
|
|
using (var command = this.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
var pfromDate = command.CreateParameter();
|
|
|
|
pfromDate.ParameterName = "FromDate";
|
|
pfromDate.Value = fromDate.Date;
|
|
|
|
var pToDate = command.CreateParameter();
|
|
|
|
pToDate.ParameterName = "toDate";
|
|
pToDate.Value = toDate.Date;
|
|
|
|
ConcessionValidationExtDto item;
|
|
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
|
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
command.CommandText = ("capv_ConValidationDate_Sel");
|
|
command.Parameters.Add(pfromDate);
|
|
command.Parameters.Add(pToDate);
|
|
|
|
this.Database.OpenConnection();
|
|
using (DbDataReader reader = await command.ExecuteReaderAsync())
|
|
{
|
|
while (await reader.ReadAsync())
|
|
{
|
|
item = PopulateItem(reader);
|
|
result.Add(item);
|
|
}
|
|
}
|
|
|
|
result.Sort((x, y) => x.ValidateDate.Date.CompareTo(y.ValidateDate.Date));
|
|
return result;
|
|
}
|
|
}
|
|
} |