Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e7d2623
feat(makefile): add database user environment variable
Utkub24 Mar 11, 2024
8218c76
feat(makefile): safer makefile prerequisites
Utkub24 Mar 11, 2024
a435e17
feat(makefile): add raffle table
Utkub24 Mar 11, 2024
c0856b1
feat: do not use sudo to switch database user
Utkub24 Mar 22, 2024
2747cb5
feat(makefile): create example database structure
Utkub24 Mar 23, 2024
704fa09
fix: fix the .PHONY segment and several naming mistakes in the makefile
HuseyinSimsek7904 Mar 24, 2024
c1de46a
fix: add dependencies to make sure the make command does not throw error
HuseyinSimsek7904 Mar 28, 2024
8d9b34a
fix: fix mistake in postgres.service.ts
HuseyinSimsek7904 Mar 28, 2024
15afc94
chore: add query statement information to PostgresService.query
HuseyinSimsek7904 Mar 28, 2024
3f8b7c5
chore: move Table and UserColumn to postgres/database_tables.ts
HuseyinSimsek7904 Mar 28, 2024
5cba928
feat: add participant service, controller and module
HuseyinSimsek7904 Mar 28, 2024
26bbce1
chore: rename User.id to User.userid and UserColumn.id to UserColumn.…
HuseyinSimsek7904 Mar 28, 2024
244c949
chore: rename Participant.id to Participant.participantid and Partici…
HuseyinSimsek7904 Mar 28, 2024
ed8c948
feat: add raffle service, controller and module
HuseyinSimsek7904 Mar 28, 2024
1fcd738
fix: fix mistake in raffle service
HuseyinSimsek7904 Apr 1, 2024
4129eda
feat: add FormQuestion interface
HuseyinSimsek7904 Apr 1, 2024
e72eca1
chore: move user, raffle, participant and formQuestion interfaces to …
HuseyinSimsek7904 Apr 1, 2024
1a6e4e5
Merge branch 'main' into db-structure
HuseyinSimsek7904 Apr 1, 2024
c718f7f
fix: fix tiny mistake
HuseyinSimsek7904 Apr 2, 2024
3578a38
wiki: update the makefile wiki
HuseyinSimsek7904 Apr 2, 2024
6d3eab3
feat: push the correct FormQuestion interface
HuseyinSimsek7904 Apr 2, 2024
482ac8d
Merge branch 'main' into db-structure
HuseyinSimsek7904 Apr 22, 2024
fcafd83
chore: refactors
HuseyinSimsek7904 Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 85 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,59 +1,108 @@
include .env
TEST_DB_NAME := ${database}

.PHONY: createdb dropdb create_user_table drop_user_table add_random_users view_users start shut status
.PHONY: createdb dropdb create_user_table drop_user_table populate_users view_users create_raffles_table drop_raffles_table populate_raffles view_raffles create_participants_table drop_participants_table populate_participants view_participants start shut status


all: start


createdb:
sudo -u postgres createdb $(TEST_DB_NAME)
all: view_users

# Database management
createdb: start
psql -U $(DB_USERNAME) -tc "SELECT 1 FROM pg_database WHERE datname = '$(DB_NAME)';"\
| grep -q 1\
|| psql -U $(DB_USERNAME) -c "CREATE DATABASE $(DB_NAME);"

dropdb:
sudo -u postgres dropdb $(TEST_DB_NAME)
psql -U $(DB_USERNAME) -c 'DROP DATABASE IF EXISTS $(DB_NAME);'


create_user_table:
sudo -u postgres psql -d $(TEST_DB_NAME) -c "CREATE TABLE users ( \
id VARCHAR(6) PRIMARY KEY, \
# User table
create_users_table: createdb
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "CREATE TABLE IF NOT EXISTS users ( \
userid INT PRIMARY KEY, \
username VARCHAR(100) UNIQUE NOT NULL, \
email VARCHAR(100) UNIQUE NOT NULL, \
password VARCHAR(100) NOT NULL, \
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP \
);"


drop_user_table:
sudo -u postgres psql -d $(TEST_DB_NAME) -c "DROP TABLE users;"


add_random_users:
sudo -u postgres psql -d $(TEST_DB_NAME) -c "INSERT INTO users (id, username, email, password) VALUES \
("10000", 'john_doe', 'john_doe@example.com', 'XyZ9@qwe'), \
("25000", 'jane_doe', 'jane_doe@example.com', 'P@ssw0rd'), \
("37000", 'alice_smith', 'alice_smith@example.com', 'Secret123'), \
("48000", 'bob_johnson', 'bob_johnson@example.com', 'Qwerty!23'), \
("55000", 'emily_wilson', 'emily_wilson@example.com', 'Welcome123'), \
("62000", 'michael_brown', 'michael_brown@example.com', 'Password!'), \
("73000", 'sarah_jackson', 'sarah_jackson@example.com', 'Test@1234'), \
("81000", 'chris_miller', 'chris_miller@example.com', 'Ch@ngeMe'), \
("92000", 'laura_taylor', 'laura_taylor@example.com', 'Summer2023'), \
("99000", 'ryan_clark', 'ryan_clark@example.com', 'Rainbow#42');"



