Skip to content

Commit

Permalink
RQ - Updated previous classes/methods with new DB changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Pyramid-Head authored and Pieloaf committed Nov 21, 2023
1 parent eb73900 commit 4b385f2
Show file tree
Hide file tree
Showing 27 changed files with 641 additions and 654 deletions.
65 changes: 65 additions & 0 deletions src/api/utils/entityDefs/entityDefs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as EntityDefsModel from './entityDefs.model';
import * as EntityDefsServices from './entityDefs.service';

/**
* Get logged in User's roster
*/
export const getUserEntityDefs = async (user_fk: number) : Promise<EntityDefsModel.EntityDef> => {
return await EntityDefsServices.getUserEntityDefsByUserId(user_fk);
};

/**
* Get logged in User's party roster
*/
export const getUserPartyEntityDefs = async (user_fk: number) : Promise<EntityDefsModel.EntityDef[]> => {
return EntityDefsServices.getUserPartyEntityDefs(user_fk);
};

/**
* gets roster unit by user id and unit id
*/
export const getUnitByUserIdAndUnitId = async (user_fk: EntityDefsModel.EntityDef['user_fk'], unit_id: EntityDefsModel.EntityDef['unit_id']) => {
return await EntityDefsServices.getUnitByUserIdAndUnitId(user_fk, unit_id);
};

/**
* Get User's Entities max unit_id by unit id
*/
export const getEntityMaxUnitId = async (user_fk: number, unit_id: string): Promise<EntityDefsModel.EntityDef> => {
return await EntityDefsServices.getEntityMaxUnitId(user_fk, unit_id);
};

/**
* gets user's roster ID based on unit id provided
*/
export const getUserEntDefByUnitId = async (user_fk: number, unit_id: string) => {
return await EntityDefsServices.getUserEntDefByUnitId(user_fk, unit_id);
};

/**
* Insert Purchasable Unit data to Roster - triggers when hiring new units
*/
export const insertUserEntity = async (new_entity: EntityDefsModel.AddEntityDef) => {
await EntityDefsServices.insertUserEntity(new_entity);
};

/**
* updates roster's name
*/
export const updateUnitName = async (user_fk: EntityDefsModel.EntityDef['user_fk'], unit_id: EntityDefsModel.EntityDef['unit_id'], name: EntityDefsModel.EntityDef['name']) => {
return await EntityDefsServices.updateUnitName(user_fk, unit_id, name);
};

/**
* updates roster's entity class
*/
export const updateUnitClass = async (user_fk: EntityDefsModel.EntityDef['user_fk'], unit_id: EntityDefsModel.EntityDef['unit_id'], entity_class: EntityDefsModel.EntityDef['entity_class']) => {
return await EntityDefsServices.updateUnitClass(user_fk, unit_id, entity_class);
};

/**
* Delete user's Roster / Unit
*/
export const deleteEntityDef = async (user_fk: EntityDefsModel.EntityDef['user_fk'], unit_id: EntityDefsModel.EntityDef['unit_id']) => {
await EntityDefsServices.deleteEntityDef(user_fk, unit_id);
};
40 changes: 40 additions & 0 deletions src/api/utils/entityDefs/entityDefs.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class EntityDef {
id: number;
user_fk: number;
classs: string;
unit_id: string;
entity_class: string;
name: string;
appearance_acquires: number;
appearance_index: number;

public constructor(id: number, user_fk: number, unit_id: string, entity_class: string, name: string, appearance_acquires: number, appearance_index: number) {
this.id = id;
this.user_fk = user_fk;
this.classs = "EntityDef";
this.unit_id = unit_id;
this.entity_class = entity_class;
this.name = name;
this.appearance_acquires = appearance_acquires;
this.appearance_index = appearance_index;
};
}

