Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data.SqlClient;
- using System.Diagnostics.Eventing.Reader;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Hotel_Management_System
- {
- internal class Clients
- {
- private Int64 client_id { get; set; }
- private string name { get; set; }
- private string surname { get; set; }
- private string email { get; set; }
- private string phone { get; set; }
- private string city { get; set; }
- private string address { get; set; }
- private string password { get; set; }
- private DateTime date { get; set; }
- private Int64 hotel_id { get; set; }
- private bool isSaveAllowed { get; set; }
- private bool isUpdateAllowed { get; set; }
- public bool CheckIfExists()
- {
- DatabaseConnection conn = new DatabaseConnection();
- using (SqlCommand cmd = conn.executeCommand("SELECT email FROM clients WHERE email = @em AND hotel_id = :hid"))
- {
- cmd.Parameters.AddWithValue("@email", this.email);
- cmd.Parameters.AddWithValue("@hid", this.hotel_id);
- SqlDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- {
- string email = reader.GetString(3);
- if (string.IsNullOrEmpty(email))
- {
- return false;
- }
- reader.Close();
- this.isSaveAllowed = false;
- this.isUpdateAllowed = true;
- return true;
- }
- }
- return false;
- }
- public void getDataByClientId(Int64 clientId)
- {
- DatabaseConnection conn = new DatabaseConnection();
- using(SqlCommand cmd = conn.executeCommand("SELECT * FROM clients WHERE client_id = @id"))
- {
- cmd.Parameters.AddWithValue("@id", clientId);
- SqlDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- {
- this.client_id = reader.GetInt64(0);
- this.name = reader.GetString(1);
- this.surname = reader.GetString(2);
- this.email = reader.GetString(3);
- this.phone = reader.GetString(4);
- this.city = reader.GetString(5);
- this.address = reader.GetString(6);
- this.password = reader.GetString(7);
- this.date = reader.GetDateTime(8);
- this.hotel_id = reader.GetInt64(9);
- }
- reader.Close();
- }
- }
- public void PreSave() {
- if (string.IsNullOrEmpty(this.name) || string.IsNullOrEmpty(this.surname) || string.IsNullOrEmpty(this.email) ||
- string.IsNullOrEmpty(this.phone) || string.IsNullOrEmpty(this.city) || string.IsNullOrEmpty(this.address) ||
- string.IsNullOrEmpty(this.password) || this.hotel_id < 1)
- {
- this.isSaveAllowed = false;
- this.isUpdateAllowed = false;
- } else
- {
- if(this.client_id > 0)
- {
- this.isUpdateAllowed = true;
- this.isSaveAllowed = false;
- } else
- {
- this.isUpdateAllowed = false;
- this.isSaveAllowed = true;
- }
- }
- }
- public void Update()
- {
- if (this.isUpdateAllowed)
- {
- DatabaseConnection conn = new DatabaseConnection();
- using (SqlCommand cmd = conn.executeCommand("select client_id from clients where client_id = @cid"))
- {
- cmd.Parameters.AddWithValue("@cid", this.client_id);
- int count = cmd.ExecuteNonQuery();
- if (count > 0)
- {
- using (SqlCommand updatecmd = conn.executeCommand("update clients set name = @n, surname=@sur,email=@em,phone=@ph,city=@cit,address=@addr,password=@pass,date=@dat,hotel_id=@hid where client_id = @cid"))
- {
- updatecmd.Parameters.AddWithValue("@n", this.name);
- updatecmd.Parameters.AddWithValue("@sur", this.surname);
- updatecmd.Parameters.AddWithValue("@em", this.email);
- updatecmd.Parameters.AddWithValue("@ph", this.phone);
- updatecmd.Parameters.AddWithValue("@cit", this.city);
- updatecmd.Parameters.AddWithValue("@addr", this.address);
- updatecmd.Parameters.AddWithValue("@pass", this.password);
- updatecmd.Parameters.AddWithValue("@dat", this.date);
- updatecmd.Parameters.AddWithValue("@hid", this.hotel_id);
- updatecmd.Parameters.AddWithValue("@cid", this.client_id);
- updatecmd.ExecuteNonQuery();
- // since we already updated the data, we will disable updating and saving again until we change something.
- this.isSaveAllowed = false;
- this.isUpdateAllowed = false;
- }
- }
- }
- }
- }
- public void Save()
- {
- this.PreSave();
- switch (this.isSaveAllowed)
- {
- case true:
- // let's check first if we already have loaded client and when we call this function we should only update the item;
- if(this.client_id > 0)
- {
- this.Update();
- } else
- {
- DatabaseConnection conn = new DatabaseConnection();
- using(SqlCommand cmd = conn.executeCommand("insert into clients values(@name,@surname,@email,@phone,@city,@address,@password,@date,@hotel)"))
- {
- cmd.Parameters.AddWithValue("@name", this.name);
- cmd.Parameters.AddWithValue("@surname", this.surname);
- cmd.Parameters.AddWithValue("@email", this.email);
- cmd.Parameters.AddWithValue("@phone", this.phone);
- cmd.Parameters.AddWithValue("@city", this.city);
- cmd.Parameters.AddWithValue("@address", this.address);
- cmd.Parameters.AddWithValue("@password", this.password);
- cmd.Parameters.AddWithValue("@date", this.date);
- cmd.Parameters.AddWithValue("@hotel", this.hotel_id);
- cmd.ExecuteScalar();
- this.isSaveAllowed = false;
- this.isUpdateAllowed = false;
- }
- conn.closeConnection();
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement