{"size": 1231, "ext": "js", "max_stars_repo_path": "server/app/result.js", "max_stars_repo_name": "minamiki/giftexhange", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/app/result.js", "max_issues_repo_name": "minamiki/giftexhange", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/app/result.js", "max_forks_repo_name": "minamiki/giftexhange", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.1944444444, "max_line_length": 175, "alphanum_fraction": 0.7116165719}
const BaseConnection = require('./baseConnection')
const User = require('./user')

module.exports = class Event extends BaseConnection {
	constructor(connectionPool) {
		super(connectionPool)
		this.selectQuery = `SELECT rs.*, 
			s.fullName as senderName, 
			r.fullName as receiverName, 
			(SELECT id from wishlist w where w.eventId = rs.eventId and w.userId = rs.receiverId) as wishlistId,
			(SELECT COUNT(wl.id) FROM wishlist_items wl where wl.wishlistId in (SELECT id from wishlist w where w.eventId = rs.eventId and w.userId = rs.receiverId)) as wishlist_count 
			FROM result rs 
			inner join users s on s.id = rs.senderId 
			inner join users r on r.id = rs.receiverId`
	}

	getAllResults() {
		return this.query(this.selectQuery)
	}
	getByEventId(eventId) {
		return this.query(`${this.selectQuery} WHERE rs.eventId=${eventId}`)
	}

	getById(id) {
		return this.query(`${this.selectQuery} WHERE rs.id = ${id}`)
	}

	deleteByEventId(id) {
		return this.query(`DELETE FROM result WHERE eventId = ${id}`)
	}

	insert(object) {
		let query = `INSERT INTO result (senderId, receiverId, eventId, emailSent) VALUES (${object.sender}, ${object.receiver}, ${object.eventId}, ${object.emailSent})`
		return this.query(query)
	}
}