Skip to content

Commit

Permalink
Merge pull request #66 from cuappdev/tony/userreview
Browse files Browse the repository at this point in the history
Add User Review Functionality and Edit Price
  • Loading branch information
akmatchev authored Sep 25, 2023
2 parents 6c845ee + e1d5e64 commit 6fba8c7
Show file tree
Hide file tree
Showing 23 changed files with 269 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/api/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class PostController {
}

@Post('edit/postId/:id/')
async editPrice(@Body() editPriceRequest : EditPostPriceRequest, @CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<EditPriceResponse> {
async editPrice(@Body() editPriceRequest: EditPostPriceRequest, @CurrentUser() user: UserModel, @Params() params: UuidParam): Promise<EditPriceResponse> {
return { new_price: await (await this.postService.editPostPrice(user, params, editPriceRequest)).altered_price };
}

Expand Down
12 changes: 6 additions & 6 deletions src/api/controllers/UserReviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
CreateUserReviewRequest,
GetUserReviewResponse,
GetUserReviewsResponse
} from 'src/types';
import { UserModel } from 'src/models/UserModel';
} from '../../types';
import { UserModel } from '../../models/UserModel';
import { UserReviewService } from '../../services/UserReviewService'
import { UuidParam } from '../validators/GenericRequests';

@JsonController('userRevier')
@JsonController('userReview/')
export class UserReviewController {
private userReviewService: UserReviewService;

Expand All @@ -23,16 +23,16 @@ export class UserReviewController {

@Get('id/:id/')
async getUserReviewsById(@Params() params: UuidParam): Promise<GetUserReviewResponse> {
return { userReview: await this.userReviewService.getUserReviewById(params)}
return { userReview: await this.userReviewService.getUserReviewById(params) };
}

@Post()
async createUserReview(@Body() createUserReviewRequest: CreateUserReviewRequest): Promise<GetUserReviewResponse> {
return { userReview: await this.userReviewService.createUserReview(createUserReviewRequest)}
return { userReview: await this.userReviewService.createUserReview(createUserReviewRequest) };
}

@Delete('id/:id/')
async deleteUserReview(@CurrentUser() buyer: UserModel, @Params() params: UuidParam): Promise<GetUserReviewResponse> {
return { userReview: await this.userReviewService.deleteUserReviewById(buyer, params)};
return { userReview: await this.userReviewService.deleteUserReviewById(buyer, params) };
}
}
2 changes: 2 additions & 0 deletions src/api/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ImageController } from './ImageController';
import { PostController } from './PostController';
import { RequestController } from './RequestController';
import { UserController } from './UserController';
import { UserReviewController } from './UserReviewController';
import { NotifController } from './NotifController'

