Skip to content

Commit

Permalink
feat: create votes and cast logic without smco yet
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljancar committed Jul 19, 2024
1 parent 58ec7c1 commit aa3080f
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Thumbs.db
.nx/workspace-data

.angular
.env
10 changes: 3 additions & 7 deletions apps/backend/src/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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) {
}
}
26 changes: 21 additions & 5 deletions apps/backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -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<string>('MONGODB_URI'),
}),
inject: [ConfigService],
}),
VotesModule,
CastModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule {
}
4 changes: 1 addition & 3 deletions apps/backend/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getData(): { message: string } {
return { message: 'Hello API' };
}

}
18 changes: 18 additions & 0 deletions apps/backend/src/app/casts/cast.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions apps/backend/src/app/casts/cast.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
19 changes: 19 additions & 0 deletions apps/backend/src/app/casts/cast.service.ts
Original file line number Diff line number Diff line change
@@ -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<Cast>) {}

async create(createCastDto: CreateCastDto): Promise<Cast> {
const createdCast = new this.CastModel(createCastDto);
return createdCast.save();
}

async findByVoteId(voteId: string): Promise<Cast[]> {
return this.CastModel.find({ voteId }).exec();
}
}
5 changes: 5 additions & 0 deletions apps/backend/src/app/casts/dto/create-cast.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class CreateCastDto {
voteId: string;
selectedOption: string;
voterPublicKey: string;
}
19 changes: 19 additions & 0 deletions apps/backend/src/app/casts/schemas/casts.schema.ts
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 6 additions & 0 deletions apps/backend/src/app/votes/dto/create-vote.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class CreateVoteDto {
title: string;
description: string;
options: string[];
createdBy: string;
}
22 changes: 22 additions & 0 deletions apps/backend/src/app/votes/schemas/vote.schema.ts
Original file line number Diff line number Diff line change
@@ -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);
23 changes: 23 additions & 0 deletions apps/backend/src/app/votes/votes.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions apps/backend/src/app/votes/votes.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
23 changes: 23 additions & 0 deletions apps/backend/src/app/votes/votes.service.ts
Original file line number Diff line number Diff line change
@@ -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<Vote>) {}

async create(createVoteDto: CreateVoteDto): Promise<Vote> {
const createdVote = new this.voteModel(createVoteDto);
return createdVote.save();
}

async findAll(): Promise<Vote[]> {
return this.voteModel.find().exec();
}

async findOne(id: string): Promise<Vote> {
return this.voteModel.findById(id).exec();
}
}
1 change: 0 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@
"unitTestRunner": "jest"
}
},
"nxCloudAccessToken": "ZGEyY2MxYzEtODUzNi00MDZmLWE2ZDEtZjNhODBiOTY3NzNifHJlYWQtd3JpdGU="
}

0 comments on commit aa3080f

Please sign in to comment.