SpaceNinjaServer/src/services/shipService.ts

32 lines
1007 B
TypeScript
Raw Normal View History

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"
): Promise<Types.ObjectId> => {
2023-06-05 04:16:49 +08:00
try {
const ship = new Ship({
2024-12-23 09:15:41 +01:00
ItemType: typeName,
ShipOwnerId: accountOwnerId
});
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
}
};
export const getShip = async (shipId: Types.ObjectId, fieldSelection: string = ""): Promise<TShipDatabaseDocument> => {
const ship = await Ship.findOne({ _id: shipId }, fieldSelection);
if (!ship) {
throw new Error(`error finding a ship with id ${shipId.toString()}`);
}
return ship;
};