Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bookmarks #87

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 57 additions & 40 deletions src/repositories/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PostModel } from 'src/models/PostModel';
import { AbstractRepository, EntityRepository } from 'typeorm';
import { PostModel } from "src/models/PostModel";
import { AbstractRepository, EntityRepository } from "typeorm";

import { ConflictError } from '../errors';
import { NotFoundError } from 'routing-controllers';
import { UserModel } from '../models/UserModel';
import { Uuid } from '../types';
import { NotFoundError } from "routing-controllers";
import { ConflictError } from "../errors";
import { UserModel } from "../models/UserModel";
import { Uuid } from "../types";

@EntityRepository(UserModel)
export class UserRepository extends AbstractRepository<UserModel> {
Expand All @@ -19,7 +19,9 @@ export class UserRepository extends AbstractRepository<UserModel> {
.getOne();
}

public async getUserWithBlockedInfo(id: Uuid): Promise<UserModel | undefined> {
public async getUserWithBlockedInfo(
id: Uuid
): Promise<UserModel | undefined> {
return this.repository
.createQueryBuilder("user")
.leftJoinAndSelect("user.blocking", "user_blocking_users.blocking")
Expand All @@ -28,7 +30,9 @@ export class UserRepository extends AbstractRepository<UserModel> {
.getOne();
}

public async getUserByGoogleId(googleId: Uuid): Promise<UserModel | undefined> {
public async getUserByGoogleId(
googleId: Uuid
): Promise<UserModel | undefined> {
return await this.repository
.createQueryBuilder("user")
.where("user.googleId = :googleId", { googleId })
Expand All @@ -41,8 +45,19 @@ export class UserRepository extends AbstractRepository<UserModel> {
return post;
}

public async unsavePost(user: UserModel, post: PostModel): Promise<PostModel> {
user.saved.splice(user.saved.indexOf(post));
public async unsavePost(
user: UserModel,
post: PostModel
): Promise<PostModel> {
const postIndex = user.saved.findIndex(
(savedPost) => savedPost.id === post.id
);
if (postIndex === -1) {
throw new NotFoundError(
"Tried to unsave post that was not found in user's saved posts."
);
}
user.saved.splice(postIndex, 1);
await this.repository.save(user);
return post;
}
Expand Down Expand Up @@ -78,28 +93,26 @@ export class UserRepository extends AbstractRepository<UserModel> {
familyName: string,
photoUrl: string,
email: string,
googleId: string,
googleId: string
): Promise<UserModel> {
let existingUser = await this.repository
.createQueryBuilder("user")
.where("user.username = :username", { username })
.orWhere("user.netid = :netid", { netid })
.orWhere("user.email = :email", { email })
.orWhere("user.googleId = :googleId", { googleId })
.getOne();
.createQueryBuilder("user")
.where("user.username = :username", { username })
.orWhere("user.netid = :netid", { netid })
.orWhere("user.email = :email", { email })
.orWhere("user.googleId = :googleId", { googleId })
.getOne();
if (existingUser) {
if (existingUser.username === username) {
throw new ConflictError('UserModel with same username already exists!');
}
else if (existingUser.netid === netid)
{
throw new ConflictError('UserModel with same netid already exists!');
}
else if (existingUser.email === email) {
throw new ConflictError('UserModel with same email already exists!');
}
else {
throw new ConflictError('UserModel with same google ID already exists!');
throw new ConflictError("UserModel with same username already exists!");
} else if (existingUser.netid === netid) {
throw new ConflictError("UserModel with same netid already exists!");
} else if (existingUser.email === email) {
throw new ConflictError("UserModel with same email already exists!");
} else {
throw new ConflictError(
"UserModel with same google ID already exists!"
);
}
}
const adminEmails = process.env.ADMIN_EMAILS?.split(",");
Expand All @@ -122,15 +135,15 @@ export class UserRepository extends AbstractRepository<UserModel> {
username: string | undefined,
photoUrl: string | undefined,
venmoHandle: string | undefined,
bio: string | undefined,
bio: string | undefined
): Promise<UserModel> {
const existingUser = this.repository
.createQueryBuilder("user")
.where("user.username = :username", { username })
.getOne();
if (await existingUser) {
if (username !== user.username) {
throw new ConflictError('UserModel with same username already exists!');
throw new ConflictError("UserModel with same username already exists!");
}
}

Expand All @@ -152,26 +165,30 @@ export class UserRepository extends AbstractRepository<UserModel> {

public async blockUser(
blocker: UserModel,
blocked: UserModel,
blocked: UserModel
): Promise<UserModel> {
if (blocker.blocking === undefined) { blocker.blocking = [blocked]; }
else { blocker.blocking.push(blocked); }
if (blocker.blocking === undefined) {
blocker.blocking = [blocked];
} else {
blocker.blocking.push(blocked);
}
return this.repository.save(blocker);
}

public async unblockUser(
blocker: UserModel,
blocked: UserModel,
blocked: UserModel
): Promise<UserModel> {
if (blocker.blocking === undefined) {
throw new NotFoundError("User has not been blocked!")
}
else {
throw new NotFoundError("User has not been blocked!");
} else {
if (!blocker.blocking.find((user) => user.id === blocked.id)) {
throw new NotFoundError("User has not been blocked!")
throw new NotFoundError("User has not been blocked!");
}
// remove blocked user from blocking list
blocker.blocking = blocker.blocking.filter((user) => user.id !== blocked.id);
blocker.blocking = blocker.blocking.filter(
(user) => user.id !== blocked.id
);
}
return this.repository.save(blocker);
}
Expand All @@ -180,4 +197,4 @@ export class UserRepository extends AbstractRepository<UserModel> {
user.isActive = false;
return this.repository.save(user);
}
}
}