Skip to content

Commit

Permalink
Restructure project
Browse files Browse the repository at this point in the history
  • Loading branch information
kevynsax committed Jun 30, 2020
1 parent 6a284b5 commit 2746aaf
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 71 deletions.
12 changes: 7 additions & 5 deletions src/model/blog/BlogApp.ts → src/blog/BlogApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Reaction} from "src/model/blog/types";
import {IReactionRepo} from "src/model/blog/ReactionRepo";
import {Reaction} from "src/blog/types";
import {IReactionRepo} from "src/blog/ReactionRepo";

export const active = true;

export interface IBlogApp {
createReaction: (reaction: Reaction) => Promise<void>;
Expand All @@ -15,9 +17,9 @@ export class BlogApp implements IBlogApp{
await this.repo.create(reaction);
};

listReactionsByUser = (id: string) => {
throw new Error("not implemented");
};
listReactionsByUser = (id: string) =>
this.repo.find({idUser: id, active})
.then(lst => lst.map(({type, idArticle, idUser}) => ({type, idArticle, idUser} as Reaction)));

deleteReaction = (idUser: string, idArticle: string) => {
throw new Error("not implemented");
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/BlogController.ts → src/blog/BlogController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {IBlogApp} from "src/model/blog/BlogApp";
import {Reaction} from "src/model/blog/types";
import {ClassMiddleware, Controller, Delete, Get, Post} from "@overnightjs/core";
import {Endpoint, injectHelpers} from "src/middleware/injectHelpers";
import {DeleteReactionViewModel} from "src/viewModel/blogViewModels";
import {httpStatusCode} from "src/constants";
import {IBlogApp} from "src/blog/BlogApp";
import {Reaction} from "src/blog/types";
import {DeleteReactionViewModel} from "src/blog/blogViewModels";

export const blogRoute = "api/blog";
export const reactionRoute = `reaction`;
export const reactionRoute = "reaction";

@Controller(blogRoute)
@ClassMiddleware(injectHelpers)
Expand All @@ -33,7 +33,7 @@ export class BlogController {
@Delete(reactionRoute)
public removeReaction: Endpoint = (req, {unwrap}) => {
const {idArticle, idUser} = req.body as DeleteReactionViewModel;

const result = this.app.deleteReaction(idUser, idArticle);
return unwrap(result, httpStatusCode.success);
};
Expand Down
9 changes: 6 additions & 3 deletions src/model/blog/ReactionRepo.ts → src/blog/ReactionRepo.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {Reaction, ReactionTypeEnum} from "src/model/blog/types";
import {Document, model, Model, Schema} from "mongoose";
import {Reaction, ReactionTypeEnum} from "src/blog/types";

export const reactionCollection = 'blogReactions';
export type ReactionModel = Reaction & Document;
export type ReactionModel = Reaction & Document & {active?: boolean};

export type IReactionRepo = Model<ReactionModel>;

const schema = new Schema<ReactionModel>({
type: {type: ReactionTypeEnum, required: true},
idArticle: {type: String, required: true},
idUser: {type: String, required: true}
idUser: {type: String, required: true},
active: {type: Boolean, required: true, default: true}
});



export const ReactionRepo =
model<ReactionModel>(reactionCollection, schema, reactionCollection);

File renamed without changes.
6 changes: 3 additions & 3 deletions src/model/blog/types.ts → src/blog/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export enum ReactionTypeEnum{
like,
dislike
dislike,
}

export interface Reaction {
type: ReactionTypeEnum,
idArticle: string
idUser: string
idArticle: string,
idUser: string,
}
65 changes: 65 additions & 0 deletions src/tests/blog/BlogApp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {successPromise} from "src/tests/utilsTest";
import {Reaction, ReactionTypeEnum} from "src/blog/types";
import {active, BlogApp} from "src/blog/BlogApp";
import {IReactionRepo, ReactionModel} from "src/blog/ReactionRepo";

const fakeReaction: Reaction = {
type: ReactionTypeEnum.dislike,
idArticle: "Fake id article",
idUser: "48597"
};

describe('Blog app', () => {
const getTarget = (repo: any) =>
new BlogApp(repo as IReactionRepo);

test('Insert Reaction', async () => {
// given
const create = jest.fn()
.mockImplementationOnce(() =>
new Promise<ReactionModel>((resolve) => resolve(fakeReaction as ReactionModel)));

const target = getTarget({create});
// when
const action = () => target.createReaction(fakeReaction);

// then
await expect(action()).resolves;
expect(create).toBeCalledWith(fakeReaction);
});

test('list reactions', async () => {

const listFakeReaction = [{
...fakeReaction,
_id: "this shouldn't show to controller"
}];
const expected = {
idUser: fakeReaction.idUser,
active
};

const find = jest.fn()
.mockImplementationOnce(() =>
new Promise<ReactionModel[]>(resolve => resolve(listFakeReaction as ReactionModel[])));

const target = getTarget({find});

const result = await target.listReactionsByUser(fakeReaction.idUser);

expect(result).toEqual([fakeReaction]);
expect(find).toBeCalledWith(expected);
});

test('delete reaction', async () => {
const findOneAndUpdate = jest.fn()
.mockImplementationOnce(successPromise);

const target = getTarget({findOneAndUpdate});

const action = () => target.deleteReaction(fakeReaction.idUser, fakeReaction.idArticle);

await expect(action()).resolves;
expect(findOneAndUpdate).toBeCalledWith(fakeReaction, {active: false});
})
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Request} from "express";
import {BlogController, blogRoute} from "src/controllers/BlogController";
import {IBlogApp} from "src/model/blog/BlogApp";
import {Reaction, ReactionTypeEnum} from "src/model/blog/types";
import {httpStatusCode} from "src/constants";
import request from 'supertest';
import {ResumeServer} from "src/ResumeServer";
import {ResumeResponse} from "src/middleware/injectHelpers";
import Mock = jest.Mock;
import {successPromise} from "src/tests/utilsTest";
import {DeleteReactionViewModel} from "src/viewModel/blogViewModels";
import {Reaction, ReactionTypeEnum} from "src/blog/types";
import {DeleteReactionViewModel} from "src/blog/blogViewModels";
import {BlogController, blogRoute} from "src/blog/BlogController";
import {IBlogApp} from "src/blog/BlogApp";


const fakeIdUser = "fake id user";
const fakeIdArticle = "fake id article";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MongoMemoryServer} from 'mongodb-memory-server-global';
import {connect, disconnect, mongo, connection} from "mongoose";
import {Reaction, ReactionTypeEnum} from "src/model/blog/types";
import {ReactionRepo} from 'src/model/blog/ReactionRepo';
import {connect} from "mongoose";
import { Reaction, ReactionTypeEnum} from "src/blog/types";
import {ReactionRepo} from "src/blog/ReactionRepo";


describe('Reaction Repo tests', () => {
Expand Down Expand Up @@ -35,5 +35,6 @@ describe('Reaction Repo tests', () => {
expect(savedReaction.type).toBe(fakeReaction.type);
expect(savedReaction.idArticle).toBe(fakeReaction.idArticle);
expect(savedReaction.idUser).toBe(fakeReaction.idUser);
expect(savedReaction.active).toBe(true);
})
});
41 changes: 0 additions & 41 deletions src/tests/model/blog/BlogApp.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IUserRepo, UserModel} from "src/model/user/UserRepo";
import {UserApp} from "src/model/user/UserApp";
import {AxiosError, AxiosResponse, AxiosStatic} from "axios";
import {Geolocation, User} from "src/model/user/types";
import {IUserRepo, UserModel} from "src/user/UserRepo";
import {UserApp} from "src/user/UserApp";
import {User, Geolocation} from "src/user/types";

const fakeGeolocation = {
continent_code: "SA",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Request} from "express";
import {IUserApp} from "src/model/user/UserApp";
import {UserController} from "src/controllers/UserController";
import {CreateUserViewModel, UserViewModel} from "src/viewModel/userViewModels";
import {ResumeResponse} from "src/middleware/injectHelpers";
import request from "supertest";
import {ResumeServer} from "src/ResumeServer";
import {httpStatusCode} from "src/constants";
import {CreateUserViewModel, UserViewModel} from "src/user/userViewModels";
import {UserController} from "src/user/UserController";
import {IUserApp} from "src/user/UserApp";

const fakeRequestUser: CreateUserViewModel = {
ipAddress: "192.404.303.022",
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/model/user/UserRepo.ts → src/user/UserRepo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {User} from "src/model/user/types";
import {Model, Document} from "mongoose";
import {User} from "src/user/types";

export type UserModel = User & Document;

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 2746aaf

Please sign in to comment.