Skip to content
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
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
PORT =
database=classManager
username=postgres
password=123456789
host=127.0.0.1
dialect=postgres
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "./src/server.js",
"type": "module",

"scripts": {
"start": "nodemon --exec babel-node ./src/server.js",
"watch:dev": "nodemon ./src/server.js",
Expand Down
106 changes: 106 additions & 0 deletions src/api/controllers/MessageController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import MessageService from '../services/MessageService.js';
import Util from '../utils/Utils.js';

const util = new Util();

class MessageController {
static async getAllMessages(req, res) {
try {
const allMessages = await MessageService.getAllMessages();
if (allMessages.length > 0) {
util.setSuccess(200, 'Messages retrieved', allMessages);
} else {
util.setSuccess(200, 'No Message found');
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}

static async addMessage(req, res) {
if (!req.body.message || !req.body.senderId || !req.body.receiverId) {
util.setError(400, 'Please provide complete details');
return util.send(res);
}
const newMessage = req.body;
try {
const createdMessage = await MessageService.addMessage(newMessage);
util.setSuccess(201, 'Message Added!', createdMessage);
return util.send(res);
} catch (error) {
util.setError(400, error.message);
return util.send(res);
}
}

static async updatedMessage(req, res) {
const alteredMessage = req.body;
const { id } = req.params;
if (!Number(id)) {
util.setError(400, 'Please input a valid numeric value');
return util.send(res);
}
try {
const updateMessage = await MessageService.updateMessage(id, alteredMessage);
if (!updateMessage) {
util.setError(404, `Cannot find Message with the id: ${id}`);
} else {
util.setSuccess(200, 'Message updated', updateMessage);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}

static async getAMessage(req, res) {
const { id } = req.params;

if (!Number(id)) {
util.setError(400, 'Please input a valid numeric value');
return util.send(res);
}

try {
const theMessage = await MessageService.getAMessage(id);

if (!theMessage) {
util.setError(404, `Cannot find Message with the id ${id}`);
} else {
util.setSuccess(200, 'Found Message', theMessage);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}

static async deleteMessage(req, res) {
const { id } = req.params;

if (!Number(id)) {
util.setError(400, 'Please provide a numeric value');
return util.send(res);
}

try {
const MessageToDelete = await MessageService.deleteMessage(id);

if (MessageToDelete) {
util.setSuccess(200, 'Message deleted');
} else {
util.setError(404, `Message with the id ${id} cannot be found`);
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}
}

export default MessageController;
124 changes: 124 additions & 0 deletions src/api/controllers/TeacherController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { response } from 'express';
import TeacherService from '../services/TeacherServices.js'
import Util from '../utils/Utils.js';

const util = new Util();

class teacherController {
static async home(req, res) {
res.send(`<h1>Welcome to the teacher's Route</h1>`)
}

static async getAllTeachers(req, res) {
try {
const allTeacher = await TeacherService.getAllTeachers();
if (allTeacher.length > 0) {
util.setSuccess(200, 'teachers retrieved', allTeacher);
} else {
util.setFound(400, 'No teahcer found in the database');
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}

static async addTeacher(req, res) {
if (!req.body.teacherId || !req.body.classId ) {
util.setError(400, 'Please provide complete details of the teacher');
return util.send(res);
}
const newTeacher = req.body;
try {
const createdTeacher = await TeacherService.addTeacher(newTeacher);
util.setSuccess(201, 'Teacher Added Successfully!', createdTeacher);
return util.send(res);
} catch (error) {
util.setError(400, error.teacher);
return util.send(res);
}
}

static async updatedTeacher(req, res) {
const alteredTeacher = req.body;
const { id } = req.params;
if (!util.checkIfValidUUID(id)) {
util.setError(400, 'Please input a valid Id');
return util.send(res);
}
try {
const updateTeacher = await TeacherService.updateTeacher(id, alteredTeacher);
if (!updateTeacher) {
util.setError(404, `Cannot find a teacher with the id: ${id}`);
} else {
util.setSuccess(200, 'Teacher updated', updateTeacher);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}

static async getATeacher(req, res) {
const { id } = req.params;

if (!util.checkIfValidUUID(id)) {
util.setError(400, 'Please input a valid numeric value');
return util.send(res);
}

try {
const theTeacher = await TeacherService.getATeacher(id);

if (!theTeacher) {
util.setError(404, `Cannot find a teacher with the id ${id}`);
} else {
util.setSuccess(200, 'Found Teacher', theTeacher);
}
return util.send(res);
} catch (error) {
util.setError(404, error);
return util.send(res);
}
}




// static getATeacher = async(req, res)=> {
// try{
// const teacherById = await TeacherService.findById(req.params.id)
// res.json(teacherById)
// }catch(err){
// res.send('Error' + err)
// }

// }

static async deleteTeacher(req, res) {
const { id } = req.params;

if (!util.checkIfValidUUID(id)) {
util.setError(400, 'Please provide a numeric value');
return util.send(res);
}

try {
const teacherToDelete = await TeacherService.deleteTeacher(id);

if (teacherToDelete) {
util.setSuccess(200, `Teacher with ${id} deleted`);
} else {
util.setError(404, `Teacher with the id ${id} cannot be found`);
}
return util.send(res);
} catch (error) {
util.setError(400, error);
return util.send(res);
}
}
}

export default teacherController;
41 changes: 25 additions & 16 deletions src/api/db/config/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
require("dotenv").config
module.exports = {
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
development: {
database: "classManager",
username: "postgres",
password: "15263",
host: process.env.host,
dialect: 'postgres'
},
local: {
"username": "tgsowrsnnwphjg",
"password": "da09381ea24589af12153a9aca04de1d9283245434520ba084ccc6b55065520d",
"database": "d3a0e6i6blrn6p",
"host": "https://ec2-3-227-68-43.compute-1.amazonaws.com",
"dialect": "postgres"
},
"test": {
test: {
"username": "root",
"password": null,
"database": "database_test",
"database": "classManager_test",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "postgres"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
production: {
"username": "tgsowrsnnwphjg",
"password": "da09381ea24589af12153a9aca04de1d9283245434520ba084ccc6b55065520d",
"database": "d3a0e6i6blrn6p",
"host": "ec2-3-227-68-43.compute-1.amazonaws.com",
"dialect": "postgres"
}
}
};
//export default config;
10 changes: 10 additions & 0 deletions src/api/db/migrations/20221030101723-create-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module.exports = {
message: {
type: Sequelize.STRING
},
senderId:{
type:Sequelize.UUID,
allowNull:false,
foreignKey:true
},
receiverId:{
type:Sequelize.UUID,
allowNull:false,
foreignKey:true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
Expand Down
13 changes: 10 additions & 3 deletions src/api/db/migrations/20221030101838-create-class-teacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ module.exports = {
await queryInterface.createTable('ClassTeachers', {
id: {
allowNull: false,
autoIncrement: true,
defaultValue:Sequelize.UUIDV4,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID
},
classId: {
type: Sequelize.STRING
type: Sequelize.UUID,
allowNull:false,
foreignKey:true
},
teacherId:{
type: Sequelize.UUID,
allowNull:false,
foreignKey:true
},
createdAt: {
allowNull: false,
Expand Down
14 changes: 9 additions & 5 deletions src/api/db/models/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,35 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
// define association here
Assignment.belongsTo(models.User, {foreignKey: 'teacherId', as: 'teacher'});
// Assignment.belongsTo(models.User, {foreignKey: 'teacherId', as: 'teacher'});
Assignment.belongsTo(models.User, {foreignKey: 'studentId', as: 'student'});
Assignment.belongsTo(models.Class, {foreignKey: 'classId', as: 'class'});
}
}
Assignment.init({
id:{
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
assignment:{
type: DataTypes.STRING,
allowNull:false
},
classId:{
type: DataTypes.UUID,
allowNull:false
allowNull:false,
foreignKey:true
},
teacherId:{
type: DataTypes.UUID,
allowNull:false
allowNull:false,
foreignKey:true
},
studentId:{
type: DataTypes.UUID,
allowNull:false
allowNull:false,
foreignKey:true
}
}, {
sequelize,
Expand Down
Loading