started implementing bulk csv import
This commit is contained in:
1 parent
317ad928ea
commit
36866dabe6
5 files changed
+112
-2
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { MovieDateSeen } from '../../shared/models/dtos/movieDateSeen.model';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Entry } from '../../shared/models/entry.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ImporterService {
|
||||
private backend_apiUrl = 'http://localhost:4000/api/v1';
|
||||
private apiAction = 'entries';
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
bulkAddWithDateSeen(values: string) {
|
||||
const separator = ',';
|
||||
|
||||
// ? converting each line into a MovieDateSeen object
|
||||
let valuesSplited = values.split('\n');
|
||||
let movies = [] as MovieDateSeen[];
|
||||
valuesSplited.forEach((m) => {
|
||||
let movie = {} as MovieDateSeen;
|
||||
|
||||
movie.tmdbId = Number.parseInt(m.split(separator)[0]);
|
||||
movie.dateSeen = new Date(m.split(separator)[1]);
|
||||
movie.userRating = Number.parseInt(m.split(separator)[2]);
|
||||
|
||||
movies.push(movie);
|
||||
});
|
||||
|
||||
// ? consuming the backend API
|
||||
return this.http.post<Entry[]>(
|
||||
`${this.backend_apiUrl}/${this.apiAction}/bulk-import-movies`,
|
||||
{
|
||||
movies: movies,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user