ldc-com-app/ComApp/src/App/Models/SendBrochure.js

117 lines
2.3 KiB
JavaScript

export const SendBrochuresSchema = {
name: 'SendBrochures',
primaryKey: 'id',
properties: {
/** @type {number} */
id: 'int',
email: 'string',
subject: 'string',
message: 'string',
documents: 'string',
/** @type {number} */
app_user_id: 'int',
},
};
/**
* @typedef {Unmanaged<typeof SendBrochuresSchema.properties> & import('realm').Object} SendBrochure
*/
class SendBrochureModel {
/**
* SendBrochureModel
* @param {Realm} realm
*/
constructor(realm) {
/** @type string */
this.schema = 'SendBrochures';
/** @type {Realm} */
this.realm = realm;
}
/**
* @param {SendBrochure} data
*/
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.error('db setting add', e);
}
});
}
/**
* @param {SendBrochure} data
*/
upsert(data) {
const tmp = this.get(data.key);
if (tmp === null) {
this.add(data);
} else {
if (data.id === undefined) {
data.id = tmp.id;
}
this.realm.write(() => {
this.realm.create(this.schema, data, 'modified');
});
}
}
/**
* @param {SendBrochure} data
*/
update(data) {
this.realm.write(() => {
this.realm.create(this.schema, data, 'modified');
});
}
/**
* get sendBrochure with id
* @param {number} id
* @returns {SendBrochure?}
*/
get(id) {
/** @type {import('realm').Results<SendBrochure>} */
const tmp = this.realm.objects(this.schema);
if (tmp.length === 0) {
return null;
}
return tmp.filtered('id = ' + id)[0] ?? null;
}
/**
* @returns {import('realm').Results<SendBrochure>}
*/
getAll() {
return this.realm.objects(this.schema);
}
/** Delete all */
clear() {
this.realm.write(() => {
this.realm.delete(this.getAll());
});
}
/**
* delete sendBrochure with id
* @param {number} id id
*/
delete(id) {
const tmp = this.get(id);
if (typeof tmp === 'object') {
this.realm.write(() => {
this.realm.delete(tmp);
});
}
}
}
export default SendBrochureModel;