add submissions data to stories and publications
This commit is contained in:
		
							parent
							
								
									e9079d3489
								
							
						
					
					
						commit
						d4f4d16dc4
					
				| 
						 | 
				
			
			@ -4,9 +4,16 @@ export class Data {
 | 
			
		|||
        this.#db = db
 | 
			
		||||
    }
 | 
			
		||||
    async init() {
 | 
			
		||||
        this.stories = await this.getStories()
 | 
			
		||||
        this.publications = await this.getPublications()
 | 
			
		||||
        this.submissions = await this.getSubmissions()
 | 
			
		||||
        this.stories = await this.getStories()
 | 
			
		||||
        this.stories.map(row=>{
 | 
			
		||||
            row.submissions=this.getSubmissionsByStoryId(row.id)
 | 
			
		||||
        })
 | 
			
		||||
        this.publications = await this.getPublications()
 | 
			
		||||
        this.publications.map(row=>{
 | 
			
		||||
            row.submissions=this.getSubmissionsByPublicationId(row.id)
 | 
			
		||||
        })
 | 
			
		||||
        return this
 | 
			
		||||
    }
 | 
			
		||||
    async getStories() {
 | 
			
		||||
        return this.#db('stories')
 | 
			
		||||
| 
						 | 
				
			
			@ -33,4 +40,12 @@ export class Data {
 | 
			
		|||
            )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getSubmissionsByStoryId(id){
 | 
			
		||||
        return this.submissions.filter(row=>row.story_id==id)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getSubmissionsByPublicationId(id){
 | 
			
		||||
        return this.submissions.filter(row=>row.pub_id==id)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -6,8 +6,7 @@ import { Data } from "../objects/Data.mjs";
 | 
			
		|||
describe("Testing Data object...",function(){
 | 
			
		||||
 | 
			
		||||
    it("once initiated, it should serve arrays at .stories, .publications, and .submissions",async function(){
 | 
			
		||||
        const data = new Data(db)
 | 
			
		||||
        await data.init()
 | 
			
		||||
        const data = await new Data(db).init()
 | 
			
		||||
        expect(data.stories).to.be.a('array')
 | 
			
		||||
        expect(data.submissions).to.be.a('array')
 | 
			
		||||
        expect(data.publications).to.be.a('array')
 | 
			
		||||
| 
						 | 
				
			
			@ -38,4 +37,38 @@ describe("Testing Data object...",function(){
 | 
			
		|||
            expect(row).to.contain.key('id')
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    it("every entry in .stories and .publications should contain an .submissions field", async function(){
 | 
			
		||||
        const data = await new Data(db).init()
 | 
			
		||||
        for (const row of data.stories) {
 | 
			
		||||
            expect(row).to.contain.key('submissions')
 | 
			
		||||
        }
 | 
			
		||||
        for (const row of data.publications) {
 | 
			
		||||
            expect(row).to.contain.key('submissions')
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    it("the submissions field of the .stories and .publications array should itself be an array",async function(){
 | 
			
		||||
        const data = await new Data(db).init()
 | 
			
		||||
        for (const row of data.stories) {
 | 
			
		||||
            expect(row.submissions).to.be.a('array')
 | 
			
		||||
        }
 | 
			
		||||
        for (const row of data.publications) {
 | 
			
		||||
            expect(row.submissions).to.be.a('array')
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    it("once initiated, it should serve arrays at .getSubmissionsByStoryId and getSubmissionsByPublicationId()",async function(){
 | 
			
		||||
        const data = new Data(db)
 | 
			
		||||
        await data.init()
 | 
			
		||||
        expect(data.getSubmissionsByStoryId()).to.be.a('array')
 | 
			
		||||
        expect(data.getSubmissionsByPublicationId()).to.be.a('array')
 | 
			
		||||
    } )
 | 
			
		||||
    it("when passed a valid id, every entry in the arrays returned by .getSubmissionsByStoryId and getSubmissionsByPublicationId() should contain an .id field", async function(){
 | 
			
		||||
        const data = new Data(db)
 | 
			
		||||
        await data.init()
 | 
			
		||||
        for (const row of data.getSubmissionsByStoryId(1)) {
 | 
			
		||||
            expect(row).to.contain.key('id')
 | 
			
		||||
        }
 | 
			
		||||
        for (const row of data.getSubmissionsByPublicationId(1)) {
 | 
			
		||||
            expect(row).to.contain.key('id')
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
})
 | 
			
		||||
		Loading…
	
		Reference in New Issue