2017-07-26 18:18:37 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using Dapper ;
using System.Data ;
using System.Windows.Forms ;
using System.Data.SqlClient ;
namespace db_AI_4
{
public class DataAccess
{
public SqlConnection connection { get ; private set ; }
//Read locations from the Database function start
public List < GetLocations > GetLocations ( string LocationName )
{
using ( IDbConnection connection = new System . Data . SqlClient . SqlConnection ( Helper . CnnVal ( "marketdb" ) ) )
{
2017-07-27 15:52:00 +02:00
var output = connection . Query < GetLocations > ( $"SELECT * FROM locations WHERE location_name like '%{ LocationName }%'" ) . ToList ( ) ;
2017-07-26 18:18:37 +02:00
return output ;
}
}
2017-08-30 15:23:13 +02:00
public void CurrentUser ( string input_mail )
2017-07-26 18:18:37 +02:00
{
2017-08-30 15:23:13 +02:00
List < int > QueryResult = new List < int > ( ) ;
2017-07-26 18:18:37 +02:00
using ( IDbConnection connection = new System . Data . SqlClient . SqlConnection ( Helper . CnnVal ( "marketdb" ) ) )
2017-08-30 19:55:25 +02:00
{
2017-08-30 15:23:13 +02:00
var command = "SELECT member_id FROM dbo.members WHERE members.email_address = @Email_Adress" ;
2017-08-30 19:55:25 +02:00
QueryResult = connection . Query < int > ( command , new { Email_Adress = input_mail } ) . ToList ( ) ;
2017-08-30 15:23:13 +02:00
GetMembers . member_id = Convert . ToInt32 ( QueryResult [ 0 ] ) ;
2017-07-26 18:18:37 +02:00
}
}
2017-07-26 20:20:14 +02:00
// Insert Rents into Database start
2017-08-30 19:55:25 +02:00
public void InsertRent ( string RentDate , int MemberID , int RentPriceID , int LocationID )
2017-07-26 20:20:14 +02:00
{
using ( IDbConnection connection = new System . Data . SqlClient . SqlConnection ( Helper . CnnVal ( "marketdb" ) ) )
{
List < GetRents > rents = new List < GetRents > ( ) ;
2017-08-30 19:55:25 +02:00
rents . Add ( new GetRents { rent_date = RentDate , member_id = GetMembers . member_id , rent_price_id = RentPriceID , location_id = LocationID } ) ;
2017-07-26 20:20:14 +02:00
2017-08-28 10:54:16 +02:00
connection . Execute ( "INSERT INTO dbo.rents (rent_date, member_id, rent_price_id, location_id)" +
" VALUES (@rent_date, @member_id, @rent_price_id, @location_id )" , rents ) ;
2017-07-26 20:20:14 +02:00
}
}
// Insert Rents into Database end
2017-08-02 22:36:24 +02:00
//Registration function start
2017-08-30 15:23:13 +02:00
public void InsertMember ( string input_mail , string input_pw )
2017-08-02 22:36:24 +02:00
{
using ( IDbConnection connection = new System . Data . SqlClient . SqlConnection ( Helper . CnnVal ( "marketdb" ) ) )
{
2017-08-30 15:23:13 +02:00
var command = "INSERT INTO dbo.members (email_address, password) VALUES ( @Email_address, @Password)" ;
connection . Execute ( command , new { Email_address = input_mail , Password = input_pw } ) ;
2017-08-02 22:36:24 +02:00
}
}
2017-08-27 09:43:05 +02:00
//Registration function end
2017-08-02 22:36:24 +02:00
2017-08-27 09:43:05 +02:00
//Read Rented locations start
2017-08-02 22:36:24 +02:00
internal List < GetRents > GetRentedLocations ( string LocationName )
{
using ( IDbConnection connection = new System . Data . SqlClient . SqlConnection ( Helper . CnnVal ( "marketdb" ) ) )
{
var rentoutput = connection . Query < GetRents > ( $"SELECT * FROM [RentedLocations] WHERE location_name like '%{ LocationName }%' ORDER BY rent_date ASC " ) . ToList ( ) ;
return rentoutput ;
}
}
2017-08-27 09:43:05 +02:00
//Read Rented locations end
2017-08-02 22:36:24 +02:00
2017-07-26 18:18:37 +02:00
//CheckLogin function start
public void CheckLogin ( string Email_address , string Password )
{
SqlConnection con = new SqlConnection ( Helper . CnnVal ( "marketdb" ) ) ;
con . Open ( ) ;
SqlCommand cmd = new SqlCommand ( $"SELECT * FROM dbo.members WHERE email_address = '{Email_address}' and password = '{Password}'" , con ) ;
SqlDataReader reader ;
reader = cmd . ExecuteReader ( ) ;
int count = 0 ;
while ( reader . Read ( ) )
{
count + = 1 ;
}
if ( count = = 1 )
{
2017-08-30 15:23:13 +02:00
CurrentUser ( GetMembers . email_address ) ;
2017-07-26 18:18:37 +02:00
Dashboard dashboard = new Dashboard ( ) ;
dashboard . Show ( ) ;
}
else if ( count > 0 )
{
MessageBox . Show ( "Already existing" ) ;
}
else
{
2017-07-27 15:52:00 +02:00
MessageBox . Show ( "Username or Password not correct" ) ;
2017-07-26 18:18:37 +02:00
}
//CheckLogin function end
}
}
}