Example #1
0
        public async Task <IActionResult> Post([FromBody] CampModel model)
        {
            try
            {
                //if (!ModelState.IsValid)
                //{
                //    return BadRequest(ModelState);
                //}
                _logger.LogInformation("Creating a new Camp");
                var camp = _mapper.Map <Camp>(model);

                _repo.Add(camp);
                if (await _repo.SaveAllAsync())
                {
                    var newUri = Url.Link("CampGet", new { moniker = model.Moniker });
                    return(Created(newUri, _mapper.Map <CampModel> (camp)));
                }
                else
                {
                    _logger.LogWarning("Could not save Camp to tha DataBase");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw Exception While Saving Camp : {ex}");
            }
            return(BadRequest());
        }
Example #2
0
        public async Task <ActionResult <CampModel> > Delete(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await _repository.GetCampAsync(model.Moniker);

                if (oldCamp == null)
                {
                    return(NotFound($"Could not found moniker of {moniker}."));
                }

                _repository.Delete(oldCamp);

                if (await _repository.SaveChangesAsync())
                {
                    return(Ok());
                }
            }
            catch
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }
Example #3
0
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var existing = await _campRepository.GetCampAsync(model.Moniker);

                if (existing != null)
                {
                    return(BadRequest("Moniker in use"));
                }

                var location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could use no moniker"));
                }

                var camp = _mapper.Map <Camp>(model);
                _campRepository.Add(camp);

                if (await _campRepository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <CampModel>(camp)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database errror"));
            }

            return(BadRequest());
        }
