using System; using System.IO; using System.Linq; using System.Web.Mvc; using MileageTraker.Web.Attributes; using MileageTraker.Web.DAL; 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 ResultsViewModel(fuelLogs, query, CustomExtensions.YearMonthList(validLogYearMonths)); //Session.Add("FuelLogPage", Request.Url.PathAndQuery); return View(viewModel); } #region Import public ActionResult ImportUpload() { return View(); } [HttpPost] [ActionLog] public ActionResult Import(ImportUploadViewModel 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 fuelLogs = FuelmanCsvImporter.Import(fileInfo); // todo: delete file? 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 (Exception ex) { TempData["StatusMessage"] = "Problem reading document: " + ex.Message; TempData["StatusMessage-Type"] = "alert-error"; } finally { if (fileInfo.Exists) fileInfo.Delete(); } } 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 } }