Repository navigation
Expand file tree
/
Copy pathReservation.cpp
More file actions
executable file
·189 lines (159 loc) · 4.72 KB
/
Copy pathReservation.cpp
File metadata and controls
executable file
·189 lines (159 loc) · 4.72 KB
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
180
181
182
183
184
185
186
187
188
189
#include <string.h>
#include <string>
#include "BaseSQLiteModel.h"
#include "Reservation.h"
#include "City.h"
#include "Room.h"
Reservation::Reservation()
{
}
Reservation::~Reservation()
{
}
std::string Reservation::get_start_date()
{
return start_date;
};
std::string Reservation::get_end_date()
{
return end_date;
};
int Reservation::get_country_id()
{
return get_country()->get_id();
};
int Reservation::get_city_id()
{
return city_id;
};
City* Reservation::get_city()
{
return BaseSQLiteModel::get_by_id<City>(city_id);
};
Country* Reservation::get_country()
{
return Country::get_by_id(this->get_city()->get_country_id());
};
Room* Reservation::get_room()
{
std::cout << "Room id: " << room_id << std::endl;
return Room::get_by_id(room_id);
};
Hotel* Reservation::get_hotel()
{
return this->get_room()->get_hotel();
};
void Reservation::set_start_date(std::string value)
{
this->start_date = value;
}
void Reservation::set_start_date(std::time_t value)
{
std::time_t now = std::time(NULL);
std::tm * ptm = std::localtime(&now);
char buffer[32];
// Format: Mo, 2009-06-15 20:20:00
std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm);
this->start_date = std::string(buffer);
}
void Reservation::set_end_date(std::string value)
{
this->end_date = value;
}
void Reservation::set_end_date(std::time_t value)
{
std::time_t now = std::time(NULL);
std::tm * ptm = std::localtime(&now);
char buffer[32];
std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm);
this->end_date = std::string(buffer);
}
void Reservation::set_price(float value)
{
this->price = value;
}
void Reservation::set_note(std::string value)
{
this->note = value;
}
float Reservation::get_price()
{
return price;
}
std::string Reservation::get_note()
{
return note;
}
Reservation* Reservation::create(int user_id, int room_id, std::string start_date, std::string end_date, std::string note)
{
Reservation* reservation = new Reservation();
reservation->user_id = user_id;
reservation->room_id = room_id;
reservation->start_date = start_date;
reservation->end_date = end_date;
reservation->note = note;
reservation->save();
reservation->bind_id(Reservation::get_table_name());
return reservation;
};
std::string Reservation::get_table_name()
{
return BaseSQLiteModel::get_table_name<Reservation>();
};
Reservation* Reservation::get_by_id(int id)
{
return BaseSQLiteModel::get_by_id<Reservation>(id);
};
Reservation* Reservation::choice(std::vector<std::string> fiedls)
{
return BaseSQLiteModel::choice<Reservation>(fiedls);
};
void Reservation::show_by_user(User* user)
{
};
void Reservation::check_free_by_date(std::string)
{
};
void Reservation::prepare_params()
{
sqlite3_stmt *stmp;
std::string sql_raw;
if (id)
sql_raw = "UPDATE `" + get_table_name() + "`\
SET `user_id`=? `room_id`=? SET `start_date`=? SET `end_date`=? SET `note`=? \
FROM `id`=?;";
else
sql_raw = "INSERT INTO `" + get_table_name() + "`\
(`user_id`, `room_id`, `start_date`, `end_date`, `note`) \
VALUES(?, ?, ?, ?, ?);";
std::cout << sql_raw << std::endl;
int rc = sqlite3_prepare_v2(db_link, sql_raw.c_str(), -1, &stmp, 0);
// data binding
if (sqlite3_bind_int(stmp, 1, user_id) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
if (sqlite3_bind_int(stmp, 2, room_id) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
if (sqlite3_bind_text(stmp, 3, start_date.c_str(), -1, 0) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
if (sqlite3_bind_text(stmp, 4, end_date.c_str(), -1, 0) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
if (sqlite3_bind_text(stmp, 5, note.c_str(), -1, 0) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
if (id && sqlite3_bind_int(stmp, 6, id) != SQLITE_OK)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
// exec sql raw
if (sqlite3_step(stmp) != SQLITE_DONE)
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
else
std::cout << "Success execute" << std::endl;
sqlite3_finalize(stmp);
};
void Reservation::load_from_stmt(sqlite3_stmt* stmt)
{
this->id = sqlite3_column_int(stmt, 0);
this->user_id = sqlite3_column_int(stmt, 1);
this->room_id = sqlite3_column_int(stmt, 2);
this->start_date = std::string((char*)sqlite3_column_text(stmt, 3));
this->end_date= std::string((char*)sqlite3_column_text(stmt, 4));
};
std::vector<std::string> Reservation::db_sorted_fields = { "id", "user_id", "room_id", "start_date", "end_date", "note"};