Skip to content

add models for all the app #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
# Logs
logs
*.log
Expand Down
4 changes: 0 additions & 4 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import expressFormidable from 'express-formidable';
// import adminBroMongoose from 'admin-bro-mongoose';

import { checkAuth } from './utils/auth';
import visitRouter from './resources/visit/visit.router';
import roadmapRouter from './resources/roadmap/roadmap.router';
import subjectRouter from './resources/subject/subject.router';
import linkRouter from './resources/link/link.router';

export const app = express();
Expand All @@ -29,9 +27,7 @@ app.use(
app.use(expressFormidable()); // needed for adminbro

// public routes
app.use('/visits', visitRouter);
app.use('/roadmaps', roadmapRouter);
app.use('/subjects', subjectRouter);

// protected routes
app.use('/', checkAuth);
Expand Down
9 changes: 2 additions & 7 deletions app/resources/link/link.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const linkSchema = new mongoose.Schema(
required: true,
default: 0,
},
topic: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'topic',
required: true,
},
createdBy: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'user',
Expand All @@ -37,10 +32,10 @@ const linkSchema = new mongoose.Schema(
}
);

linkSchema.path('url').validate((val: string) => {
/*linkSchema.path('url').validate((val: string) => {
const urlRegex = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/;
return urlRegex.test(val);
}, 'Invalid URL.');
}, 'Invalid URL.');*/

linkSchema.index({ topic: 1, url: 1 }, { unique: true });

Expand Down
42 changes: 42 additions & 0 deletions app/resources/post/post.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mongoose from 'mongoose';

const postSchema = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
image: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
author: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'user',
},
dateCreated: {
type: Date,
default: Date.now,
},
tags: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'tag',
},
],
},
{
writeConcern: {
w: 'majority',
j: true,
wtimeout: 1000,
},
timestamps: true,
}
);

export default mongoose.model('post', postSchema);
Empty file.
11 changes: 4 additions & 7 deletions app/resources/roadmap/roadmap.controllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { crudControllers } from '../../utils/crud';
import roadmapModel from './roadmap.model';
import subjectModel from '../subject/subject.model';
import topicModel from '../topic/topic.model';

export const getRoadmap = async (req: any, res: any) => {
try {
Expand All @@ -13,16 +13,13 @@ export const getRoadmap = async (req: any, res: any) => {
return res.status(400).end();
}

const subjects = await subjectModel
.find({ roadmap: roadmap._id })
.lean()
.exec();
const topic = await topicModel.find({ roadmap: roadmap._id }).lean().exec();

if (!subjects) {
if (!topic) {
return res.status(400).end();
}

res.status(200).json({ ...roadmap, subjects });
res.status(200).json({ ...roadmap, topic });
} catch (e) {
console.error(e);
res.status(400).end();
Expand Down
14 changes: 10 additions & 4 deletions app/resources/roadmap/roadmap.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import mongoose from 'mongoose';

const roadmapSchema = new mongoose.Schema(
{
name: {
title: {
type: String,
unique: true,
required: true,
},
title: {
description: {
type: String,
unique: true,
required: true,
},
description: {
image: {
type: String,
required: true,
},
tech: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'tech',
order: Number,
},
],
},
{
writeConcern: {
Expand Down
41 changes: 0 additions & 41 deletions app/resources/subject/subject.model.ts

This file was deleted.

11 changes: 0 additions & 11 deletions app/resources/subject/subject.router.ts

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import mongoose from 'mongoose';

const visitSchema = new mongoose.Schema(
const tagSchema = new mongoose.Schema(
{
created_at: { type: Date, default: Date.now },
name: {
type: String,
unique: true,
},
},
{
writeConcern: {
Expand All @@ -14,4 +17,4 @@ const visitSchema = new mongoose.Schema(
}
);

export default mongoose.model('visit', visitSchema);
export default mongoose.model('tag', tagSchema);
Empty file added app/resources/tag/tag.router.ts
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions app/resources/tech/tech.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mongoose from 'mongoose';

const techSchema = new mongoose.Schema(
{
name: {
type: String,
unique: true,
},
description: {
type: String,
required: true,
},
image: {
type: String,
required: true,
},
topics: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'topic',
order: Number,
},
],
links: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'link',
order: Number,
},
],
},
{
writeConcern: {
w: 'majority',
j: true,
wtimeout: 1000,
},
timestamps: true,
}
);

export default mongoose.model('tech', techSchema);
1 change: 1 addition & 0 deletions app/resources/tech/tech.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import subjectModel from './subject.model';
import topicModel from './topic.model';

export const createOne = async (req: any, res: any) => {
try {
const doc = await subjectModel.create(req.body);
const doc = await topicModel.create(req.body);
res.status(201).json(doc);
} catch (e) {
console.error(e);
Expand All @@ -12,7 +12,7 @@ export const createOne = async (req: any, res: any) => {

export const getMany = async (_: any, res: any) => {
try {
const docs = await subjectModel.find({}).lean().exec();
const docs = await topicModel.find({}).lean().exec();

res.status(200).json(docs);
} catch (e) {
Expand Down
21 changes: 19 additions & 2 deletions app/resources/topic/topic.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose from 'mongoose';

const topicSchema = new mongoose.Schema(
{
name: {
title: {
type: String,
unique: true,
required: true,
Expand All @@ -11,6 +11,18 @@ const topicSchema = new mongoose.Schema(
type: String,
required: true,
},
links: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'link',
},
],
posts: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'post',
},
],
},
{
writeConcern: {
Expand All @@ -22,4 +34,9 @@ const topicSchema = new mongoose.Schema(
}
);

export default mongoose.model('subject', topicSchema);
/*subjectSchema.path('image_url').validate((val: string) => {
const urlRegex = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/;
return urlRegex.test(val);
}, 'Invalid URL');*/

export default mongoose.model('topic', topicSchema);
10 changes: 0 additions & 10 deletions app/resources/topic/topic.router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
// Javascript
// Definicion de javascript

// Variables

// link1 (10)
// link2 (8)
// link3 (5)

// FP
Empty file.
38 changes: 38 additions & 0 deletions app/resources/user/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema(
{
auth_id: {
type: String,
unique: true,
},
role: {
type: String,
},
first_name: {
type: String,
},
last_name: {
type: String,
},
email: {
type: String,
},
topics: [
{
type: mongoose.SchemaTypes.ObjectId,
ref: 'topic',
},
],
},
{
writeConcern: {
w: 'majority',
j: true,
wtimeout: 1000,
},
timestamps: true,
}
);

export default mongoose.model('user', userSchema);
Empty file.
Loading