record-my-ruck/RecordMyRuck/Controllers/RuckController.cs

68 lines
1.8 KiB
C#
Raw Normal View History

2024-05-17 04:30:18 +00:00
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);
}
}
}