Esempio n. 1
0
        public async Task <BaseServiceResponse <bool> > EditarAsync(string idUsuario, EdicionAnuncioRequest request)
        {
            BaseServiceResponse <bool> response = new BaseServiceResponse <bool>();
            var usuario = await _usuarioRepository.ConsultarUsuarioAsync(idUsuario);

            var anuncioResult = await _anuncioRepository.ConsultarAsync(request.IdAnuncio.Value);

            if (anuncioResult == null)
            {
                response.Message = "No existe el anuncio.";
                return(response);
            }
            if (anuncioResult.Activo)
            {
                response.Message = "No se puedo editar el anuncio porque se encuentra activo.";
                return(response);
            }

            var anuncio = _mapper.Map <AnuncioEntity>(request);

            anuncio.IdUsuario = usuario.IdUsuario;
            var anuncioUpdated = await _anuncioRepository.EditarAnuncioAsync(anuncio);

            if (!anuncioUpdated)
            {
                response.Message = "No se puedo editar el anuncio.";
                return(response);
            }

            var anuncioDetalle       = _mapper.Map <AnuncioDetalleEntity>(request);
            var anuncioDetalleEntity = await _anuncioDetalleRepository.ConsultarAnuncioDetallePorAnuncioAsync(request.IdAnuncio.Value);

            anuncioDetalle.IdAnuncioDetalle = anuncioDetalleEntity != default ? anuncioDetalleEntity.IdAnuncioDetalle : default;
            var anuncioDetalleUpdated = await _anuncioDetalleRepository.EditarAnuncioDetalleAsync(anuncioDetalle);

            if (!anuncioDetalleUpdated)
            {
                response.Message = "No se puedo editar el detalle del anuncio.";
                return(response);
            }

            var ubicacion       = _mapper.Map <UbicacionEntity>(request);
            var ubicacionEntity = await _ubicacionRepository.ConsultarPorAnuncioAsync(request.IdAnuncio.Value);

            ubicacion.IdUbicacion = ubicacionEntity != default ? ubicacionEntity.IdUbicacion : default;
            var ubicacionUpdated = await _ubicacionRepository.EditarUbicacionAsync(ubicacion);

            if (!ubicacionUpdated)
            {
                response.Message = "No se puedo editar la ubicación.";
                return(response);
            }

            response.Data    = anuncioUpdated;
            response.Success = anuncioUpdated;
            response.Message = "Se actualizó exitosamente";

            return(response);
        }