Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
feat: update announcements modules
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Dec 26, 2019
1 parent a2b66f5 commit 0f5ab25
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/announcements/announcements.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { AnnouncementsResolver } from './announcements.resolver'
import { AnnouncementsService } from './announcements.service'
import { AnnouncementSchema } from './announcements.schema'
import { AnnouncementSchema } from './schemas/announcements.schema'

@Module({
imports: [MongooseModule.forFeature([{ name: 'Announcement', schema: AnnouncementSchema }])],
Expand Down
30 changes: 14 additions & 16 deletions src/announcements/announcements.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ID } from 'type-graphql'
// import { PubSub } from 'apollo-server-express'
// import { RecipesArgs } from './dtos/recipes.args'
import { AnnouncementsService } from './announcements.service'
import { AnnouncementsModel } from './dtos/announcements.model'
import { AnnouncementInput } from './dtos/announcement.input'
import { BatchDelete } from '../database/interfaces/batchDelete.interface'
import { AnnouncementsModel } from './models/announcements.model'
import { CreateAnnouncementInput } from './dtos/create-announcement.input'
import { UpdateAnnouncementInput } from './dtos/update-announcement.input'
import { BatchDeleteModel } from '../database/models/database.model'

// const pubSub = new PubSub();

Expand All @@ -15,44 +16,41 @@ export class AnnouncementsResolver {
this.announcementsService = announcementsService
}

@Query(() => [AnnouncementsModel], { name: 'announcements' })
@Query(() => [AnnouncementsModel])
public async getAnnouncements(): Promise<AnnouncementsModel[]> {
return this.announcementsService.findAll()
}

@Query(() => AnnouncementsModel, { name: 'announcement' })
@Query(() => AnnouncementsModel)
public async getAnnouncementById(
@Args({ name: 'id', type: () => ID }) id: string,
): Promise<AnnouncementsModel> {
return this.announcementsService.findOneById(id)
}

@Mutation(() => AnnouncementsModel, { name: 'createAnnouncement' })
@Mutation(() => AnnouncementsModel)
public async createAnnouncement(
@Args('input') input: AnnouncementInput,
@Args('input') input: CreateAnnouncementInput,
): Promise<AnnouncementsModel> {
return this.announcementsService.create(input)
}

@Mutation(() => AnnouncementsModel, { name: 'updateAnnouncement' })
@Mutation(() => AnnouncementsModel)
public async updateAnnouncementById(
@Args({ name: 'id', type: () => ID }) id: string,
@Args('announcement') announcement: string,
@Args('input') input: UpdateAnnouncementInput,
): Promise<AnnouncementsModel> {
return this.announcementsService.update(id, announcement)
return this.announcementsService.update(input)
}

@Mutation(() => AnnouncementsModel, { name: 'deleteAnnouncement' })
@Mutation(() => AnnouncementsModel)
public async deleteAnnouncementById(
@Args({ name: 'id', type: () => ID }) id: string,
): Promise<AnnouncementsModel> {
return this.announcementsService.deleteOneById(id)
}

@Mutation(() => AnnouncementsModel, { name: 'deleteAnnouncements' })
public async deleteAnnouncements(
@Args({ name: 'ids', type: () => [ID] }) ids: string[],
): Promise<BatchDelete> {
@Mutation(() => BatchDeleteModel)
public async deleteAnnouncements(@Args({ name: 'ids', type: () => [ID] }) ids: string[]) {
return this.announcementsService.batchDelete(ids)
}

Expand Down
38 changes: 19 additions & 19 deletions src/announcements/announcements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@ import { Injectable } from '@nestjs/common'
import { Model } from 'mongoose'
import { InjectModel } from '@nestjs/mongoose'
import { Announcement } from './interfaces/announcement.interface'
import { AnnouncementInput } from './dtos/announcement.input'
import { CreateAnnouncementInput } from './dtos/create-announcement.input'
import { UpdateAnnouncementInput } from './dtos/update-announcement.input'

@Injectable()
export class AnnouncementsService {
constructor(
@InjectModel('Announcement')
private readonly AnnouncementModel: Model<Announcement>,
private readonly announcementModel: Model<Announcement>,
) {
this.AnnouncementModel = AnnouncementModel
this.announcementModel = announcementModel
}

public async findAll() {
const res = await this.AnnouncementModel.find({}).sort({ updatedAt: -1 })
return res
return this.announcementModel.find({}).sort({ updatedAt: -1 })
}

public async findOneById(id: string) {
const res = await this.AnnouncementModel.findById(id)
return res
return this.announcementModel.findById(id)
}

public async create(dto: AnnouncementInput) {
const res = await this.AnnouncementModel.create(dto)
return res
public async create(dto: CreateAnnouncementInput) {
return this.announcementModel.create(dto)
}

public async update(id: string, announcement: string) {
const res = await this.AnnouncementModel.findByIdAndUpdate(id, {
announcement,
})
return res
public async update(dto: UpdateAnnouncementInput) {
const { id, content } = dto
return this.announcementModel.findByIdAndUpdate(
id,
{
content,
},
{ new: true },
)
}

public async deleteOneById(id: string) {
const res = await this.AnnouncementModel.findByIdAndDelete(id)
return res
return this.announcementModel.findByIdAndDelete(id)
}

public async batchDelete(ids: string[]) {
const res = await this.AnnouncementModel.deleteMany({
return this.announcementModel.deleteMany({
_id: { $in: ids },
})
return res
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { InputType, Field } from 'type-graphql'
import { IsString, IsNotEmpty } from 'class-validator'

@InputType()
export class AnnouncementInput {
export class CreateAnnouncementInput {
@Field()
@IsString()
@IsNotEmpty()
public readonly announcement: string
public readonly content: string
}
15 changes: 15 additions & 0 deletions src/announcements/dtos/update-announcement.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InputType, Field } from 'type-graphql'
import { IsString, IsNotEmpty } from 'class-validator'

@InputType()
export class UpdateAnnouncementInput {
@Field()
@IsString()
@IsNotEmpty()
public readonly id: string

@Field()
@IsString()
@IsNotEmpty()
public readonly content: string
}

0 comments on commit 0f5ab25

Please sign in to comment.