Beispiel #1
0
        public async Task <ActionResult <CampsModel> > CreateCamp(CampsModel model)
        {
            try
            {
                var campExists = _campRepository.GetCampAsync(model.Moniker);
                if (campExists != null)
                {
                    return(BadRequest("Moniker already in use!"));
                }
                var location = _linkGenerator.GetPathByAction($"GetCamp", $"Camps", new { moniker = model.Moniker });

                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest("Could not use that moniker"));
                }
                var camp = _mapper.Map <Camp>(model);
                _campRepository.Add(camp);

                if (await _campRepository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <CampsModel>(camp)));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Something wrong on our end."));
            }

            return(BadRequest("wrong input"));
        }
Beispiel #2
0
        public async Task <IActionResult> Post([FromBody] CampsModel model)
        {
            var camp = _mapper.Map <Camp>(model);

            _reposetory.Add(camp);
            if (await _reposetory.SaveAllAsync())
            {
                var newUri = Url.Link("newCamp", new { id = camp.Id });
                return(Created(newUri, _mapper.Map <CampsModel>(camp)));
            }
            return(BadRequest($"This is bad BadRequest"));
        }
 public async Task <ActionResult <CampsModel> > Post(CampsModel campsModel)
 {
     try
     {
         var location = linkGenerator.GetPathByAction("Get", "Camps", new { moniker = campsModel.Moniker });
         if (string.IsNullOrEmpty(location))
         {
             return(BadRequest());
         }
         var camp = mapper.Map <Camp>(campsModel);
         campRepository.Add(camp);
         if (await campRepository.SaveChangesAsync())
         {
             return(Created(location, camp));
         }
     }
     catch (Exception)
     {
         return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database issue"));
     }
     return(BadRequest());
 }
Beispiel #4
0
        public async Task <ActionResult <CampsModel> > UpdateCampModel(string moniker, CampsModel model)
        {
            try
            {
                var camp = await _campRepository.GetCampAsync(moniker);

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

                _mapper.Map(model, destination: camp);

                if (await _campRepository.SaveChangesAsync())
                {
                    return(_mapper.Map <CampsModel>(camp));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Something wrong on our end."));
            }

            return(BadRequest());
        }