commit change all folder
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var pfromDate = new SqlParameter
|
||||
{
|
||||
ParameterName = "fromDate",
|
||||
Value = fromDate.Date
|
||||
};
|
||||
var pStaffLink = new SqlParameter
|
||||
{
|
||||
ParameterName = "StafflinkNo",
|
||||
Value = string.IsNullOrEmpty(stafflinkNo) ? "" : stafflinkNo,
|
||||
};
|
||||
|
||||
var pToDate = new SqlParameter
|
||||
{
|
||||
ParameterName = "toDate",
|
||||
Value = toDate.Date
|
||||
};
|
||||
ConcessionValidationExtDto item;
|
||||
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
||||
using (var command = this.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
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)
|
||||
{
|
||||
var pfromDate = new SqlParameter
|
||||
{
|
||||
ParameterName = "FromDate",
|
||||
Value = fromDate.Date,
|
||||
};
|
||||
|
||||
var pStaffLink = new SqlParameter
|
||||
{
|
||||
ParameterName = "StaffLinkNo",
|
||||
Value = staffLinkNo,
|
||||
};
|
||||
|
||||
var pConcessionCard = new SqlParameter
|
||||
{
|
||||
ParameterName = "ConcessionCard",
|
||||
Value = concessionCard,
|
||||
};
|
||||
|
||||
ConcessionValidationExtDto item;
|
||||
List<int> result = new List<int>();
|
||||
object myobj;
|
||||
int id;
|
||||
using (var command = this.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
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)
|
||||
{
|
||||
var pfromDate = new SqlParameter
|
||||
{
|
||||
ParameterName = "fromDate",
|
||||
Value = fromDate.Date
|
||||
};
|
||||
var pToDate = new SqlParameter
|
||||
{
|
||||
ParameterName = "toDate",
|
||||
Value = toDate.Date
|
||||
};
|
||||
ConcessionValidationExtDto item;
|
||||
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
||||
using (var command = this.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user