Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop g7 #193

Merged
merged 40 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e7869df
Merge pull request #123 from CodingFactory-Repos/develop
Loule95450 Jan 19, 2023
0756293
Merged 'develop' into 'develop-G7'.
github-actions[bot] Jan 19, 2023
53cd533
Merged 'develop' into 'develop-G7'.
github-actions[bot] Jan 19, 2023
364d5d7
Create fonction to fetch data from mongodb
TheoLandemaine Feb 9, 2023
3282411
Change localhost link
TheoLandemaine Feb 9, 2023
2271814
Add route for page
TheoLandemaine Feb 9, 2023
ef8d1b7
Create file to display all documents in "articles" collection in mongodb
TheoLandemaine Feb 9, 2023
ed8bf08
Merged 'develop' into 'develop-G7'.
github-actions[bot] Feb 18, 2023
8263a75
Merge branch 'develop-G7' into G7/feature/displayArticles
TheoLandemaine Mar 20, 2023
daefe0d
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 20, 2023
a8e99b0
Merge branch 'develop-G7' into G7/feature/displayArticles
TheoLandemaine Mar 20, 2023
478accb
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 20, 2023
fd74c2c
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 20, 2023
d3d5b1f
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 21, 2023
d07873f
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 21, 2023
3dead70
Change description form && add style blog
TheoLandemaine Mar 21, 2023
0d52c59
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 21, 2023
406bcee
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 21, 2023
8fb5c39
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 22, 2023
91465ff
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 22, 2023
84d2745
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 22, 2023
a4cc72e
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 22, 2023
f274aae
Move files into folder
TheoLandemaine Mar 22, 2023
123e470
Create file to show one article
TheoLandemaine Mar 22, 2023
cb03a27
Add route for new file
TheoLandemaine Mar 22, 2023
8c47215
Add store to have a dynamic display of all articles
TheoLandemaine Mar 22, 2023
bac535d
Add function to get article by its id
TheoLandemaine Mar 22, 2023
cec7ca1
Merge branch 'develop-G7' into G7/feature/displayArticles
TheoLandemaine Mar 22, 2023
6cd6302
Add fonction to participate at an event or unsuscribe at him
TheoLandemaine Mar 23, 2023
3497f67
Add events to user when user want participate && create tab for suscr…
TheoLandemaine Mar 23, 2023
a86e530
delete console.log
TheoLandemaine Mar 23, 2023
3af544c
Resolve conflicts
TheoLandemaine Mar 24, 2023
bb146f0
Merge branch 'G7/feature/Event' into develop-G7
TheoLandemaine Mar 24, 2023
4e94e5e
test to see if all work && add condition
TheoLandemaine Mar 24, 2023
0550698
Merged 'develop' into 'develop-G7'.
github-actions[bot] Mar 24, 2023
a145935
add card component && add article in user creator
TheoLandemaine Mar 24, 2023
f6c6fec
Merge branch 'develop-G7' into G7/feature/sortType
TheoLandemaine Mar 24, 2023
cb6d517
resolve codeFactor for pr
TheoLandemaine Mar 24, 2023
91a57f1
add caption to the table
TheoLandemaine Mar 24, 2023
faa40eb
delete console.log
TheoLandemaine Mar 24, 2023
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
37 changes: 37 additions & 0 deletions back-end/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,41 @@ export class AuthController {
await this.authService.checkAuth(userId);
return res.status(200).json({ status: 'ok' });
}

@Post('addEvent')
@UseGuards(JwtAuthGuard)
async addEvents(
@Jwt() userId: ObjectId,
@Body() body: { eventId: string },
@Res() res: Response,
) {
const id = new ObjectId(body.eventId);
await this.authService.addEvent(userId, id);
return res.status(200).json({ status: 'ok' });
}

@Post('removeEvent')
@UseGuards(JwtAuthGuard)
async removeEvents(
@Jwt() userId: ObjectId,
@Body() body: { eventId: string },
@Res() res: Response,
) {
const id = new ObjectId(body.eventId);
await this.authService.removeEvent(userId, id);
return res.status(200).json({ status: 'ok' });
}

// tryAddArticles
@Post('addArticle')
@UseGuards(JwtAuthGuard)
async addArticles(
@Jwt() userId: ObjectId,
@Body() body: { articleId: string },
@Res() res: Response,
) {
const id = new ObjectId(body.articleId);
await this.authService.addArticles(userId, id);
return res.status(200).json({ status: 'ok' });
}
}
26 changes: 26 additions & 0 deletions back-end/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,30 @@ export class AuthService {
);
return user;
}

// add events into user
async addEvent(userId: ObjectId, eventId: ObjectId) {
const user = await this.usersRepository.findOneAndUpdateUser(
{ _id: userId },
{ $push: { events: eventId } },
);
if (user.value === null) throw new ServiceError('BAD_REQUEST', 'Error 400');
}

// remove events from user
async removeEvent(userId: ObjectId, eventId: ObjectId) {
const user = await this.usersRepository.findOneAndUpdateUser(
{ _id: userId },
{ $pull: { events: eventId } },
);
if (user.value === null) throw new ServiceError('BAD_REQUEST', 'Error 400');
}

