From 06ccdab9ec14aa5cb720788a5c0ba11b0eb7f464 Mon Sep 17 00:00:00 2001 From: joonashak Date: Thu, 19 Sep 2024 22:31:43 +0300 Subject: [PATCH] Implement creating maps --- server/src/entities/map/dto/create-map.dto.ts | 10 +++++++ server/src/entities/map/dto/find-map.dto.ts | 5 ++++ server/src/entities/map/map.model.ts | 11 +++++++- server/src/entities/map/map.module.ts | 4 ++- server/src/entities/map/map.resolver.ts | 28 +++++++++++++++++++ server/src/entities/map/map.service.ts | 13 ++++++++- 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 server/src/entities/map/dto/create-map.dto.ts create mode 100644 server/src/entities/map/dto/find-map.dto.ts create mode 100644 server/src/entities/map/map.resolver.ts diff --git a/server/src/entities/map/dto/create-map.dto.ts b/server/src/entities/map/dto/create-map.dto.ts new file mode 100644 index 00000000..b3c1fdf9 --- /dev/null +++ b/server/src/entities/map/dto/create-map.dto.ts @@ -0,0 +1,10 @@ +import { Field, InputType } from "@nestjs/graphql"; + +@InputType() +export class CreateMapDto { + @Field() + name: string; + + @Field() + rootSystemName: string; +} diff --git a/server/src/entities/map/dto/find-map.dto.ts b/server/src/entities/map/dto/find-map.dto.ts new file mode 100644 index 00000000..feb3bb1e --- /dev/null +++ b/server/src/entities/map/dto/find-map.dto.ts @@ -0,0 +1,5 @@ +import { ObjectType, OmitType } from "@nestjs/graphql"; +import { Map } from "../map.model"; + +@ObjectType() +export class FindMap extends OmitType(Map, ["user"]) {} diff --git a/server/src/entities/map/map.model.ts b/server/src/entities/map/map.model.ts index 20451c8c..00746b00 100644 --- a/server/src/entities/map/map.model.ts +++ b/server/src/entities/map/map.model.ts @@ -1,6 +1,7 @@ +import { User } from "@joonashak/nestjs-clone-bay"; import { Field, ObjectType } from "@nestjs/graphql"; import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; -import { Document } from "mongoose"; +import mongoose, { Document } from "mongoose"; @Schema() @ObjectType() @@ -8,6 +9,14 @@ export class Map { @Field() id: string; + @Field(() => User) + @Prop({ + type: mongoose.Schema.Types.ObjectId, + ref: User.name, + required: true, + }) + user: User; + @Field() @Prop() name: string; diff --git a/server/src/entities/map/map.module.ts b/server/src/entities/map/map.module.ts index 94884a88..1335ac56 100644 --- a/server/src/entities/map/map.module.ts +++ b/server/src/entities/map/map.module.ts @@ -1,11 +1,13 @@ +import { CloneBayUserService } from "@joonashak/nestjs-clone-bay"; import { Module } from "@nestjs/common"; import { MongooseModule } from "@nestjs/mongoose"; import { Map, MapSchema } from "./map.model"; +import { MapResolver } from "./map.resolver"; import { MapService } from "./map.service"; @Module({ imports: [MongooseModule.forFeature([{ name: Map.name, schema: MapSchema }])], - providers: [MapService], + providers: [MapService, MapResolver, CloneBayUserService], exports: [MongooseModule, MapService], }) export class MapModule {} diff --git a/server/src/entities/map/map.resolver.ts b/server/src/entities/map/map.resolver.ts new file mode 100644 index 00000000..08b6a5aa --- /dev/null +++ b/server/src/entities/map/map.resolver.ts @@ -0,0 +1,28 @@ +import { + CloneBayUserService, + RequireAuthentication, + UserId, +} from "@joonashak/nestjs-clone-bay"; +import { Args, Mutation, Resolver } from "@nestjs/graphql"; +import { CreateMapDto } from "./dto/create-map.dto"; +import { FindMap } from "./dto/find-map.dto"; +import { Map } from "./map.model"; +import { MapService } from "./map.service"; + +@Resolver() +export class MapResolver { + constructor( + private mapService: MapService, + private userService: CloneBayUserService, + ) {} + + @RequireAuthentication() + @Mutation(() => FindMap) + async createMap( + @Args("input") input: CreateMapDto, + @UserId() userId: string, + ): Promise { + const user = await this.userService.findById(userId); + return this.mapService.createMap(input, user); + } +} diff --git a/server/src/entities/map/map.service.ts b/server/src/entities/map/map.service.ts index 6d9f25d6..179da837 100644 --- a/server/src/entities/map/map.service.ts +++ b/server/src/entities/map/map.service.ts @@ -1,4 +1,15 @@ +import { User } from "@joonashak/nestjs-clone-bay"; import { Injectable } from "@nestjs/common"; +import { InjectModel } from "@nestjs/mongoose"; +import { Model } from "mongoose"; +import { CreateMapDto } from "./dto/create-map.dto"; +import { Map, MapDocument } from "./map.model"; @Injectable() -export class MapService {} +export class MapService { + constructor(@InjectModel(Map.name) private mapModel: Model) {} + + async createMap(input: CreateMapDto, user: User): Promise { + return this.mapModel.create({ ...input, user }); + } +}