Skip to content

Commit

Permalink
+ Modularizatoin :
Browse files Browse the repository at this point in the history
1. Utils : Imgur
  • Loading branch information
glantrox committed Jun 27, 2024
1 parent 206650a commit 0a4d350
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 80 deletions.
115 changes: 38 additions & 77 deletions src/data/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ const service = express.Router();
// Environments
const supabaseURL = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const imgurURL = process.env.IMGUR_URL;
const imgurClientId = process.env.IMGUR_CLIENT_ID;

// Multer
const upload = require('../../api/uploads');

// Supabase Client
const { createClient } = require('@supabase/supabase-js');
const {
createClient
} = require('@supabase/supabase-js');
const supabase = createClient(supabaseURL, supabaseKey);

// Imgur Client
const imgurUpload = require('../utils/imgur');

// Body-Parser
const bodyParser = require(`body-parser`);
service.use(bodyParser.json());
service.use(bodyParser.urlencoded({ extended: true }));
service.use(bodyParser.urlencoded({
extended: true
}));

// Delete Portofolio
service.get(`/delete-portofolio`, async(req, res) => {
service.get(`/delete-portofolio`, async (req, res) => {
try {
const id = req.query.id;
const id = req.query.id;

const response = await supabase.from(`portofolios`).delete().eq(`id`, parseInt(id));
if(!response.ok) {
return res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
if (!response.ok) {
return res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
}
res.status(200).send(`Data Successfully Deleted`);
} catch (error) {
Expand All @@ -35,45 +40,22 @@ service.get(`/delete-portofolio`, async(req, res) => {
});

// Update Portofolio
service.post(`/update-portofolio`, upload.single(`file`) , async (req, res) => {
service.post(`/update-portofolio`, upload.single(`file`), async (req, res) => {
try {
const {
id,
title,
description,
source_url
description,
source_url
} = req.body;
let imageUrl = ``;


const file = req.file;

// Validate File
if (file) {

// Format to formData
const formData = new FormData();

// Format to Blob object
const fileBlob = new Blob([file.buffer], { type: file.mimetype });
formData.append('image', fileBlob, {
filename: file.originalname
});

const imgurResponse = await fetch(`${imgurURL}`, {
method: `POST`,
headers: {
"Authorization": `Client-ID ${imgurClientId}`,
},
body: formData
});
if (!imgurResponse.ok) {
// Returns Error Message
return res.status(imgurResponse.status).send(`Imgur Upload Error : ${imgurResponse.statusText}`);
}
// Retrive Imgur Link
const imgur = await imgurResponse.json();
imageUrl = imgur.data.link
const imgurLink = await imgurUpload(file)
imageUrl = imgurLink;
}

let bodyForm = {
Expand All @@ -82,15 +64,17 @@ service.post(`/update-portofolio`, upload.single(`file`) , async (req, res) => {
source_url: source_url
}

if(imageUrl !== ``) {
bodyForm.image_url = imageUrl
}
// If imageUrl is not Empty
if (imageUrl !== ``) {
// It updates image in database
bodyForm.image_url = imageUrl
}

const response = await supabase.from(`portofolios`).update(bodyForm).eq(`id`, id);

if(response.error) {
res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
if (response.error) {
return res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
}

res.status(200).send(`Data Successfully Updated`);
} catch (error) {
res.status(500).send(`Client Exception - ${error}`);
Expand All @@ -100,13 +84,13 @@ service.post(`/update-portofolio`, upload.single(`file`) , async (req, res) => {
// Get Portofolio Data by ID
service.get(`/get-portofolio`, async (req, res) => {
try {
const id = req.query.id;
const id = req.query.id;

const response = await supabase.from(`portofolios`).select(`*`).eq(`id`, parseInt(id));
if(response.error) {
res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
if (response.error) {
return res.status(response.status).send(`Internal Server Problem : ${response.statusText}`);
}
const dataResult = await response.data;
const dataResult = response.data;
res.send(dataResult);
} catch (error) {
res.status(500).send(`Client Exception - ${error}`);
Expand All @@ -115,12 +99,12 @@ service.get(`/get-portofolio`, async (req, res) => {

// Get All Portofolios
service.get(`/get-portofolios`, async (req, res) => {
try {
try {
const response = await supabase.from(`portofolios`).select(`*`);
if(response.error) {
res.status(response.status).send(`Internal Server Problem : ${response.message}`);
if (response.error) {
return res.status(response.status).send(`Internal Server Problem : ${response.message}`);
}
const dataResult = await response.data;
const dataResult = response.data;
res.send(dataResult);
} catch (error) {
res.status(500).send(`Client Exception - ${error}`);
Expand All @@ -133,44 +117,25 @@ service.post('/upload-portofolio', upload.single(`file`), async (req, res) => {
const {
title,
description,
source_url,
source_url,
} = req.body;

const file = req.file;

// Validate File
if (!file) {
return res.status(500).send(`File is Empty`);
}

// Format to formData
const formData = new FormData();

// Format to Blob object
const fileBlob = new Blob([file.buffer], { type: file.mimetype });
formData.append('image', fileBlob, {filename: file.originalname});

const imgurResponse = await fetch(`${imgurURL}`, {
method: `POST`,
headers: {
"Authorization" : `Client-ID ${imgurClientId}`,
},
body: formData
});
if(!imgurResponse.ok) {
// Returns Error Message
return res.status(imgurResponse.status).send(`Imgur Upload Error : ${imgurResponse.statusText}`);
}
// Retrive Imgur Link
const imgur = await imgurResponse.json();

const imgurLink = await imgurUpload(file)

const {
data,
error
} = await supabase.from('portofolios').insert([{
title: title,
description: description,
image_url: imgur.data.link,
image_url: imgurLink,
source_url: source_url
}]);

Expand All @@ -185,8 +150,4 @@ service.post('/upload-portofolio', upload.single(`file`), async (req, res) => {
}
});




module.exports = service;

3 changes: 1 addition & 2 deletions src/script/edit-portofolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ function setValues(data) {
const inputTitle = document.getElementById(`title`);
const inputDescription = document.getElementById(`description`);
const inputSourceUrl = document.getElementById(`sourceUrl`);

inputTitle.value = data.title;
inputDescription.value = data.description;
inputSourceUrl.value = data.source_url;

}
27 changes: 26 additions & 1 deletion src/utils/imgur.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
function imgurUpload(file) {
const imgurURL = process.env.IMGUR_URL;
const imgurClientId = process.env.IMGUR_CLIENT_ID;

async function imgurUpload(file) {
// Format to formData
const formData = new FormData();

// Format to Blob object
const fileBlob = new Blob([file.buffer], { type: file.mimetype });
formData.append('image', fileBlob, {
filename: file.originalname
});

const imgurResponse = await fetch(`${imgurURL}`, {
method: `POST`,
headers: {
"Authorization": `Client-ID ${imgurClientId}`,
},
body: formData
});
if (!imgurResponse.ok) {
// Returns Error Message
throw `Imgur Upload Error : ${imgurResponse.statusText}`
}
// Retrieve Imgur Link
const imgur = await imgurResponse.json();
return imgur.data.link;
}

module.exports = imgurUpload;

0 comments on commit 0a4d350

Please sign in to comment.