Personmodel handled as dbtabel and as file

This commit is contained in:
2020-03-26 20:49:46 +01:00
parent 27f406b836
commit 1a686aba60
8 changed files with 135 additions and 10 deletions

View File

@ -8,5 +8,6 @@ namespace TrackerLibrary.DataAccess
public interface IDataConnection
{
PrizeModel CreatePrize(PrizeModel model);
PersonModel CreatePerson(PersonModel model);
}
}

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using TrackerLibrary.Models;
//@PlaceNumber int,
@ -15,6 +16,24 @@ namespace TrackerLibrary.DataAccess
{
public class SqlConnector : IDataConnection
{
public PersonModel CreatePerson(PersonModel model)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString("Tournaments"))) {
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

View File

@ -10,6 +10,26 @@ namespace TrackerLibrary.DataAccess
public class TextConnector : IDataConnection
{
private const string PrizesFile = "PrizeModels.csv";
private const string PeopleFile = "PersonModels.csv";
public PersonModel CreatePerson(PersonModel model)
{
List<PersonModel> people = PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
int currentId = 1;
if (people.Count > 0)
{
currentId = people.OrderByDescending(x => x.Id).First().Id + 1;
}
model.Id = currentId;
people.Add(model);
people.SaveToPeopleFile(PeopleFile);
return model;
}
// TODO - Wire up the createPrize for textFiles
public PrizeModel CreatePrize(PrizeModel model)

View File

@ -44,6 +44,23 @@ namespace TrackerLibrary.DataAccess.TextHelpers
return output;
}
public static List<PersonModel> ConvertToPersonModels(this List<string> lines)
{
List<PersonModel> output = new List<PersonModel>();
foreach(string line in lines)
{
string[] cols = line.Split(',');
PersonModel p = new PersonModel();
p.Id = int.Parse(cols[0]);
p.FirstName = cols[1];
p.LastName = cols[2];
p.EmailAddress = cols[3];
p.CellPhoneNumber = cols[4];
output.Add(p);
}
return output;
}
public static void SaveToPrizeFile(this List<PrizeModel> models, string fileName)
{
List<string> lines = new List<string>();
@ -54,5 +71,17 @@ namespace TrackerLibrary.DataAccess.TextHelpers
}
File.WriteAllLines(fileName.FullFilePath(), lines);
}
public static void SaveToPeopleFile(this List<PersonModel> models, string fileName)
{
List<string> lines = new List<string>();
foreach(PersonModel p in models)
{
lines.Add($"{p.Id},{p.FirstName},{p.LastName},{p.EmailAddress},{p.CellPhoneNumber}");
}
File.WriteAllLines(fileName.FullFilePath(), lines);
}
}
}