4dcffda1f8
Import Upload
96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web.Mvc;
|
|
using MileageTraker.Web.DAL;
|
|
using MileageTraker.Web.Models;
|
|
using MileageTraker.Web.Utility;
|
|
using MileageTraker.Web.ViewModels.FuelLog;
|
|
|
|
namespace MileageTraker.Web.Controllers
|
|
{
|
|
[Authorize(Roles = "Administrator, Developer")]
|
|
public class FuelLogController : ControllerBase
|
|
{
|
|
public ViewResult Index(FuelLogQueryViewModel query)
|
|
{
|
|
var validLogYearMonths = DataService.GetValidFuelLogMonths();
|
|
|
|
if (!validLogYearMonths.Any()) // this means no logs in DB
|
|
return View("Empty");
|
|
|
|
// default parameter processing
|
|
if (!query.HasParameters())
|
|
{
|
|
query.Year = validLogYearMonths.First().Year;
|
|
query.Month = validLogYearMonths.First().Month;
|
|
}
|
|
if (query.Year.HasValue && !query.Month.HasValue)
|
|
{
|
|
var validLogMonths = validLogYearMonths.Where(dt => dt.Year == query.Year).ToList();
|
|
query.Month = validLogMonths.Min(dt => dt.Month);
|
|
}
|
|
|
|
var fuelLogs = DataService.GetFuelLogs();
|
|
//fuelLogs.OrderBy(f => f.FuelLogId);
|
|
|
|
//var filteredLogs =
|
|
// (from log in DataService.GetFuelLogIndexViewModels(DataService.FilterLogs(fuelLogs, query))
|
|
// orderby log. descending
|
|
// select log).ToList();
|
|
|
|
var viewModel = new FuelLogResultsViewModel(fuelLogs, query, CustomExtensions.YearMonthList(validLogYearMonths));
|
|
|
|
//Session.Add("FuelLogPage", Request.Url.PathAndQuery);
|
|
|
|
return View(viewModel);
|
|
}
|
|
|
|
#region Import
|
|
|
|
public ActionResult ImportUpload()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult Import(FuelLogImportUploadViewModel viewModel)
|
|
{
|
|
if (ModelState.IsValid && viewModel.File != null && viewModel.File.ContentLength > 0)
|
|
{
|
|
var fileName = Path.GetFileName(viewModel.File.FileName);
|
|
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
|
|
var fileInfo = new FileInfo(path);
|
|
viewModel.File.SaveAs(path);
|
|
|
|
try
|
|
{
|
|
var logImports = FuelmanCsvImporter.Import(fileInfo);
|
|
// todo: add to the database
|
|
DataService.AddFuelLogs(logImports);
|
|
// todo: delete file?
|
|
return View(logImports.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;
|
|
TempData["StatusMessage-Type"] = "alert-error";
|
|
}
|
|
finally
|
|
{
|
|
if (fileInfo.Exists)
|
|
fileInfo.Delete();
|
|
}
|
|
}
|
|
return RedirectToAction("ImportUpload");
|
|
}
|
|
#endregion
|
|
}
|
|
}
|