package com.example.backend.db;

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

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()){
                User newUser = new User(rs.getString("firstname"),
                rs.getString("lastname"), rs.getString("username"),
                rs.getString("password") );
                return newUser;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
}
