forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
costtype.cpp
98 lines (87 loc) · 2.34 KB
/
costtype.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
/**
* CarBudget, Sailfish application to manage car cost
*
* Copyright (C) 2014 Fabien Proriol, 2015 Thomas Michel
*
* This file is part of CarBudget.
*
* CarBudget is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CarBudget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with CarBudget. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Thomas Michel
*/
#include "costtype.h"
#include "car.h"
Costtype::Costtype(QObject *parent) :
QObject(parent),
_id(0),
_name("Not Set")
{
}
Costtype::Costtype(unsigned int id, QString name, Car *parent):
QObject(parent),
_car(parent),
_id(id),
_name(name)
{
}
unsigned int Costtype::id() const
{
return _id;
}
void Costtype::setId(unsigned int id)
{
_id = id;
emit idChanged();
}
QString Costtype::name() const
{
return _name;
}
void Costtype::setName(QString name)
{
_name = name;
emit nameChanged();
}
void Costtype::save()
{
if(_id < 0)
{
QSqlQuery query(_car->db);
QString sql = QString("INSERT INTO CosttypeList (id,name) VALUES(NULL,'%1')").arg(_name);
if(query.exec(sql))
{
_id = query.lastInsertId().toInt();
qDebug() << "Create Costtype in database with id " << _id << "and value " << _name;
_car->db.commit();
}
else
{
qDebug() << "Error during Create Costtype in database";
qDebug() << query.lastError();
}
}
else
{
QSqlQuery query(_car->db);
QString sql = QString("UPDATE CosttypeList SET name='%1' WHERE id=%2;").arg(_name).arg(_id);
qDebug() << sql;
if(query.exec(sql))
{
qDebug() << "Update Costtype in database with id " << _id;
_car->db.commit();
}
else
{
qDebug() << "Error during Update Costtype in database";
qDebug() << query.lastError();
}
}
}