export class AddEntityDef {
user_fk: number;
classs = "EntityDef";
unit_id: string;
entity_class: string;
name: string;
appearance_acquires: number;
appearance_index: number;

public constructor(user_fk: number, unit_id: string, entity_class: string, name: string) {
this.user_fk = user_fk;
this.unit_id = unit_id;
this.entity_class = entity_class;
this.name = name;
this.appearance_acquires = 0;
this.appearance_index = 0;
};
}
97 changes: 97 additions & 0 deletions src/api/utils/entityDefs/entityDefs.queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
export const EntityDefQueries = {
GetUserEntityDefsByUserId:
`
SELECT
id,
unit_id,
entity_class,
name,
start_date,
appearance_acquires,
appearance_index
FROM db.EntityDef
WHERE
user_fk = ?;
`,

GetUserPartyEntityDefsByUserId:
`
SELECT
ed.id,
ed.unit_id,
ed.entity_class,
ed.name,
ed.appearance_acquires,
ed.appearance_index
FROM db.Parties p
LEFT JOIN db.EntityDef ed ON p.unit_id = ed.unit_id
WHERE p.user_fk = ? AND r.user_fk = ?;
`,

GetUnitByUserIdAndUnitId:
`
SELECT
*
FROM db.EntityDef as ed
WHERE
ed.user_fk = ? and ed.unit_id = ?;
`,

GetEntityMaxUnitId:
`
SELECT
*
FROM db.EntityDef
WHERE
user_fk = ?
AND
unit_id LIKE ?
ORDER BY unit_id DESC LIMIT 1;
`,

GetUserEntDefByUnitId:
`
SELECT
id
FROM db.EntityDef
WHERE user_fk = ?
AND unit_id = ?;
`,

InsertUserEntity:
`
INSERT INTO db.EntityDef
(user_fk, unit_id, entity_class, name, appearance_acquires, appearance_index)
VALUES
(?, ?, ?, ?, ?, ?);
`,

UpdateEntity_Name:
`
UPDATE db.EntityDef as ed
SET ed.name = ?
WHERE
user_fk = ?
AND
unit_id = ?;
`,

UpdateEntity_EntityClass:
`
UPDATE db.EntityDef as ed
SET ed.entity_class = ?
WHERE
user_fk = ?
AND
unit_id = ?;
`,

DeleteUserEntityUnit:
`
DELETE FROM db.EntityDef
WHERE
user_fk = ?
AND
unit_id = ?
`,
};
67 changes: 67 additions & 0 deletions src/api/utils/entityDefs/entityDefs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { execute } from "../mysql.connector";

import * as EntityDefQueries from "./entityDefs.queries";
import * as EntityDefModels from "./entityDefs.model";

/**
* gets user's party based on id provided
*/
export const getUserEntityDefsByUserId = async (user_fk: EntityDefModels.EntityDef['user_fk']) => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.GetUserEntityDefsByUserId, [user_fk]);
};

/**
* Get logged in User's party roster
*/
export const getUserPartyEntityDefs = async (user_fk: EntityDefModels.EntityDef['user_fk']) => {
return execute<EntityDefModels.EntityDef[]>(EntityDefQueries.EntityDefQueries.GetUserPartyEntityDefsByUserId, [user_fk, user_fk]);
};

/**
* gets roster unit by user id and unit id
*/
export const getUnitByUserIdAndUnitId = async (user_fk: EntityDefModels.EntityDef['user_fk'], unit_id: EntityDefModels.EntityDef['unit_id']) => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.GetUnitByUserIdAndUnitId, [user_fk, unit_id]);
};

/**
* Get User's Entity Def max unit_id by unit id
*/
export const getEntityMaxUnitId = async (user_fk: number, unit_id: string): Promise<EntityDefModels.EntityDef> => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.GetEntityMaxUnitId, [user_fk, unit_id]);
};

/**
* gets user's entity def ID based on unit id provided
*/
export const getUserEntDefByUnitId = async (user_fk: number, unit_id: string) => {
return execute<number>(EntityDefQueries.EntityDefQueries.GetUserEntDefByUnitId, [user_fk, unit_id]);
};

/**
* Insert Purchasable Unit data to Roster - triggers when hiring new units
*/
export const insertUserEntity = async (new_entity: EntityDefModels.AddEntityDef) => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.InsertUserEntity, [new_entity.user_fk, new_entity.unit_id, new_entity.entity_class, new_entity.name, new_entity.appearance_acquires, new_entity.appearance_index]);
};

/**
* updates roster's name
*/
export const updateUnitName = async (user_fk: EntityDefModels.EntityDef['user_fk'], unit_id: EntityDefModels.EntityDef['unit_id'], name: EntityDefModels.EntityDef['name']) => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.UpdateEntity_Name, [name, user_fk, unit_id]);
};

