Skip to content

Commit

Permalink
Merge pull request #14 from mohit-nagaraj/main
Browse files Browse the repository at this point in the history
Sih registration backend
  • Loading branch information
SkySingh04 authored Aug 13, 2024
2 parents 6a81f38 + 4435b79 commit b29ed51
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Set up environment variables
run: |
echo "NEXT_PUBLIC_FIREBASE_API_KEY=${{ secrets.NEXT_PUBLIC_FIREBASE_API_KEY }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=${{ secrets.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_PROJECT_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=${{ secrets.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_APP_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_APP_ID }}" >> $GITHUB_ENV
- name: Install dependencies
run: npm install

# - name: Run tests
# run: npm test

- name: Build the project
run: npm run build
40 changes: 40 additions & 0 deletions app/(default)/api/registration/sih/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { db } from '@/Firebase';
import { sihValidate } from '@/lib/utils';
import { addDoc, collection, getDocs } from 'firebase/firestore';
import { NextResponse } from 'next/server';

// Add a new registration
export async function POST(request: Request) {
const data = await request.json();
// Validate the data
const val = sihValidate(data);

if(val.error){
return NextResponse.json({ message: 'Validation error', error: val.error });
}

try{
// Save to Firebase
const docRef = await addDoc(collection(db, "sih2024"), data);
console.log("Document written with ID: ", docRef.id);
} catch (error) {
console.error(error);
return NextResponse.json({ message: 'An error occurred', error });
}
// Return a response
return NextResponse.json({ message: 'Registration successful', data });
}

//get all registrations
export async function GET() {
try{
// Get all registrations in sih2024 collection
const querySnapshot = await getDocs(collection(db, "sih2024"));
// Map the data to get only the data
const data = querySnapshot.docs.map((doc) => doc.data());
return NextResponse.json({ data });
} catch (error) {
console.error(error);
return NextResponse.json({ message: 'An error occurred', error });
}
}
19 changes: 19 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@ import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

export const sihValidate = (data: any) => {
if (!data) {
return { error: 'No data provided' }
}
if (!data.team_info) {
return { error: 'team_info is required' }
}
if (!data.team_info.team_name) {
return { error: 'team_name is required' }
}
if (!data.team_info.team_leader) {
return { error: 'team_leader is required' }
}
if (!data.project_information) {
return { error: 'project_information is required' }
}
return { error: null }
}

0 comments on commit b29ed51

Please sign in to comment.