package dev.dao;


import dev.database.DatabaseConnection;
import dev.model.MenuItem;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;



public class MenuItemDao {



    public List<MenuItem> findAll(){


        List<MenuItem> list=new ArrayList<>();


        String sql="SELECT * FROM menu_items";



        try(Connection con =
                    DatabaseConnection.getConnection();

            PreparedStatement ps =
                    con.prepareStatement(sql)){



            ResultSet rs=ps.executeQuery();



            while(rs.next()){


                list.add(new MenuItem(

                        rs.getInt("id"),

                        rs.getString("name"),

                        rs.getString("description"),

                        rs.getDouble("price"),

                        rs.getString("category")

                ));

            }



        }catch(SQLException e){

            e.printStackTrace();

        }



        return list;

    }






    public MenuItem findById(int id){


        String sql =
                "SELECT * FROM menu_items WHERE id=?";



        try(Connection con =
                    DatabaseConnection.getConnection();

            PreparedStatement ps =
                    con.prepareStatement(sql)){


            ps.setInt(1,id);



            ResultSet rs=ps.executeQuery();



            if(rs.next()){


                return new MenuItem(

                        rs.getInt("id"),

                        rs.getString("name"),

                        rs.getString("description"),

                        rs.getDouble("price"),

                        rs.getString("category")

                );

            }



        }catch(SQLException e){

            e.printStackTrace();

        }


        return null;

    }

}