Fuel Log Import basic functionality

This commit is contained in:
2015-09-18 13:02:15 -04:00
parent 4987215f0b
commit e3590b29e5
18 changed files with 438 additions and 59 deletions
+69 -13
View File
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels.FuelLog;
@@ -40,7 +39,7 @@ namespace MileageTraker.Web.Controllers
// orderby log. descending
// select log).ToList();
var viewModel = new FuelLogResultsViewModel(fuelLogs, query, CustomExtensions.YearMonthList(validLogYearMonths));
var viewModel = new ResultsViewModel(fuelLogs, query, CustomExtensions.YearMonthList(validLogYearMonths));
//Session.Add("FuelLogPage", Request.Url.PathAndQuery);
@@ -55,7 +54,8 @@ namespace MileageTraker.Web.Controllers
}
[HttpPost]
public ActionResult Import(FuelLogImportUploadViewModel viewModel)
[ActionLog]
public ActionResult Import(ImportUploadViewModel viewModel)
{
if (ModelState.IsValid && viewModel.File != null && viewModel.File.ContentLength > 0)
{
@@ -66,17 +66,23 @@ namespace MileageTraker.Web.Controllers
try
{
var logImports = FuelmanCsvImporter.Import(fileInfo);
// todo: add to the database
DataService.AddFuelLogs(logImports);
var fuelLogs = FuelmanCsvImporter.Import(fileInfo);
// todo: delete file?
return View(logImports.ToList());
var fls = (from fuelLog in fuelLogs
let prevAdds = DataService.GetDuplicateFuelLogs(fuelLog)
select new {ImportedFuelLog = fuelLog, PrevAdded = prevAdds.FirstOrDefault()}).ToList();
// add new logs to the database
DataService.AddFuelLogs(fls.Where(fl => fl.PrevAdded == null).Select(fl => fl.ImportedFuelLog));
var vms = from fl in fls
let vm = fl.PrevAdded == null
? new ImportFuelLogViewModel(fl.ImportedFuelLog)
: new ImportFuelLogViewModel(fl.PrevAdded) {PreviouslyAdded = true}
select vm;
return View(vms.ToList());
}
//catch (OutOfMemoryException)
//{
// TempData["StatusMessage"] = "Problem reading excel document - please verify that the document is in Excel 97-2003 Workbook (.xls) format";
// TempData["StatusMessage-Type"] = "alert-error";
//}
catch (Exception ex)
{
TempData["StatusMessage"] = "Problem reading document: " + ex.Message;
@@ -90,6 +96,56 @@ namespace MileageTraker.Web.Controllers
}
return RedirectToAction("ImportUpload");
}
enum MatchStatus
{
Match,
NoMatch,
Error
}
[HttpPost]
public ActionResult Match(int fuelLogId)
{
var fuelLog = DataService.GetFuelLog(fuelLogId);
if (fuelLog == null)
{
return Json(new
{
Status = MatchStatus.Error.ToString(),
Message = "Fuel Log not found with given ID: " + fuelLogId
}, JsonRequestBehavior.AllowGet);
}
if (fuelLog.Log != null)
{
return Json(new
{
Status = MatchStatus.NoMatch.ToString(),
Message = "Already matched to log",
Action = RenderRazorViewToString("ImportMatchLogView", fuelLog.Log.LogId)
}, JsonRequestBehavior.AllowGet);
}
// run the match
var matchingLog = DataService.GetMatchingLog(fuelLog);
if (matchingLog == null)
{
return Json(new
{
Status = MatchStatus.NoMatch.ToString(),
Message = "Unable to find match"
}, JsonRequestBehavior.AllowGet);
}
fuelLog.Log = matchingLog;
DataService.UpdateFuelLog(fuelLog);
return Json(new
{
Status = MatchStatus.Match.ToString(),
Action = RenderRazorViewToString("ImportMatchedLog", new ImportMatchedLogViewModel { LogId = fuelLog.Log.LogId })
}, JsonRequestBehavior.AllowGet);
}
#endregion
}
}
+1 -2
View File
@@ -259,10 +259,9 @@ namespace MileageTraker.Web.Controllers
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
viewModel.File.SaveAs(path);
IEnumerable<ImportLogViewModel> logImports;
try
{
logImports = LogImporter.Import(path);
var logImports = LogImporter.Import(path);
// todo: delete file?
return View(logImports.ToList());
}