public async Task <CourseDetailViewModel> GetCourseAsync(int id) { logger.LogInformation("Course {id} requested", id); FormattableString query = $@"SELECT Id, Title, Description, ImagePath, Author, Rating, FullPrice_Amount, FullPrice_Currency, CurrentPrice_Amount, CurrentPrice_Currency FROM Courses WHERE Id={id} ; SELECT Id, Title, Description, Duration FROM Lessons WHERE CourseId={id}"; DataSet dataSet = await db.QueryAsync(query); //Course var courseTable = dataSet.Tables[0]; if (courseTable.Rows.Count != 1) { logger.LogWarning("Course {id} not found", id); throw new CourseNotFoundException(id); } var courseRow = courseTable.Rows[0]; var courseDetailViewModel = CourseDetailViewModel.FromDataRow(courseRow); //Course lessons var lessonDataTable = dataSet.Tables[1]; foreach (DataRow lessonRow in lessonDataTable.Rows) { LessonViewModel lessonViewModel = LessonViewModel.FromDataRow(lessonRow); courseDetailViewModel.Lessons.Add(lessonViewModel); } return(courseDetailViewModel); }
public async Task <CourseDetailViewModel> GetCourseAsync(int id) { /*Per registrare i Log dell'applicazione*/ logger.LogInformation($"Course {id} richiesto"); FormattableString query = $@" SELECT * FROM Courses WHERE Id={id}; SELECT * FROM Lessons WHERE IdCourse={id}"; DataSet dataSet = await db.QueryAsync(query); var courseTable = dataSet.Tables[0]; if (courseTable.Rows.Count != 1) { logger.LogWarning($"Corso {id} non trovato!"); throw new CourseNotFoundException(id); } var courseRow = courseTable.Rows[0]; var courseDetailViewModel = CourseDetailViewModel.FromDataRow(courseRow); var lessonDataTable = dataSet.Tables[1]; foreach (DataRow lessonRow in lessonDataTable.Rows) { LessonViewModel lessonViewModel = LessonViewModel.FromDataRow(lessonRow); courseDetailViewModel.Lezioni.Add(lessonViewModel); } return(courseDetailViewModel); }
public async Task <CourseDetailViewModel> GetCourseAsync(int id) { logger.LogInformation("Course {id} richiesto", id); /*FormattableString query = $@"SELECT Id, Title, Description, ImagePath, Author, Rating, FullPrice_Amount, FullPrice_Currency, CurrentPrice_Amount, CurrentPrice_Currency FROM Courses WHERE Id={id}; * SELECT Id, CourseId, Title, Description, Duration FROM Lessons WHERE CourseId={id}";*/ DataSet dataSet = await db.QueryAsync($@"SELECT Id, Title, Description, ImagePath, Author, Rating, FullPrice_Amount, FullPrice_Currency, CurrentPrice_Amount, CurrentPrice_Currency FROM Courses WHERE Id={id}; SELECT Id, CourseId, Title, Description, Duration FROM Lessons WHERE CourseId={id}"); //Course var corsoTable = dataSet.Tables[0]; if (corsoTable.Rows.Count != 1) { logger.LogWarning("Il corso {id} non esiste", id); throw new CourseNotFoundException(id); } var corsoRow = corsoTable.Rows[0]; var corsoDetailViewModel = CourseDetailViewModel.FromDataRow(corsoRow); //Lezioni corso var lezioniDataTable = dataSet.Tables[1]; foreach (DataRow item in lezioniDataTable.Rows) { LessonViewModel lezioneViewModel = LessonViewModel.FromDataRow(item); corsoDetailViewModel.Lessons.Add(lezioneViewModel); } return(corsoDetailViewModel); }
public async Task <CourseDetailViewModel> GetCourseAsync(int id) { /* La query fornisce 2 tabelle: Corsi e lezioni. */ FormattableString query = $@"SELECT Id, Title, Description, ImagePath, Author, Rating, FullPrice_Amount, FullPrice_Currency, CurrentPrice_Amount, CurrentPrice_Currency FROM Courses WHERE Id={id} ; SELECT Id, Title, Description, Duration FROM Lessons WHERE CourseId={id}"; DataSet dataSet = await db.QueryAsync(query); // Course var courseTable = dataSet.Tables[0]; if (courseTable.Rows.Count != 1) { throw new InvalidOperationException($"Did not return exactly 1 row for Course {id}"); } // Prende la riga del corso var courseRow = courseTable.Rows[0]; // crea l'oggetto principale della pagina: Dettaglio corso. var courseDetailViewModel = CourseDetailViewModel.FromDataRow(courseRow); // Prende dati del corso dal ViewModel // Course Lessons var lessonsDataTable = dataSet.Tables[1]; foreach (DataRow lessonRow in lessonsDataTable.Rows) { CourseLessonViewModel lessonViewModel = CourseLessonViewModel.FromDataRow(lessonRow); courseDetailViewModel.Lessons.Add(lessonViewModel); } return(courseDetailViewModel); }
public async Task <CourseDetailViewModel> GetCourseAsync(int id) { logger.LogInformation("Course {id} requested", id); //permette di filtrare i log in base al valore degli id //la prima query carica il corso scelto al rispettivo id, la seconda invece carica tutte le lezioni legate all'id di quel corso //FormattableString separa la parte fissa dai suoi parametri FormattableString query = $@"SELECT Id, Title, Description, ImagePath, Author, Rating, FullPrice_Amount, FullPrice_Currency, CurrentPrice_Amount, CurrentPrice_Currency FROM Courses WHERE Id={id} ; SELECT Id, Title, Description, Duration FROM Lessons WHERE CourseID={id}"; DataSet dataSet = await db.ExecuteQueryAsync(query); //Course: //estrae i dati dalla prima datatable, se Rows >1 allora c'é un errore in quanto ci può essere solo 1 Row (evita la SqlInjection) var courseTable = dataSet.Tables[0]; if (courseTable.Rows.Count != 1) { logger.LogWarning("Course {id} not found", id); //messaggio di log, problema non grave throw new CourseNotFoundException(id); } var courseRow = courseTable.Rows[0]; //leggiamo la riga (courseRow), la passiamo a FromDataRow che farà la mappatura tra il datarow restituendo un oggetto di tipo courseDetailViewModel var courseDetailViewModel = CourseDetailViewModel.FromDataRow(courseRow); //Lessons var lessonDataTable = dataSet.Tables[1]; foreach (DataRow lessonRow in lessonDataTable.Rows) { LessonViewModel lessonViewModel = LessonViewModel.FromDataRow(lessonRow); courseDetailViewModel.Lessons.Add(lessonViewModel); //ogni oggetto di LessonViewModel trovato viene aggiunto alla lista } return(courseDetailViewModel); }