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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
JUDGE_SUBMISSIONS_URL=http://localhost:2358/submissions
MONGO_URI_DOCKER=mongodb://localhost:27017/myappdb
MONGO_URI_DOCKER=mongodb://localhost:27017/myappdb
1 change: 1 addition & 0 deletions app/ParticlesContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ class ParticlesContainer extends Component<IProps> {
}

export default ParticlesContainer;

43 changes: 43 additions & 0 deletions app/api/Class/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextRequest, NextResponse } from "next/server";
import User from "../../../db/models/User";
import Projects from "../../../db/models/project";
import Class from "../../../db/models/Class";
import mongoose from "mongoose";
const uri = process.env.MONGO_URI_DOCKER || '';

export async function POST(request: NextRequest, {params}:{params:{_id: string}}) {
try {
await mongoose.connect(uri);

const { _id } = params;

let { students } = await request.json();

let classroom = await Class.findById({ _id });

for (let i = 0; i < students.length; i++) {

classroom.student.push(students[i]);
await classroom.save();
}
NextResponse.json(classroom);
}
catch (error) {
NextResponse.json(error);
}
}

export async function GET(request: NextRequest, {params}:{params:{_id: string}}) {
try {

await mongoose.connect(uri);

const { _id } = params;
let classroom = await Class.findById({ _id });
NextResponse.json(classroom);
}
catch (error) {
NextResponse.json(error);
}

}
50 changes: 50 additions & 0 deletions app/api/Class/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { NextRequest, NextResponse } from "next/server";
import User from "../../db/models/User";
import Projects from "../../db/models/project";
import Class from "../../db/models/Class";
import mongoose from "mongoose";
const uri = process.env.MONGO_URI_DOCKER || '';




export async function POST(request: Request) {
try {
await mongoose.connect(uri);
let { name, teacherId } = await request.json();

// Input validation
if (!name || !teacherId) {
return NextResponse.json({ error: "Name and teacherId are required" });
}

let teacher = await User.findById({_id: teacherId});

// Check if teacher exists
if (!teacher) {
return NextResponse.json({ error: "Teacher not found" });
}

// Check if user is a teacher (optional)
if (teacher.role !== 'Teacher') {
return NextResponse.json({ error: "Specified user is not a teacher" });
}

let newClass = await Class.create({
name: name,
teacher: teacher,
students: []
});

// Optional: You might also want to add this class to the teacher's classesTeaching array
teacher.classesTeaching.push(newClass._id);
await teacher.save();

return NextResponse.json(newClass);
}
catch (error) {
// Log the error for debugging purposes
console.error(error);
return NextResponse.json({ error: "An error occurred while creating the class" });
}
}
31 changes: 31 additions & 0 deletions app/api/User/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { NextResponse } from "next/server";
import User from "@/app/db/models/User";
import mongoose from "mongoose";
const uri = process.env.MONGO_URI_DOCKER || '';


mongoose.connect(uri);


