Browse Source

WIP trying to get the seasonID along with the file

old-project-state
Dan 2 years ago
parent
commit
09d89a4108
  1. 2
      server.js
  2. 2
      src/app/components/season-details/season-details.component.html
  3. 4
      src/app/components/season-details/season-details.component.ts
  4. 12
      src/app/components/upload-replay-dialog/upload-replay-dialog.component.ts
  5. 6
      src/app/services/api.service.ts

2
server.js

@ -202,6 +202,8 @@ app.post('/api/race/add', verifyToken, async (req, res) => {
// route for handling requests from the Angular client // route for handling requests from the Angular client
app.post('/api/upload-replay', upload.single('file'), (req, res) => { app.post('/api/upload-replay', upload.single('file'), (req, res) => {
let file = req.file; let file = req.file;
console.log(req.body);
const { seasonId } = req.body;
console.log("File uploaded: ", req.file); console.log("File uploaded: ", req.file);
fs.readFile(file.path, function(err, buffer) fs.readFile(file.path, function(err, buffer)
{ {

2
src/app/components/season-details/season-details.component.html

@ -4,7 +4,7 @@
<div *ngIf="isAuthenticated"> <div *ngIf="isAuthenticated">
<button (click)="openNewRaceDialog()">New Race</button> <button (click)="openNewRaceDialog()">New Race</button>
</div> </div>
<button mat-raised-button class="full-width-button" color="primary" (click)="openUploadReplayDialog()">Upload Replay</button> <button mat-raised-button class="full-width-button" color="primary" (click)="openUploadReplayDialog(seasonInfo._id)">Upload Replay</button>
<mat-divider></mat-divider> <mat-divider></mat-divider>
<br/> <br/>
<mat-tab-group> <mat-tab-group>

4
src/app/components/season-details/season-details.component.ts

@ -70,10 +70,10 @@ export class SeasonDetailsComponent {
}); });
} }
openUploadReplayDialog(): void { openUploadReplayDialog(seasonId: string): void {
const dialogRef = this.dialog.open(UploadReplayDialogComponent, { const dialogRef = this.dialog.open(UploadReplayDialogComponent, {
width: '400px', width: '400px',
data: { ...this.newRaceDetails } data: { seasonId: seasonId }
}); });
dialogRef.afterClosed().subscribe(result => { dialogRef.afterClosed().subscribe(result => {

12
src/app/components/upload-replay-dialog/upload-replay-dialog.component.ts

@ -1,4 +1,5 @@
import { Component } from '@angular/core'; import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ApiService } from '../../services/api.service'; import { ApiService } from '../../services/api.service';
@Component({ @Component({
@ -8,8 +9,14 @@ import { ApiService } from '../../services/api.service';
}) })
export class UploadReplayDialogComponent { export class UploadReplayDialogComponent {
fileName = ''; fileName = '';
seasonId: string = '';
constructor(private apiService: ApiService) {} constructor(private apiService: ApiService,
@Inject(MAT_DIALOG_DATA) seasonId: string
)
{
this.seasonId = seasonId;
}
onFileSelected(event: any) onFileSelected(event: any)
{ {
@ -19,6 +26,7 @@ export class UploadReplayDialogComponent {
this.fileName = file.name; this.fileName = file.name;
let formData = new FormData(); let formData = new FormData();
formData.append("file", file); formData.append("file", file);
formData.append("seasonID", this.seasonId);
const upload$ = this.apiService.postReplayUpload(formData); const upload$ = this.apiService.postReplayUpload(formData);
upload$.subscribe(); upload$.subscribe();
} }

6
src/app/services/api.service.ts

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -20,8 +20,10 @@ export class ApiService
postReplayUpload(file: any) postReplayUpload(file: any)
{ {
const headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
return this.http.post( return this.http.post(
this.server_route+'/api/upload-replay', file); this.server_route+'/api/upload-replay', file, { headers });
} }
getSeasons() getSeasons()

Loading…
Cancel
Save