Exemplo n.º 1
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = _mapper.Map <Trip>(vm);

                    newTrip.UserName = User.Identity.Name;

                    //save to the database
                    _logger.LogInformation("Attempting to save a new trip");
                    _repository.AddTrip(newTrip);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(_mapper.Map <TripViewModel>(newTrip)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new trip"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new trip"));
        }
Exemplo n.º 2
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);
                    // Authorize on the class means that their is an authenicated user
                    newTrip.UserName = User.Identity.Name;

                    // Save to the Databae
                    _logger.LogInformation("Attempting to save new trip");

                    _repository.AddTrip(newTrip);

                    if (_repository.SaveAll())
                    {
                    }

                    Response.StatusCode = (int)HttpStatusCode.Created;
                    // Now map the model back to the view model
                    return(Json(Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            catch (Exception ex)
            {
                // Log the exception
                _logger.LogError("Failed to save new trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);

                    newTrip.UserName = User.Identity.Name;

                    this.logger.LogInformation("Attempting to save a new trip");
                    this.repository.AddTrip(newTrip);

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

                        return(Json(Mapper.Map <TripViewModel>(newTrip)));
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError("Failed to save new trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;

                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;

            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Exemplo n.º 4
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!"));
        }
Exemplo n.º 5
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);
                    newTrip.UserName = User.Identity.Name;

                    _logger.LogInformation("Attempting to save a new trip...");
                    _repository.AddTrip(newTrip);

                    if (_repository.SaveAll())
                    {
                        _repository.ClearCache(User.Identity.Name);

                        var result = Mapper.Map <TripViewModel>(newTrip);
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(new { Message = "Success", CreatedTrip = result }));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save a new trip.", ex);
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json(new { Message = "Failed", CreatedTrip = new { }, Exception = ex }));
            }

            _logger.LogError("Validation failed on new trip.");
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Validation Failed", CreatedTrip = new { }, ModelState = ModelState }));
        }
Exemplo n.º 6
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);
                    newTrip.UserName = User.Identity.Name;
                    //Save to the database
                    _logger.LogInformation("Attempting to save a new trip");
                    _worldRepository.AddTrip(newTrip);
                    if (_worldRepository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <TripViewModel>(newTrip)));
                    }
                }

                var errorsQuery = from errors in ModelState.Values.SelectMany(e => e.Errors)
                                  select errors.ErrorMessage;
                return(Json(new { Message = "Failed", Errors = errorsQuery }));
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }
        }
Exemplo n.º 7
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            //we willen geen server errors etc, ook geen redirection, het is een api. Dus Try catch

            try
            {
                //voor een api kan ook validation in het model zitten
                //en het is veel interesanter om via een viewmodel te werken natuurlijk
                if (ModelState.IsValid)
                {
                    var newtrip = Mapper.Map <Trip>(vm);

                    newtrip.UserName = User.Identity.Name;

                    //save to the db
                    _logger.LogInformation("Attempting to save a new trip.");
                    _repository.AddTrip(newtrip);
                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <TripViewModel>(newtrip)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Exemplo n.º 8
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);

                    //Save to the Database
                    _logger.LogInformation("Attempting to save a new trip...");

                    Response.StatusCode = (int)HttpStatusCode.Created;
                    return(Json(Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save bew trip", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)       // FromBody says model bind the data that is coming in with the POST to this object
        {
            if (ModelState.IsValid)
            {
                // Save to the Database
                //var newTrip = new Trip()      this is not a good approach, it is hard to copy this over and over, also handle field names are different here.
                //{                             that is why we use the AutoMapper open source project. Automapper is an NuGet Package.
                //    Name = theTrip.Name,
                //    DateCreated = theTrip.Created
                //};

                // better approach with AutoMapper
                var newTrip = Mapper.Map <Trip>(theTrip);  // we want a Trip object, and what we pass is the theTrip.
                                                           //  Maps needs to be created between these two types(i.e., Trip & TripViewModel) from Trip to TripViewModel essentially.
                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    //return Ok(true);  201 Created Status code
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));  // the opposite of what we did above.
                }
            }

            //return BadRequest("Bad Data");
            //return BadRequest(ModelState);  // if you are the only one consuming this API.
            return(BadRequest("Failed to save the trip"));
        }