Example #4
0
        public async Task <IHttpActionResult> Put(string moniker, CampModel campModel)
        {
            try
            {
                var camp = await _iCampRepository.GetCampAsync(moniker);

                if (camp == null)
                {
                    return(NotFound());
                }
                Mapper.Map(campModel, camp);
                if (await _iCampRepository.SaveChangesAsync())
                {
                    return(Ok <CampModel>(Mapper.Map <CampModel>(camp)));
                }
                else
                {
                    return(InternalServerError());
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public async Task <IHttpActionResult> Put(string moniker, CampModel model)
        {
            try
            {
                var camp = await _repository.GetCampAsync(moniker);

                if (camp == null)
                {
                    return(NotFound());
                }
                //We have to store the Camp obj(not CampModel)
                _mapper.Map(model, camp);

                if (await _repository.SaveChangesAsync())
                {
                    return(Ok(_mapper.Map <CampModel>(camp)));
                }
                else
                {
                    //Use internal server error instead of the not found because if there is an error during saving the results it is not really the user's fault
                    return(InternalServerError());
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var camp = await campRepository.GetCampAsync(moniker);

                if (camp == null)
                {
                    return(NotFound($"Couldnt find camp with moniker of {moniker}"));
                }

                mapper.Map(model, camp);

                if (await campRepository.SaveChangesAsync())
                {
                    return(mapper.Map <CampModel>(camp));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }

            return(BadRequest());
        }
Example #7
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                // Old Camp object
                var oldCamp = await _repository.GetCampAsync(moniker);

                // If not found...return error 404
                if (oldCamp == null)
                {
                    return(NotFound($"Could not find camp with moniker of {moniker}"));
                }

                // oldCamp.Name = model.Name; // Not interesting

                // Using mapper, update
                _mapper.Map(model, oldCamp); // Taking the data from the model and applying to oldCamp

                if (await _repository.SaveChangesAsync())
                {
                    // Ok, map back to the Camp model
                    return(_mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception e)
            {
                // Database failure
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure, \n {e.Message}"));
            }

            return(BadRequest("PUT request failed!"));
        }
        public async Task <ActionResult <CampModel> > Post(CampModel campModel)
        {
            var x = await campRepository.GetCampAsync(campModel.Moniker);

            if (x != null)
            {
                campRepository.Delete(x);
                await campRepository.SaveChangesAsync();

                //return BadRequest("Monikor is already in use for another camp.");
            }

            try
            {
                var camp = mapper.Map <Camp>(campModel);
                campRepository.Add(camp);
                if (await campRepository.SaveChangesAsync())
                {
                    var location = linkGenerator.GetPathByAction("Get", "Camps",
                                                                 new { moniker = campModel.Moniker });
                    var campInDb = mapper.Map <CampModel>(camp);
                    return(Created(location, campInDb));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #9
0
        public async Task <ActionResult <CampModel> > Post([FromBody] CampModel model)    //[FromBody] es para indicar que viene data y tiene que bindearla a esa entrada
        {
            try
            {
                var existing = await _repository.GetCampAsync(model.Moniker);   // quiero que el Moniker sea unico

                if (existing != null)
                {
                    return(BadRequest("Moniker in use"));
                }

                var camp = _mapper.Map <Camp>(model);   // Hago al reves, creo un campo desde un modelo
                _repository.Add(camp);                  // Y lo meto al repositorio
                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"/api/camps/{camp.Moniker}", _mapper.Map <CampModel>(camp)));   // Created es un status code 20X, pero no OK, es el que se manda cuando se agrega algo a la API
                    // Deberia usar la clase LinkGenerator en vez de hardcodear la direccion, pero no la podia importar
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }
Example #10
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await this.repository.GetCampAsync(model.Moniker);

                if (oldCamp == null)
                {
                    return(NotFound($"Could not find camp with moniker of {moniker}"));
                }

                this.mapper.Map(model, oldCamp);

                if (await this.repository.SaveChangesAsync())
                {
                    return(this.mapper.Map <CampModel>(oldCamp));
                }
                return(NotFound("Something failed."));
            }
            catch (Exception e)
            {
                this.logger.LogError($"{e}");
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
Example #11
0
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var existing = await this.repository.GetCampAsync(model.Moniker);

                if (existing != null)
                {
                    return(BadRequest("Moniker in Use"));
                }
                var location = this.linkGenerator.GetPathByAction("Get", "Camps",
                                                                  new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use the current moniker"));
                }

                var camp = this.mapper.Map <Camp>(model);
                this.repository.Add(camp);
                if (await this.repository.SaveChangesAsync())
                {
                    return(Created($"/api/camps/{camp.Moniker}", this.mapper.Map <CampModel>(camp)));
                }
                return(Ok());
            }
            catch (Exception e)
            {
                this.logger.LogError($"{e}");
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
        }
Example #12
0
        public async Task <IActionResult> Put(string moniker, [FromBody] CampModel model)
        {
            try
            {
                _logger.LogInformation("Updating an existing Code Camp");

                var oldCamp = _repo.GetCampByMoniker(moniker);
                if (oldCamp == null)
                {
                    return(NotFound($"Could not find a camp with moniker of {moniker}"));
                }

                _mapper.Map(model, oldCamp);

                if (await _repo.SaveAllAsync())
                {
                    return(Ok(_mapper.Map <CampModel>(oldCamp)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while updating Camp: {ex}");
            }

            return(BadRequest("Couldn't update Camp"));
        }
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await _repository.GetCampAsync(moniker);

                if (oldCamp == null)
                {
                    return(NotFound("Could not find the specific Camp"));
                }

                _mapper.Map(model, oldCamp);

                if (await _repository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception ex)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ex));
            }

            return(BadRequest());
        }
Example #14
0
        public async Task <IActionResult> Put(string moniker, [FromBody] CampModel model)
        {
            try
            {
                //if (!ModelState.IsValid)
                //{
                //    return BadRequest(ModelState);
                //}
                var oldCamp = _repo.GetCampByMoniker(moniker);
                if (oldCamp == null)
                {
                    return(NotFound($" colud not find a camp with ID of :{moniker}"));
                }

                _mapper.Map(model, oldCamp);

                //oldCamp.Name = model.Name ?? oldCamp.Name;
                //oldCamp.Description = model.Description ?? oldCamp.Description;
                //oldCamp.Location = model.Location ?? oldCamp.Location;
                //oldCamp.Length = model.Length > 0 ? model.Length : oldCamp.Length;
                //oldCamp.EventDate = model.EventDate != DateTime.MinValue ? model.EventDate: oldCamp.EventDate;

                if (await _repo.SaveAllAsync())
                {
                    return(Ok(_mapper.Map <CampModel>(oldCamp)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw Exception While Updating Camp : {ex}");
            }
            return(BadRequest("Couldn't update Camp"));
        }
Example #15
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await _campRepository.GetCampAsync(moniker);

                if (oldCamp == null)
                {
                    return(NotFound("Moniker doesn't exist."));
                }
                if (oldCamp.Moniker != model.Moniker)
                {
                    return(BadRequest("Moniker doesn't match"));
                }
                _mapper.Map(model, oldCamp);
                if (await _campRepository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
            return(BadRequest("Bad Request"));
        }
Example #16
0
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var existing = await _repository.GetCampAsync(model.Moniker);

                if (existing != null)
                {
                    return(BadRequest("Moniker in use"));
                }

                //Se usa para agregar un header con el location de donde fue creado el objeto.
                var location = _linkGenerator.GetPathByAction("Get", "Camps", new  { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current Moniker"));
                }
                // Create a new camp
                var camp = _mapper.Map <Camp>(model);
                _repository.Add(camp);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"/api/camps/{camp.Moniker}", _mapper.Map <CampModel>(camp)));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
            return(BadRequest());
        }
Example #17
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "this Moniker is already created");
                }
                if (ModelState.IsValid)
                {
                    var camp = _mapper.Map <Camp>(model);
                    _repository.AddCamp(camp);

                    if (await _repository.SaveChangesAsync())
                    {
                        var newModel = _mapper.Map <CampModel>(model);
                        //location is the url which is telling where new model is created
                        return(CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker }, newModel));
                    }
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
            return(BadRequest(ModelState));
        }
        public async Task <ActionResult <CampModel> > Update(string moniker, [FromBody] CampModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var oldCamp = await campRepository.GetCampAsync(model.Moniker);

                if (oldCamp is null)
                {
                    return(NotFound("Camp does not exists."));
                }

                mapper.Map(model, oldCamp);

                if (await campRepository.SaveChangesAsync())
                {
                    return(mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest($"Could not update camp {model.Name}"));
        }
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var camp = await campRepository.GetCampAsync(model.Moniker);

                if (camp != null)
                {
                    return(BadRequest("Moniker in Use!"));
                }

                var location = linkGenerator.GetPathByAction("Get", "Camps",
                                                             new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current moniker!"));
                }

                var newCamp = mapper.Map <Camp>(model);
                campRepository.Add(newCamp);

                if (await campRepository.SaveChangesAsync())
                {
                    return(Created(location, mapper.Map <CampModel>(newCamp)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }

            return(BadRequest());
        }
Example #20
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await _repository.GetCampAsync(model.Moniker);

                if (oldCamp == null)
                {
                    return(NotFound("No se puede actualizar porque el camp solicitado no existe"));
                }

                _mapper.Map(model, oldCamp);

                if (await _repository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database is down"));
            }

            return(BadRequest());
        }
Example #21
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    //return BadRequest("Moniker is in use");
                    ModelState.AddModelError("Moniker", "Moniker is in use");
                }

                if (ModelState.IsValid)
                {
                    var camp = _mapper.Map <Camp>(model);
                    _repository.AddCamp(camp);
                    if (await _repository.SaveChangesAsync())
                    {
                        var newModel = _mapper.Map <CampModel>(camp);
                        //var location = $"/api/camps/{newModel.Moniker}";
                        // or
                        //var location = Url.Link("GetCamp", new { moniker = newModel.Moniker });
                        //return Created(location, newModel);
                        // or
                        return(CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker }, newModel));
                    }
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            return(BadRequest(ModelState));
        }
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var check = await _repository.GetCampAsync(model.Moniker);

                if (check != null)
                {
                    return(BadRequest("Moniker in use"));
                }
                var location = _link.GetPathByAction("Get", "Camps", new { moniker = model.Moniker });
                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current moniker"));
                }
                var camp = _mapper.Map <Camp>(model);
                _repository.Add(camp);
                ;
                if (await _repository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <CampModel>(camp)));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
            return(BadRequest());
        }
