Example #1
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            #region

            //{
            //"id": 1,
            //"name": "US Trip",
            //"created": "2016-02-14T14:38:51.2032826",
            //"username": "",
            //"stops": null
            //}

            #endregion

            try
            {
                if (ModelState.IsValid)
                {
                    var trip = Mapper.Map <Trip>(vm);

                    trip.UserName = User.Identity.Name;

                    logger.LogInformation("attempting to save new trip");

                    repo.AddTrip(trip);

                    if (repo.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;

                        return(Json(Mapper.Map <TripViewModel>(trip)));
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        return(Json(new { ok = false, ModelState = ModelState, trip = vm }));
                    }
                }
                else
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { ok = false, ModelState = ModelState, trip = vm }));
                }
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to save new trip", ex.Message);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { ok = false }));
            }
        }
Example #2
0
        public async Task <JsonResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // map to entity
                    var newStop = Startup.Mapper.Map <Stop>(vm);

                    // lookup geos
                    var coordResult = await this.coordService.Lookup(newStop.Name);

                    if (!coordResult.Success)
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        return(Json(coordResult));
                    }

                    newStop.Longitude = coordResult.Longitude;
                    newStop.Latitude  = coordResult.Latitude;

                    // save to db
                    var userName = User.Identity.Name;
                    repo.AddStop(tripName, userName, newStop);

                    if (repo.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.OK;
                        return(Json(Startup.Mapper.Map <StopViewModel>(newStop)));
                    }
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to save new stop");
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new Stop"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new Stop"));
        }