Handle the text file part of saving
This commit is contained in:
@ -2,15 +2,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using TrackerLibrary.Models;
|
||||
using TrackerLibrary.DataAccess.TextHelpers;
|
||||
using System.Linq;
|
||||
|
||||
namespace TrackerLibrary.DataAccess
|
||||
{
|
||||
public class TextConnector : IDataConnection
|
||||
{
|
||||
private const string PrizesFile = "PrizeModels.csv";
|
||||
|
||||
// TODO - Wire up the createPrize for textFiles
|
||||
public PrizeModel CreatePrize(PrizeModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
// Load the text file
|
||||
// Convert the text to a List>PrizeModel>
|
||||
List<PrizeModel> prizes = PrizesFile.FullFilePath().LoadFile().ConvertToPrizeModels();
|
||||
|
||||
// Find the max Id
|
||||
|
||||
int currentId = 1;
|
||||
|
||||
if (prizes.Count > 0)
|
||||
{
|
||||
currentId = prizes.OrderByDescending(x => x.Id).First().Id + 1;
|
||||
}
|
||||
|
||||
model.Id = currentId;
|
||||
|
||||
// Add the new record with the new ID (max +1)
|
||||
prizes.Add(model);
|
||||
|
||||
// Convert the prizes to a List<string>
|
||||
// Save the list<String> to the text file
|
||||
prizes.SaveToPrizeFile(PrizesFile);
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user