コード例 #1
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                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 ex)
            {
                return(InternalServerError(ex));
            }

            return(BadRequest(ModelState));
        }
コード例 #2
0
        public async Task <IHttpActionResult> Post(CampReqDto reqDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var camp = _mapper.Map <Camp>(reqDto);
                _campRepository.AddCamp(camp);

                if (!await _campRepository.SaveChangesAsync())
                {
                    return(InternalServerError());
                }

                var newCamp = _mapper.Map <CampReqDto>(camp);
                return(CreatedAtRoute("GetCamp", new { moniker = newCamp.Moniker }, newCamp));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
コード例 #3
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    //return BadRequest("Moniker in use");
                    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);
                        //var location = Url.Link("GetCamp", new { moniker = newModel.Moniker });
                        //return Created(location, newModel);
                        // alternate shortcuts above 2 lines
                        return(CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker }, newModel));
                        //return CreatedAtRoute("GetCamp", new { moniker = newModel.Moniker });
                    }
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }

            return(BadRequest(ModelState));
        }
コード例 #4
0
        public async Task <IHttpActionResult> CreateCamp(CampDto campDto)
        {
            try
            {
                if (await _repository.GetCampAsync(campDto.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker is already in use / not unique!");
                }

                if (ModelState.IsValid)                                             /* validate the model, according to the data annotation (e.g. [Required]) defined in the CampDto class. */
                {
                    Camp camp = _mapper.Map <Camp>(campDto);

                    _repository.AddCamp(camp);

                    if (await _repository.SaveChangesAsync())
                    {
                        var newCamp = _mapper.Map <CampDto>(camp);                   // 'camp' here, after saving, includes an id generated from the DB...

                        //return Created(new Uri(Request.RequestUri + "/" + newCamp.Moniker), newCamp);
                        return(CreatedAtRoute("GetCamp", new { moniker = newCamp.Moniker }, newCamp));
                    }
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            return(BadRequest(ModelState));
        }
コード例 #5
0
ファイル: CampsController.cs プロジェクト: Pratik212/Demo
        public async Task <ActionResult> AddCamp([FromBody] Camp camp)
        {
            try
            {
                var result = await _campRepository.AddCamp(camp);

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

                return(Ok(new
                {
                    result.Moniker,
                    result.Name,
                    result.Description,
                    result.Length,
                    result.EventDate,
                    result.RowVersion
                }));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error in Retrieving Data from  Database"));
            }
        }
コード例 #6
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (null != await cr.GetCampAsync(model.Moniker))
                    {
                        ModelState.AddModelError(model.Moniker, "Already Exists");
                        return(BadRequest(ModelState));
                    }
                    var camp = mp.Map <Camp>(model);

                    cr.AddCamp(camp);
                    if (await cr.SaveChangesAsync())
                    {
                        var campModel = mp.Map <CampModel>(camp);
                        return(CreatedAtRoute("GetCamp", new { moniker = campModel.Moniker }, campModel));
                    }
                }
            }
            catch (Exception e) {
                return(InternalServerError(e));
            }
            return(BadRequest(ModelState));
        }
コード例 #7
0
        public async Task <ActionResult> Post(CampModel model)
        {
            try
            {
                if (await _campRepository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker in use");
                }

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

                    _campRepository.AddCamp(mapped);

                    if (await _campRepository.SaveChangesAsync())
                    {
                        var mappedCamp = _mapper.Map <CampModel>(mapped);

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

                        //return Created(Url.Link("GetCamp", new { moniker = mappedCamp.Moniker }), mappedCamp);
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }

            return(BadRequest(ModelState));
        }
コード例 #8
0
        public async Task <IHttpActionResult> CreateCamp(CampModel campModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var camp = _mapper.Map <Camp>(campModel);
                    _repository.AddCamp(camp);

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

                        var location = Url.Link("GetSingleCamp", new { moniker = newCampModel.Moniker });

                        return(Created(location, newCampModel));
                    }
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            return(BadRequest(ModelState));
        }
コード例 #9
0
        public async Task <IHttpActionResult> Post(CampModel campModel)
        {
            try
            {
                if (await campRepository.GetCampAsync(campModel.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker is in use");
                }
                if (ModelState.IsValid)
                {
                    var camp = mapper.Map <Camp>(campModel);
                    campRepository.AddCamp(camp);

                    if (await campRepository.SaveChangesAsync())
                    {
                        return(CreatedAtRoute("GetCamp", new { moniker = camp.Moniker }, mapper.Map <CampModel>(camp)));
                    }
                }
                return(BadRequest(modelState: ModelState));
            }
            // TODO: Logging
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "This moniker already exists!");
                }


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

                    _repository.AddCamp(camp);

                    //two versions of setting the location, both can be used

                    //1

                    //if (await _repository.SaveChangesAsync())
                    //{
                    //    var newModel = _mapper.Map<CampModel>(camp);
                    //    var location = Url.Link("GetCamp", new { moniker = newModel.Moniker });
                    //    return Created(location, newModel);
                    //}

                    //2

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

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

            return(BadRequest(ModelState));
        }
コード例 #11
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                //verifica que el moniker se unico
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "El moniker debe ser unico");
                }

                // verifica que se cumplan lo parametrizado
                if (ModelState.IsValid)
                {
                    //se mapea alreves  los datos json recibidos a entidad Camp
                    var camp = mapper.Map <Camp>(model);
                    _repository.AddCamp(camp);

                    if (await _repository.SaveChangesAsync())
                    {
                        //se mapea para no mostrar la id y mostrar los datos a la forma de los modelos
                        var modelParaMostrar = mapper.Map <CampModel>(camp);

                        //se le asigna la ruta de GetCamp que es la que busca por moniker y se le asigna el parametro
                        //var locacion = Url.Link("GetCamp", new { moniker = modelParaMostrar.Moniker });
                        //return Created(locacion ,modelParaMostrar);
                        //la locacion se puede ver en postman desde header
                        return(CreatedAtRoute("GetCamp", new { moniker = modelParaMostrar.Moniker }, modelParaMostrar));
                    }

                    /*para validar los datos pedidos se puede desde el model de CampModel y ponerles [Require] o otras validaciones
                     */
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            //return BadRequest("Error no se enviaron bien los datos");
            return(BadRequest(ModelState));
        }
コード例 #12
0
        public async Task <IHttpActionResult> Post(CampModel model)
        {
            try
            {
                if (await _repository.GetCampAsync(model.Moniker) != null)
                {
                    ModelState.AddModelError("Moniker", "Moniker in use");
                }

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

                    // Insert to DB
                    _repository.AddCamp(camp);

                    // Commit to DB
                    if (await _repository.SaveChangesAsync())
                    {
                        // Get the inserted CampModel
                        var newModel = _mapper.Map <CampModel>(camp);

                        // Pass to Route with new value
                        return(CreatedAtRoute("GetCamp20",
                                              new { moniker = newModel.Moniker }, newModel));
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO Add logging
                return(InternalServerError(ex));
            }
            return(BadRequest(ModelState));
        }