Skip to content

Commit

Permalink
Merge pull request #135 from 2020-NAVER-CAMPUS-HACKDAY/minsour/featur…
Browse files Browse the repository at this point in the history
…e/product-api

Change image url in product dto
  • Loading branch information
minsour authored May 30, 2020
2 parents 6792625 + 49a7d87 commit d40a0a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
4 changes: 2 additions & 2 deletions server/src/interfaces/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface IProduct {

export interface IProductDTO {
category: Object;
productNo: Number;
productNo: number;
name: String;
salePrice: Number;
productImages: Object;
productImages: any;
productInfoProvidedNoticeView: any;
like?: Boolean;
}
Expand Down
41 changes: 27 additions & 14 deletions server/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default class ProductService {
product.productInfoProvidedNoticeView.basic,
}));

products = await this.addLikeField(products);
await this.addLikeField(products);
this.changeImageUrl(products);

return { products };
} catch (e) {
Expand Down Expand Up @@ -82,12 +83,18 @@ export default class ProductService {
.skip(skip);
const products = productRecords
.map((record) => record.toObject())
.map((product) => ({
productId: product._id,
productName: product.name,
productImages: product.productImages[0],
salePrice: Number(product.salePrice),
}));
.map((product) => {
const index = product.productNo % 61;
product.productImages[0].url = `https://naver.github.io/egjs-infinitegrid/assets/image/${index}.jpg`;

return {
productId: product._id,
productName: product.name,
productImages: product.productImages[0],
salePrice: Number(product.salePrice),
};
});

return { products };
} catch (e) {
this.logger.error(e);
Expand All @@ -104,7 +111,9 @@ export default class ProductService {
throw new NotFoundError('Product is not exist');
}
let products: IProductDTO[] = [productRecord.toObject()];
products = await this.addLikeField(products);
await this.addLikeField(products);
this.changeImageUrl(products);

const product = products[0];

return {
Expand All @@ -125,7 +134,7 @@ export default class ProductService {
}
}

public async addLikeField(products: IProductDTO[]): Promise<IProductDTO[]> {
public async addLikeField(products: IProductDTO[]): Promise<void> {
try {
const userLikeRecord = await this.userModel
.findOne({
Expand All @@ -146,18 +155,22 @@ export default class ProductService {
);
const likeSet = new Set<number>(likeList);

products = products.map((product) => {
for (const product of products) {
product.like = likeSet.has(product.productNo as number) ? true : false;
return product as IProductDTO;
});

return products;
}
} catch (e) {
this.logger.error(e);
throw e;
}
}

public changeImageUrl(products: IProductDTO[]): void {
for (const product of products) {
const index = product.productNo % 61;
product.productImages[0].url = `https://naver.github.io/egjs-infinitegrid/assets/image/${index}.jpg`;
}
}

public async create(productDTO: IProductDTO): Promise<{ product: IProduct }> {
try {
this.logger.silly('Creating user db record');
Expand Down
15 changes: 10 additions & 5 deletions server/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export default class UserService {
weight,
}: any): Promise<any> => {
return new Promise(async (resolve, reject) => {
const userRecord = await this.userModel.findOne({ userName: config.personaName });
const userRecord = await this.userModel.findOne({
userName: config.personaName,
});
if (!userRecord) {
reject('User is not exist');
}
Expand Down Expand Up @@ -138,17 +140,19 @@ export default class UserService {
* @param productNo
*/
public async clickLog(productNo: number): Promise<any> {
const userLog: UserLog = { userName: config.personaName, type: 'click_log', item: String(productNo) };
const userLog: UserLog = {
userName: config.personaName,
type: 'click_log',
item: String(productNo),
};
try {
const result = await this.addWeight(productNo, config.clicklogWeight);
await kafka(config.personaId, userLog);

return result;

} catch (e) {
this.logger.error(e);
}

}

/**
Expand Down Expand Up @@ -317,7 +321,8 @@ export default class UserService {
result = await addRemainder(remainder, result);
}

result = await this.productService.addLikeField(result);
await this.productService.addLikeField(result);
this.productService.changeImageUrl(result);

return result;
} catch (e) {
Expand Down

0 comments on commit d40a0a6

Please sign in to comment.