Пример #1
0
        public async Task <IActionResult> Put(string moniker, [FromBody] CampViewModel viewModel)
        {
            try {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _logger.LogInformation($"Put code camp with moniker='{moniker}'");

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

                _mapper.Map(viewModel, camp);

                if (await _campRepository.SaveAllAsync())
                {
                    return(Ok(_mapper.Map <CampViewModel>(camp)));
                }
                else
                {
                    _logger.LogWarning("Could not save camp to the database");
                }
            } catch (Exception ex) {
                _logger.LogCritical($"Threw exception while saving camp: {ex}");
            }

            return(BadRequest("Could not save camp"));
        }
Пример #2
0
        public async Task <IActionResult> Post([FromBody] CampViewModel viewModel)
        {
            try {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _logger.LogInformation("Create a new code camp");

                var camp = _mapper.Map <Camp>(viewModel);
                _campRepository.Add(camp);
                if (await _campRepository.SaveAllAsync())
                {
                    var uri = Url.Link("GetCamp", new { moniker = camp.Moniker });
                    return(Created(uri, _mapper.Map <CampViewModel>(camp)));
                }
                else
                {
                    _logger.LogWarning("Could not save camp to the database");
                }
            } catch (Exception ex) {
                _logger.LogCritical($"Threw exception while saving camp: {ex}");
            }

            return(BadRequest("Could not save camp"));
        }
Пример #3
0
        public async Task <CampViewModel> GetCamp(string moniker)
        {
            var entity = await _repository.GetCamp(moniker);

            CampViewModel campViewModel = _mapper.Map <CampViewModel>(entity);

            return(campViewModel);
        }
Пример #4
0
        public async Task <IHttpActionResult> Put(string moniker, CampViewModel campViewModel)
        {
            var camp = await _repository.GetCampAsync(moniker);

            if (camp == null)
            {
                return(NotFound());
            }

            _mapper.Map(campViewModel, camp);

            await _repository.SaveChangesAsync();

            return(Ok());
        }
Пример #5
0
        public async Task <IHttpActionResult> Post(CampViewModel campViewModel)
        {
            if (await _repository.ExistsCampAsync(campViewModel.Moniker))
            {
                ModelState.AddModelError("campViewModel.Moniker", "The moniker is already is use, please choose a new one.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var camp = _mapper.Map <Camp>(campViewModel);

            await _repository.AddCampAsync(camp);

            await _repository.SaveChangesAsync();

            var createdCampViewModel = _mapper.Map <CampViewModel>(camp);

            return(CreatedAtRoute("GetCamp", new { moniker = createdCampViewModel.Moniker }, createdCampViewModel));
        }