improve: Replace guildDbToResponse with ✨✨✨
This commit is contained in:
parent
6c5020ac1b
commit
8bf2551565
@ -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 };
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user