-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartController.cpp
158 lines (134 loc) · 3.51 KB
/
PartController.cpp
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
/*
* PartController.cpp
*
* Created on: 15 Feb 2012
* Author: Max Foster
*/
#include "PartController.h"
#include "Databases.h"
#include "Part.h"
#include "dialogs/part/PartShow.h"
#include "dialogs/part/PartForm.h"
void PartController::Index(QWidget *) {} // Parts are only accessible through a Job, so no need for an index screen
void PartController::Show(const int partId, QWidget *caller)
{
Part part;
try { Databases::parts().findRecord("id", partId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (part.null())
{
showErrorDialog(("Part with ID " + toString(partId) + " could not be found").c_str());
return;
}
Show(part, caller);
}
void PartController::Show(Part &part, QWidget *caller)
{
PartShow view(part, caller);
view.setModal(true);
view.exec();
}
Part PartController::New(QWidget *caller)
{
Part part;
PartForm view(part, caller);
view.setModal(true);
return (view.exec() == QDialog::Rejected ? Part() : part);
}
void PartController::Edit(const int partId, QWidget *caller)
{
Part part;
try { Databases::parts().findRecord("id", partId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (part.null())
{
showErrorDialog(("Part with ID " + toString(partId) + " could not be found").c_str());
return;
}
Edit(part, caller);
}
void PartController::Edit(Part &part, QWidget *caller)
{
Part tempPart = part;
PartForm view(tempPart, caller);
view.setModal(true);
if (view.exec() == PartForm::Accepted) part = tempPart;
}
bool PartController::Create(Part &partAttributes, QWidget *)
{
try { Databases::parts().addRecord(partAttributes); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (partAttributes.null())
{
showErrorDialog("There was an error with adding the part to the database");
return false;
}
return true;
}
bool PartController::Update(const Part &part, QWidget *)
{
bool success = false;
try { success = Databases::parts().updateRecord(part); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with updating the part in the database");
return success;
}
bool PartController::Destroy(const int partId, QWidget *)
{
bool success = false;
try { success = Databases::parts().deleteRecord(partId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with removing the part from the database");
return success;
}
bool PartController::Destroy(Part &part, QWidget *caller)
{
if (Destroy(part.getId(), caller))
{
part = Part();
return true;
}
return false;
}
Part PartController::getPart(const int partId)
{
Part part;
try { part = Databases::parts().findRecord("id", partId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Part();
}
return part;
}
Database<Part>::recordListPtr PartController::getAllParts()
{
Database<Part>::recordListPtr parts;
try { parts = Databases::parts().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Part>::recordListPtr(new Database<Part>::recordList);
}
return parts;
}