Skip to content

Commit

Permalink
refactor and fix cloudinary-devs#11
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Shavin committed Jan 31, 2020
1 parent 7a6abf3 commit 078f7c1
Show file tree
Hide file tree
Showing 33 changed files with 1,082 additions and 504 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}
16 changes: 16 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Database API (FAUNADB)

We are using FaunaDB to create and maintain our DB for cloudyBadge. Below is some information about the structure of our DB

## Main DB - cloudybadge

`cloudybadge` is the child database of `cloudy`. Inside `cloudybadge` we will have `badges` which is our main collection of all the badges information.

## Indexes

Some of our indexes are as below:

1. all_badges
2. all_badges_per_event
3. badge_by_editKey
4. badge_by_viewKey
24 changes: 10 additions & 14 deletions api/getAll.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
require('dotenv').config();
const faunadb = require('faunadb');

const q = faunadb.query;
const client = new faunadb.Client({
secret: process.env.FAUNADB
});
const { q, client } = require('./setup');

module.exports = async (req, res) => {
try {
Expand All @@ -15,18 +10,19 @@ module.exports = async (req, res) => {
)
)
);
const badges = queryResponse.data;
const allBadgesQuery = badges.map(ref => q.Get(ref));
const data = queryResponse.data;
const allBadgesQuery = data.map(ref => q.Get(ref));

const response = await client.query(allBadgesQuery)
return res.json(response);
} catch(error) {
console.error(error);
const users = response.map(entry => entry.data);

return res.json({
body: {
error
}
users
});
} catch(error) {
return res.json({
error: error.message
})
}

};
29 changes: 29 additions & 0 deletions api/getAllEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require('dotenv').config();
const { q, client } = require('./setup');

module.exports = async (req, res) => {
try {
const queryResponse = await client.query(
q.Paginate(
q.Match(
q.Index(process.env.FAUNA_QUERY_EVENTS)
)
)
);

const data = queryResponse.data;
const allEventsQuery = data.map(ref => q.Get(ref));

const response = await client.query(allEventsQuery)
const events = response.map(entry => entry.data);

return res.json({
events
});
} catch(error) {
return res.json({
error: error.message
});
}

};
46 changes: 46 additions & 0 deletions api/getAllPerEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require('dotenv').config();
const { q, client } = require('./setup');

module.exports = async (req, res) => {
const eventId = req.query.id;
// const getEvent = require('./getEvent');

try {
const [eventResponse, queryResponse] = await Promise.all([client.query(
q.Get(
q.Match(
q.Index(process.env.FAUNA_QUERY_EVENT), eventId
)
)
), client.query(
q.Paginate(
q.Match(
q.Index(process.env.FAUNA_QUERY_ALL_PER_EVENT), eventId
)
)
)]);

const badges = queryResponse.data;
const allBadgesQuery = badges.map(ref => q.Get(ref));

const response = await client.query(allBadgesQuery);

const users = response.map(badge => {
delete badge.data.editKey; //remove Edit key
return badge.data;
});

return res.json({
users,
event: eventResponse.data
});

} catch(error) {
console.error(error);

return res.json({
error: error.message
});
}

};
22 changes: 22 additions & 0 deletions api/getEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require('dotenv').config();
const { q, client } = require('./setup');

module.exports = async (req, res) => {
const eventId = req.query.id;
try {
const queryResponse = await client.query(
q.Get(
q.Match(
q.Index(process.env.FAUNA_QUERY_EVENT), eventId
)
)
);

return res.json(queryResponse.data);
} catch(error) {
return res.json({
error: error.message
});
}

};
9 changes: 2 additions & 7 deletions api/getOne.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
require('dotenv').config();
const faunadb = require('faunadb');

const q = faunadb.query;
const client = new faunadb.Client({
secret: process.env.FAUNADB
});
const { q, client } = require('./setup');

