Exemplo n.º 1
0
 public ActionResult Create(AnuncioViewModel anuncio, List <IFormFile> imagens)
 {
     if (imagens.Count > 3)
     {
         ViewBag.Error = "Quantidade máxima de 3 imagens";
         return(View(anuncio));
     }
     if (ModelState.IsValid)
     {
         anuncio.UsuarioId = new Guid(User.FindFirstValue(ClaimTypes.NameIdentifier));
         if (imagens.Count > 0)
         {
             anuncio.ImagemPrincipal = @"\images\livros\" + imagens.ElementAt(0).FileName;
         }
         var anc = _anuncioService.Add(Mapper.Map <AnuncioViewModel, Anuncio>(anuncio));
         _imagemService.Add(imagens, anc.Id);
         return(RedirectToAction(nameof(Index)));
     }
     return(View(anuncio));
 }
        public async Task <Anuncio> Add(Anuncio entity)
        {
            if (string.IsNullOrEmpty(entity.Marca?.Trim()))
            {
                throw new Exception("Informe a marca.");
            }
            else if (entity.Marca.Length > 45)
            {
                throw new Exception("Informe uma marca com menos de 45 caracteres.");
            }
            else if (string.IsNullOrEmpty(entity.Modelo?.Trim()))
            {
                throw new Exception("Informe o modelo.");
            }
            else if (entity.Modelo.Length > 45)
            {
                throw new Exception("Informe um modelo com menos de 45 caracteres.");
            }
            else if (string.IsNullOrEmpty(entity.Versao?.Trim()))
            {
                throw new Exception("Informe a versão.");
            }
            else if (entity.Versao.Length > 45)
            {
                throw new Exception("Informe uma versão com menos de 45 caracteres.");
            }
            else if (entity.Ano < 1885)
            {
                throw new Exception("Informe um ano acima de 1884.");
            }
            else if (entity.Ano > DateTime.Now.Year + 1)
            {
                throw new Exception($"Informe um ano abaixo de {DateTime.Now.Year + 1}.");
            }
            else if (entity.Quilometragem < 0)
            {
                throw new Exception("Informe uma quilometragem acima de 0");
            }

            return(await _anuncioService.Add(entity));
        }
 public ActionResult Post([FromBody] AnuncioRequest request)
 {
     _anuncioService.Add(request);
     return(Ok("success"));
 }
Exemplo n.º 4
0
 public bool CreateAnuncio(AnuncioModel anuncio)
 {
     return(_anuncioService.Add(TransformObject.ModelEntity <Anuncio>(anuncio)));
 }