From 4e50c6294f2b42ab644272477179476b7d05d5c5 Mon Sep 17 00:00:00 2001 From: AidenLYT Date: Fri, 27 Sep 2024 17:32:52 +0800 Subject: [PATCH] Change server.go connection to localhost:4200 Add service.ts Edit edit question page --- peer-prep-be/src/server.go | 4 ++-- .../src/edit-page/edit-page.component.ts | 10 +++++----- .../src/services/question.service.spec.ts | 16 ++++++++++++++++ peer-prep-fe/src/services/question.service.ts | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 peer-prep-fe/src/services/question.service.spec.ts create mode 100644 peer-prep-fe/src/services/question.service.ts diff --git a/peer-prep-be/src/server.go b/peer-prep-be/src/server.go index fa63022cec..47c71caf49 100644 --- a/peer-prep-be/src/server.go +++ b/peer-prep-be/src/server.go @@ -19,7 +19,7 @@ func main() { e.Use(middleware.Recover()) // Recover // CORS e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ - AllowOrigins: []string{"*"}, + AllowOrigins: []string{"http://localhost:4200"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}, })) @@ -28,5 +28,5 @@ func main() { e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) - e.Logger.Fatal(e.Start(":1323")) + e.Logger.Fatal(e.Start(":8080")) } diff --git a/peer-prep-fe/src/edit-page/edit-page.component.ts b/peer-prep-fe/src/edit-page/edit-page.component.ts index c751906bb0..bbdf3e6ee2 100644 --- a/peer-prep-fe/src/edit-page/edit-page.component.ts +++ b/peer-prep-fe/src/edit-page/edit-page.component.ts @@ -1,8 +1,8 @@ import { Component, OnInit } from '@angular/core'; -import {HttpClient} from "@angular/common/http"; import {ActivatedRoute, Router} from "@angular/router"; import {FormsModule} from "@angular/forms"; import {NgClass, NgForOf} from "@angular/common"; +import {QuestionService} from "../services/question.service"; @Component({ selector: 'app-edit-page', @@ -31,20 +31,20 @@ export class EditPageComponent implements OnInit { dropdownOpen: boolean = false; constructor( - private http: HttpClient, + private questionService: QuestionService, private router: Router, private route: ActivatedRoute ) {} ngOnInit() { this.route.params.subscribe((params) => { - this.questionId = params['id']; + this.questionId = '66f2a8a8aea02b6b4babc749'; this.loadQuestionData(); }); } loadQuestionData() { - this.http.get(`/api/questions/$(this.questionId)`).subscribe((data: any) => { + this.questionService.getQuestion(this.questionId).subscribe((data: any) => { this.questionTitle = data.title; this.questionDescription = data.description; this.categories.forEach(cat => { @@ -65,7 +65,7 @@ export class EditPageComponent implements OnInit { categories: this.categories.filter(cat => cat.selected).map(cat => cat.name), difficulty: this.difficulty }; - this.http.put(`/api/questions/$(this.questionId)`, updatedQuestion).subscribe((response) => { + this.questionService.updateQuestion(this.questionId, updatedQuestion).subscribe((response) => { alert('Question updated successfully!'); }, (error) => { diff --git a/peer-prep-fe/src/services/question.service.spec.ts b/peer-prep-fe/src/services/question.service.spec.ts new file mode 100644 index 0000000000..54f2238efc --- /dev/null +++ b/peer-prep-fe/src/services/question.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { QuestionService } from './question.service'; + +describe('QuestionService', () => { + let service: QuestionService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(QuestionService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/peer-prep-fe/src/services/question.service.ts b/peer-prep-fe/src/services/question.service.ts new file mode 100644 index 0000000000..8c2024ee11 --- /dev/null +++ b/peer-prep-fe/src/services/question.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import {HttpClient} from "@angular/common/http"; +import {Observable} from "rxjs"; + +@Injectable({ + providedIn: 'root' +}) +export class QuestionService { + private baseUrl = 'http://localhost:8080/api/questions' + constructor(private http: HttpClient) { } + + getQuestion(id: string): Observable { + return this.http.get(`${this.baseUrl}/${id}`); + } + + updateQuestion(id: string, updatedQuestion: any): Observable { + return this.http.put(`${this.baseUrl}/${id}`, updatedQuestion); + } +}