-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryModel.cpp
More file actions
145 lines (127 loc) · 4.01 KB
/
LibraryModel.cpp
File metadata and controls
145 lines (127 loc) · 4.01 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
#include "LibraryModel.h"
#include "DatabaseManager.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
LibraryModel::LibraryModel(QObject *parent)
: QAbstractListModel(parent)
{
refresh();
}
int LibraryModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_books.count();
}
QVariant LibraryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_books.count())
return QVariant();
const Book &book = m_books[index.row()];
switch (role) {
case IdRole:
return book.id;
case TitleRole:
return book.title;
case AuthorRole:
return book.author;
case StatusRole:
return book.status;
case ContactNameRole:
return book.contactName;
case ContactNumberRole:
return book.contactNumber;
default:
return QVariant();
}
}
QHash<int, QByteArray> LibraryModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[TitleRole] = "title";
roles[AuthorRole] = "author";
roles[StatusRole] = "status";
roles[ContactNameRole] = "contactName";
roles[ContactNumberRole] = "contactNumber";
return roles;
}
void LibraryModel::refresh()
{
beginResetModel();
m_books.clear();
QSqlQuery query("SELECT id, title, author, status, contact_name, contact_number FROM books ORDER BY id DESC");
while (query.next()) {
Book book;
book.id = query.value(0).toInt();
book.title = query.value(1).toString();
book.author = query.value(2).toString();
book.status = query.value(3).toString();
book.contactName = query.value(4).toString();
book.contactNumber = query.value(5).toString();
m_books.append(book);
}
endResetModel();
emit countChanged();
}
void LibraryModel::addBook(const QString &title, const QString &author, const QString &status, const QString &contactName, const QString &contactNumber)
{
QSqlQuery query;
query.prepare("INSERT INTO books (title, author, status, contact_name, contact_number) VALUES (:title, :author, :status, :contactName, :contactNumber)");
query.bindValue(":title", title);
query.bindValue(":author", author);
query.bindValue(":status", status);
query.bindValue(":contactName", contactName);
query.bindValue(":contactNumber", contactNumber);
if (query.exec()) {
refresh();
} else {
qCritical() << "Failed to add book:" << query.lastError().text();
}
}
void LibraryModel::updateBook(int id, const QString &title, const QString &author, const QString &status, const QString &contactName, const QString &contactNumber)
{
QSqlQuery query;
query.prepare("UPDATE books SET title = :title, author = :author, status = :status, contact_name = :contactName, contact_number = :contactNumber WHERE id = :id");
query.bindValue(":title", title);
query.bindValue(":author", author);
query.bindValue(":status", status);
query.bindValue(":contactName", contactName);
query.bindValue(":contactNumber", contactNumber);
query.bindValue(":id", id);
if (query.exec()) {
refresh();
} else {
qCritical() << "Failed to update book:" << query.lastError().text();
}
}
void LibraryModel::removeBook(int index)
{
if (index < 0 || index >= m_books.count()) return;
int id = m_books[index].id;
QSqlQuery query;
query.prepare("DELETE FROM books WHERE id = :id");
query.bindValue(":id", id);
if (query.exec()) {
refresh();
} else {
qCritical() << "Failed to delete book:" << query.lastError().text();
}
}
int LibraryModel::getShelfCount() const
{
int count = 0;
for (const Book &book : m_books) {
if (book.status == "SHELF") count++;
}
return count;
}
int LibraryModel::getLoanedCount() const
{
int count = 0;
for (const Book &book : m_books) {
if (book.status == "LOANED" || book.status == "BORROWED") count++;
}
return count;
}