view_users:
sudo -u postgres psql -d $(TEST_DB_NAME) -c "SELECT * FROM users;"


drop_users_table:
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "DROP TABLE IF EXISTS users;"

populate_users: create_users_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "INSERT INTO users (userid, username, email, password) VALUES \
(10000, 'john_doe', 'john_doe@example.com', 'XyZ9@qwe'), \
(25000, 'jane_doe', 'jane_doe@example.com', 'P@ssw0rd'), \
(37000, 'alice_smith', 'alice_smith@example.com', 'Secret123'), \
(48000, 'bob_johnson', 'bob_johnson@example.com', 'Qwerty!23'), \
(55000, 'emily_wilson', 'emily_wilson@example.com', 'Welcome123'), \
(62000, 'michael_brown', 'michael_brown@example.com', 'Password!'), \
(73000, 'sarah_jackson', 'sarah_jackson@example.com', 'Test@1234'), \
(81000, 'chris_miller', 'chris_miller@example.com', 'Ch@ngeMe'), \
(92000, 'laura_taylor', 'laura_taylor@example.com', 'Summer2023'), \
(99000, 'ryan_clark', 'ryan_clark@example.com', 'Rainbow#42');"

view_users: create_users_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "SELECT * FROM users;"


# Raffles table
create_raffles_table: createdb create_users_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "CREATE TABLE IF NOT EXISTS raffles (\
raffleid INT PRIMARY KEY, \
rafflename VARCHAR(100), \
form JSONB, \
authorid INT, \
CONSTRAINT fk_author_user \
FOREIGN KEY (authorid) \
REFERENCES users(userid)\
);"

drop_raffles_table:
psql -U $(DB_USERNAME) -d $(DB_NAME) -c 'DROP TABLE IF EXISTS raffles;'

populate_raffles: create_raffles_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "INSERT INTO raffles (raffleid, rafflename, authorid) VALUES \
(1, 'Raffle 1', 10000), \
(123, 'Big Raffle', 62000), \
(124, 'Big Raffle 2', 62000), \
(125, 'Big Raffle 3', 62000), \
(666, 'Hell Raffle', 99000);"

view_raffles: create_raffles_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "SELECT * FROM raffles;"


# Participants tables
create_participants_table: createdb create_raffles_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "CREATE TABLE IF NOT EXISTS participants ( \
participantid INT PRIMARY KEY, \
info JSONB, \
raffleid INT, \
CONSTRAINT fk_raffle_participants \
FOREIGN KEY (raffleid) \
REFERENCES raffles(raffleid) \
);"

drop_participants_table:
psql -U $(DB_USERNAME) -d $(DB_NAME) -c 'DROP TABLE IF EXISTS participants;'

populate_participants: create_participants_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c "INSERT INTO participants (participantid, info, raffleid) VALUES \
(1, '{\"name\": \"John Doe\", \"phone_number\": \"+1234567890\", \"email\": \"john.doe@example.com\", \"student_id\": \"A12345\"}', 1), \
(2, '{\"name\": \"Alice Smith\", \"phone_number\": \"+1987654321\", \"email\": \"alice.smith@example.com\", \"student_id\": \"B54321\"}', 123), \
(3, '{\"name\": \"Bob Johnson\", \"phone_number\": \"+1122334455\", \"email\": \"bob.johnson@example.com\"}', 123), \
(4, '{\"name\": \"Emily Brown\", \"student_id\": \"C67890\"}', 123);"

view_participants: create_participants_table
psql -U $(DB_USERNAME) -d $(DB_NAME) -c 'SELECT * FROM participants;'


# Service management
start:
sudo systemctl start postgresql

shut:
sudo systemctl stop postgresql


status:
sudo systemctl status postgresql
7 changes: 6 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { PostgresService } from './postgres/postgres.service';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
import { ThrottlerModule } from '@nestjs/throttler';
import { ParticipantModule } from './participant/participant.module';
import { RaffleModule } from './raffle/raffle.module';

