public HttpResponseMessage Add(HttpRequestMessage request, TrainerViewModel trainerVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var trainer = new Trainer();

                    trainer.UpdateTrainer(trainerVm);

                    _trainerService.Create(trainer);

                    _trainerService.Save();

                    // Update view model
                    trainerVm = Mapper.Map <Trainer, TrainerViewModel>(trainer);
                    response = request.CreateResponse(HttpStatusCode.Created, trainerVm);
                }

                return response;
            }));
        }
Beispiel #2
0
        public IActionResult Register([FromBody] RegisterModel model)
        {
            // map model to entity
            var trainer = _mapper.Map <Trainer>(model);

            try
            {
                // create trainer
                _trainerService.Create(trainer, model.Password);
                return(Ok());
            }
            catch (Exception ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Beispiel #3
0
        public IActionResult Create([Bind("Id,FirstName,LastName,Description,PayRate,IsActive", Prefix = "Data")] TrainerDto trainer)
        {
            ResultHandler <TrainerDto> resultHandler = new ResultHandler <TrainerDto>();

            if (ModelState.IsValid)
            {
                resultHandler = _trainerService.Create(trainer);
                if (!resultHandler.HasErrors)
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            ResultViewModel <TrainerDto> resultViewModel =
                AutoMapper.Mapper.Map <ResultHandler <TrainerDto>, ResultViewModel <TrainerDto> >(resultHandler);

            return(View(resultViewModel));
        }
 public async Task <IActionResult> Create([FromBody] TrainerDTO trainer, CancellationToken cancellationToken = default)
 {
     return(Ok(await _trainerService.Create(trainer, cancellationToken)));
 }