Skip to content

Commit

Permalink
Merge pull request #90 from cuappdev/archive
Browse files Browse the repository at this point in the history
Fixed soft delete
  • Loading branch information
Daniel-jw authored Oct 23, 2024
2 parents 0bfe982 + 1a0ed82 commit 510499d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/api/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export class PostController {
return { post: await this.postService.archivePost(user, params) };
}

@Post('archiveAll/userId/:id/')
async archiveAllPostsByUserId(@Params() params: UuidParam): Promise<GetPostsResponse> {
return { posts: await this.postService.archiveAllPostsByUserId(params) };
}

@Get('save/')
async getSavedPostsByUserId(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.getSavedPostsByUserId(user) };
Expand Down
13 changes: 12 additions & 1 deletion src/api/controllers/RequestController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Body, Delete, Get, JsonController, Params, Post } from 'routing-controllers';
import { Body, CurrentUser, Delete, Get, JsonController, Params, Post } from 'routing-controllers';

import { UserModel } from '../../models/UserModel';
import { RequestService } from '../../services/RequestService';
import { CreateRequestRequest, GetPostsResponse, GetRequestResponse, GetRequestsResponse } from '../../types';
import { TimeParam, UuidParam } from '../validators/GenericRequests';
Expand Down Expand Up @@ -38,6 +39,16 @@ export class RequestController {
return { request: await this.requestService.deleteRequestById(params) };
}

@Post('archive/requestId/:id/')
async archiveRequest(@CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<GetRequestResponse> {
return { request: await this.requestService.archiveRequest(user, params) };
}

@Post('archiveAll/userId/:id/')
async archiveAllRequestsByUserId(@Params() params: UuidParam): Promise<GetRequestsResponse> {
return { requests: await this.requestService.archiveAllRequestsByUserId(params) };
}

@Get('matches/id/:id/:time?/')
async getMatchesByRequestId(@Params() params: TimeParam): Promise<GetPostsResponse> {
return { posts: await this.requestService.getMatchesByRequestId(params) };
Expand Down
3 changes: 3 additions & 0 deletions src/models/RequestModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class RequestModel {
@Column()
description: string;

@Column({ default: false })
archive: boolean;

@ManyToOne(() => UserModel, user => user.requests)
@JoinColumn({ name: 'user' })
user: UserModel
Expand Down
9 changes: 9 additions & 0 deletions src/repositories/PostRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ export class PostRepository extends AbstractRepository<PostModel> {
return await this.repository.save(post)
}

public async archiveAllPostsByUserId(userId: Uuid): Promise<void> {
await this.repository
.createQueryBuilder()
.update(PostModel)
.set({ archive: true })
.where("userId = :userId", { userId })
.execute();
}

public async editPostPrice(post: PostModel, new_price: number): Promise<PostModel> {
post.altered_price = new_price
return await this.repository.save(post)
Expand Down
14 changes: 14 additions & 0 deletions src/repositories/RequestRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ export class RequestRepository extends AbstractRepository<RequestModel> {
return this.repository.remove(request);
}

public async archiveRequest(request: RequestModel): Promise<RequestModel> {
request.archive = true;
return await this.repository.save(request);
}

public async archiveAllRequestsByUserId(userId: Uuid): Promise<void> {
await this.repository
.createQueryBuilder()
.update(RequestModel)
.set({ archive: true })
.where("userId = :userId", { userId })
.execute();
}

public async getAllMatchesByRequestId(id: Uuid): Promise<RequestModel | undefined> {
return await this.repository
.createQueryBuilder("request")
Expand Down
16 changes: 16 additions & 0 deletions src/services/PostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ export class PostService {
});
}

public async archiveAllPostsByUserId(params: UuidParam): Promise<PostModel[]> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const userRepository = Repositories.user(transactionalEntityManager);
const user = await userRepository.getUserById(params.id)
if (!user) throw new NotFoundError('User not found!');
if (!user.isActive) throw new NotFoundError('User is not active!');
const posts = await postRepository.getPostsByUserId(user.id);
for (const post of posts) {
if (!post) throw new NotFoundError('Post not found!');
await postRepository.archivePost(post);
}
return posts;
});
}

public async getSavedPostsByUserId(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
Expand Down
31 changes: 30 additions & 1 deletion src/services/RequestService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NotFoundError } from 'routing-controllers';
import { ForbiddenError, NotFoundError } from 'routing-controllers';
import { Service } from 'typedi';
import { EntityManager } from 'typeorm';
import { InjectManager } from 'typeorm-typedi-extensions';

import { UserModel } from '../models/UserModel';
import { TimeParam, UuidParam } from '../api/validators/GenericRequests';
import { PostModel } from 'src/models/PostModel';
import { RequestModel } from '../models/RequestModel';
Expand Down Expand Up @@ -61,6 +62,34 @@ export class RequestService {
});
}

public async archiveRequest(user: UserModel, params: UuidParam): Promise<RequestModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const requestRepository = Repositories.request(transactionalEntityManager);
const request = await requestRepository.getRequestById(params.id);
if (!request) throw new NotFoundError('Request not found!');
if (request.user.isActive == false) throw new NotFoundError('User is not active!');
if (user.id != request.user?.id) throw new ForbiddenError('User is not poster!');
return await requestRepository.archiveRequest(request);
});
}

public async archiveAllRequestsByUserId(params: UuidParam): Promise<RequestModel[]> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const requestRepository = Repositories.request(transactionalEntityManager);
const userRepository = Repositories.user(transactionalEntityManager);
const user = await userRepository.getUserById(params.id)
if (!user) throw new NotFoundError('User not found!');
if (!user.isActive) throw new NotFoundError('User is not active!');
const requests = await requestRepository.getRequestByUserId(user.id);
for (const request of requests) {
if (!request) throw new NotFoundError('Request not found!');
await requestRepository.archiveRequest(request);
}
return requests;
});
}


public async getMatchesByRequestId(params: TimeParam): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const requestRepository = Repositories.request(transactionalEntityManager);
Expand Down
4 changes: 4 additions & 0 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ export class UserService {
public async softDeleteUser(params: UuidParam): Promise<UserModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const userRepository = Repositories.user(transactionalEntityManager);
const postRepository = Repositories.post(transactionalEntityManager);
const requestRepository = Repositories.request(transactionalEntityManager);
const user = await userRepository.getUserById(params.id);
if (!user) throw new NotFoundError('User not found!');
await postRepository.archiveAllPostsByUserId(user.id);
await requestRepository.archiveAllRequestsByUserId(user.id);
return userRepository.softDeleteUser(user);
});
}
Expand Down

0 comments on commit 510499d

Please sign in to comment.