-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
179 lines (149 loc) · 5.79 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Leaernify
{
public partial class Main : UserControl
{
public Main()
{
InitializeComponent();
loadDoesenCard();
}
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Learnify;Integrated Security=True;");
SqlCommand cmd;
public void loadDoesenCard()
{
flowLayoutPanel1.Controls.Clear();
con.Open();
string query = "SELECT * FROM pengajar";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = reader.GetInt32(0);
string nama = reader.GetString(1);
string mataPelajaran = reader.GetString(2);
string status = reader.GetString(3);
if (status != null)
{
Panel dosenCard = CreateDosenCard(id, nama, mataPelajaran, status);
flowLayoutPanel1.Controls.Add(dosenCard);
}
else
{
Panel dosenCard = CreateDosenCard(id, "", "", status);
flowLayoutPanel1.Controls.Add(dosenCard);
}
}
}
}
}
private Panel CreateDosenCard(int id, string nama, string mataPelajaran, string status)
{
Panel card = new Panel
{
Size = new Size(250, 150),
BorderStyle = BorderStyle.FixedSingle,
BackColor = Color.White,
Margin = new Padding(10),
};
Label lblNama = new Label
{
Font = new Font("Poppins", 12, FontStyle.Regular),
Text = "Nama: " + nama,
Location = new Point(10, 30),
AutoSize = true
};
Label lblMataPelajaran = new Label
{
Font = new Font("Poppins", 10, FontStyle.Regular),
Text = "Mata Pelajaran: " + mataPelajaran,
AutoSize = true,
Location = new Point(10, 60),
};
Button btnBooking = new Button
{
Font = new Font("Poppins", 7, FontStyle.Bold),
Text = (status == "Booked") ? "Sudah Dipesan" : "Book",
Location = new Point(10, 90),
Enabled = (status != "Booked"),
Tag = id,
AutoSize = true,
BackColor = (status == "Booked") ? Color.Gray : Color.FromArgb(96, 139, 193),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat
};
btnBooking.Click += BtnBooking_Click;
card.Controls.Add(lblNama);
card.Controls.Add(lblMataPelajaran);
card.Controls.Add(btnBooking);
return card;
}
private void BtnBooking_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
int pengajarID = (int)btn.Tag;
int userID = userSession.id; // Ambil user_id dari session
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// 🔹 Cek apakah pengajar masih available sebelum booking
string checkQuery = "SELECT status FROM Pengajar WHERE id = @id";
using (SqlCommand checkCmd = new SqlCommand(checkQuery, con))
{
checkCmd.Parameters.AddWithValue("@id", pengajarID);
string status = checkCmd.ExecuteScalar()?.ToString();
if (status == "Booked")
{
MessageBox.Show("Pengajar sudah dibooking oleh orang lain!", "Gagal", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
// 🔹 Masukkan data booking ke tabel Booking
string insertQuery = "INSERT INTO Booking (user_id, pengajar_id, status) VALUES (@user_id, @pengajar_id, 'Booked')";
using (SqlCommand insertCmd = new SqlCommand(insertQuery, con))
{
insertCmd.Parameters.AddWithValue("@user_id", userID);
insertCmd.Parameters.AddWithValue("@pengajar_id", pengajarID);
insertCmd.ExecuteNonQuery();
}
MessageBox.Show("Berhasil melakukan booking!", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Terjadi kesalahan: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
loadDoesenCard(); // 🔄 Refresh tampilan setelah booking
}
}
private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}