Skip to content
Snippets Groups Projects
Commit 82fcdeef authored by elit04's avatar elit04
Browse files

databse changes

parent b36eefd4
No related branches found
No related tags found
No related merge requests found
import { Sequelize, DataTypes, Model, Optional } from "sequelize";
import { BoatTypeInput } from "./BoatType";
interface BoatAttributes {
id: number;
id: string;
name: string;
boatType: string;
boattype: string;
status: boolean;
active: boolean;
seats: number;
tags: Array<string>;
}
export interface BoatAttributesInput extends Optional<BoatAttributes, "id"> { }
export interface BoatAttributesOutput extends Required<BoatAttributes> { }
class Boat extends Model<BoatAttributes> implements BoatAttributes {
public id!: number;
public name!: string;
public boatType: string;
public status: boolean;
public active: boolean;
public seats: number;
public tags: Array<string>;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
class Boat extends Model<BoatAttributes, BoatTypeInput> implements BoatAttributes {
declare id: string;
declare name: string;
declare boattype: string;
declare status: boolean;
declare active: boolean;
declare tags: Array<string>;
declare readonly createdAt: Date;
declare readonly updatedAt: Date;
}
export const initBoat = async (sequelizeConnection: Sequelize) => {
Boat.init(
{
id: {
type: DataTypes.INTEGER,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
name: {
type: new DataTypes.STRING(),
allowNull: false,
},
boatType: {
type: new DataTypes.STRING(),
boattype: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: "boattype",
key: "id",
}
},
status: {
type: DataTypes.BOOLEAN,
......@@ -47,10 +53,6 @@ export const initBoat = async (sequelizeConnection: Sequelize) => {
type: DataTypes.BOOLEAN,
allowNull: false,
},
seats: {
type: new DataTypes.INTEGER(),
allowNull: false,
},
tags: {
type: new DataTypes.ARRAY(DataTypes.STRING),
}
......@@ -60,47 +62,6 @@ export const initBoat = async (sequelizeConnection: Sequelize) => {
sequelize: sequelizeConnection,
}
);
}
export const create = async (payload: Boat): Promise<BoatAttributesOutput> => {
return await Boat.create(payload);
};
export const update = async (
id: number,
payload: Partial<BoatAttributesInput>
): Promise<BoatAttributesOutput> => {
const boat = await Boat.findByPk(id);
if (!boat) {
throw new Error("not found");
}
return await (boat as Boat).update(payload);
};
export const getById = async (id: number): Promise<Boat> => {
const boat = await Boat.findByPk(id);
if (!boat) {
throw new Error("not found");
}
return boat;
};
export const deleteById = async (id: number): Promise<boolean> => {
const deletedBoatCount = await Boat.destroy({
where: { id },
});
return !!deletedBoatCount;
};
export const getAll = async (): Promise<BoatAttributesOutput[]> => {
return Boat.findAll();
};
export const BoatService = {
create,
update,
getById,
deleteById,
getAll,
};
export default Boat;
......@@ -37,7 +37,7 @@ export const initBoatType = async (sequelizeConnection: Sequelize) => {
},
},
{
tableName: "boatType",
tableName: "boattype",
sequelize: sequelizeConnection,
}
);
......
......@@ -2,36 +2,38 @@
import { DataTypes, Model, Optional , Sequelize} from "sequelize";
interface CheckInAttributes {
id: number;
id: string;
checkin: Date;
checkout: Date;
clientID: number;
boatID: number;
clientID: string;
boatID: string;
destination: string;
note: string;
}
export interface CheckInAttributesInput
extends Optional<CheckInAttributes, "id"> { }
export interface CheckInAttributesOutput extends Required<CheckInAttributes> { }
class CheckIn extends Model<CheckInAttributes> implements CheckInAttributes {
public id!: number;
public checkin: Date;
public checkout: Date;
public clientID: number;
public boatID: number;
public destination: string;
public note: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
class CheckIn extends Model<CheckInAttributes, CheckInAttributesInput> implements CheckInAttributes {
declare id: string;
declare checkin: Date;
declare checkout: Date;
declare clientID: string;
declare boatID: string;
declare destination: string;
declare note: string;
declare readonly createdAt: Date;
declare readonly updatedAt: Date;
}
export const initCheckIn = async (sequelizeConnection: Sequelize) => {
CheckIn.init(
{
id: {
type: DataTypes.INTEGER,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
checkin: {
type: DataTypes.DATE,
......@@ -42,14 +44,14 @@ export const initCheckIn = async (sequelizeConnection: Sequelize) => {
allowNull: false,
},
clientID: {
type: DataTypes.INTEGER,
type: DataTypes.UUID,
references: {
model: "customer",
key: "id",
},
},
boatID: {
type: DataTypes.INTEGER,
type: DataTypes.UUID,
references: {
model: "boat",
key: "id",
......@@ -63,52 +65,10 @@ export const initCheckIn = async (sequelizeConnection: Sequelize) => {
},
},
{
tableName: "checkIn",
tableName: "checkin",
sequelize: sequelizeConnection,
}
);
}
export const create = async (
payload: CheckIn
): Promise<CheckInAttributesOutput> => {
return await CheckIn.create(payload);
};
export const update = async (
id: number,
payload: Partial<CheckInAttributesInput>
): Promise<CheckInAttributesOutput> => {
const checkin = await CheckIn.findByPk(id);
if (!checkin) {
throw new Error("not found");
}
return await (checkin as CheckIn).update(payload);
};
export const getById = async (id: number): Promise<CheckIn> => {
const checkin = await CheckIn.findByPk(id);
if (!checkin) {
throw new Error("not found");
}
return checkin;
};
const deleteById = async (id: number): Promise<boolean> => {
const deletedCheckInCount = await CheckIn.destroy({
where: { id },
});
return !!deletedCheckInCount;
};
export const getAll = async (): Promise<CheckInAttributesOutput[]> => {
return CheckIn.findAll();
};
export const CheckInService = {
create,
update,
getById,
deleteById,
getAll,
};
export default CheckIn;
......@@ -2,21 +2,21 @@
import { DataTypes, Model, Optional, Sequelize } from "sequelize";
interface CustomerAttributes {
id: number;
id: string;
firstName: string;
lastName: string;
email: string;
}
export interface CustomerInput extends Optional<CustomerAttributes, "id"> { }
export interface CustomerOutput extends Required<CustomerAttributes> { }
class Customer extends Model<CustomerAttributes> implements CustomerAttributes {
public id!: number;
public firstName!: string;
public lastName: string;
public email: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
class Customer extends Model<CustomerAttributes, CustomerInput> implements CustomerAttributes {
declare id: string;
declare firstName: string;
declare lastName: string;
declare email: string;
declare readonly createdAt: Date;
declare readonly updatedAt: Date;
}
export const initCustomer = async (sequelizeConnection: Sequelize) => {
......@@ -24,9 +24,10 @@ export const initCustomer = async (sequelizeConnection: Sequelize) => {
Customer.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
firstName: {
type: new DataTypes.STRING(),
......@@ -49,44 +50,5 @@ Customer.init(
);
}
export const create = async (payload: Customer): Promise<CustomerOutput> => {
return await Customer.create(payload);
};
export const update = async (
id: number,
payload: Partial<CustomerInput>
): Promise<CustomerOutput> => {
const customer = await Customer.findByPk(id);
if (!customer) {
throw new Error("not found");
}
return await (customer as Customer).update(payload);
};
export const getById = async (id: number): Promise<CustomerInput> => {
const customer = await Customer.findByPk(id);
if (!customer) {
throw new Error("not found");
}
return customer;
};
const deleteById = async (id: number): Promise<boolean> => {
const deletedCustomerCount = await Customer.destroy({
where: { id },
});
return !!deletedCustomerCount;
};
export const getAll = async (): Promise<CustomerOutput[]> => {
return Customer.findAll();
};
export const CustomerService = {
create,
update,
getById,
deleteById,
getAll,
};
export default Customer;
......@@ -59,48 +59,5 @@ export const initWorker = async (sequelizeConnection: Sequelize) => {
}
);
};
/*
--------------------------all those wrapper-functions are not needed. Query-functions can be calles simply using Model---------------------------------------------------------
export const create = async (payload: Worker): Promise<WorkerOutput> => {
return await Worker.create(payload);
};
export const update = async (
email: string,
payload: Partial<WorkerInput>
): Promise<WorkerOutput> => {
const worker = await Worker.findByPk(email);
if (!worker) {
throw new Error("not found");
}
return await (worker as Worker).update(payload);
};
export const getById = async (email: string): Promise<WorkerInput> => {
const worker = await Worker.findByPk(email);
if (!worker) {
throw new Error("not found");
}
return worker;
};
const deleteById = async (email: string): Promise<boolean> => {
const deletedWorkerCount = await Worker.destroy({
where: { email },
});
return !!deletedWorkerCount;
};
export const getAll = async (): Promise<WorkerOutput[]> => {
return Worker.findAll();
};
export const WorkerService = {
create,
update,
getById,
deleteById,
getAll,
};
*/
export default Worker;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment