Skip to content

Commit

Permalink
Merge branch 'main' into tag&filter
Browse files Browse the repository at this point in the history
  • Loading branch information
b-fenelon authored May 15, 2023
2 parents b2a324c + 48653da commit cdbeaa6
Show file tree
Hide file tree
Showing 30 changed files with 538 additions and 246 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ Create a database specific to your testing:

3. Once finished, start the application:

`npm start`
`npm run dev`

Note: This should open a new tab in your browser, otherwise access [http://localhost:3000](http://localhost:3000) to view the app in your browser.

4. While running, open a second terminal window in your project directory and start the server (runs the code in server.js, including your database connection):

`npm run server`
This will concurrently run start and server (including the db connection code).

Now you can interact with the app and create articles!

Expand Down
Empty file removed [email protected]
Empty file.
6 changes: 5 additions & 1 deletion models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ const articleSchema = new mongoose.Schema({
author: String,
authorImg: String,
content: String,
contentImg: String,
contentImg: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Image',
},
tags: [String],
images: [String],
created: { type: Date, default: Date.now},
});

module.exports = mongoose.model("Article", articleSchema);
11 changes: 11 additions & 0 deletions models/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Description: This file contains the schema for the article model.
const mongoose = require("mongoose");

const imageSchema = new mongoose.Schema({
url: String,
data: Buffer,
type: String,
created: { type: Date, default: Date.now},
});

module.exports = mongoose.model("Image", imageSchema);
Empty file removed nodemon
Empty file.
130 changes: 130 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.3.4",
"buffer": "^6.0.3",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"file-system": "^2.2.2",
"marked": "^4.2.12",
"mongoose": "^7.0.1",
"react": "^18.2.0",
"react-contenteditable": "^3.3.7",
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
"react-router-dom": "^6.10.0",
"react-scripts": "5.0.1",
Expand Down
Binary file modified public/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "IUCG Insights",
"icons": [
{
"src": "favicon.ico",
"src": "favicon1.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
Expand Down
48 changes: 42 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

// access env variables
require('dotenv').config();

//import axios from 'axios';
//const axios = require("axios");
const express = require("express"); //import express
const mongoose = require("mongoose"); //import mongoose
const cors = require("cors"); //import cors
const fs = require('file-system'); //import file-system
const Article = require("./models/article"); //import article model
const Tag = require("./models/tags"); //import tag model
const Image = require("./models/image"); // import images model

const app = express(); //create express app

// Connect to MongoDB database
Expand All @@ -25,7 +29,8 @@ app.use(express.json());
// API routes
app.get("/api/articles", async (req, res) => {
try {
const articles = await Article.find();
// can use this scheme to implement more complicated filtering
const articles = await Article.find().sort({created: -1});
res.json(articles);
} catch (error) {
console.error(error);
Expand All @@ -45,11 +50,23 @@ app.get("/api/articles/:id", async(req, res) => {
}
})

app.get("/api/images/:id", async(req, res) => {
try {
const { id } = req.params;
const image = await Image.findById(id);
res.json(image)
}
catch(error) {
console.error(error)
res.status(500).json({ message: "Internal server error: Accessing Image" })
}
})

app.post("/login", async (req, res) => {
console.log(req.body.password)
try {
if (req.body.password === "isenbrocode") {
res.send("The request was successful, unlike the isenbros");
if (req.body.password === "secretpwd") {
res.send("The request was successful, as we wish all people to be");
} else {
res.status(401).json({ message: "Internal server error." });
}
Expand All @@ -60,6 +77,7 @@ app.post("/login", async (req, res) => {

app.post("/api/articles", async (req, res) => {
try {
console.log(req.body);
const article = new Article(req.body);
await article.save();
res.json(article);
Expand All @@ -69,6 +87,25 @@ app.post("/api/articles", async (req, res) => {
}
});

app.post("/api/images", async (req, res) => {
try {
if (req.body.data) {
rfs = fs.readFileSync(req.body.data).toString('base64');
req.body.data = Buffer.from(rfs, 'base64');
const image = new Image(req.body);
await image.save();
res.json(image);
} else {
// search for default url...
// for now just get by hard coded id
const defimage = "6461f42db24f0ac937b2b3c6";
res.json(defimage);
}
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error: Create Image" });
}
});
app.delete("/api/articles/:id", async (req, res) => {
try {
const { id } = req.params;
Expand Down Expand Up @@ -98,8 +135,7 @@ app.post("/api/articles/search/", async (req, res) => {
try {
const { title } = req.body;
const article = await Article.find({ title: { $regex: title, $options: "i" } });
res.json(article);
} catch (error) {
res.json(article); } catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error." });
}
Expand Down
Empty file removed src/#.env#
Empty file.
Loading

0 comments on commit cdbeaa6

Please sign in to comment.