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 { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
			
		||||
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) => {
 | 
			
		||||
    let payload: ICreateGuildRequest = getJSONfromString(req.body.toString());
 | 
			
		||||
@ -29,7 +29,7 @@ const createGuildController: RequestHandler = async (req, res) => {
 | 
			
		||||
        await inventory.save();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res.json(guildDbToResponse(guild));
 | 
			
		||||
    res.json(guild);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export { createGuildController };
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,6 @@
 | 
			
		||||
import { RequestHandler } from "express";
 | 
			
		||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
			
		||||
import { Guild } from "@/src/models/guildModel";
 | 
			
		||||
import { guildDbToResponse } from "@/src/types/guildTypes";
 | 
			
		||||
 | 
			
		||||
const getGuildController: RequestHandler = async (req, res) => {
 | 
			
		||||
    if (!req.query.accountId) {
 | 
			
		||||
@ -16,7 +15,7 @@ const getGuildController: RequestHandler = async (req, res) => {
 | 
			
		||||
    if (inventory.GuildId) {
 | 
			
		||||
        const guild = await Guild.findOne({ _id: inventory.GuildId });
 | 
			
		||||
        if (guild) {
 | 
			
		||||
            res.json(guildDbToResponse(guild));
 | 
			
		||||
            res.json(guild);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,16 @@
 | 
			
		||||
import { IGuild, IDatabaseGuild } from "@/src/types/guildTypes";
 | 
			
		||||
import { model, Schema } from "mongoose";
 | 
			
		||||
import { toOid } from "@/src/helpers/inventoryHelpers";
 | 
			
		||||
 | 
			
		||||
const guildSchema = new Schema<IGuild>({
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,5 @@
 | 
			
		||||
import { Types } from "mongoose";
 | 
			
		||||
import { IOid } from "@/src/types/commonTypes";
 | 
			
		||||
import { toOid } from "@/src/helpers/inventoryHelpers";
 | 
			
		||||
 | 
			
		||||
export interface IGuild {
 | 
			
		||||
    Name: string;
 | 
			
		||||
@ -14,11 +13,6 @@ export interface IGuildResponse extends IGuild {
 | 
			
		||||
    _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 {
 | 
			
		||||
    guildName: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user