Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

quiero que este metodo se ejecute solo si hay items en el carro de compra -…

  1. User

    quiero que este metodo se ejecute solo si hay items en el carro de compra - @PostMapping("/registrarVenta") public ResponseEntity<?> registrarVenta(@ModelAttribute("dvCarro") List dvCarro) { try { if (dvCarro != null && !dvCarro.isEmpty()) {

    			Cliente cliente = cliService.obtenerClientePorId(1L);
    			Usuario usuario = usrService.obtenerUsuarioPorId(1L);
    
    			Venta venta = new Venta();
    			venta.setCliente(cliente);
    			venta.setUsuario(usuario);
    			double sumaTotal = dvCarro.stream().mapToDouble(DetalleVenta::getTotal).sum();
    			venta.setSubtotal(sumaTotal);
    			vntService.registrar(venta);
    
    			for (DetalleVenta dv : dvCarro) {
    				dv.setVenta(venta);
    				detService.registrar(dv);
    
    				Inventario inventario = invService.obtenerInventarioProducto(dv.getProducto().getIdproducto());
    				invService.actualizarStockVenta(inventario.getIdinventario(), dv.getCantidad());
    				movService.registrarMovimientoVenta(inventario.getIdinventario(), dv.getCantidad());
    			}
    
    			dvCarro.clear();
    		}
    
    		return ResponseEntity.ok(dvCarro);
    	} catch (AccessDeniedException e) {
    		return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Acceso denegado");
    	} catch (Exception e) {
    		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error al registrar venta");
    	}
    }
    

    } - verifica y responde en 2 lineas

  2. Assistant

    Tu método ya verifica si hay items en el carro (dvCarro != null && !dvCarro.isEmpty()) y solo ejecuta la lógica de registrar la venta en ese caso. Por lo tanto, está correctamente implementado según tu requerimiento.