コード例 #1
0
        //public IActionResult Post([FromBody]Trip theTrip)
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                // TODO: Save Trip not TripViewModel to database!
                // LET'S USE AUTOMAPPER INSTEAD!!! (NuGet package defined in project.json)
                //var newTrip = new Trip()
                //{
                //    Name = theTrip.Name,
                //    DateCreated = theTrip.Created
                //};
                var newTrip = Mapper.Map <Trip>(theTrip);
                newTrip.UserName = User.Identity.Name;

                // Save new trip to database
                _repository.AddTrip(newTrip);
                if (await _repository.SaveChangesAsync())
                {
                    //return Created($"api/trips/{theTrip.Name}", newTrip); // <- using newTrip (Trip) instead of theTrip (TripViewModel)
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip))); // <- Returning TripViewModel instead of exposing Trip
                }
            }

            //return Ok(true);
            //return BadRequest("Bad data!");
            return(BadRequest("Failed to save the trip!"));
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                // save to Database
                var newTrip = Mapper.Map <Trip>(theTrip);
                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            return(BadRequest("Failed to save the trip!"));
        }
コード例 #3
0
ファイル: StopsController.cs プロジェクト: TomKarnik/TheWorld
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // If VM is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);

                    // Lookup the GEO codes
                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Longitude = result.Longitude;
                        newStop.Latitude  = result.Latitude;

                        // Save to the database
                        //_repository.AddStop(tripName, newStop);
                        _repository.AddStop(tripName, newStop, User.Identity.Name);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error saving new stop: {ex}");
            }

            return(BadRequest("Error saving new stop!"));
        }
コード例 #4
0
        public async Task <IActionResult> Post(string tripName, [FromBody] StopViewModel vm)
        {
            try
            {
                // if the vm is valid
                if (ModelState.IsValid)
                {
                    var newStop = Mapper.Map <Stop>(vm);
                    // Lookup the geocodes
                    var result = await _coordsService.GetCoordsAsync(newStop.Name);

                    if (!result.Success)
                    {
                        _logger.LogError(result.Message);
                    }
                    else
                    {
                        newStop.Latitude  = result.Latitude;
                        newStop.Longitude = result.Longitude;

                        //save to the database
                        _repository.AddStop(tripName, newStop);

                        if (await _repository.SaveChangesAsync())
                        {
                            return(Created($"/api/trips/{tripName}/stops/{newStop.Name}",
                                           Mapper.Map <StopViewModel>(newStop)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to save new stop {0}", e);
            }

            return(BadRequest("Failed to save new stop"));
        }