Example #23
0
 public async Task <IHttpActionResult> Post(CampModel campModel)
 {
     if (await _iCampRepository.GetCampAsync(campModel.Moniker) != null)
     {
         ModelState.AddModelError("Moniker", "Moniker in use");
     }
     if (ModelState.IsValid)
     {
         try
         {
             var camp = Mapper.Map <Camp>(campModel);
             _iCampRepository.Add(camp);
             if (await _iCampRepository.SaveChangesAsync())
             {
                 var campM    = Mapper.Map <CampModel>(camp);
                 var location = Url.Link("GetCamp", new { moniker = campM.Moniker });
                 return(Created(location, campM));
             }
         }
         catch (Exception ex)
         {
             return(InternalServerError(ex));
         }
     }
     return(BadRequest(ModelState));
 }
Example #24
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var oldCamp = await _repository.GetCampAsync(moniker);

                if (oldCamp == null)
                {
                    return(NotFound($"Couldn't find the camp with a moniker of {moniker}"));
                }

                _mapper.Map(model, oldCamp);

                if (await _repository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampModel>(oldCamp));
                }
            }
            catch (Exception)
            {
                return(ReturnStatus500InternalServerError());
            }

            return(BadRequest());
        }
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                //Check if the moniker is already defined
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker in use");
                }

                if (ModelState.IsValid)
                {
                    var camp = _mapper.Map <Camp>(model);

                    _repository.AddCamp(camp);

                    if (await _repository.SaveChangesAsync())
                    {
                        var newModel = _mapper.Map <CampModel>(camp);

                        return(CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker }, newModel));
                    }
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }

            return(BadRequest(ModelState));
        }
