Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
20 changes: 20 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"development": {
"database": "pottery_development",
"host": "127.0.0.1",
"dialect": "postgres",
"define": {
"underscored": true
}
},
"test": {
"database": "pottery_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "pottery_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
26 changes: 26 additions & 0 deletions controllers/ArtistController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { Artist, Pottery } = require('../models')

const GetArtists = async (req, res) => {
try {
const artists = await Artist.findAll()
res.send(artists)
} catch (error) {
throw error
}
}

const GetArtistGallery = async (req, res) => {
try {
const artistsPots = await Artist.findByPk(req.params.artist_id, {
include: [{ model: Pottery, as: 'pottery' }]
})
res.send(artistsPots)
} catch (error) {
throw error
}
}

module.exports = {
GetArtists,
GetArtistGallery
}
50 changes: 50 additions & 0 deletions controllers/PotteryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Pottery } = require('../models')

const GetRecentPosts = async (req, res) => {
try {
const recents = await Pottery.findAll({ order: [['created_at', 'DESC']] })
res.send(recents)
} catch (error) {
throw error
}
}

const CreatePotteryDetails = async (req, res) => {
try {
const artistId = parseInt(req.params.artist_id)
const potteryBody = { artistId, ...req.body }
const pottery = await Pottery.create(potteryBody)
res.send(pottery)
} catch (error) {
throw error
}
}

const UpdatePottery = async (req, res) => {
try {
const potteryId = parseInt(req.params.pottery_id)
const updatedPottery = await Pottery.update(req.body, {
where: { id: potteryId },
returning: true
})
res.send(updatedPottery)
} catch (error) {
throw error
}
}
const DeletePottery = async (req, res) => {
try {
const potteryId = parseInt(req.params.pottery_id)
await Pottery.destroy({ where: { id: potteryId } })
res.send({ message: `Deleted pottery with an id of ${potteryId}` })
} catch (error) {
throw error
}
}

module.exports = {
GetRecentPosts,
CreatePotteryDetails,
UpdatePottery,
DeletePottery
}
57 changes: 57 additions & 0 deletions controllers/ReviewController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { Review } = require('../models')

const GetReviewDetails = async (req, res) => {
try {
const reviews = await Review.findByPk(req.params.review_id)
res.send(reviews)
} catch (error) {
throw error
}
}

const CreateReview = async (req, res) => {
try {
const artistId = parseInt(req.params.artist_id)
const potteryId = parseInt(req.params.pottery_id)
const reviewBody = {
artistId,
potteryId,
...req.body
}
const review = await Review.create(reviewBody)
res.send(review)
} catch (error) {
throw error
}
}

const UpdateReview = async (req, res) => {
try {
const reviewId = parseInt(req.params.review_id)
const updatedReview = await Review.update(req.body, {
where: { id: reviewId },
returning: true
})
res.send(updatedReview)
} catch (error) {
throw error
}
}

const DeleteReview = async (req, res) => {
try {
const reviewId = parseInt(req.params.review_id)
await Review.destroy({ where: { id: reviewId } })
res.send({ message: `Deleted comment with an id of ${reviewId}` })
} catch (error) {
throw error
}
}

// Dont forget to export your functions
module.exports = {
GetReviewDetails,
CreateReview,
UpdateReview,
DeleteReview
}
36 changes: 36 additions & 0 deletions migrations/20220412160356-create-pottery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('potteries', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
types: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
price: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('potteries')
}
}
33 changes: 33 additions & 0 deletions migrations/20220412163130-create-artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('artists', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('artists')
}
}
19 changes: 19 additions & 0 deletions migrations/20220412165838-add-artistId-to-pottery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('potteries', 'artistId', {
type: Sequelize.INTEGER,
allowNull: false,
field: 'artist_id',
onDelete: 'CASCADE',
references: {
model: 'artists',
key: 'id'
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('potteries', 'artistId')
}
}
27 changes: 27 additions & 0 deletions migrations/20220412173138-create-review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('reviews', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
content: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('reviews')
}
}
19 changes: 19 additions & 0 deletions migrations/20220412173529-add-artistId-to-review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('reviews', 'artistId', {
type: Sequelize.INTEGER,
allowNull: false,
field: 'artist_id',
onDelete: 'CASCADE',
references: {
model: 'artists',
key: 'id'
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('reviews', 'artistId')
}
}
19 changes: 19 additions & 0 deletions migrations/20220412173731-add-potteryId-to-review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('reviews', 'potteryId', {
type: Sequelize.INTEGER,
allowNull: false,
field: 'pottery_id',
onDelete: 'CASCADE',
references: {
model: 'potteries',
key: 'id'
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('reviews', 'potteryId')
}
}
38 changes: 38 additions & 0 deletions models/artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Artist extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Artist.hasMany(models.Pottery, {
foreignKey: 'artist_id',
as: 'pottery',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
Artist.hasMany(models.Review, {
foreignKey: 'artist_id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
}
}
Artist.init(
{
name: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING
},
{
sequelize,
modelName: 'Artist',
tableName: 'artists'
}
)
return Artist
}
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
Loading