Skip to content

Commit

Permalink
Init project + /artists route
Browse files Browse the repository at this point in the history
  • Loading branch information
isaldin committed Dec 26, 2019
0 parents commit 15e509a
Show file tree
Hide file tree
Showing 10 changed files with 1,668 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Node Inspector",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/index.ts"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"TS_NODE_IGNORE": "false"
}
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"tslint.packageManager": "yarn"
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nodemon --watch 'src/**/*' -e ts,tsx --exec ts-node ./src/index.ts"
},
"dependencies": {
"koa": "2.11.0",
"koa-router": "7.4.0",
"mongoose": "5.8.3",
"ramda": "^0.26.1"
},
"devDependencies": {
"@types/koa": "2.11.0",
"@types/koa-router": "7.0.42",
"@types/mongoose": "5.5.35",
"@types/ramda": "^0.26.39",
"nodemon": "2.0.2",
"ts-node": "8.5.4",
"tslint": "5.20.1",
"tslint-config-prettier": "1.18.0",
"typescript": "3.7.4"
}
}
39 changes: 39 additions & 0 deletions src/controllers/artist.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Artist, { IArtist } from "../models/artist.model";

interface IAllArtistsInput {
offset: number;
limit: number;
search: string | null;
}
const AllArtists = async (input: IAllArtistsInput): Promise<IArtist[]> => {
return Artist.aggregate([
{
$match: {
$or: [
{ name: { $regex: new RegExp(input.search, "ig") } },
{ username: { $regex: new RegExp(input.search, "ig") } }
]
}
},
{
$lookup: {
as: "tracks",
foreignField: "_id",
from: "tracks",
localField: "tracks"
}
},
{
$addFields: {
rating: {
$sum: "$tracks.rating"
}
}
},
{ $sort: { rating: -1, name: 1, username: 1 } },
{ $skip: input.offset },
{ $limit: input.limit }
]);
};

export default { AllArtists };
47 changes: 47 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as Koa from "koa";
import { DefaultContext, DefaultState } from "koa";
import * as Router from "koa-router";
import * as mongoose from "mongoose";

import ArtistsController from "./controllers/artist.controller";

type Mongoose = typeof mongoose.connection;

interface IAppContext extends DefaultContext {
db: Mongoose;
}

const app = new Koa<DefaultState, DefaultContext>();
const router = new Router();

const port = process.env.PORT || 3000;
const uri = "";

mongoose
.connect(uri, {
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true
})
.then((db: typeof mongoose) => {
app.context.db = db.connection;
})
.catch(err => console.error("Something went wrong", err));

router.get("/", async ctx => {
ctx.body = "Hello World!";
});

router.get("/artists", async ctx => {
ctx.body = await ArtistsController.AllArtists({
limit: parseInt(ctx.query.limit || 10, 10),
offset: parseInt(ctx.query.offset || 0, 10),
search: ctx.query.search || ""
});
});

app.use(router.routes());

app.listen(port, () => {
console.log("Server running on port 3000");
});
24 changes: 24 additions & 0 deletions src/models/artist.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as mongoose from "mongoose";
import { Document, Schema } from "mongoose";

export interface IArtist extends Document {
artistId: number;
location: string | null;
name: string | null;
// tracks:
username: string | null;
}

const ArtistSchema: Schema = new Schema({
artistId: {
required: true,
type: Number,
unique: true
},
location: String,
name: String,
tracks: [{ type: Schema.Types.ObjectId, ref: "Track" }],
username: String
});

export default mongoose.model<IArtist>("Artist", ArtistSchema);
10 changes: 10 additions & 0 deletions tsconig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"noImplicitAny": true,
"outDir": "./dist",
"sourceMap": true
},
"include": ["./src/**/*"]
}
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"defaultSeverity": "error",
"extends": ["tslint:recommended", "tslint-config-prettier"],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}
Loading

0 comments on commit 15e509a

Please sign in to comment.