public ActionResult Delete(long runLogId) { if (!HttpContext.HasValidUserAccount(dataCache)) { return(Json(new { Completed = false, Reason = "Please log in / create an account." })); } logger.LogInformation("Deleting run log id {0}", runLogId); var runLogEvent = MassiveDB.Current.FindRunLogEvent(runLogId); if (runLogEvent == null) { return(Json(new { Completed = false, Reason = "Cannot find event - please refresh and try again." })); } if (runLogEvent.LogState == "D") { return(Json(new { Completed = false, Reason = "This event has already been deleted - please refresh and try again." })); } if (runLogEvent.UserAccountId != HttpContext.UserAccount(dataCache).Id) { return(Json(new { Completed = false, Reason = "You are not allowed to delete this event - please refresh and try again." })); } runLogEvent.LogState = "D"; MassiveDB.Current.UpdateRunLogEvent(runLogEvent); var model = new RunLogViewModel(HttpContext, runLogEvent, dataCache); return(Json(model.RunLogModels.Single().RunLogEventToJson())); }
public ActionResult AllEvents(string start, string end) { DateTime.TryParseExact(start, "yyyy-MM-dd", null, DateTimeStyles.AssumeUniversal, out var startDate); DateTime.TryParseExact(end, "yyyy-MM-dd", null, DateTimeStyles.AssumeUniversal, out var endDate); var model = new RunLogViewModel(HttpContext, MassiveDB.Current.FindRunLogEvents(HttpContext.UserAccount(), false, startDate, endDate), dataCache); return(Json(model.RunLogEventsToJson())); }
private Tuple <JsonResult, object> AddRunLogEvent(NewRunData newRunData) { if (!ModelState.IsValid || newRunData.Route.GetValueOrDefault() == 0 || (!newRunData.Distance.HasValue && !newRunData.Route.HasValue)) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please provide a valid route/distance and time." }), (object)null)); } if (!HttpContext.HasValidUserAccount(dataCache)) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please create an account." }), (object)null)); } logger.LogInformation("Creating run event for date {0}, route {1}, distance {2}, time {3}", newRunData.Date, newRunData.Route, newRunData.Distance, newRunData.Time); var userUnits = HttpContext.UserDistanceUnits(dataCache); dynamic route = null; Distance distance = new Distance(newRunData.Distance ?? 0, userUnits); if (newRunData.Route.HasValue) { var routeId = newRunData.Route.Value; if (routeId > 0) { route = MassiveDB.Current.FindRoute(routeId); if (route == null) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Cannot find selected route, please choose another route and try again." }), (object)null)); } distance = new Distance((double)route.Distance, (DistanceUnits)route.DistanceUnits); } else if (routeId == -1) { // manual distance...distance shoud be > 0 if (distance.BaseDistance <= 0) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please enter a distance for your run and try again." }), (object)null)); } } else if (routeId == -2) { // new mapped route if (newRunData.NewRoute == null) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please map a route, add a name and then add a run log event." }), (object)null)); } if (string.IsNullOrWhiteSpace(newRunData.NewRoute.Name)) { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please provide a route name." }), (object)null)); } if (string.IsNullOrWhiteSpace(newRunData.NewRoute.Points) || newRunData.NewRoute.Points == "[]") { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please add some points to the new route by double-clicking the map." }), (object)null)); } route = MassiveDB.Current.CreateRoute(HttpContext.UserAccount(dataCache), newRunData.NewRoute.Name, newRunData.NewRoute.Notes ?? "", distance, (newRunData.NewRoute.Public ?? false) ? Route.PublicRoute : Route.PrivateRoute, newRunData.NewRoute.Points); } else { return(Tuple.Create(new JsonResult(new { Completed = false, Reason = "Please select or create a route for your run and try again." }), (object)null)); } } var runLogEvent = MassiveDB.Current.CreateRunLogEvent(HttpContext.UserAccount(dataCache), newRunData.Date.Value, distance, route, newRunData.NormalizedTime, newRunData.Comment); var model = new RunLogViewModel(HttpContext, runLogEvent, dataCache); return(Tuple.Create(new JsonResult(model.RunLogModels.Single().RunLogEventToJson()), (object)runLogEvent)); }