示例#1
0
        public IActionResult CreatePatrimonio([FromBody] PatrimonioCreateDto patrimonio)
        {
            if (patrimonio == null)
            {
                return(BadRequest(ModelState));
            }

            if (_patrepo.PatrimonioExists(patrimonio.Nome))
            {
                ModelState.AddModelError("", "Patrimonio Exist");
                return(StatusCode(404, ModelState));
            }

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

            var patrimoniosObj = _mapper.Map <Patrimonio>(patrimonio);

            patrimoniosObj.NumeroTombo = new Random().Next();

            if (!_patrepo.CreatePatrimonio(patrimoniosObj))
            {
                ModelState.AddModelError("", $"Algo errado ao salvar {patrimonio.Nome}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetPatrimonio", new { version = HttpContext.GetRequestedApiVersion().ToString(), id = patrimoniosObj.Id }, patrimoniosObj));
        }