public async Task <IActionResult> Create([FromBody] RepublicaResource republicaResource)
        {
            if (republicaResource == null)
            {
                return(NotFound());
            }
            try
            {
                var republica = new Republica();
                if (ModelState.IsValid)
                {
                    republica = RepublicaMapper.ResourceToModel(republicaResource, republica);
                }

                _unitOfWork.Republicas.Add(republica);
                await _unitOfWork.CompleteAsync();

                RepublicaMapper.ModelToResource(republica);
                return(Ok(republica));
            }
            catch (Exception exception)
            {
                LogError.LogErrorWithSentry(exception);
                return(BadRequest());
            }
        }
        public static Republica ResourceToModel(RepublicaResource republicaResource, Republica republica)
        {
            republica.Nome         = republicaResource.Nome ?? republica.Nome;
            republica.Vagas        = republicaResource.Vagas == 0 ? republica.Vagas : republicaResource.Vagas;
            republica.Id           = (republica.Id > 0) ? republica.Id : republicaResource.Id;
            republica.IsAtivo      = republicaResource.IsAtivo;
            republica.CriadoPor    = republicaResource.CriadoPor > 0 ? republicaResource.CriadoPor : republica.CriadoPor;
            republica.DataRegistro = (republicaResource.Id > 0) ? republica.DataRegistro : DateTime.Now;

            return(republica);
        }
Example #3
0
        public async Task Values_GetAll_N_Results()
        {
            var novo = new Republica
            {
                Id    = 1,
                Nome  = "Nome Teste",
                Vagas = 3
            };

            // Act
            var result = await _republicaController.Create(RepublicaMapper.ModelToResource(novo));

            var okResult   = result.Should().BeOfType <OkObjectResult>().Subject;
            var republicas = okResult.Value.Should().BeAssignableTo <RepublicaResource>().Subject;


            republicas.Nome.Should().Be("Nome Teste");
        }
        public static RepublicaResource ModelToResource(Republica republica)
        {
            var republicaResource = new RepublicaResource()
            {
                Nome   = republica.Nome,
                Vagas  = republica.Vagas,
                Regras = republica.Regras == null ? null : republica.Regras.Select(RegraMapper.ModelToResource),
                Avisos = republica.Avisos == null ? null : republica.Avisos.Select(AvisoMapper.ModelToResource),

                Usuarios          = republica.Usuarios == null ? null : republica.Usuarios.Select(UsuarioMapper.ModelToResource),
                CarrinhosDeCompra = republica.CarrinhosDeCompra == null ? null : republica.CarrinhosDeCompra.Select(CarrinhoDeCompraMapper.ModelToResource),

                Id           = republica.Id,
                IsAtivo      = republica.IsAtivo,
                CriadoPor    = republica.CriadoPor,
                DataRegistro = republica.DataRegistro
            };

            return(republicaResource);
        }