export const controllers = [
Expand All @@ -14,4 +15,5 @@ export const controllers = [
PostController,
RequestController,
UserController,
UserReviewController,
];
3 changes: 2 additions & 1 deletion src/migrations/0000_AddAlteredPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class AddAlteredPrice1681680434289 implements MigrationInterface {
name: "altered_price",
type: "numeric",
scale: 2,
default: 0,
// originally was 0
default: -1,
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/PostModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class PostModel {
@Column("numeric", { scale: 2 })
original_price: number;

@Column("numeric", { scale: 2})
@Column("numeric", { scale: 2, default: -1 })
altered_price: number;

@Column("text", { array: true })
Expand Down
6 changes: 4 additions & 2 deletions src/models/UserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class UserModel {
@Column()
admin: boolean;

@Column({ type: "numeric" })
@Column({ type: "numeric", default: 0 })
stars: number;

@Column({ type: "integer" })
@Column({ type: "integer", default: 0 })
numReviews: number;

@Column({ nullable: true })
Expand Down Expand Up @@ -78,6 +78,8 @@ export class UserModel {
givenName: this.givenName,
familyName: this.familyName,
admin: this.admin,
stars: this.stars,
numReviews: this.numReviews,
photoUrl: this.photoUrl,
venmoHandle: this.venmoHandle,
email: this.email,
Expand Down
2 changes: 1 addition & 1 deletion src/models/UserReviewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class UserReviewModel{
comments: this.comments,
date: this.date,
buyer: this.buyer.getUserProfile(),
seller: this.seller.getUserProfile()
seller: this.seller.getUserProfile(),
};
}
}
6 changes: 3 additions & 3 deletions src/repositories/PostRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export class PostRepository extends AbstractRepository<PostModel> {
return await this.repository
.createQueryBuilder("post")
.leftJoinAndSelect("post.user", "user")
.where("post.price >= :lowerBound", {lowerBound: lowerBound})
.andWhere("post.price <= :upperBound", {upperBound: upperBound})
.where("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END >= :lowerBound", { lowerBound: lowerBound })
.andWhere("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END <= :upperBound", { upperBound: upperBound })
.andWhere("post.archive = false")
.getMany();
}
Expand All @@ -123,7 +123,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
return await this.repository.save(post)
}

public async editPostPrice(post: PostModel, new_price: number) : Promise<PostModel> {
public async editPostPrice(post: PostModel, new_price: number): Promise<PostModel> {
post.altered_price = new_price
return await this.repository.save(post)
}
Expand Down
3 changes: 2 additions & 1 deletion src/repositories/UserReviewRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export class UserReviewRepository extends AbstractRepository<UserReviewModel> {
return await this.repository
.createQueryBuilder("review")
.leftJoinAndSelect("review.buyer", "user")
.leftJoinAndSelect("review.seller", "user2")
.where("review.id = :id", { id })
.getOne();
}

public async createUserReview(
fulfilled: boolean,
stars: number,
Expand Down
18 changes: 8 additions & 10 deletions src/services/UserReviewService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { CreateUserReviewRequest } from 'src/types';
import { EntityManager } from 'typeorm';
import { ForbiddenError, NotFoundError } from 'routing-controllers';
import { InjectManager } from 'typeorm-typedi-extensions';
import Repositories from '../repositories';
import { Service } from 'typedi';
import { TransactionsManager } from 'src/repositories';
import { EntityManager } from 'typeorm';
import { InjectManager } from 'typeorm-typedi-extensions';

import { CreateUserReviewRequest } from '../types';
import Repositories, { TransactionsManager } from '../repositories';
import { UuidParam } from '../api/validators/GenericRequests';
import { UserReviewModel } from '../models/UserReviewModel';
import { UserModel } from '../models/UserModel';
import { UserReviewModel } from '../models/UserReviewModel';

@Service()
export class UserReviewService {
Expand Down Expand Up @@ -36,11 +35,10 @@ export class UserReviewService {

public async createUserReview(userReview: CreateUserReviewRequest): Promise<UserReviewModel> {
return this.transactions.readWrite(async (transactionalEntityManager) => {
const buyerRepository = Repositories.user(transactionalEntityManager);
const buyer = await buyerRepository.getUserById(userReview.buyerId);
const userRepository = Repositories.user(transactionalEntityManager);
const buyer = await userRepository.getUserById(userReview.buyerId);
if (!buyer) throw new NotFoundError('Buyer (reviewer) not found!');
const sellerRespository = Repositories.user(transactionalEntityManager);
const seller = await sellerRespository.getUserById(userReview.sellerId);
const seller = await userRepository.getUserById(userReview.sellerId);
if (!seller) throw new NotFoundError('Seller (reviewee) not found!');
const userReviewRepository = Repositories.userReview(transactionalEntityManager);
const freshUserReview = await userReviewRepository.createUserReview(userReview.fulfilled, userReview.stars, userReview.comments, buyer, seller);
Expand Down
20 changes: 18 additions & 2 deletions src/tests/PostTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ beforeEach(async () => {
expectedPost.archive = false;
expectedPost.categories = ['HANDMADE', 'OTHER'];
expectedPost.original_price = 500.15;
expectedPost.altered_price = -1;
expectedPost.images = ['https://upload.wikimedia.org/wikipedia/commons/thumb/4/48/Kombucha_Mature.jpg/640px-Kombucha_Mature.jpg', 'https://images.heb.com/is/image/HEBGrocery/001017916'];
expectedPost.location = 'The Dorm Hotel';
});
Expand Down Expand Up @@ -89,6 +90,7 @@ describe('post tests', () => {

const getPostResponse = await postController.getPostById(uuidParam);
getPostResponse.post.original_price = Number(getPostResponse.post.original_price);
getPostResponse.post.altered_price = Number(getPostResponse.post.altered_price);
expectedPost.created = getPostResponse.post.created;
expect(getPostResponse.post).toEqual(expectedPost);
});
Expand All @@ -106,11 +108,12 @@ describe('post tests', () => {

const getPostsResponse = await postController.getPostsByUserId(uuidParam);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
});

test('create post', async () => {
const user = UserFactory.fakeTemplate();

Expand Down Expand Up @@ -195,6 +198,7 @@ describe('post tests', () => {

const getPostsResponse = await postController.searchPosts(search);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand All @@ -217,6 +221,7 @@ describe('post tests', () => {

const getPostsResponse = await postController.searchPosts(search);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand All @@ -239,6 +244,7 @@ describe('post tests', () => {

const getPostsResponse = await postController.searchPosts(search);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand Down Expand Up @@ -281,6 +287,7 @@ describe('post tests', () => {

let getPostsResponse = await postController.filterPosts(filter);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand All @@ -291,6 +298,7 @@ describe('post tests', () => {

getPostsResponse = await postController.filterPosts(filter);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand Down Expand Up @@ -322,6 +330,7 @@ describe('post tests', () => {

let getPostsResponse = await postController.filterPostsByPrice(filter);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand All @@ -345,6 +354,7 @@ describe('post tests', () => {

let getPostsResponse = await postController.filterPostsByPrice(filter);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand All @@ -367,6 +377,7 @@ describe('post tests', () => {

let getPostsResponse = await postController.filterPostsByPrice(filter);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand Down Expand Up @@ -410,6 +421,7 @@ describe('post tests', () => {
postsResponse = await postController.getSavedPostsByUserId(user);
expect(postsResponse).not.toBeUndefined();
postsResponse.posts[0].original_price = Number(postsResponse.posts[0].original_price);
postsResponse.posts[0].altered_price = Number(postsResponse.posts[0].altered_price);
expectedPost.created = postsResponse.posts[0].created;
expect(postsResponse.posts).toEqual([expectedPost]);

Expand Down Expand Up @@ -474,6 +486,7 @@ describe('post tests', () => {

const getPostsResponse = await postController.getArchivedPostsByUserId(uuidParam);
getPostsResponse.posts[0].original_price = Number(getPostsResponse.posts[0].original_price);
getPostsResponse.posts[0].altered_price = Number(getPostsResponse.posts[0].altered_price);
expectedPost.created = getPostsResponse.posts[0].created;

expect(getPostsResponse.posts).toEqual([expectedPost]);
Expand Down Expand Up @@ -511,6 +524,7 @@ describe('post tests', () => {

const getPostResponse = await postController.archivePost(post.user, uuidParam);
getPostResponse.post.original_price = Number(getPostResponse.post.original_price);
getPostResponse.post.altered_price = Number(getPostResponse.post.altered_price);
expectedPost.created = getPostResponse.post.created;

expect(getPostResponse.post).toEqual(expectedPost);
Expand All @@ -532,7 +546,9 @@ describe('post tests', () => {
};

let getPostsResponse = await postController.editPrice(edit, post.user, uuidParam);
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
console.log(getPostsResponse)
expect(post.altered_price).toEqual(Number(getPostsResponse.new_price));
console.log(post.altered_price)
expect(Number(post.altered_price)).toEqual(Number(getPostsResponse.new_price));
})
});
Loading

0 comments on commit 6fba8c7

Please sign in to comment.