package com.example.backend.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.example.backend.db.DB;
import com.example.backend.models.User;

public class UserRepo implements UserRepoInterface{

    @Override
    public User login(User u) {
        try(Connection conn = DB.source().getConnection();
            PreparedStatement stm = conn.prepareStatement("select * from users where username=? and password=?")){

            stm.setString(1, u.getUsername());
            stm.setString(2, u.getPassword());
            ResultSet rs = stm.executeQuery();
            if(rs.next()){
                return new User(rs.getString("firstname"), rs.getString("lastname"), rs.getString("username"), rs.getString("password"), rs.getString("type"));
            }
        }catch(SQLException e){

        }
        return null;
    }
    
}