Exemplo n.º 10
0
        public JsonResult Post([FromBody] TripViewModel tvm) //[FromBody] binds data from body of request...else default assumes Query String  TripViewModel lets us validate/shape data
        {                                                    //
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(tvm);

                    newTrip.UserName = User.Identity.Name;

                    // Save to DB
                    _logger.LogInformation("Attempting to Save New Trip to DB");
                    _repository.AddTrip(newTrip);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <TripViewModel>(newTrip)));
                    }
                }

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = "failed", ModelState = ModelState }));
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to Save new trip", e);

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { ExceptionMsg = e.Message }));
            }
        }
Exemplo n.º 11
0
        public JsonResult Post([FromBody] TripViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newTrip = Mapper.Map <Trip>(vm);

                    //Save to DB
                    _worldRepository.AddTrip(newTrip);

                    if (_worldRepository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <TripViewModel>(newTrip)));
                    }
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Exemplo n.º 12
0
        public IActionResult Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                // Save data to the database
                var newTrip = Mapper.Map <Trip>(theTrip);

                return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
            }
            return(HttpBadRequest(ModelState));
        }
Exemplo n.º 13
0
 public async Task <IActionResult> Post([FromBody] TripViewModel trip)
 {
     if (ModelState.IsValid)
     {
         var newTrip = Mapper.Map <Trip>(trip);
         _repository.AddTrip(newTrip);
         if (await _repository.SaveChangesAsync())
         {
             return(Created($"api/trips/{trip.Name}", Mapper.Map <TripViewModel>(newTrip)));
         }
     }
     return(BadRequest("Failed to save the trip."));
 }
Exemplo n.º 14
0
        public IActionResult Post([FromBody] TripViewModel thetrips)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(thetrips);

                newTrip.UserName = User.Identity.Name;
                _worldRepository.AddTrips(newTrip);


                return(Created($"api/trips:{thetrips.Name}", newTrip));
            }
            return(BadRequest(ModelState));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> Post([FromBody] TripViewModel thetrip)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(thetrip);
                newTrip.UserName = this.User.Identity.Name;
                _repository.AddTrip(newTrip);
                if (await _repository.SaveChanges())
                {
                    return(Created($"api/trips/{thetrip.Name}", thetrip /*Mapper.Map<TripViewModel>(Mapper.Map<Trip>(thetrip))*/));
                }
            }

            return(BadRequest("Couldn't save trip"));
        }
Exemplo n.º 16
0
 public async Task <IActionResult> Post([FromBody] TripViewModel trip)
 {
     if (ModelState.IsValid)
     {
         var newTrip = Mapper.Map <Trip>(trip);
         newTrip.UserName = this.User.Identity.Name;
         _repository.AddTrip(newTrip);
         if (await _repository.SaveChangesAsync())
         {
             return(Created($"api/trips/{trip.Name}", Mapper.Map <TripViewModel>(newTrip)));
         }
     }
     // return BadRequest($"Bad data: {ModelState.Errors()}");
     // return BadRequest(ModelState);
     return(BadRequest("Failed to save the trip"));
 }
Exemplo n.º 17
0
        public async Task <IActionResult> Post([FromBody] TripViewModel trip)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(trip);
                newTrip.UserName = User.Identity.Name;
                Repository.AddTrip(newTrip);

                if (await Repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{trip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }

            return(BadRequest("Failed to save changes to the database"));
        }
