diff --git a/server/NOTICE b/server/notes similarity index 81% rename from server/NOTICE rename to server/notes index 14c57c67bc166ede17cf7bb3f4a6eaeb22f8f45a..3c0dbd2093252cf3696a25fe100f1c6fd8bfef95 100644 --- a/server/NOTICE +++ b/server/notes @@ -28,3 +28,15 @@ boat: created connection in boatHasBoatType table boattype: show boatType by give boatId must be written again. + +----------------- +19.01: +DB Sport BoatHasSport +BoatController +BoatRoutes : (fehlt) überpüfen ob SportsArrayElements UUID sind!!!! +index.ts (init-methods) + + + + + diff --git a/server/src/controllers/boat.controllers.ts b/server/src/controllers/boat.controllers.ts index ed78a21924baa3e75fb527de1fe2170919ad483c..92f13d24d275814b8127e72cd7982f13c6985946 100644 --- a/server/src/controllers/boat.controllers.ts +++ b/server/src/controllers/boat.controllers.ts @@ -1,7 +1,8 @@ import { Request, Response } from "express"; import BoatType from "../db/models/BoatType"; import Boat from "../db/models/Boat"; -import BoatHasBoatType from "../db/models/BoatHasBoatType"; +import Sport from "../db/models/Sport"; +import BoatHasSport from "../db/models/BoatHasSport"; //create boat const createBoat = async (req: Request, res: Response) => { @@ -21,29 +22,41 @@ const createBoat = async (req: Request, res: Response) => { .json({ success: false, error: "boattypeNotFound" }); } - const newBoat = await Boat.create(newBoatInput); - - //nachdem das Boot erstellt wurde: - //boatTypeTabelle: boattype-id - //beim Erstellen von neuem Boot - //boatTypeId: eingabe - const boattypeid = boatType.id; + //sport array of ids + const sportsArray = newBoatInput.sports; + //check wether each given id exists or not + for (let i = 0; i < sportsArray.length; i++) { + const found = await Sport.findByPk(sportsArray[i]); + if (!found) + return res + .status(404) + .json({ success: false, error: sportsArray[i] + " notFound" }); + } + + const newBoat = await Boat.create(newBoatInput); // create the boat + const boatid = newBoat.id; - const entry = { boatid, boattypeid }; - - //create new entry in boatHasBoatType - await BoatHasBoatType.create(entry); - + + //create entry (boatid, each id of given sportIds) + for (let i = 0; i < sportsArray.length; i++) { + const sportid = sportsArray[i]; + const entry = { boatid, sportid }; + //create new entry in boatHasBoatType + await BoatHasSport.create(entry); + } + if (newBoat) { return res.status(201).json({ success: true, result: { id: newBoat.id, name: newBoat.name, + boattype: newBoat.boattype, status: newBoat.status, tags: newBoat.tags, minP: newBoat.minP, maxP: newBoat.maxP, + sports: newBoat.sports, }, }); } @@ -63,10 +76,12 @@ const showAllBoatsController = async (req: Request, res: Response) => { return { id: boat.id, name: boat.name, + boattype: boat.boattype, status: boat.status, tags: boat.tags, minP: boat.minP, maxP: boat.maxP, + sports: boat.sports, }; }), }); @@ -85,11 +100,14 @@ const showBoatById = async (req: Request, res: Response) => { return res.status(200).json({ success: true, result: { + id: boat.id, name: boat.name, + boattype: boat.boattype, status: boat.status, tags: boat.tags, minP: boat.minP, maxP: boat.maxP, + sports: boat.sports, }, }); } @@ -156,6 +174,24 @@ const updateBoatById = async (req: Request, res: Response) => { return res.status(404).json({ success: false, error: "boatIdNotFound" }); } + //check if new boattype can be found + if (!(await BoatType.findByPk(input.boattype))) { + return res + .status(404) + .json({ success: false, error: "givenBoatTypeIdNotFound" }); + } + + //check if new given sport-ids can be found + const newSportsArray = input.sports; + //check wether each new given id exists or not + for (let i = 0; i < newSportsArray.length; i++) { + const found = await Sport.findByPk(newSportsArray[i]); + if (!found) + return res + .status(404) + .json({ success: false, error: newSportsArray[i] + " notFound" }); + } + //try to update const updatedBoat = await Boat.update(input, { where: { @@ -171,10 +207,12 @@ const updateBoatById = async (req: Request, res: Response) => { result: { id: boatDataAfterUpdate.id, name: boatDataAfterUpdate.name, + boattype: boatDataAfterUpdate.boattype, status: boatDataAfterUpdate.status, tags: boatDataAfterUpdate.tags, minP: boatDataAfterUpdate.minP, maxP: boatDataAfterUpdate.maxP, + sports: boatDataAfterUpdate.sports, }, }); } catch (error) { @@ -183,8 +221,6 @@ const updateBoatById = async (req: Request, res: Response) => { } }; -const createBoatTypeForBoat = async (req: Request, res: Response) => {}; - const boatControllers = { showAllBoatsController, showBoatById, diff --git a/server/src/controllers/boatType.controllers.ts b/server/src/controllers/boatType.controllers.ts index b42f8d45739a5f286a7f7576064c85af107a7520..d589a24ca7a502606d1718c288362d27c1a00405 100644 --- a/server/src/controllers/boatType.controllers.ts +++ b/server/src/controllers/boatType.controllers.ts @@ -1,7 +1,6 @@ import { Request, Response } from "express"; import BoatType from "../db/models/BoatType"; import Boat from "../db/models/Boat"; -import BoatHasBoatType from "../db/models/BoatHasBoatType"; //createBoatTypeController const createBoatTypeController = async (req: Request, res: Response) => { @@ -88,22 +87,9 @@ const showBoatTypeById = async (req: Request, res: Response) => { } const givenId = req.params.id; const boat = await Boat.findByPk(givenId); - if (boat) { - - //find if boatid exists in BoatHasBoatType table => if not then boat doesn't have assigned boattype - const boatid = await BoatHasBoatType.findByPk(boat.id); - if (!boatid) { - return res.status(404).json({ success: false, error: "boatIdNotFoundInBoatHasBoatIdTable" }); - } - - //get boattypeid from BoatHasBoatType table which is assigned to boatid - const boattypeid = boatid.boattypeid; - - //find this key in the BoatType table + const boattypeid = boat.boattype; const boatname = await BoatType.findByPk(boattypeid); - - //return the type/name of the given boatid return res .status(200) .json({ success: true, result: { name: boatname.name } }); diff --git a/server/src/db/index.ts b/server/src/db/index.ts index fd7d1f9fd8b31b655704021a6f70ab4ac656b8a7..26a2785b1959ebc2204bd119aa305656a8a5134c 100644 --- a/server/src/db/index.ts +++ b/server/src/db/index.ts @@ -2,10 +2,11 @@ import { Client } from "pg"; import { Dialect, Sequelize } from "sequelize"; import envVars from "../config"; import { initBoat } from "./models/Boat"; -import { initBoatHasBoatType } from "./models/BoatHasBoatType"; import { initBoatType } from "./models/BoatType"; import { initCheckIn } from "./models/CheckIn"; import { initEmployee } from "./models/Employee"; +import { initBoatHasSport } from "./models/BoatHasSport"; +import { initSport } from "./models/Sport"; const initializeDatabase = async () => { const client = new Client({ @@ -42,7 +43,8 @@ const initializeDatabase = async () => { await initBoatType(sequelize); await initCheckIn(sequelize); await initEmployee(sequelize); - await initBoatHasBoatType(sequelize); + await initBoatHasSport(sequelize); + await initSport(sequelize); await sequelize.sync(); return sequelize; diff --git a/server/src/db/models/Boat.ts b/server/src/db/models/Boat.ts index 241872dbf55e39b4cf1e96454eac7b32dc3d47b2..c0dc8fd166ad0e2d3ba959bed18bbec83eb9459d 100644 --- a/server/src/db/models/Boat.ts +++ b/server/src/db/models/Boat.ts @@ -2,22 +2,29 @@ import { Sequelize, DataTypes, Model, Optional } from "sequelize"; interface BoatAttributes { id: string; + boattype: string; name: string; status: boolean; tags: Array<string>; minP: number; maxP: number; + sports: Array<string>; } -export interface BoatAttributesInput extends Optional<BoatAttributes, "id"> { } +export interface BoatAttributesInput extends Optional<BoatAttributes, "id"> {} -class Boat extends Model<BoatAttributes, BoatAttributesInput> implements BoatAttributes { +class Boat + extends Model<BoatAttributes, BoatAttributesInput> + implements BoatAttributes +{ declare id: string; + declare boattype: string; declare minP: number; declare maxP: number; declare name: string; declare status: boolean; declare tags: Array<string>; + declare sports: Array<string>; declare readonly createdAt: Date; declare readonly updatedAt: Date; @@ -25,38 +32,52 @@ class Boat extends Model<BoatAttributes, BoatAttributesInput> implements BoatAtt export const initBoat = async (sequelizeConnection: Sequelize) => { Boat.init( - { - id: { - type: DataTypes.UUID, - defaultValue: DataTypes.UUIDV4, - primaryKey: true, - allowNull: false, + { + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + allowNull: false, + }, + name: { + type: new DataTypes.STRING(), + allowNull: false, + }, + boattype: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + allowNull: false, + references: { + model: "boattype", + key: "id", + }, + }, + status: { + type: DataTypes.BOOLEAN, + allowNull: false, + }, + tags: { + type: new DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + }, + minP: { + type: DataTypes.INTEGER, + allowNull: false, + }, + maxP: { + type: DataTypes.INTEGER, + allowNull: false, + }, + sports: { + type: DataTypes.ARRAY(DataTypes.STRING), + allowNull: true, + }, }, - name: { - type: new DataTypes.STRING(), - allowNull: false, - }, - status: { - type: DataTypes.BOOLEAN, - allowNull: false, - }, - tags: { - type: new DataTypes.ARRAY(DataTypes.STRING), - }, - minP:{ - type: DataTypes.INTEGER, - allowNull: false, - }, - maxP:{ - type: DataTypes.INTEGER, - allowNull: false, + { + tableName: "boat", + sequelize: sequelizeConnection, } - }, - { - tableName: "boat", - sequelize: sequelizeConnection, - } -); + ); }; export default Boat; diff --git a/server/src/db/models/BoatHasBoatType.ts b/server/src/db/models/BoatHasSport.ts similarity index 50% rename from server/src/db/models/BoatHasBoatType.ts rename to server/src/db/models/BoatHasSport.ts index 78cdc24feb78a47fd578a9ae6304ed8390b32e6e..3198924d193c3fb044dfd4ee3f957864cf4af13f 100644 --- a/server/src/db/models/BoatHasBoatType.ts +++ b/server/src/db/models/BoatHasSport.ts @@ -1,26 +1,26 @@ import { Sequelize, DataTypes, Model, Optional } from "sequelize"; -interface BoatHasBoatTypeAttributes { +interface BoatHasSportAttributes { boatid: string; - boattypeid: string; + sportid: string; } -export interface BoatHasBoatTypeAttributesInput - extends Optional<BoatHasBoatTypeAttributes, "boatid"|"boattypeid"> {} // optional +export interface BoatHasSportAttributesInput + extends Optional<BoatHasSportAttributes, "boatid"|"sportid"> {} // optional -class BoatHasBoatType - extends Model<BoatHasBoatTypeAttributes, BoatHasBoatTypeAttributesInput> - implements BoatHasBoatTypeAttributes +class BoatHasSport + extends Model<BoatHasSportAttributes, BoatHasSportAttributesInput> + implements BoatHasSportAttributes { declare boatid: string; - declare boattypeid: string; + declare sportid: string; declare readonly createdAt: Date; declare readonly updatedAt: Date; } -export const initBoatHasBoatType = async (sequelizeConnection: Sequelize) => { - BoatHasBoatType.init( +export const initBoatHasSport = async (sequelizeConnection: Sequelize) => { + BoatHasSport.init( { boatid: { type: DataTypes.UUID, @@ -31,22 +31,22 @@ export const initBoatHasBoatType = async (sequelizeConnection: Sequelize) => { key: "id", }, }, - boattypeid: { + sportid: { type: DataTypes.UUID, allowNull: false, primaryKey: true, references: { - model: "boattype", + model: "sport", key: "id", }, }, }, { - tableName: "boathasboattype", + tableName: "boathassport", sequelize: sequelizeConnection, } ); }; -export default BoatHasBoatType; +export default BoatHasSport; diff --git a/server/src/db/models/Sport.ts b/server/src/db/models/Sport.ts new file mode 100644 index 0000000000000000000000000000000000000000..a223eb7d7b9143578f94e7510486f8f8b523a20a --- /dev/null +++ b/server/src/db/models/Sport.ts @@ -0,0 +1,46 @@ +import { Sequelize, DataTypes, Model, Optional } from "sequelize"; + +interface SportAttributes { + id: string; + name: string; + color: string; +} +export interface SportInput extends Optional<SportAttributes, "id"> {} + +class Sport + extends Model<SportAttributes, SportInput> + implements SportAttributes +{ + declare id: string; + declare name: string; + declare color: string; + + declare readonly createdAt: Date; + declare readonly updatedAt: Date; +} + +export const initSport = async (sequelizeConnection: Sequelize) => { + Sport.init( + { + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + allowNull: false, + }, + name: { + type: new DataTypes.STRING(), + allowNull: false, + }, + color: { + type: new DataTypes.STRING(), + allowNull: true, + }, + }, + { + tableName: "sport", + sequelize: sequelizeConnection, + } + ); +}; +export default Sport; diff --git a/server/src/routes/boat.routes.ts b/server/src/routes/boat.routes.ts index f123d3410970df616cf30881fb36bcfe4b5e7006..3615a1134585520c1bbe43597c20916dcb019899 100644 --- a/server/src/routes/boat.routes.ts +++ b/server/src/routes/boat.routes.ts @@ -28,6 +28,7 @@ boatsRouter.post( body("tags").if(body("tags").exists()).isArray(), //optional tags field body("minP").isInt(), body("maxP").isInt(), + body("sports").if(body("sports").exists()).isArray().isUUID(), //optional handleValidationResult, validateToken, boatControllers.createBoat @@ -42,10 +43,10 @@ boatsRouter.patch( body("tags").if(body("tags").exists()).isArray(), body("minP").if(body("minP").exists()).isInt(), body("maxP").if(body("maxP").exists()).isInt(), + body("sports").if(body("sports").exists()).isArray().isUUID(), //optional handleValidationResult, validateToken, boatControllers.updateBoatById -) - +); export default boatsRouter;