module.exports = async (req, res) => {
const id = req.query.id;
const indexToQuery = `${process.env.FAURNA_QUERY_ONE_PREFIX}${req.query.byKey}`
const indexToQuery = `${process.env.FAUNA_QUERY_ONE_PREFIX}${req.query.byKey}`
try {
const queryResponse = await client.query(
q.Get(
Expand Down
51 changes: 51 additions & 0 deletions api/getUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require('dotenv').config();
const { q, client } = require('./setup');

const getUserByKey = async (id, byKey) => {
const indexQuery = `${process.env.FAUNA_QUERY_ONE_PREFIX}${byKey}`;

try {
return await client.query(
q.Get(
q.Match(
q.Index(indexQuery), id
)
)
);
} catch (error) {
return {
error: error.message,
}
}
}


module.exports = async (req, res) => {
const id = req.query.id;
const voteId = req.query.vid;
let isVoteIdValidate = false;
let responseData = {};

if (voteId) {
const voterQuery = await getUserByKey(voteId, 'voteid');
isVoteIdValidate = !!voterQuery;
}

responseData = await getUserByKey(id, 'viewkey');

if (responseData.error) {
isVoteIdValidate = false;
const editQuery = await getUserByKey(id, 'editkey');

responseData = editQuery;
}

if (responseData.data && responseData.data.editKey) {
delete responseData.data.editKey;
}

return res.json({
info: responseData,
hasVoter: isVoteIdValidate,
})
};
9 changes: 1 addition & 8 deletions api/insert.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
require('dotenv').config();
const faunadb = require('faunadb');
const { q, client } = require('./setup');
const shortid = require('shortid');
// const axios = require('axios');

const q = faunadb.query;
const client = new faunadb.Client({
secret: process.env.FAUNADB
});

module.exports = async (req, res) => {
const data = req.body.payload;
const uniquePath = shortid.generate();

data.voteID = uniquePath;
data.eventCode = process.env.CLOUDYBADGE_EVENT
data.editKey = shortid.generate();
data.viewKey = shortid.generate();
data.tranStr = '';
Expand Down
12 changes: 12 additions & 0 deletions api/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require('dotenv').config();
const faunadb = require('faunadb');

const q = faunadb.query;
const client = new faunadb.Client({
secret: process.env.FAUNADB
});

module.exports = {
client,
q
};
12 changes: 2 additions & 10 deletions api/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,10 @@ module.exports = async (req, res) => {
q.Update(q.Ref(q.Collection(process.env.FAUNA_COLLECTION), ref),
badge,
));
// TODO
try {
// invoke ZEIT build process programmatically
} catch(error) { }

return res.json(response.data)
return res.json(response.data);
} catch(error) {
console.error(error);
return res.json({
body: {
error
}
error: error.message
});
}

Expand Down
19 changes: 19 additions & 0 deletions components/Back.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<nuxt-link :to="link">
<button class="outline-none rounded-full hover:bg-grey-darker border-0 p-2">
<svg width="20" height="20" aria-hidden="true" focusable="false" data-icon="chevron-left" role="img" viewBox="0 0 320 512">
<path fill="#fff" d="M34.52 239.03L228.87 44.69c9.37-9.37 24.57-9.37 33.94 0l22.67 22.67c9.36 9.36 9.37 24.52.04 33.9L131.49 256l154.02 154.75c9.34 9.38 9.32 24.54-.04 33.9l-22.67 22.67c-9.37 9.37-24.57 9.37-33.94 0L34.52 272.97c-9.37-9.37-9.37-24.57 0-33.94z"/>
</svg>
</button>
</nuxt-link>
</template>
<script>
export default {
props: {
link: {
type: String,
require: true,
}
}
}
</script>
59 changes: 59 additions & 0 deletions components/Badge.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<template>
<div class="upload--result--wrapper flex mr-3">
<cld-image :public-id="badgeUrl"
class="border-2 self-center"
:transformation="transformation"
height="800" crop="fill"
/>
</div>
</template>
<script>
export default {
props: {
badgeUrl: {
type: String,
require: true,
},
name: {
type: String,
default: ''
},
company: {
type: String,
default: ''
},
title: {
type: String,
default: ''
},
avatarOverlay: {
type: String,
default: ''
},
effect: {
type: Object,
default: () => ({})
}
},
computed: {
transformation() {
return [{
overlay: this.avatarOverlay,
radius:"max",
width:"800",
height:"800",
x:"6",
y:"320",
crop:"thumb",
...this.effect,
}, {
overlay: `text:Roboto_80:${this.name}`,
y: "-1000"
}, {
overlay: `text:Roboto_50:${this.company}`,
y: "-900"
}]
},
}
}
</script>
Loading

0 comments on commit 078f7c1

Please sign in to comment.