179 lines
6.4 KiB
C#
179 lines
6.4 KiB
C#
using Dapper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using TrackerLibrary.Models;
|
|
|
|
|
|
//@PlaceNumber int,
|
|
//@PlaceName nvarchar(50),
|
|
//@PrizeAmount money,
|
|
// @PrizePercentage float,
|
|
//@id int = 0 output
|
|
|
|
namespace TrackerLibrary.DataAccess
|
|
{
|
|
public class SqlConnector : IDataConnection
|
|
{
|
|
private const string db = "Tournaments";
|
|
public PersonModel CreatePerson(PersonModel model)
|
|
{
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
var p = new DynamicParameters();
|
|
p.Add("@FirstName", model.FirstName);
|
|
p.Add("@LastName", model.LastName);
|
|
p.Add("@EmailAddress", model.EmailAddress);
|
|
p.Add("@CellPhoneNumber", model.CellPhoneNumber);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spPeople_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
model.Id = p.Get<int>("@Id");
|
|
|
|
return model;
|
|
}
|
|
}
|
|
|
|
// TODO - Make the CreatePrize method actually save to the database
|
|
/// <summary>
|
|
/// Saves a prize to the database
|
|
/// </summary>
|
|
/// <param name="model">The prize information.</param>
|
|
/// <returns>The prize information, including the unique identifier.</returns>
|
|
public Models.PrizeModel CreatePrize(Models.PrizeModel model)
|
|
{
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
var p = new DynamicParameters();
|
|
p.Add("@PlaceNumber", model.PlaceNumber);
|
|
p.Add("@PlaceName", model.PlaceName);
|
|
p.Add("@PrizeAmount", model.PrizeAmount);
|
|
p.Add("@PrizePercentage", model.PrizePercentage);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spPrizes_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
model.Id = p.Get<int>("@Id");
|
|
|
|
return model;
|
|
}
|
|
}
|
|
|
|
public TeamModel CreateTeam(TeamModel model)
|
|
{
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
var p = new DynamicParameters();
|
|
p.Add("@TeamName", model.TeamName);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spTeams_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
model.Id = p.Get<int>("@Id");
|
|
|
|
foreach (PersonModel tm in model.TeamMembers)
|
|
{
|
|
p = new DynamicParameters();
|
|
p.Add("@TeamId", model.Id);
|
|
p.Add("@PersonId", tm.Id);
|
|
|
|
connection.Execute("dbo.spTeamMembers_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
}
|
|
|
|
public void CreateTournament(TournamentModel model)
|
|
{
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
DynamicParameters p = SaveTournament(model, connection);
|
|
|
|
p = SaveTournamentPrizes(model, connection, p);
|
|
|
|
p = SaveTournamentEntries(model, connection, p);
|
|
|
|
}
|
|
}
|
|
|
|
private static DynamicParameters SaveTournament(TournamentModel model, IDbConnection connection)
|
|
{
|
|
var p = new DynamicParameters();
|
|
p.Add("@TournamentName", model.TournamentName);
|
|
p.Add("@EntryFee", model.EntryFee);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spTournaments_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
model.Id = p.Get<int>("@Id");
|
|
return p;
|
|
}
|
|
|
|
private static DynamicParameters SaveTournamentEntries(TournamentModel model, IDbConnection connection, DynamicParameters p)
|
|
{
|
|
foreach (TeamModel tm in model.EnteredTeams)
|
|
{
|
|
p = new DynamicParameters();
|
|
p.Add("@TournamentId", model.Id);
|
|
p.Add("@TeamId", tm.Id);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spTournamentEntries_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
private static DynamicParameters SaveTournamentPrizes(TournamentModel model, IDbConnection connection, DynamicParameters p)
|
|
{
|
|
foreach (PrizeModel pz in model.Prizes)
|
|
{
|
|
p = new DynamicParameters();
|
|
p.Add("@TournamentId", model.Id);
|
|
p.Add("@PrizeId", pz.Id);
|
|
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
|
|
|
|
connection.Execute("dbo.spTournamentPrizes_Insert", p, commandType: CommandType.StoredProcedure);
|
|
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
public List<PersonModel> GetPerson_All()
|
|
{
|
|
|
|
List<PersonModel> output;
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
output = connection.Query<PersonModel>("dbo.spPeople_GetAll").ToList();
|
|
}
|
|
return output;
|
|
}
|
|
|
|
public List<TeamModel> GetTeam_All()
|
|
{
|
|
List<TeamModel> output;
|
|
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
|
|
{
|
|
output = connection.Query<TeamModel>("dbo.spTeam_GetAll").ToList();
|
|
|
|
foreach (TeamModel team in output)
|
|
{
|
|
var p = new DynamicParameters();
|
|
p.Add("@TeamId", team.Id);
|
|
team.TeamMembers = connection.Query<PersonModel>("dbo.spTeamMembers_GetByTeam", p, commandType: CommandType.StoredProcedure).ToList();
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
}
|
|
}
|