Exemplo n.º 18
0
        public async Task <IActionResult> Post([FromBody] TripViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(vm);
                newTrip.UserName = User.Identity.Name;

                _logger.LogInformation("Attempting to save a new trip");
                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangeAsync())
                {
                    return(Created($"api/trips/{newTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            return(BadRequest("Failed to save changes the trip"));
        }
        public async Task <IActionResult> Post([FromBody] TripViewModel newTrip)
        {
            if (ModelState.IsValid)
            {
                var model = Mapper.Map <Trip>(newTrip);

                model.UserName = User.Identity.Name;

                _repository.AddTrip(model);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{newTrip.Name}", Mapper.Map <TripViewModel>(model)));
                }
            }

            return(BadRequest("Failed to save the trip"));
        }
Exemplo n.º 20
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip) //need from body so that it knows where to grab the trip data
        {
            if (ModelState.IsValid)
            {
                //save to database
                var newTrip = Mapper.Map <Trip>(theTrip);

                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }

            return(BadRequest("Failefd to Save the Trip"));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(theTrip);
                _repo.AddTrip(newTrip);

                if (await _repo.SaveChangesAsync())
                {
                    return(Created($"api/trip/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
                else
                {
                    return(BadRequest("Unable to save trip"));
                }
            }

            return(BadRequest("Bad data."));
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                // Save to the datatbase
                var newTrip = Mapper.Map <Trip>(theTrip);

                // Set new trip username to the current user
                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            return(BadRequest("Failed to save the trip."));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> Post([FromBody] TripViewModel tripVm)
        {
            // In order to check for ModelState, we need to have validation attributes in VM class
            if (ModelState.IsValid)
            {
                //Map from source object VMTrip to the existing entity Trip.
                //TRICK: first the MAP should be created saying from what to what => see Startup/Configure()
                Trip newTrip = Mapper.Map <Trip>(tripVm);

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    //Should return a TripVM not to expose entity class Trip
                    return(Created($"api/trips/{tripVm.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }

            return(BadRequest("Failed to save Trip to the DB"));
        }
Exemplo n.º 24
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            //SAVE TO THE DATABASE
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(theTrip);

                //definindo o usuario logado como usuario cadastrado na trip
                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                //if savechanges async is ok, call Created...
                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }
            return(BadRequest("Failed to save the trip"));
        }
Exemplo n.º 25
0
        public async Task <IActionResult> Post([FromBody] TripViewModel TheTrip)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Bad Data"));
            }

            var newTrip = Mapper.Map <Trip> (TheTrip);

            _repository.AddTrip(newTrip);

            if (await _repository.SaveChangesAsync())
            {
                return(Created($"api/trips/{TheTrip.Name}", Mapper.Map <TripViewModel> (newTrip)));
            }
            else
            {
                return(BadRequest("Failed to save changes to database."));
            }
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)  // wrap the action in async
        {
            if (ModelState.IsValid)
            {
                // Save to Database
                var newTrip = Mapper.Map <Trip>(theTrip);

                // Makes assumption that user is authenticated to get this far so we can use the username to post to DB
                newTrip.UserName = User.Identity.Name;

                // need to set in startup.cs because it assumes they are already mapped (but not)
                _repository.AddTrip(newTrip); // AddTrip needs to be built in the repository

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/ {theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
            }

            return(BadRequest("Failed to save changes to the database"));
        }
Exemplo n.º 27
0
        public async Task <IActionResult> Post([FromBody] TripViewModel trip)
        {
            if (ModelState.IsValid)
            {
                var newTrip = Mapper.Map <Trip>(trip);
                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{trip.Name}", newTrip));
                }
                else
                {
                    return(BadRequest("Fail saving."));
                }
            }

            return(BadRequest(ModelState));
        }
Exemplo n.º 28
0
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip) //! from body means: Please modelbind (map using the model) the data thats coming from the POST, and not from another URL(without this won't map it at all)
        {
            //modelbind,map it to this object by trying to match the json properties from the API!
            if (ModelState.IsValid)
            {
                // Save to the Database
                var newTrip = Mapper.Map <Trip>(theTrip); //this maps the trip model into the "thetrip" object of type TripViewModel
                                                          //we need to configure this logic into startup

                newTrip.UserName = User.Identity.Name;    //we assign the new created trip to the logged in user

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));  //this returns and creates an object with an specified URI if the data is valid
                }
            }

            return(BadRequest("Failed to save the trip")); //valud way to communicate the modelstate and any errors that are going down
        }
        public async Task <IActionResult> Post([FromBody] TripViewModel theTrip)
        {
            if (ModelState.IsValid)
            {
                // Save to the Database
                var newTrip = Mapper.Map <Trip>(theTrip);

                newTrip.UserName = User.Identity.Name;

                _repository.AddTrip(newTrip);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"api/trips/{theTrip.Name}", Mapper.Map <TripViewModel>(newTrip)));
                }
                else
                {
                    return(BadRequest("Error al guardar los cambios en la base de datos"));
                }
            }

            return(BadRequest("Error al guardar el viaje"));
        }
Exemplo n.º 30
0
        public async Task <IActionResult> PostAsync([FromBody] TripViewModel tripVM)
        {
            //return Ok();

            // Salvar na base de dados

            var trip = Mapper.Map <Trip>(tripVM);

            _repository.AddTrip(trip);

            if (await _repository.SaveChangesAsync())
            {
                return(Created($"api/trips/{tripVM.Name}", Mapper.Map <TripViewModel>(trip)));
            }

            //if (ModelState.IsValid)
            //{
            //  //return Created($"api/trips/{tripVM.Name}", trip);

            //}

            return(BadRequest("Failed Save"));
        }