using Microsoft.AspNetCore.Mvc; using RecordMyRuck.Models; namespace RecordMyRuck.Controllers { public class RuckController : Controller { private const string DateTimeFormat = "u"; private readonly IRuckRepository _ruckRepository; public RuckController(IRuckRepository ruckRepository) { _ruckRepository = ruckRepository; } public IActionResult Index() { return View(); } public IActionResult New() { return View(); } [HttpPost] public IActionResult New(RuckUpload ruckUpload) { if (!ModelState.IsValid) { return View(ruckUpload); } var gpxStream = ruckUpload.Route.OpenReadStream(); var reader = new StreamReader(gpxStream); var gpxData = reader.ReadToEnd(); _ruckRepository.CreateRuck(new Ruck { DateTime = ruckUpload.DateTime, Route = gpxData, WeightPounds = ruckUpload.WeightPounds, Notes = ruckUpload.Notes ?? "", }); return RedirectToAction("Edit", new { dateTimeString = ruckUpload.DateTime.ToString(DateTimeFormat) }); } public IActionResult Edit(string dateTimeString) { DateTime dateTime; try { dateTime = DateTime.ParseExact(dateTimeString, DateTimeFormat, null); } catch { return NotFound(); } var ruck = _ruckRepository.Get(dateTime); if (ruck == null) { return NotFound(); } return View(ruck); } } }