diff --git a/server/controllers/events.controller.js b/server/controllers/events.controller.js index dd233bc..2efe341 100644 --- a/server/controllers/events.controller.js +++ b/server/controllers/events.controller.js @@ -2,7 +2,9 @@ const mongoose = require('mongoose'); const Story = require('../model/story.model'); const Event = require('../model/event.model').Event; const Attachment = require('../model/event.model').Attachment; -const Coordinates = require('../model/event.model').Coordinates; +const Location = require('../model/event.model').Location; + +require('../db')('mapstory-backend-test'); //Adds event to events array within story object const addEvent = async (ctx, next) => { @@ -16,22 +18,22 @@ const addEvent = async (ctx, next) => { const attachmentsData = ctx.request.body.attachments; attachments = await Promise.all(attachmentsData.map(async attachment => { let attachmentData; - if (attachment.type === 'link') { + if (attachment.type === 'Link') { attachmentData = { type: attachment.type, url: attachment.url, - imageUrl: attachment.imageUrl, + urlImg: attachment.urlImg, title: attachment.title, }; - } else if (attachment.type === 'text') { + } else if (attachment.type === 'Text') { attachmentData = { type: attachment.type, text: attachment.text, }; - } else if (attachment.type === 'image') { + } else if (attachment.type === 'Image') { attachmentData = { type: attachment.type, - imageUrl: attachment.imageUrl, + urlImg: attachment.urlImg, }; } else { attachmentData = { @@ -43,14 +45,15 @@ const addEvent = async (ctx, next) => { })); } - const coordinates = ctx.request.body.coordinates; + const locationData = ctx.request.body.location; + const location = await Location.create(locationData); const eventData = { title: ctx.request.body.title, startTime: ctx.request.body.startTime, mapLocation: ctx.request.body.mapLocation, dateAndTime: ctx.request.body.dateAndTime, - coordinates, + location, attachments, }; const createdEvent = await Event.create(eventData); @@ -85,8 +88,7 @@ const editEvent = async (ctx, next) => { if (data.startTime) updatedProps.map = data.startTime; if (data.mapLocation) updatedProps.duration = data.mapLocation; if (data.dateAndTime) updatedProps.tagLine = data.dateAndTime; - if (data.attachments) updatedProps.attachments = data.attachments; - if (data.coordinates) updatedProps.coordinates = data.coordinates; + if (data.attachments) updatedProps.published = data.attachments; const eventId = ctx.params.eventId; await Event.findOneAndUpdate({'_id': eventId}, {$set: updatedProps}); @@ -111,6 +113,7 @@ const deleteEvent = async (ctx, next) => { } } story.save(); + // await Event.findByIdAndRemove(ctx.params.eventId) ctx.status = 204; } catch (error) { throw (401, 'Could not edit event!'); diff --git a/server/model/event.model.js b/server/model/event.model.js index 8da6064..f39bd2c 100644 --- a/server/model/event.model.js +++ b/server/model/event.model.js @@ -1,35 +1,34 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; -const Story = require('../model/story.model'); const attachmentSchema = new Schema({ type: String, url: String, - imageUrl: String, + urlImg: String, title: String, text: String, }); -const coordinatesSchema = new Schema({ - lat: Number, - lng: Number, +const locationSchema = new Schema({ + lat: String, + lng: String, }); const eventSchema = new Schema({ title: String, startTime: String, - dateAndTime: String, + dateAndTime: Date, mapLocation: String, - coordinates: [coordinatesSchema], + location: locationSchema, attachments: [attachmentSchema], }); const Event = mongoose.model('Event', eventSchema); -const Coordinates = mongoose.model('Coordinates', coordinatesSchema); +const Location = mongoose.model('Location', locationSchema); const Attachment = mongoose.model('Attachment', attachmentSchema); module.exports = { Event, - Coordinates, + Location, Attachment, }; diff --git a/server/tests/events.controller.test.js b/server/tests/events.controller.test.js new file mode 100644 index 0000000..b521469 --- /dev/null +++ b/server/tests/events.controller.test.js @@ -0,0 +1,165 @@ +const chai = require('chai'); +var chaiAsPromised = require('chai-as-promised'); +const sinon = require('sinon'); +const sinonChai = require('sinon-chai'); +chai.use(sinonChai); +chai.use(chaiAsPromised); +chai.should(); +require('chai').should(); +const Event = require('../controller/events.controller'); +const proxyquire = require('proxyquire'); + +describe('addEvents', () => { + + it('should not create events if no title is provided', + async () => { + const ctx = { + request: { + body: { + title: '', + startTime: 38, + dateAndTime: 'Thurs 21 December', + mapLocation: 'Barcelona', + attachments: [], + } + } + }; + await Event.addEvent(ctx).should.be.rejected; + } +); + + + it('should search for a story on the DB & save to it', + async () => { + const spy = sinon.spy(); + const EventController = proxyquire('../controller/events.controller', { + '../model/story.model': { + '@noCallThru': true, + findOne: () => ({ + id: 123, + save: spy, + events: [], + }) + } + }); + const ctx = { + params: { + id: 42 + }, + request: { + body: { + title: 'This is a story', + startTime: 38, + dateAndTime: 'Thurs 21 December', + mapLocation: 'Barcelona', + attachments: [], + } + } + }; + await EventController.addEvent(ctx); + spy.calledOnce.should.equal(true); + } + ); + + + it('should create an event', + async () => { + const spy = sinon.spy(); + const ctx = { + params: { + id: 42 + }, + request: { + body: { + title: 'This is a story', + startTime: 38, + mapLocation: 'Barcelona', + dateAndTime: 'Thurs 21 December', + attachments: [], + } + } + }; + const mockEvent = { + title: ctx.request.body.title, + startTime: ctx.request.body.startTime, + mapLocation: ctx.request.body.mapLocation, + dateAndTime: ctx.request.body.dateAndTime, + attachments: ctx.request.body.attachments + }; + const EventController = proxyquire('../controller/events.controller', + + { + '../model/story.model': { + '@noCallThru': true, + findOne: () => ({ + id: 123, + save: spy, + events: [], + }) + } + }, + + { + '../model/event.model': { + '@noCallThru': true, + create: () => mockEvent + } + }); + + await EventController.addEvent(ctx); + ctx.request.body.should.eql(mockEvent); + } + ); + + + it('should be called with the provided arguments', + function () { + var addEventSpy = sinon.spy(Event.addEvent); + Event.addEvent(true, true); + addEventSpy.calledWith(true, true); + // addEventSpy.restore(); + } +); + + + +it('should update events when edited', async () => { + +}); + + + + + it('should have unique startTime'); + + it('should update attachments'); + + }); + + + +describe('Edit Event', function () { + + +}); + + describe('Delete Event', function () { + + it('should remove event from DB when deleted', async () => { + + + + + }); + + }); + + +describe('Attachments', () => { + it('should validate text format'); + it('should validate tweet format'); + it('should validate image format'); + it('should validate video format'); + it('should validate audio format'); + it('should validate link format'); +});