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 async Task <IActionResult> Get(int id)
        {
            var republica = await _unitOfWork.Republicas.GetByIdWithRegrasEUsuariosEAvisosECarrinhosDeCompraAsync(id);

            if (republica.IsAtivo)
            {
                return(Ok(RepublicaMapper.ModelToResource(republica)));
            }

            return(NoContent());
        }
Ejemplo n.º 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 async Task <IActionResult> GetAll()
        {
            var republicas = await _unitOfWork.Republicas.GetRepublicaWithRegrasEUsuariosEAvisosECarrinhosDeCompraAsync();

            List <RepublicaResource> repbiblicaResource = new List <RepublicaResource>();

            if (republicas == null)
            {
                return(NoContent());
            }
            foreach (var republica in republicas)
            {
                if (republica.IsAtivo)
                {
                    repbiblicaResource.Add(RepublicaMapper.ModelToResource(republica));
                }
            }
            return(Ok(repbiblicaResource));
        }
        public async Task <IActionResult> Edit(int id, [FromBody] RepublicaResource republicaResource)
        {
            try
            {
                var republica = await _unitOfWork.Republicas.GetByIdAsync(id);

                if (ModelState.IsValid)
                {
                    republica = RepublicaMapper.ResourceToModel(republicaResource, republica);
                    await _unitOfWork.CompleteAsync();

                    RepublicaMapper.ModelToResource(republica);
                }
                return(Ok(republica));
            }
            catch (Exception e)
            {
                LogError.LogErrorWithSentry(e);
                return(BadRequest(ModelState));
            }
        }