export async function GET(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
try {
Expand All @@ -11,4 +15,31 @@ export async function GET(request: Request, { params }: { params: { id: string }
} catch (error) {
return NextResponse.error();
}
}

export async function PUT(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
try {
const userData = await request.json(); // Get the updated user data from the request body
const user = await User.findByIdAndUpdate(id, userData, { new: true }); // Update the user
if (!user) throw new Error('User not found'); // Handle user not found
return NextResponse.json(user); // Return the updated user
} catch (error) {
console.error(error); // Log the error for debugging
return NextResponse.error(); // Return a generic error response
}
}


export async function DELETE(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
try {
const user = await User.findByIdAndRemove(id); // Remove the user
if (!user) throw new Error('User not found'); // Handle user not found
return NextResponse.json({ message: 'User successfully deleted', userId: id }); // Return success message
} catch (error) {
console.error(error); // Log the error for debugging
return NextResponse.error(); // Return a generic error response
}

}
11 changes: 8 additions & 3 deletions app/api/User/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ const uri = process.env.MONGO_URI_DOCKER || '';
// Establish an initial connection to MongoDB
mongoose.connect(uri);

// Handle GET requests
//GET request to get all Users
export async function GET(request: Request) {
// Return a success response
return NextResponse.json("success");
try {
await mongoose.connect(uri);
let allUsers = await User.find({});
return NextResponse.json(allUsers);
} catch (err) {
return NextResponse.json(err);
}
}

// Handle POST requests
Expand Down
10 changes: 10 additions & 0 deletions app/api/message/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { pusherServer } from '@/lib/pusher'
import { NextResponse } from "next/server";
export async function POST(req: Request) {
const { text } = await req.json()

let trigger = pusherServer.trigger("my-channel", 'incoming-message', text)

return NextResponse.json(text);
}

26 changes: 26 additions & 0 deletions app/api/pusher-auth/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Currently not in use but keepin for future versions

import { pusherServer } from '@/lib/pusher'
import { nanoid } from 'nanoid'

export async function POST(req: Request) {
const data = await req.text()
const [socketId, channelName] = data
.split('&')
.map((str) => str.split('=')[1])

const id = nanoid()

const presenceData = {
user_id: id,
user_data: { user_id: id },
}

const auth = pusherServer.authorizeChannel(
socketId,
channelName,
presenceData
)

return new Response(JSON.stringify(auth))
}
1 change: 0 additions & 1 deletion app/api/teacher/student/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export async function POST(request: Request) {
await student.save();



// Fetch the updated teacher's details
let updatedTeacher = await User.findOne({ _id: teacherId });

Expand Down
26 changes: 13 additions & 13 deletions app/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ const Header = async ({}) => {

return (
<nav className=" z-10 relative flex items-center justify-between px-6 py-4 mb-5 bg-black-900" >
<div className="flex items-center">
<Link href="/">
<div className="underline decoration-sky-500 text-lg font-bold text-white">BitBox</div>
<div className='flex items-center'>
<Link href='/'>
<div className="underline decoration-sky-500 text-lg font-bold text-white">BitBox</div>
</Link>
</div>
<div className="flex items-center text-white">
<div className='flex items-center text-white'>
{!userId && (
<>
<Link
href="/sign-in"
href='/sign-in'
className="border border-white text-white hover:text-gray-300 mr-2 px-4 py-2 rounded "
>
Sign In
</Link>
<Link
href="/sign-up"
className="bg-blue-500 text-white hover:bg-blue-700 px-4 py-2 rounded"
href='/sign-up'
className='text-gray-300 hover:text-white mr-4'
>
Sign Up
</Link>
Expand All @@ -31,21 +31,21 @@ const Header = async ({}) => {
{userId && (
<div>
<Link
href="/dashboard"
className="text-gray-300 hover:text-white mr-4"
href='/dashboard'
className='text-gray-300 hover:text-white mr-4'
>
Dashboard
</Link>
<Link
href="/profile"
className="text-gray-300 hover:text-white mr-4"
href='/profile'
className='text-gray-300 hover:text-white mr-4'
>
Profile
</Link>
</div>
)}
<div className="ml-auto">
<UserButton afterSignOutUrl="/" />
<div className='ml-auto'>
<UserButton afterSignOutUrl='/' />
</div>
</div>
</nav>
Expand Down
26 changes: 26 additions & 0 deletions app/db/models/Class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose from "mongoose";


// Define a schema for a Class
const classSchema = new mongoose.Schema({
// Define a field 'name' of type String for the class's name
name: {
type: String,
required: true,
},
// Define a field 'teacher' that will store ObjectId of the User model (who is a teacher)
teacher: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
// For classes, a list of students enrolled in them. This list will store ObjectIds of the User model
students: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
// Optional: Any other information relevant to the class
});

// Export the Class model. If it already exists, use that; otherwise, create a new model using the classSchema
export default mongoose.models.Class || mongoose.model('Class', classSchema);
14 changes: 12 additions & 2 deletions app/db/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Import the mongoose library to be used for database modeling
import mongoose from "mongoose";

// Define a schema for a User
const userSchema = new mongoose.Schema({
// Define a field 'username' of type String which is required and unique for each user
Expand Down Expand Up @@ -29,6 +28,16 @@ const userSchema = new mongoose.Schema({
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
// For teachers, a list of classes they teach
classesTeaching: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Class',
}],
// For students, a list of classes they are enrolled in
classesEnrolled: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Class',
}],
// Define a field 'createdAt' of type Date which will have a default value of the current date and time
createdAt: {
type: Date,
Expand All @@ -38,7 +47,8 @@ const userSchema = new mongoose.Schema({
type: mongoose.Schema.Types.ObjectId,
ref: 'Project'
}]



});

// Export the User model. If the User model already exists, then use that, otherwise create a new model using the userSchema
Expand Down
18 changes: 16 additions & 2 deletions app/db/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,23 @@ const projectSchema = new mongoose.Schema({
},
assignedBy: {
type: String
}
},
// Define a list 'collaborators' to store ObjectIds of the User model who have access to the project
collaborators: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}],
// Define a field 'currentWriter' to store the ObjectId of the User model who currently has write access
currentWriter: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
// Optional: Timestamps for when the project was created and last updated
},
{ timestamps: true }
);

});


// Export the Project model. If the Project model already exists, then use that, otherwise create a new model using the projectSchema
export default mongoose.models.Project || mongoose.model('Project', projectSchema);
Loading