feat: Clan creation & persistence #146

Merged
Sainan merged 5 commits from clans into main 2024-05-04 06:42:25 -07:00
4 changed files with 11 additions and 10 deletions
Showing only changes of commit 8bf2551565 - Show all commits

View File

@ -2,7 +2,7 @@ import { RequestHandler } from "express";
import { getJSONfromString } from "@/src/helpers/stringHelpers"; import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import { Guild } from "@/src/models/guildModel"; import { Guild } from "@/src/models/guildModel";
import { IGuild, guildDbToResponse, ICreateGuildRequest } from "@/src/types/guildTypes"; import { IGuild, ICreateGuildRequest } from "@/src/types/guildTypes";
const createGuildController: RequestHandler = async (req, res) => { const createGuildController: RequestHandler = async (req, res) => {
let payload: ICreateGuildRequest = getJSONfromString(req.body.toString()); let payload: ICreateGuildRequest = getJSONfromString(req.body.toString());
@ -29,7 +29,7 @@ const createGuildController: RequestHandler = async (req, res) => {
await inventory.save(); await inventory.save();
} }
res.json(guildDbToResponse(guild)); res.json(guild);
}; };
export { createGuildController }; export { createGuildController };

View File

@ -1,7 +1,6 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import { Guild } from "@/src/models/guildModel"; import { Guild } from "@/src/models/guildModel";
import { guildDbToResponse } from "@/src/types/guildTypes";
const getGuildController: RequestHandler = async (req, res) => { const getGuildController: RequestHandler = async (req, res) => {
if (!req.query.accountId) { if (!req.query.accountId) {
@ -16,7 +15,7 @@ const getGuildController: RequestHandler = async (req, res) => {
if (inventory.GuildId) { if (inventory.GuildId) {
const guild = await Guild.findOne({ _id: inventory.GuildId }); const guild = await Guild.findOne({ _id: inventory.GuildId });
if (guild) { if (guild) {
res.json(guildDbToResponse(guild)); res.json(guild);
return; return;
} }
} }

View File

@ -1,8 +1,16 @@
import { IGuild, IDatabaseGuild } from "@/src/types/guildTypes"; import { IGuild, IDatabaseGuild } from "@/src/types/guildTypes";
import { model, Schema } from "mongoose"; import { model, Schema } from "mongoose";
import { toOid } from "@/src/helpers/inventoryHelpers";
const guildSchema = new Schema<IGuild>({ const guildSchema = new Schema<IGuild>({
Name: { type: String, required: true } Name: { type: String, required: true }
}); });
guildSchema.set("toJSON", {
virtuals: true,
transform(_document, guild) {
guild._id = toOid(guild._id);
}
});
export const Guild = model<IGuild>("Guild", guildSchema); export const Guild = model<IGuild>("Guild", guildSchema);

View File

@ -1,6 +1,5 @@
import { Types } from "mongoose"; import { Types } from "mongoose";
import { IOid } from "@/src/types/commonTypes"; import { IOid } from "@/src/types/commonTypes";
import { toOid } from "@/src/helpers/inventoryHelpers";
export interface IGuild { export interface IGuild {
Name: string; Name: string;
@ -14,11 +13,6 @@ export interface IGuildResponse extends IGuild {
_id: IOid; _id: IOid;
} }
export function guildDbToResponse(guild: IDatabaseGuild): IGuildResponse {
(guild as IGuild as IGuildResponse)._id = toOid(guild._id);
return guild as IGuild as IGuildResponse;
}
export interface ICreateGuildRequest { export interface ICreateGuildRequest {
guildName: string; guildName: string;
} }