@Module({
imports: [
Expand All @@ -15,7 +17,10 @@ import { ThrottlerModule } from '@nestjs/throttler';
limit: 2
}]),
UserModule,
AuthModule, ],
AuthModule,
ParticipantModule,
RaffleModule,
],
controllers: [AppController],
providers: [AppService, PostgresService],
})
Expand Down
5 changes: 2 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export class AuthController {

@HttpCode(HttpStatus.CREATED)
@Post('signup')
async signUp(@Body() signInDto: SignUpDto, @Res({passthrough: true}) response: Response): Promise<void> {
async signUp(@Body() signUpDto: SignUpDto, @Res({passthrough: true}) response: Response): Promise<void> {
try {
const { username, email, password } = signInDto;
const { access_token } = await this.authService.signUp(username, email, password);
const { access_token } = await this.authService.signUp(signUpDto.username, signUpDto.email, signUpDto.password);
response.cookie('wd_access_token', access_token, cookieOptions);
response.send();
}
Expand Down
7 changes: 4 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { User, UserService } from 'src/user/user.service';
import { UserService } from 'src/user/user.service';
import { User } from 'src/interfaces/user';
import { jwtConstants } from './constants';

@Injectable()
Expand All @@ -13,7 +14,7 @@ export class AuthService {
async logIn(username: string, password: string): Promise<{access_token: string}> {
const user: User = await this.userService.getByUsername(username);
if (user && user.password === password) {
const payload = { username: user.username, sub: user.id };
const payload = { username: user.username, sub: user.userid };
const token = await this.jwtService.signAsync(payload);
return { access_token: token };
}
Expand All @@ -32,7 +33,7 @@ export class AuthService {
throw new Error('Email already exists');
}
const newUser: User = await this.userService.createUser(username, email, password);
const payload = { username: newUser.username, sub: newUser.id };
const payload = { username: newUser.username, sub: newUser.userid };
const token = await this.jwtService.signAsync(payload);
return { access_token: token };
}
Expand Down
23 changes: 23 additions & 0 deletions src/interfaces/formQuestion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface FormQuestion {
// necessary fields
title: string;
description: string;
type: "text" | "number" | "radio" | "checkbox" | "slider" | "email" | "phone" | "date";

// optional fields
isOptional?: boolean;

// fields that changes based on the type
/// text and number
minimum?: number;
maximum?: number;

/// radio and checkbox
options?: string[];

/// slider
start?: number;
end?: number;
step?: number;
defaultValue?: number;
}
5 changes: 5 additions & 0 deletions src/interfaces/participant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Participant {
participantid: string;
info: object;
raffleid: string;
}
8 changes: 8 additions & 0 deletions src/interfaces/raffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FormQuestion } from "src/interfaces/formQuestion";

export interface Raffle {
raffleid: string,
rafflename: string,
form: FormQuestion[],
authorid: string,
}
7 changes: 7 additions & 0 deletions src/interfaces/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface User {
userid: string;
username: string;
email: string;
password: string;
created_at: string;
};
18 changes: 18 additions & 0 deletions src/participant/participant.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ParticipantController } from './participant.controller';

describe('ParticipantController', () => {
let controller: ParticipantController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ParticipantController],
}).compile();

controller = module.get<ParticipantController>(ParticipantController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
56 changes: 56 additions & 0 deletions src/participant/participant.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ParticipantService } from './participant.service';

@Controller('participant')
export class ParticipantController {
constructor(private readonly participantService: ParticipantService) {}

@Get('all')
async getAll() {
try {
const data = await this.participantService.getAll();
return data;
}
catch (e) {
console.log(e);
return e;
}
}

@Get('one/:id')
async getByID(@Param('id') id: string) {
try {
const data = await this.participantService.getByID(id);
return data;
}
catch (e) {
console.log(e);
return e;
}
}

@Get('raffleid/:id')
async getByRaffleID(@Param('id') id: string) {
try {
const data = await this.participantService.getByRaffleID(id);
return data;
}
catch (e) {
console.log(e);
return e;
}
}

@Get('ids')
async getAllIDs() {
console.log('getting all ids');
try {
const data = await this.participantService.getAllIDs();
return data;
}
catch (e) {
console.log(e);
return e;
}
}
}
10 changes: 10 additions & 0 deletions src/participant/participant.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ParticipantService } from 'src/participant/participant.service';
import { ParticipantController } from 'src/participant/participant.controller';
import { PostgresService } from 'src/postgres/postgres.service';

@Module({
providers: [PostgresService, ParticipantService],
controllers: [ParticipantController]
})
export class ParticipantModule {}
18 changes: 18 additions & 0 deletions src/participant/participant.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ParticipantService } from './participant.service';

describe('ParticipantService', () => {
let service: ParticipantService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ParticipantService],
}).compile();

service = module.get<ParticipantService>(ParticipantService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
Loading