refactoring

main
jolatime 5 months ago
parent 8630b06376
commit afb1fb6c6f

@ -183,7 +183,7 @@ export async function setNewFlag(flag: Flag, el: ElementRef, elements: any, ts:
const email = getEmail(); const email = getEmail();
const data = { FlagImage: flag.Image, email: email }; const data = { FlagImage: flag.Image, email: email };
const res = await fetch('api/setNewActiveFlag', { const res = await fetch('api/flags/setNewActiveFlag', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'
@ -279,14 +279,14 @@ async function checkSubmission(isPractice: boolean, el: ElementRef, socket: Sock
} }
let res, result; let res, result;
if(!isPractice){ if(!isPractice){
res = await fetch('api/checkFlagSubmission', { res = await fetch('api/submissions/checkFlagSubmission', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json'}, headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({ email, flagID, submittedFlag }) body: JSON.stringify({ email, flagID, submittedFlag })
}); });
} }
else{ else{
res = await fetch('api/checkPracSubmission', { res = await fetch('api/submissions/checkPracSubmission', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json'}, headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({ email, flagID, submittedFlag }) body: JSON.stringify({ email, flagID, submittedFlag })

@ -20,7 +20,7 @@ export class AddContestComponent {
const isActive = 0; const isActive = 0;
console.log("EMAIL IS:", this.getEmail()) console.log("EMAIL IS:", this.getEmail())
const data = {Name: name, IsActive: isActive, email: this.getEmail(), Desc: desc}; const data = {Name: name, IsActive: isActive, email: this.getEmail(), Desc: desc};
const res = await fetch('api/AddContest', { const res = await fetch('api/contests/AddContest', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'

@ -50,8 +50,9 @@
<!-- image of the flag to create container --> <!-- image of the flag to create container -->
<label> Image: </label> <label> Image: </label>
<select id="Images"> <select id="Images" [(ngModel)]="selectedImage">
<option> ubuntu </option> <option> ubuntu </option>
<option *ngFor="let image of allImages" [value] ="image.Name">{{image.Name}}</option>
</select> </select>
<br><br> <br><br>

@ -1,16 +1,26 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { gotoPage } from '../Helper/Helpers'; import { gotoPage, getEmail } from '../Helper/Helpers';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ModifyContestService } from '../modify-contest/modify-contest.service';
import { Image } from '../models/image.model';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
@Component({ @Component({
selector: 'app-add-flag', selector: 'app-add-flag',
imports: [], imports: [CommonModule, FormsModule],
templateUrl: './add-flag.component.html', templateUrl: './add-flag.component.html',
styleUrl: './add-flag.component.css' styleUrl: './add-flag.component.css'
}) })
export class AddFlagComponent { export class AddFlagComponent {
constructor(private router: Router){} allImages: Image[] = [];
selectedImage: string = "Select an Image";
constructor(private router: Router, private modifyContestService: ModifyContestService){}
ngOnInit(): void {
this.getImages();
}
// add a flag to a contest // add a flag to a contest
async AddFlag(){ async AddFlag(){
// get contestId from URL // get contestId from URL
@ -38,7 +48,7 @@ export class AddFlagComponent {
// add the flag // add the flag
const data = {name: flagName.value, desc: description.value, contest: contestId, image: image.value, path: path.value, hint1: Hint1.value || '', hint2: Hint2.value || '', hint3: Hint3.value || ''}; const data = {name: flagName.value, desc: description.value, contest: contestId, image: image.value, path: path.value, hint1: Hint1.value || '', hint2: Hint2.value || '', hint3: Hint3.value || ''};
try{ try{
const response = await fetch('api/AddFlag', { const response = await fetch('api/flags/AddFlag', {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/json',}, headers: {'Content-Type': 'application/json',},
body: JSON.stringify(data) body: JSON.stringify(data)
@ -58,7 +68,21 @@ export class AddFlagComponent {
} }
} }
async getImages(): Promise<void>{
const email = this.getEmail();
try{
const imageData = await this.modifyContestService.getImages(email);
this.allImages = imageData || [];
}catch(error){
console.error('Error loading images');
}
}
navtoPageCI(){ navtoPageCI(){
gotoPage(this.router, '/create-image'); gotoPage(this.router, '/create-image');
} }
getEmail(): string{
return getEmail();
}
} }

@ -36,7 +36,7 @@ export class AddStudentComponent {
if (this.password == this.confirmPassword) { // passwords match if (this.password == this.confirmPassword) { // passwords match
const data = {name: this.Uname, email: this.email, Aemail: Aemail, password: this.password}; const data = {name: this.Uname, email: this.email, Aemail: Aemail, password: this.password};
const res = await fetch('api/AddStudent', { const res = await fetch('api/users/AddStudent', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'

@ -33,7 +33,6 @@ export class AdminProfileComponent implements OnInit{
this.populateTable(); this.populateTable();
} }
async populateTable(){ async populateTable(){
try{ try{
const email = this.getEmail(); const email = this.getEmail();
@ -54,6 +53,8 @@ export class AdminProfileComponent implements OnInit{
const result = await this.adminProfileService.getContestFlagsSubs(email, this.selectedContest); const result = await this.adminProfileService.getContestFlagsSubs(email, this.selectedContest);
this.flags = result.flags; this.flags = result.flags;
this.subs = result.subs; this.subs = result.subs;
console.log("flags", this.flags);
console.log("Subs", this.subs);
}catch(error){ }catch(error){
console.error('Failed to populate flag table'); console.error('Failed to populate flag table');
} }

@ -281,7 +281,7 @@ export class CreateImageComponent {
formdata.append('data', JSON.stringify(data)); formdata.append('data', JSON.stringify(data));
// send the post request // send the post request
const res = await fetch('api/AddImage', { const res = await fetch('api/images/AddImage', {
method: 'POST', method: 'POST',
body: formdata body: formdata
}); });

@ -25,7 +25,7 @@ export class EditStudentComponent {
if (ps1 == ps2) { // if both passwords are equal, continue if (ps1 == ps2) { // if both passwords are equal, continue
const data = { email: this.emailinput, password: ps1}; const data = { email: this.emailinput, password: ps1};
const res = await fetch('api/UpdateStudent', { const res = await fetch('api/users/UpdateStudent', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'

@ -104,9 +104,13 @@ handlePopupMessage(event: MessageEvent): void {
if (this.selectedContestId !== null) { if (this.selectedContestId !== null) {
try { try {
const email = this.getEmail(); const email = this.getEmail();
await this.modifyContestService.setContestActive(this.selectedContestId, email); const actOrDeact = await this.modifyContestService.setContestActive(this.selectedContestId, email);
this.loadContests(); if(actOrDeact === 1){
console.log('Contest set as active'); this.loadContests();
console.log('Contest set as active');
}
else if(actOrDeact === 0)
this.activeContest = null;
} catch (error) { } catch (error) {
console.error('Error activating contest:', error); console.error('Error activating contest:', error);
} }

@ -9,7 +9,7 @@ export class ModifyContestService {
async getImages(email: string): Promise<any> { async getImages(email: string): Promise<any> {
try { try {
const response = await fetch('api/getImages', { const response = await fetch('api/images/getImages', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -71,7 +71,7 @@ export class ModifyContestService {
async addContest(data: any): Promise<any> { async addContest(data: any): Promise<any> {
try { try {
const response = await fetch('api/AddContest', { const response = await fetch('api/constests/AddContest', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -93,7 +93,7 @@ export class ModifyContestService {
// Send a POST request to delete the contest by its name // Send a POST request to delete the contest by its name
let data = {contest: contestId}; let data = {contest: contestId};
await this.deleteFlagsFromContest(contestId); await this.deleteFlagsFromContest(contestId);
let res = await fetch('api/DeleteContest', { let res = await fetch('api/contests/DeleteContest', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -111,7 +111,7 @@ export class ModifyContestService {
async deleteFlagsFromContest(contestId: number): Promise<any> { async deleteFlagsFromContest(contestId: number): Promise<any> {
try { try {
const response = await fetch('api/DeleteFlagsFromContest', { const response = await fetch('api/flags/DeleteFlagsFromContest', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -131,7 +131,7 @@ export class ModifyContestService {
async AddFlag(data: any): Promise<any> { async AddFlag(data: any): Promise<any> {
try { try {
const response = await fetch('api/AddFlag', { const response = await fetch('api/flags/AddFlag', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -151,7 +151,7 @@ export class ModifyContestService {
async DeleteFlag(flagId: number): Promise<any> { async DeleteFlag(flagId: number): Promise<any> {
let data = { flag: flagId }; let data = { flag: flagId };
let res = await fetch('api/DeleteFlag', { let res = await fetch('api/flags/DeleteFlag', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -164,7 +164,7 @@ export class ModifyContestService {
const image = flagImage; const image = flagImage;
// set up the container for the image // set up the container for the image
const data = { FlagImage: image, email: Email }; const data = { FlagImage: image, email: Email };
const res = await fetch('/api/setNewActiveFlag', { const res = await fetch('/api/flags/setNewActiveFlag', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'
@ -174,28 +174,50 @@ export class ModifyContestService {
} }
async setContestActive(contestId: number, email: string): Promise<any> { async setContestActive(contestId: number, email: string): Promise<any> {
try { const oldcontestID = await this.getActiveContest(email);
const response = await fetch('api/setContestActive', { if(oldcontestID !== 0 && oldcontestID === contestId){
method: 'POST', try{
headers: { const response = await fetch('api/contests/EndContest', {
'Content-Type': 'application/json' method: 'POST',
}, headers: {
body: JSON.stringify({ contest: contestId, email }) 'Content-Type': 'application/json'
}); },
body: JSON.stringify({contest: oldcontestID, email})
if (!response.ok) { });
throw new Error('Failed to set contest active'); if(!response.ok)
} throw new Error('Failed to end contest');
return await response.json(); await response.json();
} catch (error) { return 0;
console.error('Error setting contest active:', error); }catch(error){
console.error('Error ending contest');
throw error; throw error;
} }
}
else{
try {
const response = await fetch('api/contests/setContestActive', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ contest: contestId, email })
});
if (!response.ok) {
throw new Error('Failed to set contest active');
}
await response.json();
return 1;
} catch (error) {
console.error('Error setting contest active:', error);
throw error;
}
}
} }
async deleteImage(imageName: string): Promise<any> { async deleteImage(imageName: string): Promise<any> {
try { try {
const response = await fetch('api/DeleteImageReplaceFlags', { const response = await fetch('api/images/DeleteImageReplaceFlags', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

@ -1,3 +1 @@
<h1>Thought you could pull a fast one eh?</h1> <h1>You do not have permission to access this page.</h1>
<p>I dont think so</p>
<h2>👎</h2>
Loading…
Cancel
Save