Skip to content

Commit

Permalink
Merge pull request #2 from thejoltjoker/api/user/favorites
Browse files Browse the repository at this point in the history
Remove UserFavorite model, add hash to all img results,
  • Loading branch information
thejoltjoker authored Mar 3, 2024
2 parents 8c68e15 + 52e92ff commit 5c06878
Show file tree
Hide file tree
Showing 14 changed files with 810 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,8 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
server/image-search-db.json
server/exampleResponse.json
server/fest.js
server/db.json
server/src/services/mockResult.json
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"version": "1.0.0",
"description": "Full-stack app integrating Google Custom Search and Auth0 for user login, image search, and saving favorites. First assignment of FSU23D Integration course.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {}
}
2 changes: 1 addition & 1 deletion server/favorites.rest
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Content-Type: application/json
"thumbnailLink": "http://example.com/thumb.jpg",
"thumbnailWidth": 123,
"thumbnailHeight": 456,
"favoriteId":"6163f09ac470dd7d34061aa26f24eaafc263a44b"
"imageId":"6163f09ac470dd7d34061aa26f24eaafc263a44b"
}

###
Expand Down
7 changes: 3 additions & 4 deletions server/src/controllers/favorites.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextFunction, Request, Response } from "express";
import { ImageItem } from "../models/ImageItem";
import { UserFavorite } from "../models/UserFavorite";
import {
create as createFavorite,
get as getFavorites,
Expand Down Expand Up @@ -44,7 +43,7 @@ export const create = async (
};

export const update = async (
req: Request<{ userId: string; favoriteId: string }, {}, UserFavorite, {}>,
req: Request<{ userId: string; imageId: string }, {}, ImageItem, {}>,
res: Response,
next: NextFunction
) => {
Expand All @@ -61,14 +60,14 @@ export const update = async (
};

export const remove = async (
req: Request<{ userId: string; favoriteId: string }, {}, {}, {}>,
req: Request<{ userId: string; imageId: string }, {}, {}, {}>,
res: Response,
next: NextFunction
) => {
try {
const response = await removeFavorite(
req.params.userId,
req.params.favoriteId
req.params.imageId
);
if (!response) {
return res.status(404).json({ message: "User not found" });
Expand Down
2 changes: 2 additions & 0 deletions server/src/controllers/search.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextFunction, Request, Response } from "express";
import { sha1digest } from "../helpers/strings";
import { ImageItem } from "../models/ImageItem";
import { search } from "../services/googleCustomSearch";

Expand All @@ -12,6 +13,7 @@ export const get = async (req: Request, res: Response, next: NextFunction) => {
const { contextLink, thumbnailLink, thumbnailWidth, thumbnailHeight } =
item.image;
return new ImageItem(
sha1digest(link),
title,
snippet,
contextLink,
Expand Down
1 change: 1 addition & 0 deletions server/src/models/ImageItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export class ImageItem {
constructor(
public imageId: string,
public title: string,
public snippet: string,
public contextLink: string,
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UserFavorite } from "./UserFavorite";
import { ImageItem } from "./ImageItem";

export class User {
constructor(
public email: string,
public favorites: UserFavorite[],
public favorites: ImageItem[],
public userId?: string
) {}
}
5 changes: 0 additions & 5 deletions server/src/models/UserFavorite.ts

This file was deleted.

7 changes: 3 additions & 4 deletions server/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
update as updateFavorite,
} from "../controllers/favorites.controller";
import { create, get, remove, update } from "../controllers/user.controller";
import { UserFavoriteSchema } from "../schemas/favorite.schema";
import { ImageItemSchema } from "../schemas/imageItem.schema";
import { UserSchema } from "../schemas/user.schema";
import { validate } from "../validate";
Expand All @@ -21,10 +20,10 @@ router.delete("/:userId", remove);
router.get("/:userId/favorites", getFavorite);
router.post("/:userId/favorites", validate(ImageItemSchema), createFavorite);
router.put(
"/:userId/favorites/:favoriteId",
validate(UserFavoriteSchema),
"/:userId/favorites/:imageId",
validate(ImageItemSchema),
updateFavorite
);
router.delete("/:userId/favorites/:favoriteId", removeFavorite);
router.delete("/:userId/favorites/:imageId", removeFavorite);

export default router;
12 changes: 0 additions & 12 deletions server/src/schemas/favorite.schema.ts

This file was deleted.

1 change: 1 addition & 0 deletions server/src/schemas/imageItem.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ImageItemSchema = Joi.object({
thumbnailLink: Joi.string().uri(),
thumbnailWidth: Joi.number(),
thumbnailHeight: Joi.number(),
imageId: Joi.string().length(40).required(),
});

export const ImageSchemaSwagger = j2s(ImageItemSchema).swagger;
Expand Down
16 changes: 7 additions & 9 deletions server/src/services/favorites.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { sha1digest } from "../helpers/strings";
import { ImageItem } from "../models/ImageItem";
import { UserFavorite } from "../models/UserFavorite";
import { read } from "./database";
import { update as updateUser } from "./user.service";

Expand All @@ -14,18 +12,18 @@ export const get = async (userId: string): Promise<ImageItem[] | undefined> => {
export const create = async (
userId: string,
image: ImageItem
): Promise<UserFavorite | undefined> => {
): Promise<ImageItem | undefined> => {
const database = await read();
const user = database.users.find((u) => u.userId === userId);
if (!user) return;

const existingFavorite = user.favorites.find(
(img) => img.link === image.link
(img) => img.imageId === image.imageId
);
if (existingFavorite) {
return existingFavorite;
}
const newFavorite = { ...image, favoriteId: sha1digest(image.link) };
const newFavorite = image;
user.favorites.push(newFavorite);
await updateUser(user);

Expand All @@ -34,8 +32,8 @@ export const create = async (

export const update = async (
userId: string,
favorite: UserFavorite
): Promise<UserFavorite | undefined> => {
favorite: ImageItem
): Promise<ImageItem | undefined> => {
const database = await read();
const user = database.users.find((u) => u.userId === userId);
if (!user) return;
Expand All @@ -51,13 +49,13 @@ export const update = async (

export const remove = async (
userId: string,
favoriteId: string
imageId: string
): Promise<boolean | undefined> => {
const database = await read();
const user = database.users.find((u) => u.userId === userId);
if (!user) return;

user.favorites = user.favorites.filter((img) => img.favoriteId != favoriteId);
user.favorites = user.favorites.filter((img) => img.imageId != imageId);

await updateUser(user);
return true;
Expand Down
30 changes: 14 additions & 16 deletions server/src/services/googleCustomSearch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import axios from "axios";
import "dotenv/config";
import { promises as fs } from "fs";
import path from "path";
import { SocksProxyAgent } from "socks-proxy-agent";
import { SearchResult } from "../models/SearchResult";

Expand Down Expand Up @@ -33,20 +31,20 @@ export const search = async (
url.searchParams.append("num", num);
url.searchParams.append("q", query);

// TODO Remove proxy before publishing
if (process.env.NODE_ENV == "production") {
const response = await axios.get<SearchResult>(url.toString(), {
httpAgent: proxyAgent,
httpsAgent: proxyAgent,
});
return response.data;
} else {
const data = await fs.readFile(
path.resolve(__dirname, "./mockResult.json"),
"utf-8"
);
return JSON.parse(data);
}
// // TODO Remove proxy before publishing
// if (process.env.NODE_ENV == "production") {
const response = await axios.get<SearchResult>(url.toString(), {
httpAgent: proxyAgent,
httpsAgent: proxyAgent,
});
return response.data;
// } else {
// const data = await fs.readFile(
// path.resolve(__dirname, "./mockResult.json"),
// "utf-8"
// );
// return JSON.parse(data);
// }
} catch (error) {
console.error("Error while performing search", error);
throw error;
Expand Down
Loading

0 comments on commit 5c06878

Please sign in to comment.