Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 5 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
"express": "^4.18.1",
"mongoose": "^6.4.0"
}
}
}
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import express, { Application } from 'express';
import mongoose from 'mongoose';
import dbConsts from './constants/database';
import { Controller } from './controller/controller';
import { DatabaseUriNotFoundException } from './exceptions/database-uri-not-found-exception';
import { DatabaseUriNotFoundException } from './exceptions/database_url_not_found_exception';

export class App {
private _server: Application;
Expand Down Expand Up @@ -36,7 +36,7 @@ export class App {
try {
if (!dbConsts.DATABASE_ADDRESS) {
throw new DatabaseUriNotFoundException(
'missing environment variable [MONGO_CONNECTION_URI]'
'missing environment variable [DATABASE_ADDRESS]'
);
}

Expand Down
93 changes: 68 additions & 25 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { UserDocument } from '../document/user';
import { Service } from '../service/service';
import { validateUserPayload } from './validations/register_user';
import { toUserDocument } from './dto/user-request-dto';
import { InvalidPayloadException } from '../exceptions/invalid-payload-exception';
import { InvalidPayloadException } from '../exceptions/invalid_payload_exception';
import { UserFoundException } from '../exceptions/user_found';
import { validateFreelaPayload } from './validations/register_freela';
import { FreelaDocument } from '../document/freela';
import { AmountOfLargeCharacters } from '../exceptions/amount-of-large-characters';
import { AmountOfLargeCharacters } from '../exceptions/amount_of_large_characters';
import { toFreelaDocument } from './dto/freela-request-dto';
import { UserNotFound } from '../exceptions/user_not_found_exception';
import { FreelaNotFound } from '../exceptions/freela_not_found_exception';

import { UserHasBannedException } from '../exceptions/user_has_banned';
export class Controller {
private _router = Router();
private Service = new Service();
Expand All @@ -21,33 +21,73 @@ export class Controller {
}

constructor() {
this._router.post('/', this.register_user);
this._router.post('/freela', this.register_freela);
this._router.post('/:user_id/:freela_id', this.update_freela);
this._router.get('/freela', this.get_freelas);
this._router.get('/:id', this.get_user);
this._router.get('/:user_id/:freela_id', this.get_freela);
this._router.delete('/:user_id/:freela_id', this.delete_freela);
this._router.put('/:user_id/:freela_id', this.complete_freela);
this._router.post('/', this.registerUser);
this._router.post('/freela', this.registerFreela);
this._router.post('/ban/:user_id', this.banUser);
this._router.get('/ban/:user_id', this.hasBan);
this._router.get('/freela', this.getFreelas);
this._router.get('/:id', this.getUser);
this._router.get('/:user_id/:freela_id', this.getFreela);
this._router.delete('/:user_id/:freela_id', this.deleteFreela);
this._router.delete('/ban/:user_id', this.removeBan);
this._router.put('/:user_id/:freela_id', this.completeFreela);
this._router.post('/:user_id/:freela_id', this.updateFreela);
}

private update_freela = async (req: Request, res: Response) => {
private banUser = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
console.log('Ban User');
try {
await this.Service.registerBan(user_id);
return res.sendStatus(201);
} catch (error) {
if (error instanceof UserFoundException) {
return res.sendStatus(409);
}
res.sendStatus(500);
}
};

private hasBan = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
try {
const hasBan = await this.Service.findBan(user_id);
if (hasBan) {
return res.sendStatus(302);
}
return res.sendStatus(404);
} catch (error) {
res.sendStatus(500);
}
};

private removeBan = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
try {
await this.Service.removeBan(user_id);
return res.sendStatus(200);
} catch (error) {
res.sendStatus(404);
}
};

private updateFreela = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
const freela_id = req.params.freela_id;
const freela_updated = req.body;
try {
await this.Service.update_freela(user_id, freela_id, freela_updated);
await this.Service.updateFreela(user_id, freela_id, freela_updated);
return res.sendStatus(200);
} catch (error) {
res.sendStatus(404);
}
};

private delete_freela = async (req: Request, res: Response) => {
private deleteFreela = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
const freela_id = req.params.freela_id;
try {
await this.Service.quit_freela(user_id, freela_id, true);
await this.Service.quitFreela(user_id, freela_id, true);
return res.sendStatus(202);
} catch (error) {
if (error instanceof UserNotFound) {
Expand All @@ -59,11 +99,11 @@ export class Controller {
}
};

private complete_freela = async (req: Request, res: Response) => {
private completeFreela = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
const freela_id = req.params.freela_id;
try {
await this.Service.quit_freela(user_id, freela_id, false);
await this.Service.quitFreela(user_id, freela_id, false);
return res.sendStatus(202);
} catch (error) {
if (error instanceof UserNotFound) {
Expand All @@ -75,12 +115,12 @@ export class Controller {
}
};

private get_freela = async (req: Request, res: Response) => {
private getFreela = async (req: Request, res: Response) => {
const user_id = req.params.user_id;
const freela_id = req.params.freela_id;

try {
const freela = await this.Service.get_freela(user_id, freela_id);
const freela = await this.Service.getFreela(user_id, freela_id);
return res.status(200).send(freela);
} catch (error) {
if (error instanceof UserNotFound) {
Expand All @@ -92,10 +132,10 @@ export class Controller {
}
};

private get_user = async (req: Request, res: Response) => {
private getUser = async (req: Request, res: Response) => {
const _id: string = req.params.id;
try {
const user = await this.Service.get_user(_id);
const user = await this.Service.getUser(_id);
return res.status(200).send(user);
} catch (error) {
if (error instanceof UserNotFound) {
Expand All @@ -104,17 +144,17 @@ export class Controller {
}
};

private get_freelas = async (req: Request, res: Response) => {
private getFreelas = async (req: Request, res: Response) => {
const freelas = await this.Service.get();
res.status(200).send(freelas);
};

private register_user = async (req: Request, res: Response) => {
private registerUser = async (req: Request, res: Response) => {
let user: UserDocument = req.body;
try {
validateUserPayload(user);
user = toUserDocument(user);
await this.Service.update_user(user._id, user);
await this.Service.updateUser(user._id, user);
return res.sendStatus(201);
} catch (error) {
if (error instanceof InvalidPayloadException) {
Expand All @@ -123,10 +163,13 @@ export class Controller {
if (error instanceof UserFoundException) {
return res.status(302).send(error.message);
}
if (error instanceof UserHasBannedException) {
return res.status(403).send(error.message);
}
}
};

private register_freela = async (req: Request, res: Response) => {
private registerFreela = async (req: Request, res: Response) => {
try {
const data = req.body;
validateFreelaPayload(data);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/validations/register_freela.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InvalidPayloadException } from '../../exceptions/invalid-payload-exception';
import { InvalidPayloadException } from '../../exceptions/invalid_payload_exception';

const REQUIRED_FIELDS = [
'title',
Expand Down
2 changes: 1 addition & 1 deletion src/controller/validations/register_user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InvalidPayloadException } from '../../exceptions/invalid-payload-exception';
import { InvalidPayloadException } from '../../exceptions/invalid_payload_exception';

const REQUIRED_FIELDS = ['_id', 'name', 'avatar_url'];

Expand Down
6 changes: 6 additions & 0 deletions src/document/bannedUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Document } from 'mongoose';

export interface BannedUserDocument extends Document {
id: string;
banned_at: Date;
}
2 changes: 0 additions & 2 deletions src/document/freela_get.ts → src/document/freelaGet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import mongoose from 'mongoose';

export interface FreelaGetDocument {
id: string;
title: string;
Expand Down
6 changes: 6 additions & 0 deletions src/exceptions/user_has_banned.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class UserHasBannedException extends Error {
constructor(message: string) {
super(message);
this.name = 'UserHasBannedException';
}
}
10 changes: 10 additions & 0 deletions src/models/bannedUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { model, Schema } from 'mongoose';
import { BannedUserDocument } from '../document/bannedUser';

export const BannedSchema = new Schema({
_id: { type: String, required: true },
banned_at: { type: Date, required: false, default: new Date() }
});

const BannedUsers = model<BannedUserDocument>('BannedUsers', BannedSchema);
export default BannedUsers;
27 changes: 25 additions & 2 deletions src/repository/repository.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { UserDocument } from '../document/user';
import User from '../models/user';
import BannedUsers from '../models/bannedUsers';
export class Repository {
public registerBan = (_id: string) => {
return new BannedUsers({ _id }).save();
};

public findBanById = (_id: string): Promise<Boolean> => {
return BannedUsers.findOne({ _id })
.exec()
.then((user) => {
if (user) {
return true;
}
return false;
});
};

public removeBan = (_id: string) => {
return BannedUsers.deleteOne({ _id }).exec();
};

public findById = (_id: string): Promise<UserDocument> =>
User.findOne({ _id }).exec() as any;

public get(): Promise<UserDocument[]> {
return User.find().exec();
}

public register_user(user: UserDocument) {
public registerUser(user: UserDocument) {
return new User(user).save();
}

public async save_user(user) {
public deleteUser(_id: string) {
return User.deleteOne({ _id }).exec();
}
public async saveUser(user) {
return user.save();
}
}
Loading