From aa3080f9ab801c33ea2a4fdbf9230cb55ce67a6a Mon Sep 17 00:00:00 2001 From: Daniel Jancar Date: Fri, 19 Jul 2024 18:30:21 +0200 Subject: [PATCH] feat: create votes and cast logic without smco yet --- .gitignore | 1 + apps/backend/src/app/app.controller.ts | 10 +++---- apps/backend/src/app/app.module.ts | 26 +++++++++++++++---- apps/backend/src/app/app.service.ts | 4 +-- apps/backend/src/app/casts/cast.controller.ts | 18 +++++++++++++ apps/backend/src/app/casts/cast.module.ts | 12 +++++++++ apps/backend/src/app/casts/cast.service.ts | 19 ++++++++++++++ .../src/app/casts/dto/create-cast.dto.ts | 5 ++++ .../src/app/casts/schemas/casts.schema.ts | 19 ++++++++++++++ .../src/app/votes/dto/create-vote.dto.ts | 6 +++++ .../src/app/votes/schemas/vote.schema.ts | 22 ++++++++++++++++ .../backend/src/app/votes/votes.controller.ts | 23 ++++++++++++++++ apps/backend/src/app/votes/votes.module.ts | 12 +++++++++ apps/backend/src/app/votes/votes.service.ts | 23 ++++++++++++++++ nx.json | 1 - 15 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 apps/backend/src/app/casts/cast.controller.ts create mode 100644 apps/backend/src/app/casts/cast.module.ts create mode 100644 apps/backend/src/app/casts/cast.service.ts create mode 100644 apps/backend/src/app/casts/dto/create-cast.dto.ts create mode 100644 apps/backend/src/app/casts/schemas/casts.schema.ts create mode 100644 apps/backend/src/app/votes/dto/create-vote.dto.ts create mode 100644 apps/backend/src/app/votes/schemas/vote.schema.ts create mode 100644 apps/backend/src/app/votes/votes.controller.ts create mode 100644 apps/backend/src/app/votes/votes.module.ts create mode 100644 apps/backend/src/app/votes/votes.service.ts diff --git a/.gitignore b/.gitignore index b368b80..5419c68 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ Thumbs.db .nx/workspace-data .angular +.env diff --git a/apps/backend/src/app/app.controller.ts b/apps/backend/src/app/app.controller.ts index dff210a..b9486a5 100644 --- a/apps/backend/src/app/app.controller.ts +++ b/apps/backend/src/app/app.controller.ts @@ -1,13 +1,9 @@ -import { Controller, Get } from '@nestjs/common'; +import {Controller, Get} from '@nestjs/common'; -import { AppService } from './app.service'; +import {AppService} from './app.service'; @Controller() export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getData() { - return this.appService.getData(); + constructor(private readonly appService: AppService) { } } diff --git a/apps/backend/src/app/app.module.ts b/apps/backend/src/app/app.module.ts index 6a9bc16..9037510 100644 --- a/apps/backend/src/app/app.module.ts +++ b/apps/backend/src/app/app.module.ts @@ -1,11 +1,27 @@ -import { Module } from '@nestjs/common'; +import {Module} from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; +import {AppController} from './app.controller'; +import {AppService} from './app.service'; +import {ConfigModule, ConfigService} from "@nestjs/config"; +import {MongooseModule} from "@nestjs/mongoose"; +import {VotesModule} from "./votes/votes.module"; +import {CastModule} from "./casts/cast.module"; @Module({ - imports: [], + imports: [ + ConfigModule.forRoot(), + MongooseModule.forRootAsync({ + imports: [ConfigModule], + useFactory: async (configService: ConfigService) => ({ + uri: configService.get('MONGODB_URI'), + }), + inject: [ConfigService], + }), + VotesModule, + CastModule + ], controllers: [AppController], providers: [AppService], }) -export class AppModule {} +export class AppModule { +} diff --git a/apps/backend/src/app/app.service.ts b/apps/backend/src/app/app.service.ts index cd8cede..519a5e3 100644 --- a/apps/backend/src/app/app.service.ts +++ b/apps/backend/src/app/app.service.ts @@ -2,7 +2,5 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { - getData(): { message: string } { - return { message: 'Hello API' }; - } + } diff --git a/apps/backend/src/app/casts/cast.controller.ts b/apps/backend/src/app/casts/cast.controller.ts new file mode 100644 index 0000000..ede06a8 --- /dev/null +++ b/apps/backend/src/app/casts/cast.controller.ts @@ -0,0 +1,18 @@ +import { Controller, Post, Body, Param, Get } from '@nestjs/common'; +import {CastService} from "./cast.service"; +import {CreateCastDto} from "./dto/create-cast.dto"; + +@Controller('casts') +export class CastController { + constructor(private readonly castService: CastService) {} + + @Post() + async create(@Body() createCastDto: CreateCastDto) { + return this.castService.create(createCastDto); + } + + @Get(':voteId') + async findByVoteId(@Param('voteId') voteId: string) { + return this.castService.findByVoteId(voteId); + } +} diff --git a/apps/backend/src/app/casts/cast.module.ts b/apps/backend/src/app/casts/cast.module.ts new file mode 100644 index 0000000..83883f8 --- /dev/null +++ b/apps/backend/src/app/casts/cast.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { MongooseModule } from '@nestjs/mongoose'; +import {Cast, CastSchema} from "./schemas/casts.schema"; +import {CastController} from "./cast.controller"; +import {CastService} from "./cast.service"; + +@Module({ + imports: [MongooseModule.forFeature([{ name: Cast.name, schema: CastSchema }])], + controllers: [CastController], + providers: [CastService], +}) +export class CastModule {} diff --git a/apps/backend/src/app/casts/cast.service.ts b/apps/backend/src/app/casts/cast.service.ts new file mode 100644 index 0000000..e4863a9 --- /dev/null +++ b/apps/backend/src/app/casts/cast.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import {Cast} from "./schemas/casts.schema"; +import {CreateCastDto} from "./dto/create-cast.dto"; + +@Injectable() +export class CastService { + constructor(@InjectModel(Cast.name) private CastModel: Model) {} + + async create(createCastDto: CreateCastDto): Promise { + const createdCast = new this.CastModel(createCastDto); + return createdCast.save(); + } + + async findByVoteId(voteId: string): Promise { + return this.CastModel.find({ voteId }).exec(); + } +} diff --git a/apps/backend/src/app/casts/dto/create-cast.dto.ts b/apps/backend/src/app/casts/dto/create-cast.dto.ts new file mode 100644 index 0000000..844f9ab --- /dev/null +++ b/apps/backend/src/app/casts/dto/create-cast.dto.ts @@ -0,0 +1,5 @@ +export class CreateCastDto { + voteId: string; + selectedOption: string; + voterPublicKey: string; +} diff --git a/apps/backend/src/app/casts/schemas/casts.schema.ts b/apps/backend/src/app/casts/schemas/casts.schema.ts new file mode 100644 index 0000000..56bec5f --- /dev/null +++ b/apps/backend/src/app/casts/schemas/casts.schema.ts @@ -0,0 +1,19 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Document, Schema as MongooseSchema } from 'mongoose'; + +@Schema() +export class Cast extends Document { + @Prop({ type: MongooseSchema.Types.ObjectId, ref: 'Vote', required: true }) + voteId: string; + + @Prop({ required: true }) + selectedOption: string; + + @Prop({ required: true }) + voterPublicKey: string; // Public key of the voter's wallet + + @Prop({ default: Date.now }) + createdAt: Date; +} + +export const CastSchema = SchemaFactory.createForClass(Cast); diff --git a/apps/backend/src/app/votes/dto/create-vote.dto.ts b/apps/backend/src/app/votes/dto/create-vote.dto.ts new file mode 100644 index 0000000..c601eed --- /dev/null +++ b/apps/backend/src/app/votes/dto/create-vote.dto.ts @@ -0,0 +1,6 @@ +export class CreateVoteDto { + title: string; + description: string; + options: string[]; + createdBy: string; +} diff --git a/apps/backend/src/app/votes/schemas/vote.schema.ts b/apps/backend/src/app/votes/schemas/vote.schema.ts new file mode 100644 index 0000000..ef97e8d --- /dev/null +++ b/apps/backend/src/app/votes/schemas/vote.schema.ts @@ -0,0 +1,22 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + +@Schema() +export class Vote extends Document { + @Prop({ required: true }) + title: string; + + @Prop({ required: true }) + description: string; + + @Prop({ required: true }) + options: string[]; + + @Prop({ default: Date.now }) + createdAt: Date; + + @Prop({ required: true }) + createdBy: string; // Public key of the creator's wallet +} + +export const VoteSchema = SchemaFactory.createForClass(Vote); diff --git a/apps/backend/src/app/votes/votes.controller.ts b/apps/backend/src/app/votes/votes.controller.ts new file mode 100644 index 0000000..20b4482 --- /dev/null +++ b/apps/backend/src/app/votes/votes.controller.ts @@ -0,0 +1,23 @@ +import { Controller, Get, Post, Body, Param } from '@nestjs/common'; +import { VotesService } from './votes.service'; +import { CreateVoteDto } from './dto/create-vote.dto'; + +@Controller('votes') +export class VotesController { + constructor(private readonly votesService: VotesService) {} + + @Post() + async create(@Body() createVoteDto: CreateVoteDto) { + return this.votesService.create(createVoteDto); + } + + @Get() + async findAll() { + return this.votesService.findAll(); + } + + @Get(':id') + async findOne(@Param('id') id: string) { + return this.votesService.findOne(id); + } +} diff --git a/apps/backend/src/app/votes/votes.module.ts b/apps/backend/src/app/votes/votes.module.ts new file mode 100644 index 0000000..7aad133 --- /dev/null +++ b/apps/backend/src/app/votes/votes.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { MongooseModule } from '@nestjs/mongoose'; +import { VotesController } from './votes.controller'; +import { VotesService } from './votes.service'; +import { Vote, VoteSchema } from './schemas/vote.schema'; + +@Module({ + imports: [MongooseModule.forFeature([{ name: Vote.name, schema: VoteSchema }])], + controllers: [VotesController], + providers: [VotesService], +}) +export class VotesModule {} diff --git a/apps/backend/src/app/votes/votes.service.ts b/apps/backend/src/app/votes/votes.service.ts new file mode 100644 index 0000000..66358c1 --- /dev/null +++ b/apps/backend/src/app/votes/votes.service.ts @@ -0,0 +1,23 @@ +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { Vote } from './schemas/vote.schema'; +import { CreateVoteDto } from './dto/create-vote.dto'; + +@Injectable() +export class VotesService { + constructor(@InjectModel(Vote.name) private voteModel: Model) {} + + async create(createVoteDto: CreateVoteDto): Promise { + const createdVote = new this.voteModel(createVoteDto); + return createdVote.save(); + } + + async findAll(): Promise { + return this.voteModel.find().exec(); + } + + async findOne(id: string): Promise { + return this.voteModel.findById(id).exec(); + } +} diff --git a/nx.json b/nx.json index 7c03d07..058c363 100644 --- a/nx.json +++ b/nx.json @@ -80,5 +80,4 @@ "unitTestRunner": "jest" } }, - "nxCloudAccessToken": "ZGEyY2MxYzEtODUzNi00MDZmLWE2ZDEtZjNhODBiOTY3NzNifHJlYWQtd3JpdGU=" }