示例#1
0
        public async Task <IActionResult> Post([FromBody] TripViewModel tripViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var trip = Mapper.Map <Trip>(tripViewModel);
                    trip.UserName = User.Identity.Name;

                    TravelRepository.AddTrip(trip);

                    if (await TravelRepository.SaveChangesAsync())
                    {
                        // Use map in case database modified the trip in any way.
                        var value = Mapper.Map <TripViewModel>(trip);

                        // todo|jdevl32: contant(s)...
                        return(Created($"/api/trip/{value.Name}", value));
                    }             // if
                }                 // if
                else if (HostingEnvironment.IsDevelopment())
                {
                    return(BadRequest(ModelState));
                }         // if
            }             // try
            catch (Exception ex)
            {
                Logger.LogError($"Error adding trip ({tripViewModel}):  {ex}");
            }             // catch

            return(BadRequest());
        }