Skip to content

Commit

Permalink
Merge pull request #100 from cuappdev/filter
Browse files Browse the repository at this point in the history
Filter
  • Loading branch information
Daniel-jw authored Dec 2, 2024
2 parents 3197f64 + ad9b3d2 commit 925fb59
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 23 deletions.
21 changes: 21 additions & 0 deletions src/api/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EditPriceResponse,
FilterPostsRequest,
FilterPostsByPriceRequest,
FilterPostsByConditionRequest,
GetPostResponse,
GetPostsResponse,
GetSearchedPostsRequest,
Expand Down Expand Up @@ -63,6 +64,26 @@ export class PostController {
return { posts: await this.postService.filterPostsByPrice(user, filterPostsByPriceRequest) };
}

@Post('filterPriceHighToLow/')
async filterPriceHighToLow(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPriceHighToLow(user) };
}

@Post('filterPriceLowToHigh/')
async filterPriceLowToHigh(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.filterPriceLowToHigh(user) };
}

@Post('filterNewlyListed/')
async filterNewlyListed(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.filterNewlyListed(user) };
}

@Post('filterByCondition/')
async filterByCondition(@CurrentUser() user: UserModel, @Body() filterPostsByConditionRequest: FilterPostsByConditionRequest): Promise<GetPostsResponse> {
return { posts: await this.postService.filterByCondition(user, filterPostsByConditionRequest) };
}

