コード例 #1
0
        public async System.Threading.Tasks.Task SaveUpdate()
        {
            try
            {
                int idTopologia = 1;
                RequestTipoUnidadeSave topologiaToBeSave = new RequestTipoUnidadeSave
                {
                    Nome = "Update" + DateTime.Now,
                    Id   = idTopologia
                };

                HttpResponseMessage response = await _clientCall.Save(_baseController + "Save/", JsonConvert.SerializeObject(topologiaToBeSave));

                Assert.IsTrue(response.IsSuccessStatusCode);
                if (response.IsSuccessStatusCode)
                {
                    var retorno = await response.Content.ReadAsStringAsync();

                    RequestTipoUnidadeSave topologiaRetorno = JsonConvert.DeserializeObject <RequestTipoUnidadeSave>(JObject.Parse(retorno)["data"].ToString());

                    TipoUnidade tipoUnidadeAfterUpdate = _unitOfw.TipoUnidadeRepository.Get(y => y.Id == idTopologia).FirstOrDefault();

                    Assert.AreEqual(topologiaToBeSave.Nome, tipoUnidadeAfterUpdate.Nome);
                    Assert.AreEqual(topologiaRetorno.Nome, tipoUnidadeAfterUpdate.Nome);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
コード例 #2
0
        public IActionResult Post([FromBody] RequestTipoUnidadeSave model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(new BadRequestObjectResult(ModelState.GenerateValidation()));
                }


                if (_unitOfw.TipoUnidadeRepository.Count(y => y.Nome == model.Nome && y.Id != model.Id) > 0)
                {
                    BaseViewModel <string> already = new BaseViewModel <string>("Nome Already!");
                    return(BadRequest(already));
                }

                TipoUnidade tobeSave = _mapper.Map <TipoUnidade>(model);

                if (!model.Id.HasValue || model.Id == 0)
                {
                    _unitOfw.TipoUnidadeRepository.Insert(tobeSave);
                    model.Id = tobeSave.Id;
                }
                else
                {
                    if (_unitOfw.TipoUnidadeRepository.Count(y => y.Id == model.Id) == 0)
                    {
                        BaseViewModel <string> notFound = new BaseViewModel <string>("Produto Not Found!");
                        return(NotFound(notFound));
                    }

                    _unitOfw.TipoUnidadeRepository.Update(tobeSave);
                }

                BaseViewModel <RequestTipoUnidadeSave> baseObj = new BaseViewModel <RequestTipoUnidadeSave>(model, "Tipo Unidade Saved Successfully!", "");
                return(Ok(baseObj));
            }
            catch (Exception ex)
            {
                throw;
            }
        }