2025-03-15 03:24:39 -07:00
|
|
|
import { Ship, TShipDatabaseDocument } from "@/src/models/shipModel";
|
2023-06-05 04:16:49 +08:00
|
|
|
import { Types } from "mongoose";
|
|
|
|
|
2024-12-23 09:15:41 +01:00
|
|
|
export const createShip = async (
|
|
|
|
accountOwnerId: Types.ObjectId,
|
|
|
|
typeName: string = "/Lotus/Types/Items/Ships/DefaultShip"
|
2025-03-15 03:24:39 -07:00
|
|
|
): Promise<Types.ObjectId> => {
|
2023-06-05 04:16:49 +08:00
|
|
|
try {
|
2023-12-14 17:34:15 +01:00
|
|
|
const ship = new Ship({
|
2024-12-23 09:15:41 +01:00
|
|
|
ItemType: typeName,
|
2024-02-18 13:58:43 +01:00
|
|
|
ShipOwnerId: accountOwnerId
|
2023-12-14 17:34:15 +01:00
|
|
|
});
|
2024-02-18 13:58:43 +01:00
|
|
|
const newShip = await ship.save();
|
|
|
|
return newShip._id;
|
2023-06-05 04:16:49 +08:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
2023-06-05 00:17:01 +02:00
|
|
|
throw new Error(`error creating ship" ${error.message}`);
|
2023-06-05 04:16:49 +08:00
|
|
|
}
|
2023-06-05 00:17:01 +02:00
|
|
|
throw new Error("error creating ship that is not of instance Error");
|
2023-06-05 04:16:49 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-15 03:24:39 -07:00
|
|
|
export const getShip = async (shipId: Types.ObjectId, fieldSelection: string = ""): Promise<TShipDatabaseDocument> => {
|
2024-02-18 13:58:43 +01:00
|
|
|
const ship = await Ship.findOne({ _id: shipId }, fieldSelection);
|
|
|
|
|
|
|
|
if (!ship) {
|
2024-12-29 06:00:51 +01:00
|
|
|
throw new Error(`error finding a ship with id ${shipId.toString()}`);
|
2024-02-18 13:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ship;
|
|
|
|
};
|