@Get('archive/')
async getArchivedPosts(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
return { posts: await this.postService.getArchivedPosts(user) };
Expand Down
3 changes: 2 additions & 1 deletion src/factories/PostFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ define(PostModel, (_, context?: { index?: number, user?: UserModel }) => {
// Use consistent data for post fields, utilizing index for uniqueness
post.title = `Sample Post Title - User ${post.user.givenName} - Index: ${index}`;
post.description = `This is a sample description for post - User ${post.user.givenName} - Index: ${index}.`;
post.categories = ['Electronics', 'Books'];
post.category = 'Electronics';
post.condition = 'New';
post.original_price = 100.00 + index;
post.altered_price = 80.00 + index;
post.images = [
Expand Down
24 changes: 24 additions & 0 deletions src/migrations/1731271779741-updatecategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class updatecategories1731271779741 implements MigrationInterface {
name = 'updatecategories1731271779741'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "Post" RENAME COLUMN "categories" TO "category"`);
await queryRunner.query(`ALTER TABLE "Post" DROP COLUMN "category"`);
await queryRunner.query(`ALTER TABLE "Post" ADD "category" character varying`);
await queryRunner.query(`UPDATE "Post" SET "category" = 'Other' WHERE "category" IS NULL`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "category" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "original_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "altered_price" TYPE numeric`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "altered_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "original_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" DROP COLUMN "category"`);
await queryRunner.query(`ALTER TABLE "Post" ADD "category" text array NOT NULL`);
await queryRunner.query(`ALTER TABLE "Post" RENAME COLUMN "category" TO "categories"`);
}

}
20 changes: 20 additions & 0 deletions src/migrations/1732146942548-addconditiontoposts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class addconditiontoposts1732146942548 implements MigrationInterface {
name = 'addconditiontoposts1732146942548'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "Post" ADD "condition" character varying`);
await queryRunner.query(`UPDATE "Post" SET "condition" = 'NEW' WHERE "condition" IS NULL`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "condition" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "original_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "altered_price" TYPE numeric`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "altered_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" ALTER COLUMN "original_price" TYPE numeric`);
await queryRunner.query(`ALTER TABLE "Post" DROP COLUMN "condition"`);
}

}
10 changes: 7 additions & 3 deletions src/models/PostModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export class PostModel {
@Column()
description: string;

@Column("text", { array: true })
categories: string[];
@Column()
category: string;

@Column()
condition: string;

@Column("numeric", { scale: 2 })
original_price: number;
Expand Down Expand Up @@ -74,7 +77,8 @@ export class PostModel {
id: this.id,
title: this.title,
description: this.description,
categories: this.categories,
category: this.category,
condition: this.condition,
original_price: this.original_price,
altered_price: this.altered_price,
images: this.images,
Expand Down
46 changes: 42 additions & 4 deletions src/repositories/PostRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ export class PostRepository extends AbstractRepository<PostModel> {
public async createPost(
title: string,
description: string,
categories: string[],
category: string,
condition: string,
price: number,
images: string[],
user: UserModel,
): Promise<PostModel> {
const post = new PostModel();
post.title = title;
post.description = description;
post.categories = categories;
post.category = category;
post.condition = condition;
post.original_price = price;
post.altered_price = price;
post.images = images;
Expand Down Expand Up @@ -82,11 +84,11 @@ export class PostRepository extends AbstractRepository<PostModel> {
.getMany();
}

public async filterPosts(category: string): Promise<PostModel[]> {
public async filterPosts(categories: string[]): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where(":category = ANY (post.categories)", { category: category })
.where("post.category IN (:...categories)", { categories: categories })
.andWhere("post.archive = false")
.getMany();
}
Expand All @@ -101,6 +103,42 @@ export class PostRepository extends AbstractRepository<PostModel> {
.getMany();
}

public async filterPriceHighToLow(): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where("post.archive = false")
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "DESC")
.getMany();
}

public async filterPriceLowToHigh(): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where("post.archive = false")
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "ASC")
.getMany();
}

public async filterNewlyListed(): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where("post.archive = false")
.orderBy("post.created", "DESC")
.getMany();
}

public async filterByCondition(conditions: string[]): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where("post.condition IN (:...conditions)", { conditions })
.andWhere("post.archive = false")
.getMany();
}

public async getArchivedPosts(): Promise<PostModel[]> {
return await this.repository
.createQueryBuilder("post")
Expand Down
42 changes: 39 additions & 3 deletions src/services/PostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UuidParam } from '../api/validators/GenericRequests';
import { PostModel } from '../models/PostModel';
import { UserModel } from '../models/UserModel';
import Repositories, { TransactionsManager } from '../repositories';
import { CreatePostRequest, FilterPostsRequest, FilterPostsByPriceRequest, GetSearchedPostsRequest, EditPostPriceRequest } from '../types';
import { CreatePostRequest, FilterPostsRequest, FilterPostsByPriceRequest, FilterPostsByConditionRequest, GetSearchedPostsRequest, EditPostPriceRequest } from '../types';
import { uploadImage } from '../utils/Requests';
import { getLoadedModel } from '../utils/SentenceEncoder';
import { PostRepository } from 'src/repositories/PostRepository';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class PostService {
const imageUrl = image.data;
images.push(imageUrl);
}
const freshPost = await postRepository.createPost(post.title, post.description, post.categories, post.original_price, images, user);
const freshPost = await postRepository.createPost(post.title, post.description, post.category, post.condition, post.original_price, images, user);
const requestRepository = Repositories.request(transactionalEntityManager);
const requests = await requestRepository.getAllRequest();
for (const request of requests) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class PostService {
public async filterPosts(user: UserModel, filterPostsRequest: FilterPostsRequest): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterPosts(filterPostsRequest.category);
const posts = await postRepository.filterPosts(filterPostsRequest.categories);
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
Expand All @@ -138,6 +138,42 @@ export class PostService {
})
}

public async filterPriceHighToLow(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterPriceHighToLow();
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async filterPriceLowToHigh(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterPriceLowToHigh();
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async filterNewlyListed(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterNewlyListed();
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async filterByCondition(user: UserModel, filterPostsByConditionRequest: FilterPostsByConditionRequest): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
const posts = await postRepository.filterByCondition(filterPostsByConditionRequest.condition);
const activePosts = this.filterInactiveUserPosts(posts);
return this.filterBlockedUserPosts(activePosts, user);
});
}

public async getArchivedPosts(user: UserModel): Promise<PostModel[]> {
return this.transactions.readOnly(async (transactionalEntityManager) => {
const postRepository = Repositories.post(transactionalEntityManager);
Expand Down
Loading

0 comments on commit 925fb59

Please sign in to comment.