Add SiteRoutes, StoryRoutes and TeamRoutes

This commit is contained in:
Jerko Steiner 2019-01-22 13:30:15 +01:00
parent d0e94808bb
commit f0943cfe80
8 changed files with 199 additions and 6 deletions

View File

@ -1,6 +1,9 @@
import {ICommentTree} from './ICommentTree'
import {IComment} from './IComment'
import {ICredentials} from './ICredentials'
import {ISite} from './ISite'
import {IStory} from './IStory'
import {ITeam} from './ITeam'
import {IUser} from './IUser'
export interface IAPIDef {
@ -33,7 +36,86 @@ export interface IAPIDef {
}
}
}
'/story/:storyId/comments': {
// TEAM
'/teams': {
post: {
body: {
name: string
}
response: ITeam
}
}
'/teams/:id': {
get: {
params: {
id: number
}
response: ITeam | undefined
}
}
'/my/teams': {
get: {
response: ITeam[]
}
}
// SITE
'/sites/:domain': {
'get': {
params: {domain: string}
}
}
'/teams/:teamId/sites/:id': {
get: {
params: {
teamId: number
id: number
}
response: ISite | undefined
}
}
'/teams/:teamId/sites': {
get: {
params: {
teamId: number
}
response: ISite[]
}
post: {
params: {
teamId: number
}
body: {
name: string
}
response: ISite
}
}
'/my/sites': {
get: {
response: ISite[]
}
}
// STORY
'/stories/by-url': {
'get': {
response: IStory | undefined
query: {
url: string
}
}
}
'/stories/:storyId/comments': {
'get': {
response: ICommentTree,
params: {

View File

@ -7,7 +7,7 @@ import {User} from './User'
@Entity()
export class Team extends BaseEntity {
@Column()
url!: string
name!: string
@Column()
@Index()

View File

@ -14,14 +14,14 @@ export class CommentRoutes extends BaseRoute<IAPIDef> {
setup(t: AsyncRouter<IAPIDef>) {
t.get('/story/:storyId/comments', async req => {
t.get('/stories/:storyId/comments', async req => {
const {storyId} = req.params
return this.commentService.find(storyId)
})
t.use(ensureLoggedInApi)
t.post('/story/:storyId/comments', async req => {
t.post('/stories/:storyId/comments', async req => {
const {storyId} = req.params
const comment = req.body
comment.storyId = storyId

View File

@ -0,0 +1,45 @@
import {AsyncRouter} from '../router'
import {BaseRoute} from './BaseRoute'
import {IAPIDef} from '@rondo/common'
import {ISiteService} from '../services/ISiteService'
import {ensureLoggedInApi} from '../middleware'
export class SiteRoutes extends BaseRoute<IAPIDef> {
constructor(
protected readonly siteService: ISiteService,
protected readonly t: AsyncRouter<IAPIDef>,
) {
super(t)
}
setup(t: AsyncRouter<IAPIDef>) {
t.get('/sites/:domain', async req => {
const {domain} = req.params
return this.siteService.findByDomain(domain)
})
t.get('/teams/:teamId/sites/:id', async req => {
const {id, teamId} = req.params
return this.siteService.findOne(id, teamId)
})
t.get('/teams/:teamId/sites', async req => {
return this.siteService.findByTeam(req.params.teamId)
})
t.use(ensureLoggedInApi)
t.get('/my/sites', async req => {
return this.siteService.findByUser(req.user!.id)
})
t.post('/teams/:teamId/sites', async req => {
const {name} = req.body
const {teamId} = req.params
return this.siteService.create(name, teamId, req.user!.id)
})
}
}

View File

@ -0,0 +1,23 @@
import {AsyncRouter} from '../router'
import {BaseRoute} from './BaseRoute'
import {IAPIDef} from '@rondo/common'
import {IStoryService} from '../services/IStoryService'
export class StoryRoutes extends BaseRoute<IAPIDef> {
constructor(
protected readonly storyService: IStoryService,
protected readonly t: AsyncRouter<IAPIDef>,
) {
super(t)
}
setup(t: AsyncRouter<IAPIDef>) {
t.get('/stories/by-url', async req => {
const {url} = req.query
return this.storyService.findOne(url)
})
}
}

View File

@ -0,0 +1,35 @@
import {AsyncRouter} from '../router'
import {BaseRoute} from './BaseRoute'
import {IAPIDef} from '@rondo/common'
import {ITeamService} from '../services/ITeamService'
import {ensureLoggedInApi} from '../middleware'
export class TeamRoutes extends BaseRoute<IAPIDef> {
constructor(
protected readonly teamService: ITeamService,
protected readonly t: AsyncRouter<IAPIDef>,
) {
super(t)
}
setup(t: AsyncRouter<IAPIDef>) {
t.get('/teams/:id', async req => {
const {id} = req.params
return this.teamService.findOne(id, req.user!.id)
})
t.use(ensureLoggedInApi)
t.get('/my/teams', async req => {
return this.teamService.find(req.user!.id)
})
t.post('/teams', async req => {
const {name} = req.body
return this.teamService.create(name, req.user!.id)
})
}
}

View File

@ -5,7 +5,9 @@ export interface ISiteService {
findOne(id: number, teamId: number): Promise<ISite | undefined>
find(userId: number): Promise<ISite[]>
findByUser(userId: number): Promise<ISite[]>
findByTeam(teamId: number): Promise<ISite[]>
findByDomain(domain: string): Promise<ISite | undefined>

View File

@ -29,7 +29,13 @@ export class SiteService extends BaseService implements ISiteService {
})
}
async find(teamId: number) {
async findByUser(userId: number) {
return this.getRepository(Site).find({
where: { userId },
})
}
async findByTeam(teamId: number) {
return this.getRepository(Site).find({
where: { teamId },
})