Example #26
0
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var existing = await _repository.GetCampAsync(model.Moniker);

                if (existing != null)
                {
                    return(BadRequest("Moniker entered is already in use"));
                }


                var location = _linkGenerator.GetPathByAction("GetCamp", "Camps",
                                                              new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Cannot use the current moniker"));
                }

                //create a new camp
                var camp = _mapper.Map <Camp>(model);
                _repository.Add(camp);
                if (await _repository.SaveChangesAsync())
                {
                    return(Created("", _mapper.Map <CampModel>(camp)));
                }
            }
            catch (Exception)
            {
                return(ReturnStatus500InternalServerError());
            }

            return(BadRequest());
        }
Example #27
0
        public async Task <ActionResult <CampModel> > Put(string moniker, CampModel model)
        {
            try
            {
                var olsCamp = await _campRepository.GetCampAsync(moniker);

                if (olsCamp == null)
                {
                    return(NotFound($"Could not find {moniker}"));
                }

                //////////////
                _mapper.Map(model, olsCamp);

                if (await _campRepository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampModel>(olsCamp));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database errror"));
            }

            return(BadRequest());
        }
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _campsRepository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker In Use");
                }
                if (ModelState.IsValid)
                {
                    //Mapping
                    var mapModel = _mapper.Map <Camp>(model);
                    _campsRepository.AddCamp(mapModel);
                    if (await _campsRepository.SaveChangesAsync())
                    {
                        var newModel = _mapper.Map <CampModel>(mapModel);

                        return(CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker }, newModel));
                    }
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> Post([FromBody] CampModel model)
        {
            try
            {
                _logger.LogInformation("Creating a new Code Camp");
                var camp = _mapper.Map <Camp>(model);
                _repo.Add(camp);

                if (await _repo.SaveAllAsync())
                {
                    var newURI = Url.Link("CampGet", new { moniker = model.Moniker });
                    return(Created(newURI, _mapper.Map <CampModel>(camp)));
                }
                else
                {
                    _logger.LogWarning("Could not save Camp to the database");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while saving Camp: {ex}");
            }

            return(BadRequest());
        }
Example #30
0
        public async Task <ActionResult <CampModel> > Post(CampModel model)
        {
            try
            {
                var existingCamp = await _repository.GetCampAsync(model.Moniker);

                if (existingCamp != null)
                {
                    return(BadRequest("Moniker already in use"));
                }

                string location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use current moniker"));
                }

                Camp camp = _mapper.Map <Camp>(model);

                _repository.Add(camp);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <CampModel>(camp)));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"Database failure {ex}"));
            }

            return(BadRequest());
        }