Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd1ff76
commit 98f40b8
Showing
23 changed files
with
393 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { BaseModel, Entity, Field } from '@lib/mongoose'; | ||
import { Types, models, modelNames } from 'mongoose'; | ||
import { Constants } from '@core/helpers'; | ||
|
||
@Entity(Constants.Schemas.favorites) | ||
export class FavoritesSchema { | ||
@Field({ lowercase: false }) public user_id: Types.ObjectId; | ||
@Field({ lowercase: false }) item_id: Types.ObjectId; | ||
@Field({ | ||
enum: [Constants.Schemas.meals, Constants.Schemas.menus] | ||
}) public type: string; | ||
|
||
// @Virtual('assigned', { | ||
// ref: doc => doc.type, | ||
// localField: 'item_id', | ||
// foreignField: '_id', | ||
// justOne: false | ||
// }) | ||
// t(){} | ||
} | ||
export const FavoritesModel = BaseModel<FavoritesSchema>(FavoritesSchema); | ||
FavoritesModel.schema.virtual('items', { | ||
ref: doc => doc.type, | ||
localField: 'item_id', | ||
foreignField: '_id', | ||
justOne: true | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Body } from '@lib/mongoose'; | ||
import { FavoritesModel, FavoritesSchema } from './favorites.model'; | ||
import { models } from 'mongoose'; | ||
|
||
type FavoritesModelBody = Body<FavoritesSchema>; | ||
|
||
export class FavoritesRepo extends FavoritesModel { | ||
|
||
public static async createEntity(doc: FavoritesModelBody) { | ||
const entity = new FavoritesRepo(doc); | ||
const type = doc.type; | ||
if (!models[type]) { | ||
return { passed: false, msg: 'is not one of the supported type' } | ||
} | ||
const isThere = await models[type].findById(doc.item_id).lean(); | ||
if (!isThere) { | ||
return { passed: false, msg: 'type is not associated with the item_id' } | ||
} | ||
console.warn('passed'); | ||
return { passed: true, entity: entity.save() }; | ||
} | ||
|
||
public static deleteEntity(id: string) { | ||
return this.findOneAndDelete({ _id: id }); | ||
} | ||
|
||
public static fetchEntities(obj?: Partial<FavoritesModelBody>, ...args) { | ||
return this.find(obj, ...args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Auth } from '@api/portal'; | ||
import { ErrorResponse, NetworkStatus, SuccessResponse, tokenService } from '@core/helpers'; | ||
import { Logger } from '@core/utils'; | ||
import { Delete, Get, Post, Router } from '@lib/methods'; | ||
import { translate } from '@lib/translation'; | ||
import { Request, Response } from 'express'; | ||
import { FavoritesRepo } from './favorites.repo'; | ||
const log = new Logger('FavoritesRouter'); | ||
import { Constants } from '@core/helpers'; | ||
import { Types } from 'mongoose'; | ||
|
||
@Router(Constants.Endpoints.favorites) | ||
export class FavoritesRouter { | ||
private repo = FavoritesRepo; | ||
|
||
@Post('', Auth.isAuthenticated) | ||
public async create(req: Request, res: Response) { | ||
const { item_id, type } = req.body; | ||
// TODO: make an interface for user token | ||
const decodedToken = await tokenService.decodeToken<any>(req.headers.authorization); | ||
const result = await this.repo.createEntity({ type, user_id: decodedToken.id, item_id }); | ||
let response; | ||
if (result.passed) { | ||
response = new SuccessResponse(result.entity, translate('success'), NetworkStatus.CREATED); | ||
} else { | ||
response = new ErrorResponse(result.msg, NetworkStatus.BAD_REQUEST); | ||
} | ||
res.status(response.code).json(response); | ||
} | ||
|
||
@Delete(':id', Auth.isAuthenticated) | ||
public async delete(req: Request, res: Response) { | ||
const { id } = req.params; | ||
const entity = await this.repo.deleteEntity(id); | ||
if (!entity) { | ||
throw new ErrorResponse(translate('entity_not_found'), NetworkStatus.NOT_ACCEPTABLE); | ||
} | ||
const response = new SuccessResponse(null, translate('success'), NetworkStatus.OK); | ||
res.status(response.code).json(response); | ||
} | ||
|
||
@Get(':type') | ||
public async fetchFavMeals(req: Request, res: Response) { | ||
const decodedToken = await tokenService.decodeToken<any>(req.headers.authorization); | ||
const entites = await this.repo.fetchEntities({ user_id: decodedToken.id, type: req.params.type }).populate('items'); | ||
const response = new SuccessResponse(entites, translate('success'), NetworkStatus.OK); | ||
response['count'] = entites.length; | ||
res.status(response.code).json(response); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './favorites.model'; | ||
export * from './favorites.repo'; | ||
export * from './favorites.routes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export namespace Constants { | ||
export class Endpoints { | ||
static readonly meals = 'meals'; | ||
static readonly favorites = 'favorites'; | ||
static readonly menus = 'menus'; | ||
|
||
} | ||
export class Schemas { | ||
static readonly favorites = 'favorites'; | ||
static readonly meals = 'meals'; | ||
static readonly menus = 'menus'; | ||
} | ||
} |
Oops, something went wrong.