async addArticles(userId: ObjectId, articleId: ObjectId) {
const user = await this.usersRepository.findOneAndUpdateUser(
{ _id: userId },
{ $push: { myArticles: articleId } },
);
if (user.value === null) throw new ServiceError('BAD_REQUEST', 'Error 400');
}
}
42 changes: 37 additions & 5 deletions back-end/src/base/articles/articles.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Res, UseFilters, Post, Req } from '@nestjs/common';
import { Controller, Get, Res, UseFilters, Post, Req, Put } from '@nestjs/common';
import { Response, Request } from 'express';

import { ServiceErrorCatcher } from 'src/common/decorators/catch.decorator';
Expand All @@ -10,13 +10,45 @@ export class ArticlesController {
constructor(private readonly articlesService: ArticlesService) {}

@Get('')
index(@Res() res: Response) {
return res.status(201).json({ status: 'ok' });
getArticle(@Req() req: Request, @Res() res: Response) {
this.articlesService.getArticle().then((article) => {
return res.status(201).json(article);
});
}

@Post('/add')
addArticle(@Req() req: Request, @Res() res: Response) {
this.articlesService.addArticle(req.body).then((article) => {
async addArticle(@Req() req: Request, @Res() res: Response) {
const article = await this.articlesService.addArticle(req.body);
return res.status(201).json({ article, id: article.insertedId });
}

@Get('/:id')
getArticleById(@Req() req: Request, @Res() res: Response) {
this.articlesService.getArticleById(req.params.id).then((article) => {
return res.status(201).json(article);
});
}

// add participant to the array of participants in article in the database
@Put('/participant/:id')
addParticipant(@Req() req: Request, @Res() res: Response) {
this.articlesService.addParticipant(req.params.id, req.body).then((article) => {
return res.status(201).json(article);
});
}

// remove participant from the array of participants in article in the database
@Put('/removeParticipant/:id')
removeParticipant(@Req() req: Request, @Res() res: Response) {
this.articlesService.removeParticipant(req.params.id, req.body).then((article) => {
return res.status(201).json(article);
});
}

// add comment
@Put('/comment/:id')
addComment(@Req() req: Request, @Res() res: Response) {
this.articlesService.addComment(req.params.id, req.body).then((article) => {
return res.status(201).json(article);
});
}
Expand Down
15 changes: 14 additions & 1 deletion back-end/src/base/articles/articles.repository.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { Inject, Injectable } from '@nestjs/common';
import { Filter, UpdateFilter, FindOneAndUpdateOptions, Db } from 'mongodb';
import { Filter, UpdateFilter, FindOneAndUpdateOptions, Db, ObjectId } from 'mongodb';

import { Article } from 'src/base/articles/interfaces/articles.interface';

@Injectable()
export class ArticlesRepository {
constructor(@Inject('DATABASE_CONNECTION') private db: Db) {}

// Function to get the articles collection
get articles() {
return this.db.collection<Article>('articles');
}

// Function to get an article by its id
async getArticleById(id: ObjectId) {
id = new ObjectId(id);
return this.articles.findOne({ _id: id });
}

// Function to get all articles
async getAllArticles() {
return this.articles.find({}).toArray();
}

// Function to add an article
async createArticle(query: Article) {
return this.articles.insertOne(query);
}
Expand Down
34 changes: 34 additions & 0 deletions back-end/src/base/articles/articles.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { ObjectId } from 'mongodb';

import { ArticlesRepository } from 'src/base/articles/articles.repository';
import { UsersRepository } from 'src/base/users/users.repository';
Expand All @@ -12,9 +13,42 @@ export class ArticlesService {
private articlesRepository: ArticlesRepository,
) {}

// Function to add an article
async addArticle(queryArticle) {
return await this.articlesRepository.createArticle(queryArticle);
}

// Function to get all articles
async getArticle() {
return await this.articlesRepository.getAllArticles();
}

// Function to get an article by its id
async getArticleById(id) {
return await this.articlesRepository.getArticleById(id);
}

// add participant to the array of participants in article in the database
async addParticipant(id, queryParticipant) {
const update = { $push: { participants: queryParticipant } };

return await this.articlesRepository.updateOneArticle({ _id: new ObjectId(id) }, update);
}

// remove participant from the array of participants in article in the database
async removeParticipant(id, queryParticipant) {
const update = { $pull: { participants: queryParticipant } };

return await this.articlesRepository.updateOneArticle({ _id: new ObjectId(id) }, update);
}

// add comment
async addComment(id, queryComment) {
const update = { $push: { comments: queryComment } };

return await this.articlesRepository.updateOneArticle({ _id: new ObjectId(id) }, update);
}

// Business logic methods goes there...
// Define your own methods
}
12 changes: 12 additions & 0 deletions front-end/src/api/auth-req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ export const tryResetPassword = (password: string, resetToken: string) => {
export const tryGetMe = async () => {
return http.get<Status<{ user: User }>>('/auth/me');
};

export const tryAddEvents = async (eventId: string) => {
return http.post<Status>('/auth/addEvent', { eventId });
};

export const tryRemoveEvents = async (eventId: string) => {
return http.post<Status>('/auth/removeEvent', { eventId });
};

export const tryAddArticles = async (articleId: string) => {
return http.post<Status>('/auth/addArticle', { articleId });
};
Loading