ldc-com-app/ComApp/src/App/Models/SendBrochure.js
2023-01-04 11:38:44 +01:00

86 lines
1.6 KiB
JavaScript

export const SendBrochuresSchema = {
name: 'SendBrochures',
primaryKey: 'id',
properties: {
id: 'int',
email: 'string',
subject: 'string',
message: 'string',
documents: 'string',
user_id: 'int'
},
};
class SendBrochureModel {
constructor(realm) {
this.schema = 'SendBrochures';
this.realm = realm;
}
add(data) {
this.realm.write(() => {
if (data.id === undefined) {
let id = this.realm.objects(this.schema).max('id');
data.id = id === undefined ? 1 : id + 1;
}
try {
this.realm.create(this.schema, data, 'never');
} catch (e) {
console.log('db setting add', e);
}
});
}
upsert(data) {
let tmp = this.get(data.key);
if (tmp === false || tmp === undefined) {
this.add(data);
} else {
if (data.id === undefined) {
data.id = tmp.id;
}
this.realm.write(() => {
this.realm.create(this.schema, data, 'modified');
});
}
}
update(data) {
this.realm.write(() => {
this.realm.create(this.schema, data, 'modified');
});
}
get(id) {
let tmp = this.realm.objects(this.schema);
if (tmp.length === 0) {
return false;
} else {
tmp = tmp.filtered('id = ' + id)[0];
}
return tmp;
}
getAll() {
let tmp = this.realm.objects(this.schema);
return tmp;
}
clear() {
this.realm.write(() => {
this.realm.delete(this.getAll());
});
}
delete(key) {
const tmp = this.get(key);
if (typeof tmp === 'object') {
this.realm.write(() => {
this.realm.delete(tmp);
});
}
}
}
export default SendBrochureModel;