-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 15e509a
Showing
10 changed files
with
1,668 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
node_modules/ |
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,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" | ||
} | ||
} | ||
] | ||
} |
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,4 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"tslint.packageManager": "yarn" | ||
} |
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,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" | ||
} | ||
} |
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,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 }; |
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,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"); | ||
}); |
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,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); |
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2017", | ||
"noImplicitAny": true, | ||
"outDir": "./dist", | ||
"sourceMap": true | ||
}, | ||
"include": ["./src/**/*"] | ||
} |
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,7 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"extends": ["tslint:recommended", "tslint-config-prettier"], | ||
"jsRules": {}, | ||
"rules": {}, | ||
"rulesDirectory": [] | ||
} |
Oops, something went wrong.