From a6e617e59c788e55bdd80c0368de48ca5ebe8fac Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 26 Jul 2023 17:28:00 +0200 Subject: [PATCH] Improvement of the model (addition of the class artist and modification of the class music) :art: and realization of the class MusicViewModel with test :white_check_mark: --- Sources/justMUSIC/lib/model/Artist.dart | 10 ++ Sources/justMUSIC/lib/model/Music.dart | 32 ++++--- Sources/justMUSIC/lib/model/Post.dart | 6 +- .../lib/view_model/CommentViewModel.dart | 4 +- .../lib/view_model/MusicViewModel.dart | 93 +++++++++++++++++-- .../lib/view_model/TokenSpotify.dart | 5 +- Sources/justMUSIC/test/Music_test.dart | 35 +++++++ 7 files changed, 158 insertions(+), 27 deletions(-) create mode 100644 Sources/justMUSIC/lib/model/Artist.dart create mode 100644 Sources/justMUSIC/test/Music_test.dart diff --git a/Sources/justMUSIC/lib/model/Artist.dart b/Sources/justMUSIC/lib/model/Artist.dart new file mode 100644 index 0000000..1a7ae04 --- /dev/null +++ b/Sources/justMUSIC/lib/model/Artist.dart @@ -0,0 +1,10 @@ +class Artist { + String _id; + String _name; + + Artist(this._id, this._name); + + String get id => _id; + + String get name => _name; +} diff --git a/Sources/justMUSIC/lib/model/Music.dart b/Sources/justMUSIC/lib/model/Music.dart index 7b690d8..e50223f 100644 --- a/Sources/justMUSIC/lib/model/Music.dart +++ b/Sources/justMUSIC/lib/model/Music.dart @@ -1,15 +1,19 @@ +import 'Artist.dart'; + class Music { - final int _id; + final String _id; String _title; String _cover; - String _singer; + String _previewUrl; DateTime _date; + List _artists; // Constructor - Music(this._id, this._title, this._cover, this._singer, this._date); + Music(this._id, this._title, this._cover, this._previewUrl, this._date, + this._artists); //Getters and setters - int get id => _id; + String get id => _id; String get title => _title; @@ -19,19 +23,25 @@ class Music { String get cover => _cover; + set cover(String value) { + _cover = value; + } + + String get previewUrl => _previewUrl; + + set previewUrl(String value) { + _previewUrl = value; + } + DateTime get date => _date; set date(DateTime value) { _date = value; } - String get singer => _singer; - - set singer(String value) { - _singer = value; - } + List get artists => _artists; - set cover(String value) { - _cover = value; + set artists(List value) { + _artists = value; } } diff --git a/Sources/justMUSIC/lib/model/Post.dart b/Sources/justMUSIC/lib/model/Post.dart index c92f3fd..0c248dd 100644 --- a/Sources/justMUSIC/lib/model/Post.dart +++ b/Sources/justMUSIC/lib/model/Post.dart @@ -2,7 +2,7 @@ class Post { final int _id; final int _idUser; String? _description; - int _idMusic; + String _idMusic; String _location; int _nblikes; String? _selfie; @@ -23,9 +23,9 @@ class Post { _description = value; } - int get idMusic => _idMusic; + String get idMusic => _idMusic; - set idMusic(int value) { + set idMusic(String value) { _idMusic = value; } diff --git a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart index c4d591f..f4684e1 100644 --- a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart @@ -1,10 +1,8 @@ import '../model/Comment.dart'; class CommentViewModel { - // Methods List getCommentsPost(int idPost) { throw new Error(); } - -} \ No newline at end of file +} diff --git a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart index f5f8b3b..5f789ee 100644 --- a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart @@ -1,19 +1,96 @@ +import 'dart:convert'; + +import 'package:justmusic/view_model/TokenSpotify.dart'; +import 'package:http/http.dart' as http; +import '../model/Artist.dart'; import '../model/Music.dart'; class MusicViewModel { + final String API_URL = "https://api.spotify.com/v1"; + late TokenSpotify _token; - final String API_URL = ""; + MusicViewModel() { + _token = new TokenSpotify(); + } // Methods - Music getMusic(int id) { - throw new Error(); + Future getMusic(String id) async { + var accessToken = await _token.getAccessToken(); + var response = await http.get(Uri.parse('$API_URL/tracks/$id'), headers: { + 'Authorization': 'Bearer $accessToken', + }); + + if (response.statusCode == 200) { + final responseData = jsonDecode(response.body); + List artists = + List.from(responseData['artists'].map((artist) { + return Artist(artist['id'], artist['name']); + })); + + return Music( + responseData['id'], + responseData['name'], + responseData['album']['images'][0]['url'], + responseData['preview_url'], + DateTime.parse(responseData['album']['release_date']), + artists); + } else { + throw Exception( + 'Error retrieving music information : ${response.statusCode} ${response.reasonPhrase}'); + } } - Music getMusicWithName(String name) { - throw new Error(); + List _getMusicsFromResponse(Map responseData) { + List musics = []; + + List tracks = responseData['tracks']['items']; + for (var track in tracks) { + List artists = List.from(track['artists'].map((artist) { + return Artist(artist['id'], artist['name']); + })); + + musics.add(Music( + track['id'], + track['name'], + track['album']['images'][0]['url'], + track['preview_url'], + DateTime.now(), + artists)); + } + + return musics; } - List getMusics(String name) { - throw new Error(); + Future> getMusicsWithName(String name, {int limit = 20, int offset = 1}) async { + var accessToken = await _token.getAccessToken(); + var response = await http + .get(Uri.parse('$API_URL/search?q=track%3A$name&type=track&limit=$limit&offset=$offset'), headers: { + 'Authorization': 'Bearer $accessToken', + }); + + if (response.statusCode == 200) { + Map responseData = jsonDecode(response.body); + return _getMusicsFromResponse(responseData); + } else { + throw Exception( + 'Error while retrieving music : ${response.statusCode} ${response.reasonPhrase}'); + } + } + + Future> getMusicsWithArtistName(String name, {int limit = 20, int offset = 1}) async { + var accessToken = await _token.getAccessToken(); + var response = await http.get( + Uri.parse('$API_URL/search?q=artist%3A$name&type=track&limit=$limit&offset=$offset'), + headers: { + 'Authorization': 'Bearer $accessToken', + }); + + if (response.statusCode == 200) { + Map responseData = jsonDecode(response.body); + return _getMusicsFromResponse(responseData); + } else { + throw Exception( + 'Error while retrieving music : ${response.statusCode} ${response.reasonPhrase}'); + } } -} \ No newline at end of file +} diff --git a/Sources/justMUSIC/lib/view_model/TokenSpotify.dart b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart index b84d15c..fbdffac 100644 --- a/Sources/justMUSIC/lib/view_model/TokenSpotify.dart +++ b/Sources/justMUSIC/lib/view_model/TokenSpotify.dart @@ -32,9 +32,10 @@ class TokenSpotify { if (response.statusCode == 200) { final responseData = jsonDecode(response.body); _accessToken = responseData['access_token']; - _tokenEnd = DateTime.now().add(Duration(seconds: responseData['expires_in'])); + _tokenEnd = + DateTime.now().add(Duration(seconds: responseData['expires_in'])); } else { - print('Erreur lors de l\'actualisation du token : ${response.statusCode}'); + print('Error refreshing token : ${response.statusCode}'); } } diff --git a/Sources/justMUSIC/test/Music_test.dart b/Sources/justMUSIC/test/Music_test.dart new file mode 100644 index 0000000..a5ef914 --- /dev/null +++ b/Sources/justMUSIC/test/Music_test.dart @@ -0,0 +1,35 @@ +import 'package:justmusic/model/Artist.dart'; +import 'package:justmusic/model/Music.dart'; +import 'package:justmusic/view_model/MusicViewModel.dart'; + +Future main() async { + MusicViewModel musicVM = new MusicViewModel(); + Music m = await musicVM.getMusic('295SxdR1DqunCNwd0U767w'); + print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title); + print(m.date.toString() + " " + m.previewUrl); + for (Artist a in m.artists) { + print(a.id + ":" + a.name); + } + + print('\nMusics :'); + + List musics = await musicVM.getMusicsWithName('Onizuka'); + for (Music m in musics) { + print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title); + print(m.date.toString() + " " + m.previewUrl); + for (Artist a in m.artists) { + print(a.id + ":" + a.name); + } + } + + print('\nMusics With Artist:'); + + List musics2 = await musicVM.getMusicsWithArtistName('PNL'); + for (Music m in musics2) { + print("id :" + m.id.toString() + " cover :" + m.cover + " title :" + m.title); + print(m.date.toString() + " " + m.previewUrl); + for (Artist a in m.artists) { + print(a.id + ":" + a.name); + } + } +}