Skip to content

Commit fa38225

Browse files
committed
set rating
1 parent 1c03c93 commit fa38225

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

iLcatraz/HTTPConnectionIL.m

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,78 @@ + (NSString*) sanitizePID:(NSString*) pid{
169169
}
170170
}
171171
}
172-
172+
} else if ([[components objectAtIndex:2] isEqual:@"tracks"]){
173+
if ([components count]>3){
174+
/*
175+
/media/tracks/ID...
176+
*/
177+
NSString* tid=[HTTPConnectionIL sanitizePID:[components objectAtIndex:3]];
178+
if ([components count]==4){
179+
/*
180+
/media/tracks/ID
181+
*/
182+
if ([method isEqualToString:@"PATCH"]){
183+
NSData *data = [request body];
184+
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
185+
186+
NSArray *fields=[str componentsSeparatedByString:@"&"];
187+
188+
int updatec=0;
189+
NSString *field;
190+
NSEnumerator *fi=[fields objectEnumerator];
191+
192+
while (field = [fi nextObject]){
193+
NSArray *pair=[field componentsSeparatedByString:@"="];
194+
if ([pair count]!=2){
195+
return [[RESTResponse alloc]
196+
initWithJSON:@"PATCH must be in form param1=value&param2=value2&..." andStatus:400];
197+
}
198+
NSString *param=[pair objectAtIndex:0];
199+
NSString *value=[pair objectAtIndex:1];
200+
if ([@"rating" isEqualToString:param]){
201+
updatec++;
202+
[service setRatingOfTrackWithID:tid to:value];
203+
}
204+
}
205+
if (updatec==0){
206+
return [[RESTResponse alloc]
207+
initWithJSON:@"No parameters" andStatus:400];
208+
}
209+
}
210+
211+
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"PATCH"]) {
212+
NSString *trackPath=[service pathForTrackWithID:tid];
213+
trackLocation=[service locationForTrackPath:trackPath];
214+
ret=[service jsonTrackWithID:tid];
215+
}else {
216+
return [[RESTResponse alloc] initWithJSON:@"Only GET and PATCH methods are allowed to this resource" andStatus:400];
217+
}
218+
}else{
219+
/*
220+
/media/tracks/ID/...
221+
*/
222+
NSString* trackAttr=[HTTPConnectionIL sanitizePID:[components objectAtIndex:4]];
223+
if ([trackAttr isEqualToString:@"file"] && [components count]==5 ){
224+
NSString *trackPath=[service pathForTrackWithID:tid];
225+
trackLocation=[service locationForTrackPath:trackPath];
226+
if ([method isEqualToString:@"GET"])
227+
return [[FileResponse alloc] initWithFilePath:trackPath andLocation:trackLocation forConnection:self];
228+
else if ([method isEqualToString:@"HEAD"]){
229+
ret=@"";
230+
}
231+
}else{
232+
return [[RESTResponse alloc] initWithJSON:[NSString stringWithFormat:@"\"%@\" is not known resource of a track. \"file\" expected.",[components objectAtIndex:4]] andStatus:404];
233+
}
234+
}
235+
}else{
236+
return [[RESTResponse alloc] initWithJSON:@"ID must follow \"tracks\" resources request" andStatus:400];
237+
}
173238
}else{
174239
/*
175240
/media/<bad>
176241
*/
177242

178-
return [[RESTResponse alloc] initWithJSON:[NSString stringWithFormat:@"\"%@\" is not known resource of the media library. \"playlists\" expected.",[components objectAtIndex:2]] andStatus:404];
243+
return [[RESTResponse alloc] initWithJSON:[NSString stringWithFormat:@"\"%@\" is not known resource of the media library. \"playlists\" or \"tracks\" expected.",[components objectAtIndex:2]] andStatus:404];
179244
}
180245

181246
if (ret!=NULL)
@@ -196,4 +261,23 @@ + (NSString*) sanitizePID:(NSString*) pid{
196261
return [[RESTResponse alloc] initWithText:[exception reason] andStatus:500];
197262
}
198263
}
264+
- (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path
265+
{
266+
if ([method isEqualToString:@"PATCH"])
267+
return YES;
268+
269+
return [super expectsRequestBodyFromMethod:method atPath:path];
270+
}
271+
272+
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
273+
{
274+
if ([method isEqualToString:@"PATCH"] && [path rangeOfString:@"/media/tracks/"].location == 0)
275+
return YES;
276+
return [super supportsMethod:method atPath:path];
277+
}
278+
279+
- (void)processBodyData:(NSData *)postDataChunk
280+
{
281+
[request setBody:postDataChunk];
282+
}
199283
@end

iLcatraz/ITunesService.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
- (NSInteger) countOfTracksOfPlaylistWithID:(NSString*) pid;
3232
- (NSString*) jsonTracksOfPlaylistWithID:(NSString*) pid;
3333
- (NSString*) jsonPlaylitsOfFolderWithID:(NSString*) pid;
34+
- (NSString*) setRatingOfTrackWithID:(NSString*) pid to:(NSString*)rating;
3435
- (NSString*) jsonTrackWithID:(NSString*)tid ofPlaysitWithID:(NSString*)pid;
3536
- (NSString*) pathForTrackWithID:(NSString*)tid ofPlaysitWithID:(NSString*)pid;
37+
- (NSString*) jsonTrackWithID:(NSString*)tid;
38+
- (NSString*) pathForTrackWithID:(NSString*)tid;
3639
- (NSString*) locationForTrackPath:(NSString*)path;
3740

3841
@end

iLcatraz/ITunesService.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,15 @@ - (NSInteger) countOfTracksOfPlaylistWithID:(NSString*) pid;
724724
{
725725
return [self resultAsCount:[NSString stringWithFormat:@"tell application \"iTunes\"\n count of tracks of first item of (playlists whose persistent ID is \"%@\")\n end tell",pid]];
726726
};
727+
728+
- (NSString*) setRatingOfTrackWithID:(NSString*) pid to:(NSString*)rating {
729+
return [self jsonForCode:[NSString stringWithFormat:@"tell application \"iTunes\"\n set the rating of first item of (tracks whose persistent ID is \"%@\") to %@\n end tell",pid,rating]];
730+
}
731+
727732
- (NSString*) jsonTrackWithID:(NSString*) pid {
728733
return [self jsonForCode:[NSString stringWithFormat:@"tell application \"iTunes\"\n properties of first item of (tracks whose persistent ID is \"%@\")\n end tell",pid]];
729734
}
730-
- (NSString*) locationForTrackWithID:(NSString*) pid{
735+
- (NSString*) pathForTrackWithID:(NSString*) pid{
731736
return [self copyResultAsString:[NSString stringWithFormat:@"tell application \"iTunes\"\n location of first item of (tracks whose persistent ID is \"%@\")\n end tell",pid]];
732737
}
733738

@@ -741,6 +746,7 @@ - (NSString*) jsonMediaPlaylists {
741746
- (NSString*) jsonMediaPlaylistWithID:(NSString*)pid{
742747
return [self jsonForCode:[NSString stringWithFormat:@"tell application \"iTunes\"\n properties of first item of (playlists whose persistent ID is \"%@\")\n end tell",pid]];
743748
};
749+
744750
- (NSString*) jsonTrackWithID:(NSString*)tid ofPlaysitWithID:(NSString*)pid{
745751
return [self jsonForCode:[NSString stringWithFormat:@"tell application \"iTunes\"\n properties of first item of (tracks of (first item of (playlists whose persistent ID is \"%@\")) whose persistent ID is \"%@\")\n end tell",pid,tid]];
746752
};

0 commit comments

Comments
 (0)