/**
* updates roster's entity class
*/
export const updateUnitClass = async (user_fk: EntityDefModels.EntityDef['user_fk'], unit_id: EntityDefModels.EntityDef['unit_id'], entity_class: EntityDefModels.EntityDef['entity_class']) => {
return execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.UpdateEntity_EntityClass, [entity_class, user_fk, unit_id]);
};

/**
* Delete user's Roster / Unit
*/
export const deleteEntityDef = async (user_fk: EntityDefModels.EntityDef['user_fk'], unit_id: EntityDefModels.EntityDef['unit_id']) => {
execute<EntityDefModels.EntityDef>(EntityDefQueries.EntityDefQueries.DeleteUserEntityUnit, [user_fk, unit_id]);
};
21 changes: 13 additions & 8 deletions src/api/utils/mysql.connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ let connection: Connection;
*/
export const init = () => {
try {
/*console.log(dataSource.DB_DATABASE);
connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : 'root',
database : 'banner_saga_factions'
});
/*pool = createPool({
connectionLimit: dataSource.DB_CONNECTION_LIMIT,
host: dataSource.DB_HOST,
user: dataSource.DB_USER,
password: dataSource.DB_PASSWORD,
database: dataSource.DB_DATABASE,
database: dataSource.DB_DATABASE
});*/
//console.log(connection);
connection = mysql.createConnection({
port : 3300,
host : '127.0.0.1',
user : 'factions',
password : '4Z7&R!wyCyRs',
database : 'factions-dev'
});

console.debug('MySql Adapter Pool generated successfully');
} catch (error) {
Expand All @@ -43,6 +45,9 @@ export const execute = <T>(query: string, params: string[] | Object): Promise<T>
if (!connection) throw new Error('Pool was not created. Ensure pool is created when running the app.');

return new Promise<T>((resolve, reject) => {
let db: any = '`' + connection.config.database + '`';
query = query.replace(/db/gi, db);

connection.query(query, params, (error, results) => {
if (error) reject(error);
else resolve(results);
Expand Down
20 changes: 17 additions & 3 deletions src/api/utils/parties/parties.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ import * as PartiesModel from './parties.model';
import * as PartiesServices from './parties.service';

/**
* get stat by name
* Get User's active party by user ID
*/
export const getStatByName = async (stat: string): Promise<PartiesModel.Stat> => {
return await PartiesServices.getStatByName(stat);
export const getUserParty = async (user_fk: number) : Promise<PartiesModel.Party> => {
return await PartiesServices.getUserPartyById(user_fk);
};

/**
* Insert User's Party - triggers when sorting party roster
*/
export const insertUserParty = async (user_fk: PartiesModel.Party['user_fk'], unit_id: PartiesModel.Party['unit_id']) => {
await PartiesServices.insertUserParty(user_fk, unit_id);
};

/**
* Delete user's party - triggers when sorting party roster
*/
export const deletetUserParty = async (user_fk: PartiesModel.Party['user_fk']) => {
await PartiesServices.deletetUserParty(user_fk);
};
14 changes: 7 additions & 7 deletions src/api/utils/parties/parties.model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export class Stat {
id: number;
stat: string;

constructor(id: number, stat: string) {
this.id = id;
this.stat = stat;
export class Party {
user_fk: number;
unit_id: string;
constructor(user_fk: number, unit_id: string) {
this.user_fk = user_fk;
this.unit_id = unit_id;
}
}
31 changes: 24 additions & 7 deletions src/api/utils/parties/parties.queries.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
export const StatsQueries = {
GetStatByName: `
SELECT
export const PartiesQueries = {
GetUserPartyByUserId:
`
SELECT
*
FROM banner_saga_factions.stats
WHERE stat = ?;
`
};
FROM db.Parties
WHERE
user_fk = ?;
`,

InsertUserParty:
`
INSERT INTO db.Parties
(user_fk, unit_id)
VALUES
(?, ?);
`,

DeleteUserParty:
`
DELETE FROM db.Parties
WHERE
user_fk = ?
`,
};
Loading

0 comments on commit 4b385f2

Please sign in to comment.